class TestAlgorithmSchemas(MockApiTest, ObjectTest): base_url = "/v1/algorithms/{algorithm.ext_id}/schemas" obj_key = "algorithmschema" obj_create_data = {"data": {"foo": "bar"}} obj_update_data = {"data": {"bar": "bam"}} fixtures = [ {"auth_client": auth_client}, {"organization": organization}, {"user": user}, {"project": lambda test, fixtures: project(test, fixtures, "example")}, {"algorithm": lambda test, fixtures: algorithm(test, fixtures, "project")}, {"algorithmschema": lambda test, fixtures: algorithmschema(test, fixtures)}, { "algorithm_algorithmschema": lambda test, fixtures: algorithm_algorithmschema( test, fixtures, algorithm="algorithm", algoschema="algorithmschema" ) }, { "object_role": lambda test, fixtures: object_role( test, fixtures, "admin", "admin", "organization", "project" ) }, ]
class TestCreateObjectRole(DatabaseTest): """ Test listing of projects """ fixtures = [ { "auth_client": auth_client }, { "organization": organization }, { "user": user }, { "project": lambda test, fixtures: project(test, fixtures, "example") }, ] def test_create_role(self): obj_role = ObjectRole.get_or_create(self.session, self.project, self.organization, "admin", "admin") self.session.commit() assert obj_role.id is not None same_obj_role = ObjectRole.get_or_create(self.session, self.project, self.organization, "admin", "admin") self.session.commit() assert same_obj_role.id == obj_role.id
class TestProjects(MockApiTest, ObjectTest): base_url = "/v1/projects" obj_key = "project" obj_create_data = { "path": "example/project", "name": "test", "data": {"foo": "bar"}, "description": "", "tags": ["my", "tags"], } obj_update_data = { "path": "another/path", "name": "another test", "data": {"bar": "baz"}, "description": "foo", "tags": ["one", "two"], } @property def create_url(self): return "v1/organizations/{}/projects".format(self.organization.source_id.hex()) fixtures = [ {"auth_client": auth_client}, {"organization": organization}, { "another_organization": lambda test, fixtures: organization( test, fixtures, name="another one" ) }, {"user": user}, {"project": lambda test, fixtures: project(test, fixtures, "example")}, # the next project is not visible to the user { "another_project": lambda test, fixtures: project( test, fixtures, "another_example", "another_organization" ) }, { "object_role": lambda test, fixtures: object_role( test, fixtures, "admin", "admin", "organization", "project" ) }, ]
class TestDatasetModelResults(MockApiTest, ObjectTest): obj_key = "dataset_model_result" obj_create_data = {"data": {"foo": "bar"}, "name": "test"} obj_update_data = {"data": {"bar": "bam"}, "name": "bar"} fixtures = [ { "auth_client": auth_client }, { "organization": organization }, { "user": user }, { "project": lambda test, fixtures: project(test, fixtures, "example") }, { "algorithm": lambda test, fixtures: algorithm(test, fixtures, "project") }, { "dataset": lambda test, fixtures: dataset(test, fixtures, name="foo/bar") }, { "model": model }, { "dataset_model_result": dataset_model_result }, { "object_role": lambda test, fixtures: object_role( test, fixtures, "admin", "admin", "organization", "project") }, ] @property def url(self): model = self.model return "/v1/models/{}/datasetresults".format(model.ext_id) @property def list_url(self): ds = self.dataset model = self.model return "/v1/datasets/{}/models/{}/results".format( ds.ext_id, model.ext_id) @property def create_url(self): return self.list_url
class TestDatasetResults(MockApiTest, ObjectTest): base_url = "/v1/datasets/{dataset.ext_id}/results" obj_key = "dataset_result" obj_create_data = {"data": {"foo": "bar"}, "name": "test"} obj_update_data = {"data": {"bar": "bam"}, "name": "bar"} fixtures = [ {"auth_client": auth_client}, {"organization": organization}, {"user": user}, {"project": lambda test, fixtures: project(test, fixtures, "example")}, {"dataset": lambda test, fixtures: dataset(test, fixtures, name="foo/bar")}, {"dataset_result": dataset_result}, { "object_role": lambda test, fixtures: object_role( test, fixtures, "admin", "admin", "organization", "project" ) }, ]
class TestAlgorithmResults(MockApiTest, ObjectTest): base_url = "/v1/algorithms/{algorithm.ext_id}/results" obj_key = "algorithm_result" obj_create_data = {"data": {"foo": "bars"}, "name": "another test"} obj_update_data = {"data": {"bar": "bam"}, "name": "bar"} fixtures = [ { "auth_client": auth_client }, { "organization": organization }, { "user": user }, { "project": lambda test, fixtures: project(test, fixtures, "example") }, { "algorithm": lambda test, fixtures: algorithm(test, fixtures, "project") }, { "dataset": lambda test, fixtures: dataset(test, fixtures, name="foo/bar") }, { "model": model }, { "algorithm_result": algorithm_result }, { "object_role": lambda test, fixtures: object_role( test, fixtures, "admin", "admin", "organization", "project") }, ]
class TestAlgorithmModels(MockApiTest, ObjectTest): base_url = "/v1/models" obj_key = "model" obj_create_data = {"data": {"foo": "bar"}} obj_update_data = {"data": {"bar": "bam"}} @property def list_url(self): return "/v1/algorithms/{}/models".format(self.algorithm.ext_id) @property def create_url(self): return "/v1/datasets/{}/algorithms/{}/models".format( self.dataset.ext_id, self.algorithm.ext_id ) fixtures = [ {"auth_client": auth_client}, {"organization": organization}, {"user": user}, {"project": lambda test, fixtures: project(test, fixtures, "example")}, {"algorithm": lambda test, fixtures: algorithm(test, fixtures, "project")}, {"dataset": lambda test, fixtures: dataset(test, fixtures, name="foo/bar")}, {"model": model}, {"model_result": model_result}, { "object_role": lambda test, fixtures: object_role( test, fixtures, "admin", "admin", "organization", "project" ) }, ] def _create(self, data): ds = self.dataset av = self.algorithm return self.app.post( "/v1/datasets/{}/algorithms/{}/models".format(ds.ext_id, av.ext_id), headers={"Authorization": "bearer test"}, json=data, )
class Tests(MockApiTest, ObjectTest): base_url = "/v1/algorithms" obj_key = "algorithm" obj_create_data = {"data": {"foo": "bar"}, "name": "test"} obj_update_data = {"data": {"bar": "bam"}, "name": "foo"} @property def list_url(self): return "/v1/projects/{}/algorithms".format(self.project.ext_id) @property def create_url(self): return self.list_url fixtures = [ { "auth_client": auth_client }, { "organization": organization }, { "user": user }, { "project": lambda test, fixtures: project(test, fixtures, "example") }, { "algorithm": lambda test, fixtures: algorithm(test, fixtures, "project") }, { "object_role": lambda test, fixtures: object_role( test, fixtures, "admin", "admin", "organization", "project") }, ]
class TestBulkInserts(DatabaseTest): """ Test bulk insertion of datapoints. """ fixtures = [ { "auth_client": auth_client }, { "organization": organization }, { "user": user }, { "project": lambda test, fixtures: project(test, fixtures, "example") }, { "dataset": dataset }, ] def test_bulk_inserts(self): dps = [] dsdps = [] ds = self.dataset n = 100 for i in range(n): dp = Datapoint(data={"i": i}) dps.append(dp) self.session.bulk_save_objects(dps, return_defaults=True) for i, dp in enumerate(dps): dsdp = DatasetDatapoint(dataset_id=ds.id, datapoint_id=dp.id) dsdps.append(dsdp) self.session.bulk_save_objects(dsdps, return_defaults=True) self.session.commit() assert self.session.query(Datapoint).count() == n assert self.session.query(DatasetDatapoint).count() == n def test_upserts(self): """ * We first upsert 1000 datapoints into the database. * Then we associate them with a given dataset. * We repeat this two times to ensure it works. """ ds = self.dataset n = 100 for j in range(2): dsdps = [] dps = [] for i in range(n): dp = { "data": { "i": i }, "hash": "sdfdsfsdf{}".format(i).encode("utf-8") } dps.append(dp) ids = self.session.execute( postgresql.insert( Datapoint.__table__).values(dps).on_conflict_do_update( set_={ "updated_at": func.now() }, index_elements=[Datapoint.hash], index_where=Datapoint.deleted_at == None, ).returning(Datapoint.id)) for i, id in enumerate(ids): dsdps.append({"dataset_id": ds.id, "datapoint_id": id[0]}) self.session.commit() ids = self.session.execute( postgresql.insert(DatasetDatapoint.__table__).values( dsdps).on_conflict_do_update( set_={ "updated_at": func.now() }, index_elements=[ DatasetDatapoint.dataset_id, DatasetDatapoint.datapoint_id, ], index_where=Datapoint.deleted_at == None, ).returning(DatasetDatapoint.id)) # we ensure the datapoints have been correctly inserted assert self.session.query(Datapoint).count() == n assert self.session.query(DatasetDatapoint).count() == n
class TestDatasets(MockApiTest, ObjectTest): base_url = "/v1/datasets" obj_key = "dataset" obj_create_data = { "name": "example/algo", "data": { "foo": "bar" }, "tags": ["my", "tags"], } obj_update_data = { "name": "another/path", "data": { "bar": "baz" }, "tags": ["one", "two"], } @property def list_url(self): return "/v1/projects/{}/datasets".format(self.project.ext_id) @property def create_url(self): return self.list_url fixtures = [ { "auth_client": auth_client }, { "organization": organization }, { "another_organization": lambda test, fixtures: organization( test, fixtures, name="another one") }, { "user": user }, { "project": project }, { "another_project": lambda test, fixtures: project(test, fixtures, "another/project", "another_organization") }, { "dataset": lambda test, fixtures: dataset(test, fixtures, "example") }, # the next dataset is not visible to the user { "another_dataset": lambda test, fixtures: dataset(test, fixtures, "another_example", "another_project") }, { "object_role": lambda test, fixtures: object_role( test, fixtures, "admin", "admin", "organization", "project") }, ]