def test_model_with_invalid_properties(dataset): invalid_schema = [('an_integer', int, 'An Integer')] new_model = dataset.create_model( 'New_Model_{}'.format(current_ts()), 'A New Model', 'a new model', invalid_schema ) assert not new_model.schema
def test_get_connected(dataset): model_1 = dataset.create_model('Model_A', description="model a", schema=[ ModelProperty("prop1", data_type=ModelPropertyType(data_type=str), title=True)]) related_models = model_1.get_connected() # For a single, unconnected model, it should return nothing assert len(related_models) == 0 model_2 = dataset.create_model('Model_B', description="model b", schema=[ ModelProperty("prop1", data_type=ModelPropertyType(data_type=str), title=True)]) relationship = dataset.create_relationship_type( 'New_Relationship_{}'.format(current_ts()), 'a new relationship') model_instance_1 = model_1.create_record({"prop1": "val1"}) model_instance_2 = model_2.create_record({"prop1": "val1"}) model_instance_1.relate_to(model_instance_2, relationship) related_models = model_1.get_connected() assert len(related_models) == 2 # For a connected model, return all connections + the model itself related_models = model_2.get_connected() assert len(related_models) == 2 # Check that get_connected_models from the dataset object also works related_models = dataset.get_connected_models(model_1.id) assert len(related_models) == 2 related_models = dataset.get_connected_models(model_2.id) assert len(related_models) == 2
def test_rollback_model_creation_with_invalid_properties(dataset): model_name = "New_Model_{}".format(current_ts()) invalid_schema = [("an_integer", int, "An Integer")] # Creating the model succeeds, but then creating properties fails with pytest.raises(Exception) as e: dataset.create_model( model_name, schema=invalid_schema, ) assert "Could not create model properties" in str(e.value) # Model creation is rolled back assert model_name not in dataset.models()
def simple_graph(client): """ Creates a small test graph in an independent dataset to de-couple from other tests """ test_dataset = create_test_dataset(client) model_1 = test_dataset.create_model( "Model_A", description="model a", schema=[ ModelProperty( "prop1", data_type=ModelPropertyType(data_type=str), title=True ) ], ) model_2 = test_dataset.create_model( "Model_B", description="model b", schema=[ ModelProperty( "prop1", data_type=ModelPropertyType(data_type=str), title=True ) ], ) relationship = test_dataset.create_relationship_type( "New_Relationship_{}".format(current_ts()), "a new relationship" ) model_instance_1 = model_1.create_record({"prop1": "val1"}) model_instance_2 = model_2.create_record({"prop1": "val1"}) model_instance_1.relate_to(model_instance_2, relationship) graph = Graph( test_dataset, models=[model_1, model_2], model_records=[model_instance_1, model_instance_2], relationships=[relationship], relationship_records=None, ) yield graph ds_id = test_dataset.id client._api.datasets.delete(test_dataset) all_dataset_ids = [x.id for x in client.datasets()] assert ds_id not in all_dataset_ids assert not test_dataset.exists
def test_models(dataset): schema = [('an_integer', int, 'An Integer', True), ('a_bool', bool), ('a_string', str), ('a_datetime', datetime.datetime)] display_name = 'A New Property' description = 'a new description' values = {'an_integer': 100, 'a_bool': True, 'a_string': 'fnsdlkn#$#42nlfds$3nlds$#@$23fdsnfkls', 'a_datetime': datetime.datetime.now()} ################################# ## Models ################################ models = dataset.models() new_model = dataset.create_model('New_Model_{}'.format(current_ts()), 'A New Model', 'a new model', schema) assert len(dataset.models()) == len(models) + 1 assert dataset.get_model(new_model.id) == new_model assert dataset.get_model(new_model.type) == new_model # Check that local changes get propagated new_model.add_property('a_new_property', str, display_name) new_model.description = description new_model.update() new_model = dataset.get_model(new_model.id) assert new_model.description == description assert new_model.get_property('a_new_property').display_name == display_name new_model.add_properties([('a_new_float', float), {'name': 'a_new_int', 'data_type': int}, 'a_new_string']) assert new_model.get_property('a_new_float').type == float assert new_model.get_property('a_new_int').type == int assert new_model.get_property('a_new_string').type == unicode nc_one = new_model.create_record(values) nc_two = new_model.create_record({'an_integer': 1, 'a_bool': False, 'a_string': '', 'a_datetime': datetime.datetime.now()}) nc_three = new_model.create_record({'an_integer': 10000, 'a_bool': False, 'a_string': '43132312', 'a_datetime': datetime.datetime.now()}) nc_four = new_model.create_record({'a_datetime': datetime.datetime.now()}) nc_delete_one = new_model.create_record({'a_datetime': datetime.datetime.now()}) nc_delete_two = new_model.create_record({'a_datetime': datetime.datetime.now()}) with pytest.raises(Exception): new_model.create_record() new_models_old = new_model.get_all() assert new_model.get_all(limit=1) == new_models_old[:1] assert new_model.get_all(limit=2, offset=2) == new_models_old[2:4] new_model.delete_records(nc_delete_one, nc_delete_two.id) new_models = new_model.get_all() assert len(new_models) == (len(new_models_old) - 2) assert nc_two.model == new_model assert nc_two.get('a_string') == '' nc_four.set('a_string', 'hello') assert nc_four.get('a_string') == new_model.get(nc_four).get('a_string') with pytest.raises(Exception): nc_four.set('an_integer', datetime.datetime.now()) assert nc_four.get('an_integer') == None nc_four.set('an_integer', 10) assert nc_four.get('an_integer') == 10 nc_delete_three = new_model.create_record({'a_string': 'delete me'}) assert len(new_model.get_all()) == len(new_models) + 1 nc_delete_three.delete() assert len(new_model.get_all()) == len(new_models) # cannot add a record id column using an existing name with pytest.raises(ValueError): new_model.get_all().as_dataframe( record_id_column_name=list(new_model.get_all().type.schema.keys())[0] ) # assert no extra columns are added by default df_cs_no_rec = new_model.get_all().as_dataframe() assert len(df_cs_no_rec.columns) == len(new_model.get_all().type.schema.keys()) # assert record id column is added when arg is present and valid df_cs = new_model.get_all().as_dataframe( record_id_column_name='record_id' ) # confirm that all record ids are present in this dataframe assert 'record_id' in df_cs.columns for record in new_model.get_all(): assert not df_cs.query('record_id == @record.id').empty ################################# ## Relationships ################################ relationships = dataset.relationships() new_relationship = dataset.create_relationship_type('New_Relationship_{}'.format( current_ts()), 'a new relationship') assert len(dataset.relationships()) == len(relationships) + 1 assert dataset.get_relationship(new_relationship.id) == new_relationship assert dataset.get_relationship(new_relationship.type) == new_relationship nr_one = new_relationship.relate(nc_one, nc_two) nr_four = new_relationship.relate(nc_four, nc_one) nr_five = new_relationship.relate(nc_four, nc_two) nr_two = nc_two.relate_to(nc_three, new_relationship) nr_three = nc_three.relate_to(nc_four, new_relationship) nr_six = nc_four.relate_to(nc_three, new_relationship) nr_seven = nc_four.relate_to(nc_one, relationship_type="goes_to") assert nr_seven[0].destination == nc_one.id assert nr_seven[0].source == nc_four.id assert nr_seven[0].type == "goes_to" assert len(nc_four.get_related(new_model.type)) == 5 new_relationships = new_relationship.get_all() nr_delete_three = new_relationship.relate(nc_one, nc_two) assert len(new_relationship.get_all()) == len(new_relationships) + 1 nr_delete_three.delete() assert len(new_relationship.get_all()) == len(new_relationships) df_rs = new_relationship.get_all().as_dataframe() p = DataPackage('test-csv', package_type='Tabular') dataset.add(p) dataset.update() assert p.exists p.relate_to(nc_one) p.relate_to(nc_two) nc_three.relate_to(p, new_relationship) new_relationship.relate(nc_four, p) assert len(nc_four.get_related(new_model.type)) == 5
def test_models(dataset): schema = [ ("an_integer", int, "An Integer", True), ("a_bool", bool), ("a_string", str), ("a_datetime", datetime.datetime), ] display_name = "A New Property" description = "a new description" values = { "an_integer": 100, "a_bool": True, "a_string": "fnsdlkn#$#42nlfds$3nlds$#@$23fdsnfkls", "a_datetime": datetime.datetime.now(), } ################################# ## Models ################################ models = dataset.models() new_model = dataset.create_model("New_Model_{}".format(current_ts()), "A New Model", "a new model", schema) assert len(dataset.models()) == len(models) + 1 assert dataset.get_model(new_model.id) == new_model assert dataset.get_model(new_model.type) == new_model # Check that local changes get propagated new_model.add_property("a_new_property", str, display_name) new_model.description = description new_model.update() new_model = dataset.get_model(new_model.id) assert new_model.description == description assert new_model.get_property( "a_new_property").display_name == display_name new_model.add_properties([ ("a_new_float", float), { "name": "a_new_int", "data_type": int }, "a_new_string", ]) assert new_model.get_property("a_new_float").type == float assert new_model.get_property("a_new_int").type == int assert new_model.get_property("a_new_string").type == unicode nc_one = new_model.create_record(values) nc_two = new_model.create_record({ "an_integer": 1, "a_bool": False, "a_string": "", "a_datetime": datetime.datetime.now(), }) nc_three = new_model.create_record({ "an_integer": 10000, "a_bool": False, "a_string": "43132312", "a_datetime": datetime.datetime.now(), }) nc_four = new_model.create_record({ "an_integer": 9292, "a_datetime": datetime.datetime.now() }) nc_delete_one = new_model.create_record({ "an_integer": 28, "a_datetime": datetime.datetime.now() }) nc_delete_two = new_model.create_record({ "an_integer": 300, "a_datetime": datetime.datetime.now() }) with pytest.raises(Exception): new_model.create_record() new_models_old = new_model.get_all() assert new_model.get_all(limit=1) == new_models_old[:1] assert new_model.get_all(limit=2, offset=2) == new_models_old[2:4] new_model.delete_records(nc_delete_one, nc_delete_two.id) new_models = new_model.get_all() assert len(new_models) == (len(new_models_old) - 2) assert nc_two.model == new_model assert nc_two.get("a_string") == "" nc_four.set("a_string", "hello") assert nc_four.get("a_string") == new_model.get(nc_four).get("a_string") with pytest.raises(Exception): nc_four.set("an_integer", datetime.datetime.now()) assert nc_four.get("an_integer") == 9292 nc_four.set("an_integer", 10) assert nc_four.get("an_integer") == 10 nc_delete_three = new_model.create_record({ "an_integer": 684, "a_string": "delete me" }) assert len(new_model.get_all()) == len(new_models) + 1 nc_delete_three.delete() assert len(new_model.get_all()) == len(new_models) # cannot add a record id column using an existing name with pytest.raises(ValueError): new_model.get_all().as_dataframe(record_id_column_name=list( new_model.get_all().type.schema.keys())[0]) # assert no extra columns are added by default df_cs_no_rec = new_model.get_all().as_dataframe() assert len(df_cs_no_rec.columns) == len( new_model.get_all().type.schema.keys()) # assert record id column is added when arg is present and valid df_cs = new_model.get_all().as_dataframe(record_id_column_name="record_id") # confirm that all record ids are present in this dataframe assert "record_id" in df_cs.columns for record in new_model.get_all(): assert not df_cs.query("record_id == @record.id").empty ################################# ## Relationships ################################ relationships = dataset.relationships() new_relationship = dataset.create_relationship_type( "New_Relationship_{}".format(current_ts()), "a new relationship") assert len(dataset.relationships()) == len(relationships) + 1 assert dataset.get_relationship(new_relationship.id) == new_relationship assert dataset.get_relationship(new_relationship.type) == new_relationship nr_one = new_relationship.relate(nc_one, nc_two) nr_four = new_relationship.relate(nc_four, nc_one) nr_five = new_relationship.relate(nc_four, nc_two) nr_two = nc_two.relate_to(nc_three, new_relationship) nr_three = nc_three.relate_to(nc_four, new_relationship) nr_six = nc_four.relate_to(nc_three, new_relationship) nr_seven = nc_four.relate_to(nc_one, relationship_type="goes_to") assert nr_seven[0].destination == nc_one.id assert nr_seven[0].source == nc_four.id assert nr_seven[0].type == "goes_to" assert len(nc_four.get_related(new_model.type)) == 5 new_relationships = new_relationship.get_all() nr_delete_three = new_relationship.relate(nc_one, nc_three) assert len(new_relationship.get_all()) == len(new_relationships) + 1 nr_delete_three.delete() assert len(new_relationship.get_all()) == len(new_relationships) df_rs = new_relationship.get_all().as_dataframe() p = DataPackage("test-csv", package_type="CSV") dataset.add(p) dataset.update() assert p.exists p.relate_to(nc_one) p.relate_to(nc_two) nc_three.relate_to(p, new_relationship) new_relationship.relate(nc_four, p) assert len(nc_four.get_related(new_model.type)) == 5