def __init__(self, scheme=None, host=None, prefix=None, alternative_namespace=None, auth_client=None): self.version = None self.namespace = alternative_namespace if alternative_namespace is not None else "{}://{}".format(scheme, host) self.env = None self.config = NexusConfig(scheme, host, prefix, alternative_namespace) self._http_client = MockHttpClient(self.config.NEXUS_ENDPOINT, self.config.NEXUS_PREFIX, auth_client=auth_client, alternative_endpoint_writing=self.config.NEXUS_NAMESPACE) self.domains = DomainRepository(self._http_client) self.contexts = ContextRepository(self._http_client) self.organizations = OrganizationRepository(self._http_client) self.instances = InstanceRepository(self._http_client) self.schemas = SchemaRepository(self._http_client)
def setUp(self): logging.basicConfig(level=logging.DEBUG) self.repository = InstanceRepository(NexusClient()._http_client)
def _get_instance_uuid(self): single_result = self.repository.list( subpath="/hbp/core/schematest/v0.0.1", size=1, deprecated=None) instance = single_result.results[0] return InstanceRepository._extract_uuid(instance.result_id)
class TestInstanceRepository(TestCase): default_prefix = "hbp" def _get_instance_uuid(self): single_result = self.repository.list( subpath="/hbp/core/schematest/v0.0.1", size=1, deprecated=None) instance = single_result.results[0] return InstanceRepository._extract_uuid(instance.result_id) @staticmethod def _assert_valid_default_entity(result, identifier): assert_that(result, instance_of(Instance)) assert_that(result.data, not_none()) assert_that(result.get_revision(), greater_than(0)) assert_that(result.id, equal_to(identifier)) assert_that(result.path, equal_to("/data/" + identifier)) @staticmethod def _assert_valid_search_list_result(search, expected_length=None): assert_that(search, not_none()) assert_that(search, instance_of(SearchResultList)) assert_that(search.results, not_none()) if expected_length is not None: assert_that(len(search.results), expected_length) def setUp(self): logging.basicConfig(level=logging.DEBUG) self.repository = InstanceRepository(NexusClient()._http_client) def test_read_latest_revision(self): uuid = self._get_instance_uuid() result = self.repository.read(self.default_prefix, "core", "schematest", "v0.0.1", uuid) self._assert_valid_default_entity(result, "hbp/core/schematest/v0.0.1/" + uuid) def test_read_revision_one(self): uuid = self._get_instance_uuid() result = self.repository.read(self.default_prefix, "core", "schematest", "v0.0.1", uuid, 1) self._assert_valid_default_entity(result, "hbp/core/schematest/v0.0.1/" + uuid) assert_that(result.get_revision(), equal_to(1)) def test_read_unknown(self): result = self.repository.read("acme_corp", "core", "schematest", "v0.0.1", "something") assert_that(result, none()) # ATTENTION: This test creates entities. The execution of the test therefore increases data volume def test_create_and_deprecate(self): entity = Instance.create_new(self.default_prefix, "core", "schematest", "v0.0.1", self.test_instance) result = self.repository.create(entity) assert_that(result, equal_to(entity)) self._assert_valid_default_entity(result, entity.id) assert_that(result.get_revision(), equal_to(1)) self._test_deprecate(entity) def _test_deprecate(self, entity): # deprecate organization result = self.repository.delete(entity) self._assert_valid_default_entity(result, entity.id) assert_that(result.get_revision(), equal_to(2)) def test_update(self): entity = self.repository.read(self.default_prefix, "core", "schematest", "v0.0.1", self._get_instance_uuid()) entity.data["description"] = "New description" entity = self.repository.update(entity) assert_that(entity.get_data("description"), not_none()) entity.data["description"] = None self.repository.update(entity) def test_search_fulltext(self): search = self.repository.list(full_text_query="Test") self._assert_valid_search_list_result(search, 1) def test_search_limit(self): search = self.repository.list(size=1) self._assert_valid_search_list_result(search, 1) assert_that(len(search.results), less_than(search.total)) def test_list_default(self): search = self.repository.list() self._assert_valid_search_list_result(search) assert_that(len(search.results), greater_than(0)) def test_list_incl_deprecated(self): search = self.repository.list(deprecated=None) self._assert_valid_search_list_result(search) assert_that(len(search.results), greater_than(0)) def test_list_and_resolve_first(self): search = self.repository.list() self._assert_valid_search_list_result(search) assert_that(len(search.results), greater_than(0)) result = self.repository.resolve(search.results[0]) self._assert_valid_default_entity(result, result.id) def test_list_and_resolve_all(self): search = self.repository.list() self._assert_valid_search_list_result(search) assert_that(len(search.results), greater_than(0)) result = self.repository.resolve_all(search) assert_that(result, not_none()) assert_that(result, instance_of(list)) assert_that(len(result), greater_than(0)) self._assert_valid_default_entity(result[0], result[0].id) def test_list_resolved(self): search = self.repository.list(resolved=True) self._assert_valid_search_list_result(search) assert_that(len(search.results), greater_than(0)) test_instance = { "hbp:hello_world": "Test", "@context": [{ "hbp": "http://nexus.example.com/v0/vocab/hbp/core/schematest/", "@vocab": "http://*****:*****@type": "hbp:SchemaTestEntity" } def test_find_by_field(self): search = self.repository.find_by_field("/shape/core/activity/v0.0.1", "http://schema.org/identifier", "NCRMI_210") print search
test_schema_obj = Schema.create_new(organisation_name, domain_name, schema_name, version, test_schema) schema_repo = SchemaRepository(nar_client) schema_repo.create(test_schema_obj) schema_repo.publish(test_schema_obj, True) # # Read a schema # testschema_read = schema_repo.read(organisation_name, domain_name, schema_name, version) print(testschema_read) # # 5. Create a test instance. (test0) # TODO test_instance = {} # from pyxus.resources.repository import InstanceRepository from pyxus.resources.entity import Instance # Create an instance of naro/tests/testschema (v0.0.2) instance_repo = InstanceRepository(nar_client) test0 = Instance.create_new(organisation_name, domain_name, schema_name, version, test_instance) instance_repo.create(test0) # get uuid single_result = instance_repo.list(subpath=fpath, size=1, deprecated=None) instance = single_result.results[0] InstanceRepository._extract_uuid(instance.result_id)