Ejemplo n.º 1
0
 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)
Ejemplo n.º 2
0
 def setUp(self):
     logging.basicConfig(level=logging.DEBUG)
     self.repository = SchemaRepository(NexusClient()._http_client)
     self._create_and_publish_testschema()
     self._create_testschema_v2()
Ejemplo n.º 3
0
class TestSchemaRepository(TestCase):

    default_prefix = "hbp"

    def setUp(self):
        logging.basicConfig(level=logging.DEBUG)
        self.repository = SchemaRepository(NexusClient()._http_client)
        self._create_and_publish_testschema()
        self._create_testschema_v2()

    @staticmethod
    def _assert_valid_default_entity(result, identifier="hbp/core/testschema/v0.0.1"):
        assert_that(result, instance_of(Schema))
        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("/schemas/" + 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 test_read_latest_revision(self):
        result = self.repository.read(self.default_prefix, "core", "testschema", "v0.0.1")
        self._assert_valid_default_entity(result)

    def test_read_revision_one(self):
        result = self.repository.read(self.default_prefix, "core", "testschema", "v0.0.1", 1)
        self._assert_valid_default_entity(result)
        assert_that(result.get_revision(), equal_to(1))

    def test_read_unknown(self):
        result = self.repository.read("acme_corp", "core", "testschema", "v0.0.1")
        assert_that(result, none())

#   ATTENTION: This test creates entities with random names. The execution of the test therefore increases data volume
    def test_create_and_deprecate(self):
        random_name = "{}".format(uuid.uuid4()).replace("-", "")
        entity = Schema.create_new(self.default_prefix, "core", random_name, "v0.0.1", self.test_schema)
        result = self.repository.create(entity)
        assert_that(result, equal_to(entity))
        self._assert_valid_default_entity(result, self.default_prefix+"/core/"+random_name+"/v0.0.1")
        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))

    #   ATTENTION: This test creates entities with random names. The execution of the test therefore increases data volume
    def test_create_and_publish(self):
        random_name = "{}".format(uuid.uuid4()).replace("-", "")
        entity = Schema.create_new(self.default_prefix, "core", random_name, "v0.0.1", self.test_schema)
        result = self.repository.create(entity)
        assert_that(result, equal_to(entity))
        self._assert_valid_default_entity(result, self.default_prefix + "/core/" + random_name + "/v0.0.1")
        assert_that(result.get_revision(), equal_to(1))

        result = self.repository.publish(entity, True)
        self._assert_valid_default_entity(result, self.default_prefix + "/core/" + random_name + "/v0.0.1")
        assert_that(result.is_published(), equal_to(True))

    def test_update(self):
        entity = self.repository.read(self.default_prefix, "core", "testschema", "v0.0.1")
        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):
        self.test_create_and_publish()
        search = self.repository.list(full_text_query="schematest")
        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 _create_and_publish_testschema(self):
        schema = self.repository.read("hbp", "core", "testschema", "v0.0.1")
        if schema is None:
            schema = self.repository.create(Schema.create_new("hbp", "core", "testschema", "v0.0.1", self.test_schema))
        if not schema.is_published():
            self.repository.publish(schema, True)

    def _create_testschema_v2(self):
        schema = self.repository.read("hbp", "core", "testschema", "v0.0.1")
        if schema is None:
            self.repository.create(Schema.create_new("hbp", "core", "testschema", "v0.0.1", self.test_schema))

    test_schema = {
        "@id": "http://nexus.example.com/v0/schemas/hbp/core/testschema/v0.0.1",
        "@type": "owl:Ontology",
        "@context": {
            "maxCount": {
                "@id": "sh:maxCount",
                "@type": "xsd:integer"
            },
            "minCount": {
                "@id": "sh:minCount",
                "@type": "xsd:integer"
            },
            "datatype": {
                "@id": "sh:datatype",
                "@type": "@id"
            },
            "name": "sh:name",
            "path": {
                "@id": "sh:path",
                "@type": "@id"
            },
            "nodeKind": {
                "@id": "node:Kind",
                "@type": "@id"
            },
            "description": "sh:description",
            "class": {
                "@id": "sh:class",
                "@type": "@id"
            },
            "property": {
                "@id": "sh:property",
                "@type": "@id"
            },
            "isDefinedBy": {
                "@id": "rdfs:isDefinedBy",
                "@type": "@id"
            },
            "targetClass": {
                "@id": "sh:targetClass",
                "@type": "@id"
            },
            "targetObjectOf": {
                "@id": "sh:targetObjectOf",
                "@type": "@id"
            },
            "node": {
                "@id": "sh:node",
                "@type": "@id"
            },
            "rest": {
                "@id": "http://www.w3.org/1999/02/22-rdf-syntax-ns#rest",
                "@type": "@id"
            },
            "first": "http://www.w3.org/1999/02/22-rdf-syntax-ns#first",
            "in": {
                "@id": "sh:in",
                "@type": "@id"
            },
            "schema": "http://schema.org/",
            "this": "http://nexus.example.com/v0/vocab/hbp/core/testschema/v0.0.1/shape/",
            "hbp": "http://nexus.example.com/v0/vocab/hbp/core/testschema/",
            "sh": "http://www.w3.org/ns/shacl#",
            "owl": "http://www.w3.org/2002/07/owl#",
            "xsd": "http://www.w3.org/2001/XMLSchema#",
            "rdfs": "http://www.w3.org/2000/01/rdf-schema#",
            "shape": {
                "@reverse": "rdfs:isDefinedBy",
                "@type": "@id"
            }
        },
        "shape": [
            {
                "@id": "hbp:SchemaTestShape",
                "@type": "sh:NodeShape",
                "property": [
                    {
                        "description": "Hello World",
                        "path": "hbp:hello_world",
                        "maxCount": 1,
                        "datatype": "xsd:string",
                        "name": "hello world"
                    }
                ],
                "targetClass": "hbp:SchemaTestEntity"
            }
        ]
    }
Ejemplo n.º 4
0
            "maxCount": 1,
            "datatype": "xsd:string"
        }],
        "targetClass":
        "foaf:Person"
    }]
}

from pyxus.resources.repository import SchemaRepository, Schema
#
# Create/publish a dummy schema called 'foafsh_schema'
# organization , domain, schema_name, version, json_ld_owl
#
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 = {}
#