From f9eec185bf3d857c220e5d78de75ec6713437330 Mon Sep 17 00:00:00 2001 From: Matthias Baumgartner Date: Wed, 1 Mar 2023 12:39:42 +0100 Subject: Construct Graph and Nodes with AC instead of user --- test/front/test_bsfs.py | 3 +- test/front/test_builder.py | 3 +- test/graph/ac/test_base.py | 83 +++++++++++++++++++++++++ test/graph/ac/test_null.py | 30 +++++++++ test/graph/test_graph.py | 74 +++++++++++----------- test/graph/test_nodes.py | 151 +++++++++++++++++++++++---------------------- test/graph/test_walk.py | 4 +- 7 files changed, 235 insertions(+), 113 deletions(-) create mode 100644 test/graph/ac/test_base.py (limited to 'test') diff --git a/test/front/test_bsfs.py b/test/front/test_bsfs.py index 0d7f383..4eb36c3 100644 --- a/test/front/test_bsfs.py +++ b/test/front/test_bsfs.py @@ -9,6 +9,7 @@ import unittest # bsie imports from bsfs.graph import Graph +from bsfs.graph.ac import NullAC from bsfs.triple_store import SparqlStore from bsfs.utils import errors, URI @@ -25,7 +26,7 @@ class TestBSFS(unittest.TestCase): graph = Open(config) self.assertIsInstance(graph, Graph) self.assertIsInstance(graph._backend, SparqlStore) - self.assertEqual(graph._user, URI('http://example.com/me')) + self.assertEqual(graph._ac, NullAC(graph._backend, URI('http://example.com/me'))) # invalid config raises an error self.assertRaises(errors.ConfigError, Open, {}) diff --git a/test/front/test_builder.py b/test/front/test_builder.py index 08f2027..0328a0a 100644 --- a/test/front/test_builder.py +++ b/test/front/test_builder.py @@ -9,6 +9,7 @@ import unittest # bsie imports from bsfs.graph import Graph +from bsfs.graph.ac import NullAC from bsfs.triple_store import SparqlStore from bsfs.utils import errors, URI @@ -40,7 +41,7 @@ class TestBuilder(unittest.TestCase): graph = build_graph({'Graph': {'backend': {'SparqlStore': {}}, 'user': 'http://example.com/me'}}) self.assertIsInstance(graph, Graph) self.assertIsInstance(graph._backend, SparqlStore) - self.assertEqual(graph._user, URI('http://example.com/me')) + self.assertEqual(graph._ac, NullAC(graph._backend, URI('http://example.com/me'))) # cannot create an invalid graph self.assertRaises(errors.ConfigError, build_graph, {'MyGraph': {}}) # must pass a dict diff --git a/test/graph/ac/test_base.py b/test/graph/ac/test_base.py new file mode 100644 index 0000000..ad24e3d --- /dev/null +++ b/test/graph/ac/test_base.py @@ -0,0 +1,83 @@ +""" + +Part of the bsfs test suite. +A copy of the license is provided with the project. +Author: Matthias Baumgartner, 2022 +""" +# imports +import unittest + +# bsie imports +from bsfs import schema as bsc +from bsfs.namespace import ns +from bsfs.query import ast +from bsfs.triple_store import SparqlStore +from bsfs.utils import URI + +# objects to test +from bsfs.graph.ac.base import AccessControlBase + + +## code ## + +class StubAC(AccessControlBase): + def is_protected_predicate(self, pred): + pass + def create(self, node_type, guids): + pass + def link_from_node(self, node_type, guids): + pass + def link_to_node(self, node_type, guids): + pass + def write_literal(self, node_type, guids): + pass + def createable(self, node_type, guids): + pass + def filter_read(self, node_type, query): + pass + def fetch_read(self, node_type, query): + pass + + +class TestAccessControlBase(unittest.TestCase): + def setUp(self): + self.backend = SparqlStore() + self.user = URI('http://www.example.com/me') + + def test_essentials(self): + ac = StubAC(self.backend, self.user) + # equal construction means equal instance + self.assertEqual(StubAC(self.backend, self.user), StubAC(self.backend, self.user)) + self.assertEqual(hash(StubAC(self.backend, self.user)), hash(StubAC(self.backend, self.user))) + self.assertEqual(ac, StubAC(self.backend, self.user)) + self.assertEqual(hash(ac), hash(StubAC(self.backend, self.user))) + # equivalence respects type + class Foo(): pass + self.assertNotEqual(ac, 1234) + self.assertNotEqual(hash(ac), hash(1234)) + self.assertNotEqual(ac, 'hello') + self.assertNotEqual(hash(ac), hash('hello')) + self.assertNotEqual(ac, Foo()) + self.assertNotEqual(hash(ac), hash(Foo())) + # equivalence respects backend + self.assertNotEqual(ac, StubAC(SparqlStore(), self.user)) + self.assertNotEqual(hash(ac), hash(StubAC(SparqlStore(), self.user))) + # equivalence respects user + self.assertNotEqual(ac, StubAC(self.backend, URI('http://www.example.com/you'))) + self.assertNotEqual(hash(ac), hash(StubAC(self.backend, URI('http://www.example.com/you')))) + # string conversion + self.assertEqual(str(ac), f'StubAC({self.user})') + self.assertEqual(repr(ac), f'StubAC({self.user})') + # string conversion respects user + self.assertEqual(str(StubAC(self.backend, URI('http://www.example.com/you'))), + f'StubAC(http://www.example.com/you)') + self.assertEqual(repr(StubAC(self.backend, URI('http://www.example.com/you'))), + f'StubAC(http://www.example.com/you)') + + +## main ## + +if __name__ == '__main__': + unittest.main() + +## EOF ## diff --git a/test/graph/ac/test_null.py b/test/graph/ac/test_null.py index e35852d..7d25980 100644 --- a/test/graph/ac/test_null.py +++ b/test/graph/ac/test_null.py @@ -68,6 +68,36 @@ class TestNullAC(unittest.TestCase): self.ent_type = self.backend.schema.node(ns.bsfs.Entity) self.ent_ids = {URI('http://www.example.com/me/entity#1234'), URI('http://www.example.com/me/entity#4321')} + def test_essentials(self): + ac = NullAC(self.backend, self.user) + # equal construction means equal instance + self.assertEqual(NullAC(self.backend, self.user), NullAC(self.backend, self.user)) + self.assertEqual(hash(NullAC(self.backend, self.user)), hash(NullAC(self.backend, self.user))) + self.assertEqual(ac, NullAC(self.backend, self.user)) + self.assertEqual(hash(ac), hash(NullAC(self.backend, self.user))) + # equivalence respects type + class Foo(): pass + self.assertNotEqual(ac, 1234) + self.assertNotEqual(hash(ac), hash(1234)) + self.assertNotEqual(ac, 'hello') + self.assertNotEqual(hash(ac), hash('hello')) + self.assertNotEqual(ac, Foo()) + self.assertNotEqual(hash(ac), hash(Foo())) + # equivalence respects backend + self.assertNotEqual(ac, NullAC(SparqlStore(), self.user)) + self.assertNotEqual(hash(ac), hash(NullAC(SparqlStore(), self.user))) + # equivalence respects user + self.assertNotEqual(ac, NullAC(self.backend, URI('http://www.example.com/you'))) + self.assertNotEqual(hash(ac), hash(NullAC(self.backend, URI('http://www.example.com/you')))) + # string conversion + self.assertEqual(str(ac), f'NullAC({self.user})') + self.assertEqual(repr(ac), f'NullAC({self.user})') + # string conversion respects user + self.assertEqual(str(NullAC(self.backend, URI('http://www.example.com/you'))), + f'NullAC(http://www.example.com/you)') + self.assertEqual(repr(NullAC(self.backend, URI('http://www.example.com/you'))), + f'NullAC(http://www.example.com/you)') + def test_is_protected_predicate(self): ac = NullAC(self.backend, self.user) self.assertTrue(ac.is_protected_predicate(self.p_created)) diff --git a/test/graph/test_graph.py b/test/graph/test_graph.py index 5db1fd2..d89d346 100644 --- a/test/graph/test_graph.py +++ b/test/graph/test_graph.py @@ -9,6 +9,7 @@ import unittest # bsie imports from bsfs import schema +from bsfs.graph.ac import NullAC from bsfs.graph.nodes import Nodes from bsfs.namespace import ns from bsfs.query import ast @@ -23,94 +24,95 @@ from bsfs.graph.graph import Graph class TestGraph(unittest.TestCase): def setUp(self): - self.user = URI('http://example.com/me') self.backend = SparqlStore.Open() self.backend.schema = schema.from_string(''' prefix rdfs: prefix bsfs: bsfs:Entity rdfs:subClassOf bsfs:Node . ''') + self.user = URI('http://example.com/me') + self.ac = NullAC(self.backend, self.user) def test_str(self): - self.assertEqual(str(Graph(self.backend, self.user)), - 'Graph(SparqlStore(uri=None), http://example.com/me)') - self.assertEqual(repr(Graph(self.backend, self.user)), - 'Graph(backend=SparqlStore(uri=None), user=http://example.com/me)') + self.assertEqual(str(Graph(self.backend, self.ac)), + 'Graph(SparqlStore(uri=None))') + self.assertEqual(repr(Graph(self.backend, self.ac)), + 'Graph(SparqlStore(uri=None), NullAC(http://example.com/me))') # str respects backend class Foo(SparqlStore): pass - self.assertEqual(str(Graph(Foo.Open(), self.user)), - 'Graph(Foo(uri=None), http://example.com/me)') - self.assertEqual(repr(Graph(Foo.Open(), self.user)), - 'Graph(backend=Foo(uri=None), user=http://example.com/me)') + self.assertEqual(str(Graph(Foo.Open(), self.ac)), + 'Graph(Foo(uri=None))') + self.assertEqual(repr(Graph(Foo.Open(), self.ac)), + 'Graph(Foo(uri=None), NullAC(http://example.com/me))') # str respect user - self.assertEqual(str(Graph(self.backend, URI('http://example.com/you'))), - 'Graph(SparqlStore(uri=None), http://example.com/you)') - self.assertEqual(repr(Graph(self.backend, URI('http://example.com/you'))), - 'Graph(backend=SparqlStore(uri=None), user=http://example.com/you)') + self.assertEqual(str(Graph(self.backend, NullAC(self.backend, URI('http://example.com/you')))), + 'Graph(SparqlStore(uri=None))') + self.assertEqual(repr(Graph(self.backend, NullAC(self.backend, URI('http://example.com/you')))), + 'Graph(SparqlStore(uri=None), NullAC(http://example.com/you))') # str respects type class Bar(Graph): pass - self.assertEqual(str(Bar(self.backend, self.user)), - 'Bar(SparqlStore(uri=None), http://example.com/me)') - self.assertEqual(repr(Bar(self.backend, self.user)), - 'Bar(backend=SparqlStore(uri=None), user=http://example.com/me)') + self.assertEqual(str(Bar(self.backend, self.ac)), + 'Bar(SparqlStore(uri=None))') + self.assertEqual(repr(Bar(self.backend, self.ac)), + 'Bar(SparqlStore(uri=None), NullAC(http://example.com/me))') def test_equality(self): - graph = Graph(self.backend, self.user) + graph = Graph(self.backend, self.ac) # instance is equal to itself self.assertEqual(graph, graph) self.assertEqual(hash(graph), hash(graph)) # instance is equal to a clone - self.assertEqual(graph, Graph(self.backend, self.user)) - self.assertEqual(hash(graph), hash(Graph(self.backend, self.user))) + self.assertEqual(graph, Graph(self.backend, self.ac)) + self.assertEqual(hash(graph), hash(Graph(self.backend, self.ac))) # equality respects backend - self.assertNotEqual(graph, Graph(SparqlStore.Open(), self.user)) - self.assertNotEqual(hash(graph), hash(Graph(SparqlStore.Open(), self.user))) + self.assertNotEqual(graph, Graph(SparqlStore.Open(), self.ac)) + self.assertNotEqual(hash(graph), hash(Graph(SparqlStore.Open(), self.ac))) # equality respects user self.assertNotEqual(graph, Graph(self.backend, URI('http://example.com/you'))) self.assertNotEqual(hash(graph), hash(Graph(self.backend, URI('http://example.com/you')))) def test_essentials(self): - graph = Graph(self.backend, self.user) + graph = Graph(self.backend, self.ac) # schema self.assertEqual(graph.schema, self.backend.schema) self.assertRaises(AttributeError, setattr, graph, 'schema', None) def test_node(self): - graph = Graph(self.backend, self.user) + graph = Graph(self.backend, self.ac) guid = URI('http://example.com/me/entity#1234') # returns a Nodes instance self.assertEqual( graph.node(ns.bsfs.Entity, guid), - Nodes(self.backend, self.user, graph.schema.node(ns.bsfs.Entity), {guid})) + Nodes(self.backend, self.ac, graph.schema.node(ns.bsfs.Entity), {guid})) # node_type must be in the schema self.assertRaises(KeyError, graph.node, ns.bsfs.Invalid, guid) def test_nodes(self): - graph = Graph(self.backend, self.user) + graph = Graph(self.backend, self.ac) guids = {URI('http://example.com/me/entity#1234'), URI('http://example.com/me/entity#4321')} # returns a Nodes instance self.assertEqual( graph.nodes(ns.bsfs.Entity, guids), - Nodes(self.backend, self.user, graph.schema.node(ns.bsfs.Entity), guids)) + Nodes(self.backend, self.ac, graph.schema.node(ns.bsfs.Entity), guids)) # node_type must be in the schema self.assertRaises(KeyError, graph.nodes, ns.bsfs.Invalid, guids) def test_all(self): - graph = Graph(self.backend, self.user) + graph = Graph(self.backend, self.ac) # resulting nodes can be empty self.assertEqual(graph.all(ns.bsfs.Entity), - Nodes(self.backend, self.user, graph.schema.node(ns.bsfs.Entity), set())) + Nodes(self.backend, self.ac, graph.schema.node(ns.bsfs.Entity), set())) # resulting nodes contains all nodes of the respective type guids = {URI('http://example.com/me/entity#1234'), URI('http://example.com/me/entity#4321')} self.backend.create(graph.schema.node(ns.bsfs.Entity), guids) self.assertEqual(graph.all(ns.bsfs.Entity), - Nodes(self.backend, self.user, graph.schema.node(ns.bsfs.Entity), guids)) + Nodes(self.backend, self.ac, graph.schema.node(ns.bsfs.Entity), guids)) # node_type must be in the schema self.assertRaises(KeyError, graph.all, ns.bsfs.Invalid) def test_migrate(self): # setup - graph = Graph(self.backend, self.user) + graph = Graph(self.backend, self.ac) # argument must be a schema class Foo(): pass @@ -162,10 +164,10 @@ class TestGraph(unittest.TestCase): prefix bsfs: prefix bsm: bsfs:Number rdfs:subClassOf bsfs:Literal . - xsd:integer rdfs:subClassOf bsfs:Number . + xsd:float rdfs:subClassOf bsfs:Number . bsm:t_created rdfs:subClassOf bsfs:Predicate ; rdfs:domain bsfs:Node ; - rdfs:range xsd:integer ; + rdfs:range xsd:float ; bsfs:unique "true"^^xsd:boolean . ''')) @@ -203,16 +205,16 @@ class TestGraph(unittest.TestCase): prefix bsfs: prefix bsm: bsfs:Number rdfs:subClassOf bsfs:Literal . - xsd:integer rdfs:subClassOf bsfs:Number . + xsd:float rdfs:subClassOf bsfs:Number . bsm:t_created rdfs:subClassOf bsfs:Predicate ; rdfs:domain bsfs:Node ; - rdfs:range xsd:integer ; + rdfs:range xsd:float ; bsfs:unique "true"^^xsd:boolean . ''')) def test_get(self): # setup - graph = Graph(self.backend, self.user) + graph = Graph(self.backend, self.ac) graph.migrate(schema.from_string(''' prefix rdfs: prefix xsd: diff --git a/test/graph/test_nodes.py b/test/graph/test_nodes.py index 6bb3ef3..9541656 100644 --- a/test/graph/test_nodes.py +++ b/test/graph/test_nodes.py @@ -14,6 +14,7 @@ import rdflib # bsie imports from bsfs import schema as bsc +from bsfs.graph.ac import NullAC from bsfs.graph.walk import Walk from bsfs.namespace import Namespace, ns from bsfs.triple_store.sparql import SparqlStore @@ -107,6 +108,7 @@ class TestNodes(unittest.TestCase): } # Nodes constructor args self.user = URI('http://example.com/me') + self.ac = NullAC(self.backend, self.user) # set args self.tag_type = self.backend.schema.node(ns.bsfs.Tag) self.ent_type = self.backend.schema.node(ns.bsfs.Entity) @@ -128,65 +130,65 @@ class TestNodes(unittest.TestCase): def test_str(self): # str baseline - nodes = Nodes(self.backend, self.user, self.ent_type, self.ent_ids) + nodes = Nodes(self.backend, self.ac, self.ent_type, self.ent_ids) self.assertEqual(str(nodes), f'Nodes({self.ent_type}, {self.ent_ids})') - self.assertEqual(repr(nodes), f'Nodes({self.backend}, {self.user}, {self.ent_type}, {self.ent_ids})') + self.assertEqual(repr(nodes), f'Nodes({self.backend}, {self.ac}, {self.ent_type}, {self.ent_ids})') # str respects node_type - nodes = Nodes(self.backend, self.user, self.tag_type, self.tag_ids) + nodes = Nodes(self.backend, self.ac, self.tag_type, self.tag_ids) self.assertEqual(str(nodes), f'Nodes({self.tag_type}, {self.tag_ids})') - self.assertEqual(repr(nodes), f'Nodes({self.backend}, {self.user}, {self.tag_type}, {self.tag_ids})') + self.assertEqual(repr(nodes), f'Nodes({self.backend}, {self.ac}, {self.tag_type}, {self.tag_ids})') # str respects guids - nodes = Nodes(self.backend, self.user, self.ent_type, {URI('http://example.com/me/entity#foo')}) + nodes = Nodes(self.backend, self.ac, self.ent_type, {URI('http://example.com/me/entity#foo')}) self.assertEqual(str(nodes), f'Nodes({self.ent_type}, {{\'http://example.com/me/entity#foo\'}})') - self.assertEqual(repr(nodes), f'Nodes({self.backend}, {self.user}, {self.ent_type}, {{\'http://example.com/me/entity#foo\'}})') + self.assertEqual(repr(nodes), f'Nodes({self.backend}, {self.ac}, {self.ent_type}, {{\'http://example.com/me/entity#foo\'}})') # repr respects backend class Foo(SparqlStore): pass backend = Foo.Open() backend.schema = self.backend.schema - nodes = Nodes(backend, self.user, self.ent_type, self.ent_ids) - self.assertEqual(repr(nodes), f'Nodes({backend}, {self.user}, {self.ent_type}, {self.ent_ids})') + nodes = Nodes(backend, self.ac, self.ent_type, self.ent_ids) + self.assertEqual(repr(nodes), f'Nodes({backend}, {self.ac}, {self.ent_type}, {self.ent_ids})') # repr respects user - nodes = Nodes(self.backend, URI('http://example.com/you'), self.ent_type, self.ent_ids) - self.assertEqual(repr(nodes), f'Nodes({self.backend}, http://example.com/you, {self.ent_type}, {self.ent_ids})') + nodes = Nodes(self.backend, NullAC(self.backend, URI('http://example.com/you')), self.ent_type, self.ent_ids) + self.assertEqual(repr(nodes), f'Nodes({self.backend}, NullAC(http://example.com/you), {self.ent_type}, {self.ent_ids})') def test_equality(self): - nodes = Nodes(self.backend, self.user, self.ent_type, self.ent_ids) + nodes = Nodes(self.backend, self.ac, self.ent_type, self.ent_ids) # instance is equal to itself self.assertEqual(nodes, nodes) self.assertEqual(hash(nodes), hash(nodes)) # instance is equal to a clone - self.assertEqual(nodes, Nodes(self.backend, self.user, self.ent_type, self.ent_ids)) - self.assertEqual(Nodes(self.backend, self.user, self.ent_type, self.ent_ids), nodes) - self.assertEqual(hash(nodes), hash(Nodes(self.backend, self.user, self.ent_type, self.ent_ids))) + self.assertEqual(nodes, Nodes(self.backend, self.ac, self.ent_type, self.ent_ids)) + self.assertEqual(Nodes(self.backend, self.ac, self.ent_type, self.ent_ids), nodes) + self.assertEqual(hash(nodes), hash(Nodes(self.backend, self.ac, self.ent_type, self.ent_ids))) # equality respects backend backend = SparqlStore.Open() backend.schema = self.backend.schema - self.assertNotEqual(nodes, Nodes(backend, self.user, self.ent_type, self.ent_ids)) - self.assertNotEqual(hash(nodes), hash(Nodes(backend, self.user, self.ent_type, self.ent_ids))) + self.assertNotEqual(nodes, Nodes(backend, self.ac, self.ent_type, self.ent_ids)) + self.assertNotEqual(hash(nodes), hash(Nodes(backend, self.ac, self.ent_type, self.ent_ids))) # equality respects user - self.assertNotEqual(nodes, Nodes(self.backend, URI('http://example.com/you'), self.ent_type, self.ent_ids)) - self.assertNotEqual(hash(nodes), hash(Nodes(self.backend, URI('http://example.com/you'), self.ent_type, self.ent_ids))) + self.assertNotEqual(nodes, Nodes(self.backend, NullAC(self.backend, URI('http://example.com/you')), self.ent_type, self.ent_ids)) + self.assertNotEqual(hash(nodes), hash(Nodes(self.backend, NullAC(self.backend, URI('http://example.com/you')), self.ent_type, self.ent_ids))) # equality respects node_type - self.assertNotEqual(nodes, Nodes(self.backend, self.user, self.tag_type, self.ent_ids)) - self.assertNotEqual(hash(nodes), hash(Nodes(self.backend, self.user, self.tag_type, self.ent_ids))) + self.assertNotEqual(nodes, Nodes(self.backend, self.ac, self.tag_type, self.ent_ids)) + self.assertNotEqual(hash(nodes), hash(Nodes(self.backend, self.ac, self.tag_type, self.ent_ids))) # equality respects guids - self.assertNotEqual(nodes, Nodes(self.backend, self.user, self.ent_type, self.tag_ids)) - self.assertNotEqual(hash(nodes), hash(Nodes(self.backend, self.user, self.ent_type, self.tag_ids))) + self.assertNotEqual(nodes, Nodes(self.backend, self.ac, self.ent_type, self.tag_ids)) + self.assertNotEqual(hash(nodes), hash(Nodes(self.backend, self.ac, self.ent_type, self.tag_ids))) def test_properties(self): # node_type self.assertEqual(self.ent_type, Nodes( - self.backend, self.user, self.ent_type, self.ent_ids).node_type) + self.backend, self.ac, self.ent_type, self.ent_ids).node_type) self.assertEqual(self.tag_type, Nodes( - self.backend, self.user, self.tag_type, self.tag_ids).node_type) + self.backend, self.ac, self.tag_type, self.tag_ids).node_type) # guids self.assertSetEqual(self.ent_ids, set(Nodes( - self.backend, self.user, self.ent_type, self.ent_ids).guids)) + self.backend, self.ac, self.ent_type, self.ent_ids).guids)) self.assertSetEqual(self.tag_ids, set(Nodes( - self.backend, self.user, self.tag_type, self.tag_ids).guids)) + self.backend, self.ac, self.tag_type, self.tag_ids).guids)) def test__ensure_nodes(self): - nodes = Nodes(self.backend, self.user, self.ent_type, self.ent_ids) + nodes = Nodes(self.backend, self.ac, self.ent_type, self.ent_ids) # missing nodes are created self.assertSetEqual(self.ent_ids, nodes._ensure_nodes(self.ent_type, self.ent_ids)) @@ -235,7 +237,7 @@ class TestNodes(unittest.TestCase): def test___set(self): # setup - nodes = Nodes(self.backend, self.user, self.ent_type, {URI('http://example.com/me/entity#1234'), URI('http://example.com/me/entity#4321')}) + nodes = Nodes(self.backend, self.ac, self.ent_type, {URI('http://example.com/me/entity#1234'), URI('http://example.com/me/entity#4321')}) self.assertSetEqual(set(self.backend._graph), self.schema_triples | set()) set_ = nodes._Nodes__set @@ -264,7 +266,7 @@ class TestNodes(unittest.TestCase): }) # set node value - tags = Nodes(self.backend, self.user, self.tag_type, {URI('http://example.com/me/tag#1234'), URI('http://example.com/me/tag#4321')}) + tags = Nodes(self.backend, self.ac, self.tag_type, {URI('http://example.com/me/tag#1234'), URI('http://example.com/me/tag#4321')}) set_(self.p_tag.uri, tags) # get creation time from backend manually time_triples = list(self.backend._graph.objects(rdflib.URIRef('http://example.com/me/tag#1234'), rdflib.URIRef(self.t_created.uri))) @@ -296,11 +298,11 @@ class TestNodes(unittest.TestCase): self.assertRaises(TypeError, set_, self.p_tag.uri, URI('http://example.com/me/tag#1234')) # value's node_type must match the predicate's range self.assertRaises(errors.ConsistencyError, set_, self.p_tag.uri, - Nodes(self.backend, self.user, self.ent_type, self.ent_ids)) + Nodes(self.backend, self.ac, self.ent_type, self.ent_ids)) def test_set(self): self.assertSetEqual(set(self.backend._graph), self.schema_triples | set()) - nodes = Nodes(self.backend, self.user, self.ent_type, self.ent_ids) + nodes = Nodes(self.backend, self.ac, self.ent_type, self.ent_ids) # can set literal values self.assertEqual(nodes, nodes.set(self.p_filesize.uri, 1234)) self.assertTrue(set(self.backend._graph).issuperset({ @@ -312,7 +314,7 @@ class TestNodes(unittest.TestCase): (rdflib.URIRef('http://example.com/me/entity#4321'), rdflib.URIRef(self.p_filesize.uri), rdflib.Literal(1234, datatype=rdflib.XSD.integer)), })) # can set node values - self.assertEqual(nodes, nodes.set(self.p_tag.uri, Nodes(self.backend, self.user, self.tag_type, self.tag_ids))) + self.assertEqual(nodes, nodes.set(self.p_tag.uri, Nodes(self.backend, self.ac, self.tag_type, self.tag_ids))) self.assertTrue(set(self.backend._graph).issuperset({ # nodes exist (rdflib.URIRef('http://example.com/me/entity#1234'), rdflib.RDF.type, rdflib.URIRef('http://bsfs.ai/schema/Entity')), @@ -341,17 +343,17 @@ class TestNodes(unittest.TestCase): self.assertSetEqual(curr, set(self.backend._graph)) # cannot assing multiple values to unique predicate self.assertRaises(ValueError, nodes.set, self.p_author.uri, - Nodes(self.backend, self.user, self.user_type, {URI('http://example.com/me/user#1234'), URI('http://example.com/me/user#4321')})) + Nodes(self.backend, self.ac, self.user_type, {URI('http://example.com/me/user#1234'), URI('http://example.com/me/user#4321')})) self.assertSetEqual(curr, set(self.backend._graph)) def test_set_from_iterable(self): self.assertSetEqual(set(self.backend._graph), self.schema_triples | set()) - nodes = Nodes(self.backend, self.user, self.ent_type, self.ent_ids) + nodes = Nodes(self.backend, self.ac, self.ent_type, self.ent_ids) # can set literal and node values simultaneously self.assertEqual(nodes, nodes.set_from_iterable({ self.p_filesize.uri: 1234, - self.p_tag.uri: Nodes(self.backend, self.user, self.tag_type, self.tag_ids), + self.p_tag.uri: Nodes(self.backend, self.ac, self.tag_type, self.tag_ids), }.items())) self.assertTrue(set(self.backend._graph).issuperset({ # nodes exist @@ -383,24 +385,24 @@ class TestNodes(unittest.TestCase): self.assertSetEqual(curr, set(self.backend._graph)) # cannot assing multiple values to unique predicate self.assertRaises(ValueError, nodes.set_from_iterable, ((self.p_filesize.uri, 1234), - (self.p_author.uri, Nodes(self.backend, self.user, self.user_type, {URI('http://example.com/me/user#1234'), URI('http://example.com/me/user#4321')})))) + (self.p_author.uri, Nodes(self.backend, self.ac, self.user_type, {URI('http://example.com/me/user#1234'), URI('http://example.com/me/user#4321')})))) self.assertSetEqual(curr, set(self.backend._graph)) def test_get(self): # setup: add some instances - Nodes(self.backend, self.user, self.ent_type, {'http://example.com/me/entity#1234'}) \ + Nodes(self.backend, self.ac, self.ent_type, {'http://example.com/me/entity#1234'}) \ .set(ns.bse.comment, 'hello world') \ .set(ns.bse.filesize, 1234) \ - .set(ns.bse.tag, Nodes(self.backend, self.user, self.tag_type, {'http://example.com/me/tag#1234'})) - Nodes(self.backend, self.user, self.ent_type, {'http://example.com/me/entity#4321'}) \ + .set(ns.bse.tag, Nodes(self.backend, self.ac, self.tag_type, {'http://example.com/me/tag#1234'})) + Nodes(self.backend, self.ac, self.ent_type, {'http://example.com/me/entity#4321'}) \ .set(ns.bse.filesize, 4321) \ - .set(ns.bse.tag, Nodes(self.backend, self.user, self.tag_type, {'http://example.com/me/tag#4321'})) - Nodes(self.backend, self.user, self.tag_type, {'http://example.com/me/tag#1234'}) \ + .set(ns.bse.tag, Nodes(self.backend, self.ac, self.tag_type, {'http://example.com/me/tag#4321'})) + Nodes(self.backend, self.ac, self.tag_type, {'http://example.com/me/tag#1234'}) \ .set(bst.label, 'tag_label_1234') - Nodes(self.backend, self.user, self.tag_type, {'http://example.com/me/tag#4321'}) \ + Nodes(self.backend, self.ac, self.tag_type, {'http://example.com/me/tag#4321'}) \ .set(bst.label, 'tag_label_4321') # setup: get nodes instance - nodes = Nodes(self.backend, self.user, self.ent_type, self.ent_ids) + nodes = Nodes(self.backend, self.ac, self.ent_type, self.ent_ids) # must pass at least one path self.assertRaises(AttributeError, nodes.get) # view must be list or dict @@ -409,22 +411,22 @@ class TestNodes(unittest.TestCase): self.assertRaises(ValueError, nodes.get, ns.bse.filesize, view=tuple) # can pass path as URI self.assertDictEqual(nodes.get(ns.bse.filesize), { - Nodes(self.backend, self.user, self.ent_type, {'http://example.com/me/entity#1234'}): 1234, - Nodes(self.backend, self.user, self.ent_type, {'http://example.com/me/entity#4321'}): 4321, + Nodes(self.backend, self.ac, self.ent_type, {'http://example.com/me/entity#1234'}): 1234, + Nodes(self.backend, self.ac, self.ent_type, {'http://example.com/me/entity#4321'}): 4321, }) # can pass path as sequence of URI self.assertDictEqual(nodes.get((ns.bse.tag, bst.label)), { - Nodes(self.backend, self.user, self.ent_type, {'http://example.com/me/entity#1234'}): {'tag_label_1234'}, - Nodes(self.backend, self.user, self.ent_type, {'http://example.com/me/entity#4321'}): {'tag_label_4321'}, + Nodes(self.backend, self.ac, self.ent_type, {'http://example.com/me/entity#1234'}): {'tag_label_1234'}, + Nodes(self.backend, self.ac, self.ent_type, {'http://example.com/me/entity#4321'}): {'tag_label_4321'}, }) # get returns the same path that was passed self.assertCountEqual(list(nodes.get((ns.bse.tag, bst.label), path=True, view=list)), [ - (Nodes(self.backend, self.user, self.ent_type, {'http://example.com/me/entity#1234'}), (ns.bse.tag, bst.label), 'tag_label_1234'), - (Nodes(self.backend, self.user, self.ent_type, {'http://example.com/me/entity#4321'}), (ns.bse.tag, bst.label), 'tag_label_4321'), + (Nodes(self.backend, self.ac, self.ent_type, {'http://example.com/me/entity#1234'}), (ns.bse.tag, bst.label), 'tag_label_1234'), + (Nodes(self.backend, self.ac, self.ent_type, {'http://example.com/me/entity#4321'}), (ns.bse.tag, bst.label), 'tag_label_4321'), ]) self.assertCountEqual(list(nodes.get([ns.bse.tag, bst.label], path=True, view=list)), [ - (Nodes(self.backend, self.user, self.ent_type, {'http://example.com/me/entity#1234'}), [ns.bse.tag, bst.label], 'tag_label_1234'), - (Nodes(self.backend, self.user, self.ent_type, {'http://example.com/me/entity#4321'}), [ns.bse.tag, bst.label], 'tag_label_4321'), + (Nodes(self.backend, self.ac, self.ent_type, {'http://example.com/me/entity#1234'}), [ns.bse.tag, bst.label], 'tag_label_1234'), + (Nodes(self.backend, self.ac, self.ent_type, {'http://example.com/me/entity#4321'}), [ns.bse.tag, bst.label], 'tag_label_4321'), ]) # paths must be URI or sequence thereof self.assertRaises(TypeError, nodes.get, 1234) @@ -435,34 +437,34 @@ class TestNodes(unittest.TestCase): self.assertRaises(errors.ConsistencyError, nodes.get, (ns.bse.tag, bst.invalid)) # can pass multiple paths self.assertDictEqual(nodes.get(ns.bse.filesize, (ns.bse.tag, bst.label)), { - Nodes(self.backend, self.user, self.ent_type, {'http://example.com/me/entity#1234'}): { + Nodes(self.backend, self.ac, self.ent_type, {'http://example.com/me/entity#1234'}): { ns.bse.filesize: 1234, (ns.bse.tag, bst.label): {'tag_label_1234'}, }, - Nodes(self.backend, self.user, self.ent_type, {'http://example.com/me/entity#4321'}): { + Nodes(self.backend, self.ac, self.ent_type, {'http://example.com/me/entity#4321'}): { ns.bse.filesize: 4321, (ns.bse.tag, bst.label): {'tag_label_4321'}, }, }) # get respects view self.assertDictEqual(nodes.get(ns.bse.filesize, view=dict), { - Nodes(self.backend, self.user, self.ent_type, {'http://example.com/me/entity#1234'}): 1234, - Nodes(self.backend, self.user, self.ent_type, {'http://example.com/me/entity#4321'}): 4321, + Nodes(self.backend, self.ac, self.ent_type, {'http://example.com/me/entity#1234'}): 1234, + Nodes(self.backend, self.ac, self.ent_type, {'http://example.com/me/entity#4321'}): 4321, }) self.assertSetEqual(set(nodes.get(ns.bse.filesize, view=list)), { - (Nodes(self.backend, self.user, self.ent_type, {'http://example.com/me/entity#1234'}), 1234), - (Nodes(self.backend, self.user, self.ent_type, {'http://example.com/me/entity#4321'}), 4321), + (Nodes(self.backend, self.ac, self.ent_type, {'http://example.com/me/entity#1234'}), 1234), + (Nodes(self.backend, self.ac, self.ent_type, {'http://example.com/me/entity#4321'}), 4321), }) # get returns Nodes instance when fetching a node self.assertDictEqual(nodes.get(ns.bse.tag), { - Nodes(self.backend, self.user, self.ent_type, {'http://example.com/me/entity#1234'}): - {Nodes(self.backend, self.user, self.tag_type, {'http://example.com/me/tag#1234'})}, - Nodes(self.backend, self.user, self.ent_type, {'http://example.com/me/entity#4321'}): - {Nodes(self.backend, self.user, self.tag_type, {'http://example.com/me/tag#4321'})}, + Nodes(self.backend, self.ac, self.ent_type, {'http://example.com/me/entity#1234'}): + {Nodes(self.backend, self.ac, self.tag_type, {'http://example.com/me/tag#1234'})}, + Nodes(self.backend, self.ac, self.ent_type, {'http://example.com/me/entity#4321'}): + {Nodes(self.backend, self.ac, self.tag_type, {'http://example.com/me/tag#4321'})}, }) # get returns a value when fetching a value and omits missing values self.assertDictEqual(nodes.get(ns.bse.comment), { - Nodes(self.backend, self.user, self.ent_type, {'http://example.com/me/entity#1234'}): {'hello world'}, + Nodes(self.backend, self.ac, self.ent_type, {'http://example.com/me/entity#1234'}): {'hello world'}, }) # FIXME: What if I call `get` with a single predicate and a single node, but @@ -471,7 +473,7 @@ class TestNodes(unittest.TestCase): raise NotImplementedError() def test_getattr(self): - nodes = Nodes(self.backend, self.user, self.ent_type, {'http://example.com/me/entity#1234'}) + nodes = Nodes(self.backend, self.ac, self.ent_type, {'http://example.com/me/entity#1234'}) # can get walks to values self.assertEqual(nodes.filesize, Walk(nodes, (self.p_filesize, ))) # can get walks to nodes @@ -482,11 +484,11 @@ class TestNodes(unittest.TestCase): self.assertRaises(ValueError, getattr, nodes, 'foobar') def test_schema(self): - self.assertEqual(Nodes(self.backend, self.user, self.ent_type, + self.assertEqual(Nodes(self.backend, self.ac, self.ent_type, {URI('http://example.com/me/entity#1234')}).schema, self.backend.schema) def test_operators(self): # __add__, __or__, __sub__, __and__ - gen = partial(Nodes, self.backend, self.user, self.ent_type) + gen = partial(Nodes, self.backend, self.ac, self.ent_type) nodes = gen({URI('http://example.com/me/entity#1234')}) # add/or concatenates guids self.assertEqual( @@ -544,23 +546,24 @@ class TestNodes(unittest.TestCase): self.assertRaises(TypeError, op, nodes, 'hello world') # backend must match self.assertRaises(ValueError, op, nodes, - Nodes(None, self.user, self.ent_type, {URI('http://example.com/me/entity#1234')})) - # user must match + Nodes(None, self.ac, self.ent_type, {URI('http://example.com/me/entity#1234')})) + # ac must match self.assertRaises(ValueError, op, nodes, - Nodes(self.backend, '', self.ent_type, {URI('http://example.com/me/entity#1234')})) + Nodes(self.backend, NullAC(self.backend, ''), + self.ent_type, {URI('http://example.com/me/entity#1234')})) # node type must match self.assertRaises(ValueError, op, nodes, - Nodes(self.backend, self.user, self.tag_type, {URI('http://example.com/me/entity#1234')})) + Nodes(self.backend, self.ac, self.tag_type, {URI('http://example.com/me/entity#1234')})) def test_len(self): - self.assertEqual(1, len(Nodes(self.backend, self.user, self.ent_type, { + self.assertEqual(1, len(Nodes(self.backend, self.ac, self.ent_type, { URI('http://example.com/me/entity#1234'), }))) - self.assertEqual(2, len(Nodes(self.backend, self.user, self.ent_type, { + self.assertEqual(2, len(Nodes(self.backend, self.ac, self.ent_type, { URI('http://example.com/me/entity#1234'), URI('http://example.com/me/entity#4321'), }))) - self.assertEqual(4, len(Nodes(self.backend, self.user, self.ent_type, { + self.assertEqual(4, len(Nodes(self.backend, self.ac, self.ent_type, { URI('http://example.com/me/entity#1234'), URI('http://example.com/me/entity#4321'), URI('http://example.com/me/entity#5678'), @@ -568,8 +571,8 @@ class TestNodes(unittest.TestCase): }))) def test_iter(self): # __iter__ - gen = partial(Nodes, self.backend, self.user, self.ent_type) - self.assertSetEqual(set(Nodes(self.backend, self.user, self.ent_type, { + gen = partial(Nodes, self.backend, self.ac, self.ent_type) + self.assertSetEqual(set(Nodes(self.backend, self.ac, self.ent_type, { URI('http://example.com/me/entity#1234'), URI('http://example.com/me/entity#4321'), URI('http://example.com/me/entity#5678'), diff --git a/test/graph/test_walk.py b/test/graph/test_walk.py index f9dbc7a..e5c8981 100644 --- a/test/graph/test_walk.py +++ b/test/graph/test_walk.py @@ -10,6 +10,7 @@ import unittest # bsfs imports from bsfs import schema as bsc from bsfs.graph import Graph +from bsfs.graph.ac import NullAC from bsfs.namespace import Namespace, ns from bsfs.triple_store.sparql import SparqlStore from bsfs.utils import URI @@ -65,7 +66,8 @@ class TestWalk(unittest.TestCase): ''') self.backend = SparqlStore.Open() self.user = URI('http://example.com/me') - self.graph = Graph(self.backend, self.user) + self.ac = NullAC(self.backend, self.user) + self.graph = Graph(self.backend, self.ac) self.graph.migrate(self.schema) # nodes setup -- cgit v1.2.3 From d822df3dad0525f35dbacda9d5a66f4756f079ff Mon Sep 17 00:00:00 2001 From: Matthias Baumgartner Date: Wed, 1 Mar 2023 12:49:47 +0100 Subject: Integrate main app into package --- test/apps/test_main.py | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 test/apps/test_main.py (limited to 'test') diff --git a/test/apps/test_main.py b/test/apps/test_main.py new file mode 100644 index 0000000..ae19b5e --- /dev/null +++ b/test/apps/test_main.py @@ -0,0 +1,42 @@ +""" + +Part of the bsfs test suite. +A copy of the license is provided with the project. +Author: Matthias Baumgartner, 2022 +""" +# imports +import contextlib +import io +import json +import unittest + +# objects to test +from bsfs.apps import main + + +## code ## + +class TestMain(unittest.TestCase): + def test_main(self): + # must at least pass an app + with contextlib.redirect_stderr(io.StringIO()): + self.assertRaises(SystemExit, main, []) + # app takes over + with contextlib.redirect_stderr(io.StringIO()): + self.assertRaises(SystemExit, main, ['init']) + outbuf = io.StringIO() + with contextlib.redirect_stdout(outbuf): + main(['init', 'sparql']) + self.assertEqual(json.loads(outbuf.getvalue()), { + 'Graph': { + 'user': 'http://example.com/me', + 'backend': { + 'SparqlStore': {}}}}) + + +## main ## + +if __name__ == '__main__': + unittest.main() + +## EOF ## -- cgit v1.2.3 From a5695dcc4e1be8fccd0b35ba4672cdb3c2c119d7 Mon Sep 17 00:00:00 2001 From: Matthias Baumgartner Date: Wed, 1 Mar 2023 13:06:59 +0100 Subject: minor fixes --- test/graph/ac/test_null.py | 1 - 1 file changed, 1 deletion(-) (limited to 'test') diff --git a/test/graph/ac/test_null.py b/test/graph/ac/test_null.py index 7d25980..e33d46a 100644 --- a/test/graph/ac/test_null.py +++ b/test/graph/ac/test_null.py @@ -132,7 +132,6 @@ class TestNullAC(unittest.TestCase): ast.filter.Any(ns.bse.author, ast.filter.Equals('Me, Myself, and I'))) ac = NullAC(self.backend, self.user) self.assertEqual(query, ac.filter_read(self.ent_type, query)) - return query ## main ## -- cgit v1.2.3 From 87f437380c1dd8f420437cddc028c0f3174ee1c9 Mon Sep 17 00:00:00 2001 From: Matthias Baumgartner Date: Thu, 2 Mar 2023 12:19:58 +0100 Subject: Node getters in bsfs.Graph: * Empty nodes instance (Graph.empty) * Order-preserving get query (Graph.sorted) * Collect common code in private Graph.__get * Empty query in Graph.get * Empty query in Graph.resolve.Filter * Empty query in AC: filter_read --- test/graph/ac/test_null.py | 3 ++ test/graph/test_graph.py | 95 ++++++++++++++++++++++++++++++++++++++++------ test/graph/test_resolve.py | 3 ++ 3 files changed, 90 insertions(+), 11 deletions(-) (limited to 'test') diff --git a/test/graph/ac/test_null.py b/test/graph/ac/test_null.py index e33d46a..544a01e 100644 --- a/test/graph/ac/test_null.py +++ b/test/graph/ac/test_null.py @@ -131,7 +131,10 @@ class TestNullAC(unittest.TestCase): ast.filter.Any(ns.bse.tag, ast.filter.Is('http://example.com/tag#4321')), ast.filter.Any(ns.bse.author, ast.filter.Equals('Me, Myself, and I'))) ac = NullAC(self.backend, self.user) + # NullAC returns query self.assertEqual(query, ac.filter_read(self.ent_type, query)) + # query can be none + self.assertIsNone(ac.filter_read(self.ent_type, None)) ## main ## diff --git a/test/graph/test_graph.py b/test/graph/test_graph.py index d89d346..93f8db7 100644 --- a/test/graph/test_graph.py +++ b/test/graph/test_graph.py @@ -5,6 +5,8 @@ A copy of the license is provided with the project. Author: Matthias Baumgartner, 2022 """ # imports +from functools import reduce +import operator import unittest # bsie imports @@ -97,18 +99,14 @@ class TestGraph(unittest.TestCase): # node_type must be in the schema self.assertRaises(KeyError, graph.nodes, ns.bsfs.Invalid, guids) - def test_all(self): + def test_empty(self): graph = Graph(self.backend, self.ac) - # resulting nodes can be empty - self.assertEqual(graph.all(ns.bsfs.Entity), + # returns a Nodes instance + self.assertEqual( + graph.empty(ns.bsfs.Entity), Nodes(self.backend, self.ac, graph.schema.node(ns.bsfs.Entity), set())) - # resulting nodes contains all nodes of the respective type - guids = {URI('http://example.com/me/entity#1234'), URI('http://example.com/me/entity#4321')} - self.backend.create(graph.schema.node(ns.bsfs.Entity), guids) - self.assertEqual(graph.all(ns.bsfs.Entity), - Nodes(self.backend, self.ac, graph.schema.node(ns.bsfs.Entity), guids)) # node_type must be in the schema - self.assertRaises(KeyError, graph.all, ns.bsfs.Invalid) + self.assertRaises(KeyError, graph.empty, ns.bsfs.Invalid) def test_migrate(self): # setup @@ -248,10 +246,10 @@ class TestGraph(unittest.TestCase): graph.node(ns.bsfs.Tag, URI('http://example.com/tag#1234')).set(ns.bse.comment, 'foo') graph.node(ns.bsfs.Tag, URI('http://example.com/tag#4321')).set(ns.bse.comment, 'bar') - # get exception for invalid query + # invalid query raises exception self.assertRaises(errors.ConsistencyError, graph.get, ns.bsfs.Entity, ast.filter.Any(ns.bse.tag, ast.filter.Equals('hello world'))) - # query returns nodes + # get returns nodes self.assertEqual(graph.get(ns.bsfs.Entity, ast.filter.Any(ns.bse.tag, ast.filter.Is(tags))), ents) self.assertEqual(graph.get(ns.bsfs.Entity, ast.filter.Any(ns.bse.comment, ast.filter.StartsWith('foo'))), graph.node(ns.bsfs.Entity, URI('http://example.com/entity#1234'))) @@ -262,6 +260,81 @@ class TestGraph(unittest.TestCase): ast.filter.Any(ns.bse.tag, ast.filter.All(ns.bse.comment, ast.filter.Equals('bar'))))), ents) + # query can be None + self.assertEqual(graph.get(ns.bsfs.Entity, None), ents) + + def test_sorted(self): + # setup + graph = Graph(self.backend, self.ac) + graph.migrate(schema.from_string(''' + prefix rdfs: + prefix xsd: + prefix bsfs: + prefix bse: + + bsfs:Entity rdfs:subClassOf bsfs:Node . + bsfs:Tag rdfs:subClassOf bsfs:Node . + xsd:string rdfs:subClassOf bsfs:Literal . + + bse:tag rdfs:subClassOf bsfs:Predicate ; + rdfs:domain bsfs:Entity ; + rdfs:range bsfs:Tag ; + bsfs:unique "false"^^xsd:boolean . + + bse:comment rdfs:subClassOf bsfs:Predicate ; + rdfs:domain bsfs:Node ; + rdfs:range xsd:string ; + bsfs:unique "false"^^xsd:boolean . + + ''')) + # add some instances + ents = [ + # default is alphabetical order + graph.node(ns.bsfs.Entity, URI('http://example.com/entity#1234')), + graph.node(ns.bsfs.Entity, URI('http://example.com/entity#4321')), + ] + tags = graph.nodes(ns.bsfs.Tag, {URI('http://example.com/tag#1234'), URI('http://example.com/tag#4321')}) + # add some node links + reduce(operator.add, ents).set(ns.bse.tag, tags) + # add some literals + graph.node(ns.bsfs.Entity, URI('http://example.com/entity#1234')).set(ns.bse.comment, 'hello world') + graph.node(ns.bsfs.Entity, URI('http://example.com/entity#1234')).set(ns.bse.comment, 'foo') + graph.node(ns.bsfs.Entity, URI('http://example.com/entity#1234')).set(ns.bse.comment, 'foobar') + graph.node(ns.bsfs.Tag, URI('http://example.com/tag#1234')).set(ns.bse.comment, 'foo') + graph.node(ns.bsfs.Tag, URI('http://example.com/tag#4321')).set(ns.bse.comment, 'bar') + + # invalid query raises exception + self.assertRaises(errors.ConsistencyError, list, graph.sorted(ns.bsfs.Entity, ast.filter.Any(ns.bse.tag, ast.filter.Equals('hello world')))) + + # get returns nodes + self.assertListEqual(list(graph.sorted(ns.bsfs.Entity, ast.filter.Any(ns.bse.tag, ast.filter.Is(tags)))), ents) + self.assertListEqual(list(graph.sorted(ns.bsfs.Entity, ast.filter.Any(ns.bse.comment, ast.filter.StartsWith('foo')))), + [graph.node(ns.bsfs.Entity, URI('http://example.com/entity#1234'))]) + self.assertListEqual(list(graph.sorted(ns.bsfs.Node, ast.filter.Any(ns.bse.comment, ast.filter.StartsWith('foo')))), [ + graph.node(ns.bsfs.Node, URI('http://example.com/entity#1234')), + graph.node(ns.bsfs.Node, URI('http://example.com/tag#1234')), + ]) + self.assertListEqual(list(graph.sorted(ns.bsfs.Entity, ast.filter.Or( + ast.filter.Any(ns.bse.comment, ast.filter.EndsWith('bar')), + ast.filter.Any(ns.bse.tag, ast.filter.All(ns.bse.comment, ast.filter.Equals('bar')))))), + ents) + + # query can be None + self.assertListEqual(list(graph.sorted(ns.bsfs.Entity, None)), ents) + + + def test_all(self): + graph = Graph(self.backend, self.ac) + # resulting nodes can be empty + self.assertEqual(graph.all(ns.bsfs.Entity), + Nodes(self.backend, self.ac, graph.schema.node(ns.bsfs.Entity), set())) + # resulting nodes contains all nodes of the respective type + guids = {URI('http://example.com/me/entity#1234'), URI('http://example.com/me/entity#4321')} + self.backend.create(graph.schema.node(ns.bsfs.Entity), guids) + self.assertEqual(graph.all(ns.bsfs.Entity), + Nodes(self.backend, self.ac, graph.schema.node(ns.bsfs.Entity), guids)) + # node_type must be in the schema + self.assertRaises(KeyError, graph.all, ns.bsfs.Invalid) diff --git a/test/graph/test_resolve.py b/test/graph/test_resolve.py index 0918b02..b4d76c7 100644 --- a/test/graph/test_resolve.py +++ b/test/graph/test_resolve.py @@ -79,6 +79,9 @@ class TestFilter(unittest.TestCase): {'http://example.com/you/invalid#1234', 'http://example.com/you/invalid#4321'}) resolver = Filter(schema) + # query can be None + self.assertIsNone(resolver(schema.node(ns.bsfs.Entity), None)) + # immediate Is self.assertEqual(resolver(schema.node(ns.bsfs.Entity), ast.filter.Is(ents)), -- cgit v1.2.3 From cd27775b406482b11f44575ab196501a30d9b075 Mon Sep 17 00:00:00 2001 From: Matthias Baumgartner Date: Thu, 2 Mar 2023 12:23:49 +0100 Subject: default sort order in sparql backend --- test/triple_store/sparql/test_utils.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'test') diff --git a/test/triple_store/sparql/test_utils.py b/test/triple_store/sparql/test_utils.py index 073b8f8..edcf6d7 100644 --- a/test/triple_store/sparql/test_utils.py +++ b/test/triple_store/sparql/test_utils.py @@ -118,13 +118,13 @@ class TestQuery(unittest.TestCase): return value # query composes a valid query q = Query(self.root_type, self.root_head, self.select, self.where) - self.assertEqual(normalize(q.query), normalize(f'select ?root (?head as ?name) where {{ ?root <{ns.rdf.type}>/<{ns.rdfs.subClassOf}>* . ?root <{ns.bse.tag}> ?head }}')) + self.assertEqual(normalize(q.query), normalize(f'select distinct ?root (?head as ?name) where {{ ?root <{ns.rdf.type}>/<{ns.rdfs.subClassOf}>* . ?root <{ns.bse.tag}> ?head }} order by str(?root)')) # select and where are optional q = Query(self.root_type, self.root_head) - self.assertEqual(normalize(q.query), normalize(f'select ?root where {{ ?root <{ns.rdf.type}>/<{ns.rdfs.subClassOf}>* . }}')) + self.assertEqual(normalize(q.query), normalize(f'select distinct ?root where {{ ?root <{ns.rdf.type}>/<{ns.rdfs.subClassOf}>* . }} order by str(?root)')) # select and where need not to correspond q = Query(self.root_type, self.root_head, (('?head', 'name'), )) - self.assertEqual(normalize(q.query), normalize(f'select ?root (?head as ?name) where {{ ?root <{ns.rdf.type}>/<{ns.rdfs.subClassOf}>* . }}')) + self.assertEqual(normalize(q.query), normalize(f'select distinct ?root (?head as ?name) where {{ ?root <{ns.rdf.type}>/<{ns.rdfs.subClassOf}>* . }} order by str(?root)')) # query is used for string representation self.assertEqual(str(q), q.query) -- cgit v1.2.3 From 36d07cc6e0ec0f53001bfc5045437a374ebb895f Mon Sep 17 00:00:00 2001 From: Matthias Baumgartner Date: Thu, 2 Mar 2023 14:55:57 +0100 Subject: empty fetch result in Nodes --- test/graph/ac/test_null.py | 9 +++++++ test/graph/test_nodes.py | 60 ++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 65 insertions(+), 4 deletions(-) (limited to 'test') diff --git a/test/graph/ac/test_null.py b/test/graph/ac/test_null.py index 544a01e..6053f81 100644 --- a/test/graph/ac/test_null.py +++ b/test/graph/ac/test_null.py @@ -136,6 +136,15 @@ class TestNullAC(unittest.TestCase): # query can be none self.assertIsNone(ac.filter_read(self.ent_type, None)) + def test_fetch_read(self): + query = ast.fetch.All( + ast.fetch.Fetch(ns.bse.tag, ast.fetch.Value(ns.bse.label, 'tag_label')), + ast.fetch.Node(ns.bse.tag, 'tag_node'), + ast.fetch.Value(ns.bse.iso, 'iso')) + ac = NullAC(self.backend, self.user) + # NullAC returns query + self.assertEqual(query, ac.fetch_read(self.ent_type, query)) + ## main ## diff --git a/test/graph/test_nodes.py b/test/graph/test_nodes.py index 9541656..bf73e6e 100644 --- a/test/graph/test_nodes.py +++ b/test/graph/test_nodes.py @@ -346,6 +346,10 @@ class TestNodes(unittest.TestCase): Nodes(self.backend, self.ac, self.user_type, {URI('http://example.com/me/user#1234'), URI('http://example.com/me/user#4321')})) self.assertSetEqual(curr, set(self.backend._graph)) + # can set on empty nodes + nodes = Nodes(self.backend, self.ac, self.ent_type, {}) + self.assertEqual(nodes, nodes.set(self.p_filesize.uri, 1234)) + def test_set_from_iterable(self): self.assertSetEqual(set(self.backend._graph), self.schema_triples | set()) @@ -388,6 +392,11 @@ class TestNodes(unittest.TestCase): (self.p_author.uri, Nodes(self.backend, self.ac, self.user_type, {URI('http://example.com/me/user#1234'), URI('http://example.com/me/user#4321')})))) self.assertSetEqual(curr, set(self.backend._graph)) + # can set on empty nodes + nodes = Nodes(self.backend, self.ac, self.ent_type, {}) + self.assertEqual(nodes, nodes.set_from_iterable([(self.p_filesize.uri, 1234)])) + + def test_get(self): # setup: add some instances Nodes(self.backend, self.ac, self.ent_type, {'http://example.com/me/entity#1234'}) \ @@ -403,6 +412,7 @@ class TestNodes(unittest.TestCase): .set(bst.label, 'tag_label_4321') # setup: get nodes instance nodes = Nodes(self.backend, self.ac, self.ent_type, self.ent_ids) + # must pass at least one path self.assertRaises(AttributeError, nodes.get) # view must be list or dict @@ -467,10 +477,52 @@ class TestNodes(unittest.TestCase): Nodes(self.backend, self.ac, self.ent_type, {'http://example.com/me/entity#1234'}): {'hello world'}, }) - # FIXME: What if I call `get` with a single predicate and a single node, but - # that node has no value for that predicate? - # so, essentially, what if triples is empty? -> Also check in test_result! - raise NotImplementedError() + # results can be empty + nodes = Nodes(self.backend, self.ac, self.ent_type, {'http://example.com/me/entity#4321'}) # has filesize, tag but no comment + # unique paths return the default value + self.assertIsNone(nodes.get(ns.bse.author)) + self.assertEqual(nodes.get(ns.bse.author, default=1234), 1234) + # non-unique paths return an empty set + self.assertSetEqual(nodes.get(ns.bse.comment), set()) + + # nodes can have no guids + nodes = Nodes(self.backend, self.ac, self.ent_type, set()) + # empty nodes does not excuse an invalid request + self.assertRaises(TypeError, nodes.get, 1234) + self.assertRaises(errors.ConsistencyError, nodes.get, ns.bse.invalid) + # list view always returns an empty list + self.assertListEqual(list(nodes.get(ns.bse.comment, view=list)), []) + self.assertListEqual(list(nodes.get(ns.bse.comment, view=list, node=True)), []) + self.assertListEqual(list(nodes.get(ns.bse.comment, view=list, path=True)), []) + self.assertListEqual(list(nodes.get(ns.bse.comment, view=list, node=True, path=True, value=True)), []) + self.assertListEqual(list(nodes.get(ns.bse.comment, view=list, node=False)), []) + self.assertListEqual(list(nodes.get(ns.bse.comment, view=list, path=False)), []) + self.assertListEqual(list(nodes.get(ns.bse.comment, view=list, node=False, path=False, value=False)), []) + self.assertListEqual(list(nodes.get(ns.bse.comment, ns.bse.filesize, view=list)), []) + self.assertListEqual(list(nodes.get(ns.bse.comment, ns.bse.filesize, view=list, node=True)), []) + self.assertListEqual(list(nodes.get(ns.bse.comment, ns.bse.filesize, view=list, path=True)), []) + self.assertListEqual(list(nodes.get(ns.bse.comment, ns.bse.filesize, view=list, node=True, path=True, value=True)), []) + self.assertListEqual(list(nodes.get(ns.bse.comment, ns.bse.filesize, view=list, node=False)), []) + self.assertListEqual(list(nodes.get(ns.bse.comment, ns.bse.filesize, view=list, path=False)), []) + self.assertListEqual(list(nodes.get(ns.bse.comment, ns.bse.filesize, view=list, node=False, path=False, value=False)), []) + # dict view returns an empty dict or an empty set + self.assertDictEqual(nodes.get(ns.bse.comment, view=dict), {}) + self.assertDictEqual(nodes.get(ns.bse.comment, view=dict, node=True), {}) + self.assertDictEqual(nodes.get(ns.bse.comment, view=dict, path=True), {}) + self.assertDictEqual(nodes.get(ns.bse.comment, view=dict, node=True, path=True, value=True, default=None), {}) + self.assertSetEqual(nodes.get(ns.bse.comment, view=dict, node=False), set()) + self.assertDictEqual(nodes.get(ns.bse.comment, view=dict, path=False), {}) + self.assertSetEqual(nodes.get(ns.bse.comment, view=dict, node=False, path=False), set()) + self.assertSetEqual(nodes.get(ns.bse.comment, view=dict, node=False, path=False, value=False, default=None), set()) + self.assertDictEqual(nodes.get(ns.bse.comment, ns.bse.filesize, view=dict), {}) + self.assertDictEqual(nodes.get(ns.bse.comment, ns.bse.filesize, view=dict, node=True), {}) + self.assertDictEqual(nodes.get(ns.bse.comment, ns.bse.filesize, view=dict, path=True), {}) + self.assertDictEqual(nodes.get(ns.bse.comment, ns.bse.filesize, view=dict, node=True, path=True, value=True, default=None), {}) + self.assertDictEqual(nodes.get(ns.bse.comment, ns.bse.filesize, view=dict, node=False), {}) + self.assertDictEqual(nodes.get(ns.bse.comment, ns.bse.filesize, view=dict, path=False), {}) + self.assertSetEqual(nodes.get(ns.bse.comment, ns.bse.filesize, view=dict, node=False, path=False), set()) + self.assertSetEqual(nodes.get(ns.bse.comment, ns.bse.filesize, view=dict, node=False, path=False, value=False, default=None), set()) + def test_getattr(self): nodes = Nodes(self.backend, self.ac, self.ent_type, {'http://example.com/me/entity#1234'}) -- cgit v1.2.3 From 2e07f33314c238e42bfadc5f39805f93ffbc622e Mon Sep 17 00:00:00 2001 From: Matthias Baumgartner Date: Thu, 2 Mar 2023 15:10:05 +0100 Subject: removed author and license notices from individual files --- test/apps/test_init.py | 5 ----- test/apps/test_main.py | 5 ----- test/apps/test_migrate.py | 5 ----- test/front/test_bsfs.py | 5 ----- test/front/test_builder.py | 5 ----- test/graph/ac/test_base.py | 5 ----- test/graph/ac/test_null.py | 5 ----- test/graph/test_graph.py | 5 ----- test/graph/test_nodes.py | 5 ----- test/graph/test_resolve.py | 5 ----- test/graph/test_result.py | 5 ----- test/graph/test_walk.py | 5 ----- test/namespace/test_namespace.py | 5 ----- test/query/ast_test/test_fetch.py | 5 ----- test/query/ast_test/test_filter_.py | 5 ----- test/query/test_matcher.py | 5 ----- test/query/test_validator.py | 5 ----- test/schema/test_schema.py | 5 ----- test/schema/test_serialize.py | 5 ----- test/schema/test_types.py | 5 ----- test/triple_store/sparql/test_distance.py | 5 ----- test/triple_store/sparql/test_parse_fetch.py | 5 ----- test/triple_store/sparql/test_parse_filter.py | 5 ----- test/triple_store/sparql/test_sparql.py | 5 ----- test/triple_store/sparql/test_utils.py | 5 ----- test/triple_store/test_base.py | 5 ----- test/utils/test_commons.py | 5 ----- test/utils/test_uri.py | 5 ----- test/utils/test_uuid.py | 5 ----- 29 files changed, 145 deletions(-) (limited to 'test') diff --git a/test/apps/test_init.py b/test/apps/test_init.py index bae6a68..59e10eb 100644 --- a/test/apps/test_init.py +++ b/test/apps/test_init.py @@ -1,9 +1,4 @@ -""" -Part of the bsfs test suite. -A copy of the license is provided with the project. -Author: Matthias Baumgartner, 2022 -""" # imports import contextlib import io diff --git a/test/apps/test_main.py b/test/apps/test_main.py index ae19b5e..d61372f 100644 --- a/test/apps/test_main.py +++ b/test/apps/test_main.py @@ -1,9 +1,4 @@ -""" -Part of the bsfs test suite. -A copy of the license is provided with the project. -Author: Matthias Baumgartner, 2022 -""" # imports import contextlib import io diff --git a/test/apps/test_migrate.py b/test/apps/test_migrate.py index 230c032..618cb37 100644 --- a/test/apps/test_migrate.py +++ b/test/apps/test_migrate.py @@ -1,9 +1,4 @@ -""" -Part of the bsfs test suite. -A copy of the license is provided with the project. -Author: Matthias Baumgartner, 2022 -""" # imports import contextlib import io diff --git a/test/front/test_bsfs.py b/test/front/test_bsfs.py index 4eb36c3..8905bf8 100644 --- a/test/front/test_bsfs.py +++ b/test/front/test_bsfs.py @@ -1,9 +1,4 @@ -""" -Part of the bsfs test suite. -A copy of the license is provided with the project. -Author: Matthias Baumgartner, 2022 -""" # imports import unittest diff --git a/test/front/test_builder.py b/test/front/test_builder.py index 0328a0a..875fa8a 100644 --- a/test/front/test_builder.py +++ b/test/front/test_builder.py @@ -1,9 +1,4 @@ -""" -Part of the bsfs test suite. -A copy of the license is provided with the project. -Author: Matthias Baumgartner, 2022 -""" # imports import unittest diff --git a/test/graph/ac/test_base.py b/test/graph/ac/test_base.py index ad24e3d..addecd4 100644 --- a/test/graph/ac/test_base.py +++ b/test/graph/ac/test_base.py @@ -1,9 +1,4 @@ -""" -Part of the bsfs test suite. -A copy of the license is provided with the project. -Author: Matthias Baumgartner, 2022 -""" # imports import unittest diff --git a/test/graph/ac/test_null.py b/test/graph/ac/test_null.py index 6053f81..b695e7e 100644 --- a/test/graph/ac/test_null.py +++ b/test/graph/ac/test_null.py @@ -1,9 +1,4 @@ -""" -Part of the bsfs test suite. -A copy of the license is provided with the project. -Author: Matthias Baumgartner, 2022 -""" # imports import unittest diff --git a/test/graph/test_graph.py b/test/graph/test_graph.py index 93f8db7..e6d5ae4 100644 --- a/test/graph/test_graph.py +++ b/test/graph/test_graph.py @@ -1,9 +1,4 @@ -""" -Part of the bsfs test suite. -A copy of the license is provided with the project. -Author: Matthias Baumgartner, 2022 -""" # imports from functools import reduce import operator diff --git a/test/graph/test_nodes.py b/test/graph/test_nodes.py index bf73e6e..083b2d8 100644 --- a/test/graph/test_nodes.py +++ b/test/graph/test_nodes.py @@ -1,9 +1,4 @@ -""" -Part of the bsfs test suite. -A copy of the license is provided with the project. -Author: Matthias Baumgartner, 2022 -""" # standard imports from functools import partial import operator diff --git a/test/graph/test_resolve.py b/test/graph/test_resolve.py index b4d76c7..0223c49 100644 --- a/test/graph/test_resolve.py +++ b/test/graph/test_resolve.py @@ -1,9 +1,4 @@ -""" -Part of the bsfs test suite. -A copy of the license is provided with the project. -Author: Matthias Baumgartner, 2022 -""" # imports import unittest diff --git a/test/graph/test_result.py b/test/graph/test_result.py index 749b8ad..099234a 100644 --- a/test/graph/test_result.py +++ b/test/graph/test_result.py @@ -1,9 +1,4 @@ -""" -Part of the bsfs test suite. -A copy of the license is provided with the project. -Author: Matthias Baumgartner, 2022 -""" # imports import unittest diff --git a/test/graph/test_walk.py b/test/graph/test_walk.py index e5c8981..346896b 100644 --- a/test/graph/test_walk.py +++ b/test/graph/test_walk.py @@ -1,9 +1,4 @@ -""" -Part of the bsfs test suite. -A copy of the license is provided with the project. -Author: Matthias Baumgartner, 2022 -""" # imports import unittest diff --git a/test/namespace/test_namespace.py b/test/namespace/test_namespace.py index 2536203..ec2f393 100644 --- a/test/namespace/test_namespace.py +++ b/test/namespace/test_namespace.py @@ -1,9 +1,4 @@ -""" -Part of the tagit test suite. -A copy of the license is provided with the project. -Author: Matthias Baumgartner, 2022 -""" # imports import operator import unittest diff --git a/test/query/ast_test/test_fetch.py b/test/query/ast_test/test_fetch.py index 0c48a1f..ccb680e 100644 --- a/test/query/ast_test/test_fetch.py +++ b/test/query/ast_test/test_fetch.py @@ -1,9 +1,4 @@ -""" -Part of the tagit test suite. -A copy of the license is provided with the project. -Author: Matthias Baumgartner, 2022 -""" # imports import unittest diff --git a/test/query/ast_test/test_filter_.py b/test/query/ast_test/test_filter_.py index 39b98f8..cdc530c 100644 --- a/test/query/ast_test/test_filter_.py +++ b/test/query/ast_test/test_filter_.py @@ -1,9 +1,4 @@ -""" -Part of the tagit test suite. -A copy of the license is provided with the project. -Author: Matthias Baumgartner, 2022 -""" # imports import unittest diff --git a/test/query/test_matcher.py b/test/query/test_matcher.py index e830cf8..6b975b2 100644 --- a/test/query/test_matcher.py +++ b/test/query/test_matcher.py @@ -1,9 +1,4 @@ -""" -Part of the tagit test suite. -A copy of the license is provided with the project. -Author: Matthias Baumgartner, 2022 -""" # imports import operator import unittest diff --git a/test/query/test_validator.py b/test/query/test_validator.py index fec3d23..ca93118 100644 --- a/test/query/test_validator.py +++ b/test/query/test_validator.py @@ -1,9 +1,4 @@ -""" -Part of the tagit test suite. -A copy of the license is provided with the project. -Author: Matthias Baumgartner, 2022 -""" # imports import unittest diff --git a/test/schema/test_schema.py b/test/schema/test_schema.py index 414e542..f9ddb68 100644 --- a/test/schema/test_schema.py +++ b/test/schema/test_schema.py @@ -1,9 +1,4 @@ -""" -Part of the tagit test suite. -A copy of the license is provided with the project. -Author: Matthias Baumgartner, 2022 -""" # imports import operator import unittest diff --git a/test/schema/test_serialize.py b/test/schema/test_serialize.py index fc6b20a..84512e9 100644 --- a/test/schema/test_serialize.py +++ b/test/schema/test_serialize.py @@ -1,9 +1,4 @@ -""" -Part of the tagit test suite. -A copy of the license is provided with the project. -Author: Matthias Baumgartner, 2022 -""" # imports import re import unittest diff --git a/test/schema/test_types.py b/test/schema/test_types.py index c5895d2..f87d857 100644 --- a/test/schema/test_types.py +++ b/test/schema/test_types.py @@ -1,9 +1,4 @@ -""" -Part of the tagit test suite. -A copy of the license is provided with the project. -Author: Matthias Baumgartner, 2022 -""" # imports import operator import unittest diff --git a/test/triple_store/sparql/test_distance.py b/test/triple_store/sparql/test_distance.py index 0659459..e95be5a 100644 --- a/test/triple_store/sparql/test_distance.py +++ b/test/triple_store/sparql/test_distance.py @@ -1,9 +1,4 @@ -""" -Part of the bsfs test suite. -A copy of the license is provided with the project. -Author: Matthias Baumgartner, 2022 -""" # imports import numpy as np import unittest diff --git a/test/triple_store/sparql/test_parse_fetch.py b/test/triple_store/sparql/test_parse_fetch.py index 0961789..9284608 100644 --- a/test/triple_store/sparql/test_parse_fetch.py +++ b/test/triple_store/sparql/test_parse_fetch.py @@ -1,9 +1,4 @@ -""" -Part of the bsfs test suite. -A copy of the license is provided with the project. -Author: Matthias Baumgartner, 2022 -""" # imports import rdflib import unittest diff --git a/test/triple_store/sparql/test_parse_filter.py b/test/triple_store/sparql/test_parse_filter.py index 6fa0cd3..8a9940e 100644 --- a/test/triple_store/sparql/test_parse_filter.py +++ b/test/triple_store/sparql/test_parse_filter.py @@ -1,9 +1,4 @@ -""" -Part of the bsfs test suite. -A copy of the license is provided with the project. -Author: Matthias Baumgartner, 2022 -""" # imports import rdflib import unittest diff --git a/test/triple_store/sparql/test_sparql.py b/test/triple_store/sparql/test_sparql.py index 30876f2..b1d99ac 100644 --- a/test/triple_store/sparql/test_sparql.py +++ b/test/triple_store/sparql/test_sparql.py @@ -1,9 +1,4 @@ -""" -Part of the bsfs test suite. -A copy of the license is provided with the project. -Author: Matthias Baumgartner, 2022 -""" # imports import rdflib import unittest diff --git a/test/triple_store/sparql/test_utils.py b/test/triple_store/sparql/test_utils.py index edcf6d7..8f894bb 100644 --- a/test/triple_store/sparql/test_utils.py +++ b/test/triple_store/sparql/test_utils.py @@ -1,9 +1,4 @@ -""" -Part of the bsfs test suite. -A copy of the license is provided with the project. -Author: Matthias Baumgartner, 2022 -""" # standard imports import operator import re diff --git a/test/triple_store/test_base.py b/test/triple_store/test_base.py index 56a2539..4c4a9b6 100644 --- a/test/triple_store/test_base.py +++ b/test/triple_store/test_base.py @@ -1,9 +1,4 @@ -""" -Part of the bsfs test suite. -A copy of the license is provided with the project. -Author: Matthias Baumgartner, 2022 -""" # imports import unittest diff --git a/test/utils/test_commons.py b/test/utils/test_commons.py index 3ad6dea..29e3046 100644 --- a/test/utils/test_commons.py +++ b/test/utils/test_commons.py @@ -1,9 +1,4 @@ -""" -Part of the tagit test suite. -A copy of the license is provided with the project. -Author: Matthias Baumgartner, 2022 -""" # imports import unittest diff --git a/test/utils/test_uri.py b/test/utils/test_uri.py index 770e65a..6ee2ef7 100644 --- a/test/utils/test_uri.py +++ b/test/utils/test_uri.py @@ -1,9 +1,4 @@ -""" -Part of the tagit test suite. -A copy of the license is provided with the project. -Author: Matthias Baumgartner, 2022 -""" # imports import operator import unittest diff --git a/test/utils/test_uuid.py b/test/utils/test_uuid.py index 804b063..8f519d9 100644 --- a/test/utils/test_uuid.py +++ b/test/utils/test_uuid.py @@ -1,9 +1,4 @@ -""" -Part of the tagit test suite. -A copy of the license is provided with the project. -Author: Matthias Baumgartner, 2022 -""" # imports import os import re -- cgit v1.2.3 From b66ed641d5cbb4cb83f4a571223e4d65d80ed05c Mon Sep 17 00:00:00 2001 From: Matthias Baumgartner Date: Thu, 2 Mar 2023 15:29:12 +0100 Subject: check non-serializable URIs in the sparql store --- test/triple_store/sparql/test_parse_filter.py | 2 ++ test/triple_store/sparql/test_sparql.py | 6 ++++++ 2 files changed, 8 insertions(+) (limited to 'test') diff --git a/test/triple_store/sparql/test_parse_filter.py b/test/triple_store/sparql/test_parse_filter.py index 8a9940e..6db9224 100644 --- a/test/triple_store/sparql/test_parse_filter.py +++ b/test/triple_store/sparql/test_parse_filter.py @@ -157,6 +157,8 @@ class TestParseFilter(unittest.TestCase): def test_is(self): # _is requires a node self.assertRaises(errors.BackendError, self.parser._is, self.schema.literal(ns.bsfs.Literal), ast.filter.Is('http://example.com/entity#1234'), '?ent') + # _is requires a serializable guid + self.assertRaises(errors.BackendError, self.parser._is, self.schema.node(ns.bsfs.Entity), ast.filter.Is('http://example.com/entity#foo and bar'), '?ent') # a single Is statement q = self.parser(self.schema.node(ns.bsfs.Entity), ast.filter.Is('http://example.com/entity#1234')) self.assertSetEqual({str(guid) for guid, in q(self.graph)}, diff --git a/test/triple_store/sparql/test_sparql.py b/test/triple_store/sparql/test_sparql.py index b1d99ac..d880082 100644 --- a/test/triple_store/sparql/test_sparql.py +++ b/test/triple_store/sparql/test_sparql.py @@ -678,6 +678,9 @@ class TestSparqlStore(unittest.TestCase): self.assertRaises(errors.ConsistencyError, store.create, self.schema.node(ns.bsfs.Entity).child(ns.bsfs.invalid), { URI('http://example.com/me/entity#1234'), URI('http://example.com/me/entity#4321')}) + # guid must be valid + self.assertRaises(ValueError, store.create, self.schema.node(ns.bsfs.Entity), {URI('http://example.com/me/foo and bar')}) + # can create some nodes ent_type = store.schema.node(ns.bsfs.Entity) store.create(ent_type, {URI('http://example.com/me/entity#1234'), URI('http://example.com/me/entity#4321')}) @@ -766,6 +769,9 @@ class TestSparqlStore(unittest.TestCase): # invalid predicate is not permitted self.assertRaises(errors.ConsistencyError, store.set, ent_type, ent_ids, p_invalid, {'http://example.com/me/tag#1234'}) + # invalid guid is not permitted + self.assertRaises(ValueError, store.set, ent_type, {URI('http://example.com/me/foo and bar')}, p_filesize, {1234}) + # predicate must match node_type self.assertRaises(errors.ConsistencyError, store.set, tag_type, tag_ids, p_filesize, {1234}) -- cgit v1.2.3 From 28a021483c13e974e00b6159f0653b0727df9d10 Mon Sep 17 00:00:00 2001 From: Matthias Baumgartner Date: Thu, 2 Mar 2023 16:40:00 +0100 Subject: prohibit certain characters in URI and ensure URIs in bsfs.graph --- test/graph/test_nodes.py | 19 ++++++++++++------- test/query/ast_test/test_filter_.py | 2 +- test/triple_store/sparql/test_parse_filter.py | 2 +- test/triple_store/sparql/test_sparql.py | 4 ++-- test/utils/test_uri.py | 19 ++++++++++++++----- 5 files changed, 30 insertions(+), 16 deletions(-) (limited to 'test') diff --git a/test/graph/test_nodes.py b/test/graph/test_nodes.py index 083b2d8..dca887a 100644 --- a/test/graph/test_nodes.py +++ b/test/graph/test_nodes.py @@ -123,15 +123,19 @@ class TestNodes(unittest.TestCase): URI('http://example.com/me/tag#4321'), } + def test_construct(self): + self.assertIsInstance(Nodes(self.backend, self.ac, self.ent_type, {'http://example.com/me-and-you'}), Nodes) + self.assertRaises(ValueError, Nodes, self.backend, self.ac, self.ent_type, {'http://example.com/me and you'}) + def test_str(self): # str baseline - nodes = Nodes(self.backend, self.ac, self.ent_type, self.ent_ids) - self.assertEqual(str(nodes), f'Nodes({self.ent_type}, {self.ent_ids})') - self.assertEqual(repr(nodes), f'Nodes({self.backend}, {self.ac}, {self.ent_type}, {self.ent_ids})') + nodes = Nodes(self.backend, self.ac, self.ent_type, {URI('http://example.com/me/entity#1234')}) + self.assertEqual(str(nodes), f"Nodes({self.ent_type}, {{'http://example.com/me/entity#1234'}})") + self.assertEqual(repr(nodes), f"Nodes({self.backend}, {self.ac}, {self.ent_type}, {{'http://example.com/me/entity#1234'}})") # str respects node_type - nodes = Nodes(self.backend, self.ac, self.tag_type, self.tag_ids) - self.assertEqual(str(nodes), f'Nodes({self.tag_type}, {self.tag_ids})') - self.assertEqual(repr(nodes), f'Nodes({self.backend}, {self.ac}, {self.tag_type}, {self.tag_ids})') + nodes = Nodes(self.backend, self.ac, self.tag_type, {URI('http://example.com/me/tag#1234')}) + self.assertEqual(str(nodes), f"Nodes({self.tag_type}, {{'http://example.com/me/tag#1234'}})") + self.assertEqual(repr(nodes), f"Nodes({self.backend}, {self.ac}, {self.tag_type}, {{'http://example.com/me/tag#1234'}})") # str respects guids nodes = Nodes(self.backend, self.ac, self.ent_type, {URI('http://example.com/me/entity#foo')}) self.assertEqual(str(nodes), f'Nodes({self.ent_type}, {{\'http://example.com/me/entity#foo\'}})') @@ -437,7 +441,8 @@ class TestNodes(unittest.TestCase): self.assertRaises(TypeError, nodes.get, 1234) self.assertRaises(TypeError, nodes.get, (ns.bse.tag, 1234)) self.assertRaises(TypeError, nodes.get, (1234, ns.bse.tag)) - self.assertRaises(errors.ConsistencyError, nodes.get, 'hello world') + self.assertRaises(ValueError, nodes.get, 'hello world') + self.assertRaises(errors.ConsistencyError, nodes.get, 'hello_world') self.assertRaises(errors.ConsistencyError, nodes.get, ns.bse.invalid) self.assertRaises(errors.ConsistencyError, nodes.get, (ns.bse.tag, bst.invalid)) # can pass multiple paths diff --git a/test/query/ast_test/test_filter_.py b/test/query/ast_test/test_filter_.py index cdc530c..d0d42ea 100644 --- a/test/query/ast_test/test_filter_.py +++ b/test/query/ast_test/test_filter_.py @@ -382,7 +382,7 @@ class TestPredicate(unittest.TestCase): # member returns predicate # predicate must be an URI self.assertEqual(Predicate(ns.bse.filesize).predicate, ns.bse.filesize) - self.assertEqual(Predicate(URI('hello world')).predicate, URI('hello world')) + self.assertEqual(Predicate(URI('hello_world')).predicate, URI('hello_world')) self.assertRaises(TypeError, Predicate, 1234) self.assertRaises(TypeError, Predicate, FilterExpression()) self.assertRaises(TypeError, Predicate, FilterExpression()) diff --git a/test/triple_store/sparql/test_parse_filter.py b/test/triple_store/sparql/test_parse_filter.py index 6db9224..5b6ca8a 100644 --- a/test/triple_store/sparql/test_parse_filter.py +++ b/test/triple_store/sparql/test_parse_filter.py @@ -158,7 +158,7 @@ class TestParseFilter(unittest.TestCase): # _is requires a node self.assertRaises(errors.BackendError, self.parser._is, self.schema.literal(ns.bsfs.Literal), ast.filter.Is('http://example.com/entity#1234'), '?ent') # _is requires a serializable guid - self.assertRaises(errors.BackendError, self.parser._is, self.schema.node(ns.bsfs.Entity), ast.filter.Is('http://example.com/entity#foo and bar'), '?ent') + self.assertRaises(ValueError, self.parser._is, self.schema.node(ns.bsfs.Entity), ast.filter.Is('http://example.com/entity#foo and bar'), '?ent') # a single Is statement q = self.parser(self.schema.node(ns.bsfs.Entity), ast.filter.Is('http://example.com/entity#1234')) self.assertSetEqual({str(guid) for guid, in q(self.graph)}, diff --git a/test/triple_store/sparql/test_sparql.py b/test/triple_store/sparql/test_sparql.py index d880082..f45ca37 100644 --- a/test/triple_store/sparql/test_sparql.py +++ b/test/triple_store/sparql/test_sparql.py @@ -679,7 +679,7 @@ class TestSparqlStore(unittest.TestCase): URI('http://example.com/me/entity#1234'), URI('http://example.com/me/entity#4321')}) # guid must be valid - self.assertRaises(ValueError, store.create, self.schema.node(ns.bsfs.Entity), {URI('http://example.com/me/foo and bar')}) + self.assertRaises(ValueError, store.create, self.schema.node(ns.bsfs.Entity), {'http://example.com/me/foo and bar'}) # can create some nodes ent_type = store.schema.node(ns.bsfs.Entity) @@ -770,7 +770,7 @@ class TestSparqlStore(unittest.TestCase): self.assertRaises(errors.ConsistencyError, store.set, ent_type, ent_ids, p_invalid, {'http://example.com/me/tag#1234'}) # invalid guid is not permitted - self.assertRaises(ValueError, store.set, ent_type, {URI('http://example.com/me/foo and bar')}, p_filesize, {1234}) + self.assertRaises(ValueError, store.set, ent_type, {'http://example.com/me/foo and bar'}, p_filesize, {1234}) # predicate must match node_type self.assertRaises(errors.ConsistencyError, store.set, tag_type, tag_ids, p_filesize, {1234}) diff --git a/test/utils/test_uri.py b/test/utils/test_uri.py index 6ee2ef7..1c4c9f9 100644 --- a/test/utils/test_uri.py +++ b/test/utils/test_uri.py @@ -35,6 +35,16 @@ class TestURI(unittest.TestCase): self.assertTrue(URI.is_parseable('telnet://192.0.2.16:80/')) self.assertTrue(URI.is_parseable('urn:oasis:names:specification:docbook:dtd:xml:4.1.2')) + # some characters are prohibited + self.assertFalse(URI.is_parseable('http://example.com/foobar')) + self.assertFalse(URI.is_parseable('http://example.com/foo bar')) + self.assertFalse(URI.is_parseable('http://example.com/foo{bar')) + self.assertFalse(URI.is_parseable('http://example.com/foo}bar')) + self.assertFalse(URI.is_parseable('http://example.com/foo|bar')) + self.assertFalse(URI.is_parseable('http://example.com/foo^bar')) + self.assertFalse(URI.is_parseable('http://example.com/foo\\bar')) + # uri cannot end with a scheme delimiter self.assertFalse(URI.is_parseable('http://')) # port must be a number @@ -159,10 +169,10 @@ class TestURI(unittest.TestCase): def test_overloaded(self): # composition - self.assertIsInstance(URI('http://user@www.example.com:1234/{}/path1?{}#fragment') + 'hello', URI) - self.assertIsInstance(URI('http://user@www.example.com:1234/{}/path1?{}#fragment') * 2, URI) - self.assertIsInstance(2 * URI('http://user@www.example.com:1234/{}/path1?{}#fragment'), URI) # rmul - self.assertIsInstance(URI('http://user@www.example.com:1234/{}/path1?{}#fragment').join(['hello', 'world']) , URI) + self.assertIsInstance(URI('http://user@www.example.com:1234/path0/path1?query#fragment') + 'hello', URI) + self.assertIsInstance(URI('http://user@www.example.com:1234/path0/path1?query#fragment') * 2, URI) + self.assertIsInstance(2 * URI('http://user@www.example.com:1234/path0/path1?query#fragment'), URI) # rmul + self.assertIsInstance(URI('http://user@www.example.com:1234/path0/path1?query#fragment').join(['hello', 'world']) , URI) # stripping self.assertIsInstance(URI('http://user@www.example.com:1234/path0/path1?query#fragment').strip(), URI) self.assertIsInstance(URI('http://user@www.example.com:1234/path0/path1?query#fragment').lstrip(), URI) @@ -171,7 +181,6 @@ class TestURI(unittest.TestCase): self.assertIsInstance(URI('http://user@www.example.com:1234/path0/path1?query#fragment').lower(), URI) self.assertIsInstance(URI('http://user@www.example.com:1234/path0/path1?query#fragment').upper(), URI) # formatting - self.assertIsInstance(URI('http://user@www.example.com:1234/{}/path1?{}#fragment').format('hello', 'world'), URI) self.assertIsInstance(URI('http://user@www.example.com:1234/%s/path1?%s#fragment') % ('hello', 'world'), URI) self.assertIsInstance(URI('http://user@www.example.com:1234/path0/path1?query#fragment').replace('path0', 'pathX'), URI) -- cgit v1.2.3 From 6b9379d75198082054c35e44bc2cd880353a7485 Mon Sep 17 00:00:00 2001 From: Matthias Baumgartner Date: Thu, 2 Mar 2023 16:40:43 +0100 Subject: hardening --- test/graph/test_resolve.py | 2 +- test/query/test_validator.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) (limited to 'test') diff --git a/test/graph/test_resolve.py b/test/graph/test_resolve.py index 0223c49..accb565 100644 --- a/test/graph/test_resolve.py +++ b/test/graph/test_resolve.py @@ -25,7 +25,7 @@ class TestFilter(unittest.TestCase): """ - def test_call(self): + def test_call(self): # tests resolve implicitly schema = bsc.from_string(''' prefix rdfs: prefix xsd: diff --git a/test/query/test_validator.py b/test/query/test_validator.py index ca93118..bbfd2e6 100644 --- a/test/query/test_validator.py +++ b/test/query/test_validator.py @@ -70,7 +70,7 @@ class TestFilter(unittest.TestCase): ''') self.validate = Filter(self.schema) - def test_call(self): + def test_call(self): # tests validate implicitly # root_type must be a _schema.Node self.assertRaises(TypeError, self.validate, 1234, None) self.assertRaises(TypeError, self.validate, '1234', None) @@ -309,7 +309,7 @@ class TestFetch(unittest.TestCase): ''') self.validate = Fetch(self.schema) - def test_call(self): + def test_call(self): # tests validate implicitly # call accepts correct expressions self.assertTrue(self.validate(self.schema.node(ns.bsfs.Entity), ast.fetch.Fetch(ns.bse.tag, ast.fetch.Value(ns.bse.label, 'value')))) -- cgit v1.2.3 From 4fead04055be4967d9ea3b24ff61fe37a93108dd Mon Sep 17 00:00:00 2001 From: Matthias Baumgartner Date: Sat, 4 Mar 2023 13:31:11 +0100 Subject: namespace refactoring and cleanup --- test/apps/schema-1.nt | 4 +- test/apps/schema-2.nt | 4 +- test/graph/ac/test_null.py | 17 +- test/graph/test_graph.py | 52 ++-- test/graph/test_nodes.py | 124 +++++----- test/graph/test_resolve.py | 18 +- test/graph/test_result.py | 2 + test/graph/test_walk.py | 48 ++-- test/namespace/test_namespace.py | 126 +++------- test/query/test_validator.py | 25 +- test/schema/test_schema.py | 13 +- test/schema/test_serialize.py | 335 ++++++++++++++------------ test/schema/test_types.py | 2 + test/triple_store/sparql/test_parse_fetch.py | 95 ++++---- test/triple_store/sparql/test_parse_filter.py | 39 +-- test/triple_store/sparql/test_sparql.py | 104 ++++---- test/triple_store/sparql/test_utils.py | 24 +- 17 files changed, 516 insertions(+), 516 deletions(-) (limited to 'test') diff --git a/test/apps/schema-1.nt b/test/apps/schema-1.nt index e57146d..4daf0ad 100644 --- a/test/apps/schema-1.nt +++ b/test/apps/schema-1.nt @@ -3,8 +3,8 @@ prefix rdfs: prefix xsd: # common bsfs prefixes -prefix bsfs: -prefix bse: +prefix bsfs: +prefix bse: # essential nodes bsfs:Entity rdfs:subClassOf bsfs:Node . diff --git a/test/apps/schema-2.nt b/test/apps/schema-2.nt index 4c5468f..4eb2467 100644 --- a/test/apps/schema-2.nt +++ b/test/apps/schema-2.nt @@ -3,8 +3,8 @@ prefix rdfs: prefix xsd: # common bsfs prefixes -prefix bsfs: -prefix bse: +prefix bsfs: +prefix bse: # essential nodes bsfs:Entity rdfs:subClassOf bsfs:Node . diff --git a/test/graph/ac/test_null.py b/test/graph/ac/test_null.py index b695e7e..142bc23 100644 --- a/test/graph/ac/test_null.py +++ b/test/graph/ac/test_null.py @@ -15,6 +15,8 @@ from bsfs.graph.ac.null import NullAC ## code ## +ns.bse = ns.bsfs.Entity() + class TestNullAC(unittest.TestCase): def setUp(self): self.backend = SparqlStore() @@ -22,18 +24,19 @@ class TestNullAC(unittest.TestCase): prefix rdfs: prefix xsd: - prefix bsfs: - prefix bsm: - prefix bse: + prefix bsfs: + prefix bsl: + prefix bsn: + prefix bse: bsfs:Entity rdfs:subClassOf bsfs:Node . bsfs:Tag rdfs:subClassOf bsfs:Node . xsd:string rdfs:subClassOf bsfs:Literal . - bsfs:Number rdfs:subClassOf bsfs:Literal . - xsd:integer rdfs:subClassOf bsfs:Number . + bsl:Number rdfs:subClassOf bsfs:Literal . + xsd:integer rdfs:subClassOf bsl:Number . # predicates mandated by Nodes - bsm:t_created rdfs:subClassOf bsfs:Predicate ; + bsn:t_created rdfs:subClassOf bsfs:Predicate ; rdfs:domain bsfs:Node ; rdfs:range xsd:integer ; bsfs:unique "true"^^xsd:boolean . @@ -59,7 +62,7 @@ class TestNullAC(unittest.TestCase): self.p_author = self.backend.schema.predicate(ns.bse.author) self.p_filesize = self.backend.schema.predicate(ns.bse.filesize) self.p_tag = self.backend.schema.predicate(ns.bse.tag) - self.p_created = self.backend.schema.predicate(ns.bsm.t_created) + self.p_created = self.backend.schema.predicate(ns.bsn.t_created) self.ent_type = self.backend.schema.node(ns.bsfs.Entity) self.ent_ids = {URI('http://www.example.com/me/entity#1234'), URI('http://www.example.com/me/entity#4321')} diff --git a/test/graph/test_graph.py b/test/graph/test_graph.py index e6d5ae4..167168d 100644 --- a/test/graph/test_graph.py +++ b/test/graph/test_graph.py @@ -19,12 +19,14 @@ from bsfs.graph.graph import Graph ## code ## +ns.bse = ns.bsfs.Entity() + class TestGraph(unittest.TestCase): def setUp(self): self.backend = SparqlStore.Open() self.backend.schema = schema.from_string(''' prefix rdfs: - prefix bsfs: + prefix bsfs: bsfs:Entity rdfs:subClassOf bsfs:Node . ''') self.user = URI('http://example.com/me') @@ -129,12 +131,13 @@ class TestGraph(unittest.TestCase): target_1 = schema.from_string(''' prefix rdfs: prefix xsd: - prefix bsfs: - prefix bse: + prefix bsfs: + prefix bse: + prefix bsl: bsfs:Entity rdfs:subClassOf bsfs:Node . xsd:string rdfs:subClassOf bsfs:Literal . - bsfs:Number rdfs:subClassOf bsfs:Literal . - xsd:integer rdfs:subClassOf bsfs:Number . + bsl:Number rdfs:subClassOf bsfs:Literal . + xsd:integer rdfs:subClassOf bsl:Number . bse:filename rdfs:subClassOf bsfs:Predicate ; rdfs:domain bsfs:Entity ; @@ -154,11 +157,12 @@ class TestGraph(unittest.TestCase): self.assertEqual(graph.schema, target_1 + schema.from_string(''' prefix rdfs: prefix xsd: - prefix bsfs: - prefix bsm: - bsfs:Number rdfs:subClassOf bsfs:Literal . - xsd:float rdfs:subClassOf bsfs:Number . - bsm:t_created rdfs:subClassOf bsfs:Predicate ; + prefix bsfs: + prefix bsn: + prefix bsl: + bsl:Number rdfs:subClassOf bsfs:Literal . + xsd:float rdfs:subClassOf bsl:Number . + bsn:t_created rdfs:subClassOf bsfs:Predicate ; rdfs:domain bsfs:Node ; rdfs:range xsd:float ; bsfs:unique "true"^^xsd:boolean . @@ -168,12 +172,13 @@ class TestGraph(unittest.TestCase): target_2 = schema.from_string(''' prefix rdfs: prefix xsd: - prefix bsfs: - prefix bse: + prefix bsfs: + prefix bse: + prefix bsl: bsfs:Entity rdfs:subClassOf bsfs:Node . xsd:string rdfs:subClassOf bsfs:Literal . - bsfs:Number rdfs:subClassOf bsfs:Literal . - xsd:integer rdfs:subClassOf bsfs:Number . + bsl:Number rdfs:subClassOf bsfs:Literal . + xsd:integer rdfs:subClassOf bsl:Number . bse:filename rdfs:subClassOf bsfs:Predicate ; rdfs:domain bsfs:Entity ; @@ -195,11 +200,12 @@ class TestGraph(unittest.TestCase): self.assertEqual(graph.schema, target_2 + schema.from_string(''' prefix rdfs: prefix xsd: - prefix bsfs: - prefix bsm: - bsfs:Number rdfs:subClassOf bsfs:Literal . - xsd:float rdfs:subClassOf bsfs:Number . - bsm:t_created rdfs:subClassOf bsfs:Predicate ; + prefix bsfs: + prefix bsn: + prefix bsl: + bsl:Number rdfs:subClassOf bsfs:Literal . + xsd:float rdfs:subClassOf bsl:Number . + bsn:t_created rdfs:subClassOf bsfs:Predicate ; rdfs:domain bsfs:Node ; rdfs:range xsd:float ; bsfs:unique "true"^^xsd:boolean . @@ -211,8 +217,8 @@ class TestGraph(unittest.TestCase): graph.migrate(schema.from_string(''' prefix rdfs: prefix xsd: - prefix bsfs: - prefix bse: + prefix bsfs: + prefix bse: bsfs:Entity rdfs:subClassOf bsfs:Node . bsfs:Tag rdfs:subClassOf bsfs:Node . @@ -264,8 +270,8 @@ class TestGraph(unittest.TestCase): graph.migrate(schema.from_string(''' prefix rdfs: prefix xsd: - prefix bsfs: - prefix bse: + prefix bsfs: + prefix bse: bsfs:Entity rdfs:subClassOf bsfs:Node . bsfs:Tag rdfs:subClassOf bsfs:Node . diff --git a/test/graph/test_nodes.py b/test/graph/test_nodes.py index dca887a..afe7522 100644 --- a/test/graph/test_nodes.py +++ b/test/graph/test_nodes.py @@ -21,7 +21,8 @@ from bsfs.graph.nodes import Nodes ## code ## -bst = Namespace('http://bsfs.ai/schema/Tag') +ns.bse = ns.bsfs.Entity() +ns.bst = ns.bsfs.Tag() class TestNodes(unittest.TestCase): def setUp(self): @@ -31,20 +32,21 @@ class TestNodes(unittest.TestCase): prefix rdfs: prefix xsd: - prefix bsfs: - prefix bsm: - prefix bse: - prefix bst: + prefix bsfs: + prefix bsl: + prefix bsn: + prefix bse: + prefix bst: bsfs:Entity rdfs:subClassOf bsfs:Node . bsfs:Tag rdfs:subClassOf bsfs:Node . bsfs:User rdfs:subClassOf bsfs:Node . xsd:string rdfs:subClassOf bsfs:Literal . - bsfs:Number rdfs:subClassOf bsfs:Literal . - xsd:integer rdfs:subClassOf bsfs:Number . + bsl:Number rdfs:subClassOf bsfs:Literal . + xsd:integer rdfs:subClassOf bsl:Number . # predicates mandated by Nodes - bsm:t_created rdfs:subClassOf bsfs:Predicate ; + bsn:t_created rdfs:subClassOf bsfs:Predicate ; rdfs:domain bsfs:Node ; rdfs:range xsd:integer ; bsfs:unique "true"^^xsd:boolean . @@ -87,19 +89,19 @@ class TestNodes(unittest.TestCase): (rdflib.URIRef(ns.bsfs.Tag), rdflib.RDFS.subClassOf, rdflib.URIRef(ns.bsfs.Node)), (rdflib.URIRef(ns.bsfs.User), rdflib.RDFS.subClassOf, rdflib.URIRef(ns.bsfs.Node)), (rdflib.URIRef(ns.xsd.string), rdflib.RDFS.subClassOf, rdflib.URIRef(ns.bsfs.Literal)), - (rdflib.URIRef(ns.bsfs.Array), rdflib.RDFS.subClassOf, rdflib.URIRef(ns.bsfs.Literal)), - (rdflib.URIRef(ns.bsfs.BinaryBlob), rdflib.RDFS.subClassOf, rdflib.URIRef(ns.bsfs.Literal)), - (rdflib.URIRef(ns.bsfs.Feature), rdflib.RDFS.subClassOf, rdflib.URIRef(ns.bsfs.Array)), - (rdflib.URIRef(ns.bsfs.Number), rdflib.RDFS.subClassOf, rdflib.URIRef(ns.bsfs.Literal)), - (rdflib.URIRef(ns.bsfs.Time), rdflib.RDFS.subClassOf, rdflib.URIRef(ns.bsfs.Literal)), - (rdflib.URIRef(ns.xsd.integer), rdflib.RDFS.subClassOf, rdflib.URIRef(ns.bsfs.Number)), - (rdflib.URIRef(ns.bsm.t_created), rdflib.RDFS.subClassOf, rdflib.URIRef(ns.bsfs.Predicate)), + (rdflib.URIRef(ns.bsl.Array), rdflib.RDFS.subClassOf, rdflib.URIRef(ns.bsfs.Literal)), + (rdflib.URIRef(ns.bsl.BinaryBlob), rdflib.RDFS.subClassOf, rdflib.URIRef(ns.bsfs.Literal)), + (rdflib.URIRef(ns.bsl.Array.Feature), rdflib.RDFS.subClassOf, rdflib.URIRef(ns.bsl.Array)), + (rdflib.URIRef(ns.bsl.Number), rdflib.RDFS.subClassOf, rdflib.URIRef(ns.bsfs.Literal)), + (rdflib.URIRef(ns.bsl.Time), rdflib.RDFS.subClassOf, rdflib.URIRef(ns.bsfs.Literal)), + (rdflib.URIRef(ns.xsd.integer), rdflib.RDFS.subClassOf, rdflib.URIRef(ns.bsl.Number)), + (rdflib.URIRef(ns.bsn.t_created), rdflib.RDFS.subClassOf, rdflib.URIRef(ns.bsfs.Predicate)), (rdflib.URIRef(ns.bse.comment), rdflib.RDFS.subClassOf, rdflib.URIRef(ns.bsfs.Predicate)), (rdflib.URIRef(ns.bse.filesize), rdflib.RDFS.subClassOf, rdflib.URIRef(ns.bsfs.Predicate)), (rdflib.URIRef(ns.bse.tag), rdflib.RDFS.subClassOf, rdflib.URIRef(ns.bsfs.Predicate)), (rdflib.URIRef(ns.bse.author), rdflib.RDFS.subClassOf, rdflib.URIRef(ns.bsfs.Predicate)), - (rdflib.URIRef(bst.representative), rdflib.RDFS.subClassOf, rdflib.URIRef(ns.bsfs.Predicate)), - (rdflib.URIRef(bst.label), rdflib.RDFS.subClassOf, rdflib.URIRef(ns.bsfs.Predicate)), + (rdflib.URIRef(ns.bst.representative), rdflib.RDFS.subClassOf, rdflib.URIRef(ns.bsfs.Predicate)), + (rdflib.URIRef(ns.bst.label), rdflib.RDFS.subClassOf, rdflib.URIRef(ns.bsfs.Predicate)), } # Nodes constructor args self.user = URI('http://example.com/me') @@ -111,9 +113,9 @@ class TestNodes(unittest.TestCase): self.p_filesize = self.backend.schema.predicate(ns.bse.filesize) self.p_author = self.backend.schema.predicate(ns.bse.author) self.p_tag = self.backend.schema.predicate(ns.bse.tag) - self.p_representative = self.backend.schema.predicate(bst.representative) - self.p_label = self.backend.schema.predicate(bst.label) - self.t_created = self.backend.schema.predicate(ns.bsm.t_created) + self.p_representative = self.backend.schema.predicate(ns.bst.representative) + self.p_label = self.backend.schema.predicate(ns.bst.label) + self.t_created = self.backend.schema.predicate(ns.bsn.t_created) self.ent_ids = { URI('http://example.com/me/entity#1234'), URI('http://example.com/me/entity#4321'), @@ -144,11 +146,11 @@ class TestNodes(unittest.TestCase): class Foo(SparqlStore): pass backend = Foo.Open() backend.schema = self.backend.schema - nodes = Nodes(backend, self.ac, self.ent_type, self.ent_ids) - self.assertEqual(repr(nodes), f'Nodes({backend}, {self.ac}, {self.ent_type}, {self.ent_ids})') + nodes = Nodes(backend, self.ac, self.ent_type, {URI('http://example.com/me/entity#1234')}) + self.assertEqual(repr(nodes), f"Nodes({backend}, {self.ac}, {self.ent_type}, {{'http://example.com/me/entity#1234'}})") # repr respects user - nodes = Nodes(self.backend, NullAC(self.backend, URI('http://example.com/you')), self.ent_type, self.ent_ids) - self.assertEqual(repr(nodes), f'Nodes({self.backend}, NullAC(http://example.com/you), {self.ent_type}, {self.ent_ids})') + nodes = Nodes(self.backend, NullAC(self.backend, URI('http://example.com/you')), self.ent_type, {URI('http://example.com/me/entity#1234')}) + self.assertEqual(repr(nodes), f"Nodes({self.backend}, NullAC(http://example.com/you), {self.ent_type}, {{'http://example.com/me/entity#1234'}})") def test_equality(self): nodes = Nodes(self.backend, self.ac, self.ent_type, self.ent_ids) @@ -197,8 +199,8 @@ class TestNodes(unittest.TestCase): # check triples self.assertSetEqual(set(self.backend._graph), self.schema_triples | { # entity definitions - (rdflib.URIRef('http://example.com/me/entity#1234'), rdflib.RDF.type, rdflib.URIRef('http://bsfs.ai/schema/Entity')), - (rdflib.URIRef('http://example.com/me/entity#4321'), rdflib.RDF.type, rdflib.URIRef('http://bsfs.ai/schema/Entity')), + (rdflib.URIRef('http://example.com/me/entity#1234'), rdflib.RDF.type, rdflib.URIRef('https://schema.bsfs.io/core/Entity')), + (rdflib.URIRef('http://example.com/me/entity#4321'), rdflib.RDF.type, rdflib.URIRef('https://schema.bsfs.io/core/Entity')), # bookkeeping (rdflib.URIRef('http://example.com/me/entity#1234'), rdflib.URIRef(self.t_created.uri), rdflib.Literal(t_ent_created, datatype=rdflib.XSD.integer)), (rdflib.URIRef('http://example.com/me/entity#4321'), rdflib.URIRef(self.t_created.uri), rdflib.Literal(t_ent_created, datatype=rdflib.XSD.integer)), @@ -208,8 +210,8 @@ class TestNodes(unittest.TestCase): self.assertSetEqual(self.ent_ids, nodes._ensure_nodes(self.ent_type, self.ent_ids)) self.assertSetEqual(set(self.backend._graph), self.schema_triples | { # entity definitions - (rdflib.URIRef('http://example.com/me/entity#1234'), rdflib.RDF.type, rdflib.URIRef('http://bsfs.ai/schema/Entity')), - (rdflib.URIRef('http://example.com/me/entity#4321'), rdflib.RDF.type, rdflib.URIRef('http://bsfs.ai/schema/Entity')), + (rdflib.URIRef('http://example.com/me/entity#1234'), rdflib.RDF.type, rdflib.URIRef('https://schema.bsfs.io/core/Entity')), + (rdflib.URIRef('http://example.com/me/entity#4321'), rdflib.RDF.type, rdflib.URIRef('https://schema.bsfs.io/core/Entity')), # bookkeeping (rdflib.URIRef('http://example.com/me/entity#1234'), rdflib.URIRef(self.t_created.uri), rdflib.Literal(t_ent_created, datatype=rdflib.XSD.integer)), (rdflib.URIRef('http://example.com/me/entity#4321'), rdflib.URIRef(self.t_created.uri), rdflib.Literal(t_ent_created, datatype=rdflib.XSD.integer)), @@ -223,13 +225,13 @@ class TestNodes(unittest.TestCase): # check triples self.assertSetEqual(set(self.backend._graph), self.schema_triples | { # previous triples - (rdflib.URIRef('http://example.com/me/entity#1234'), rdflib.RDF.type, rdflib.URIRef('http://bsfs.ai/schema/Entity')), - (rdflib.URIRef('http://example.com/me/entity#4321'), rdflib.RDF.type, rdflib.URIRef('http://bsfs.ai/schema/Entity')), + (rdflib.URIRef('http://example.com/me/entity#1234'), rdflib.RDF.type, rdflib.URIRef('https://schema.bsfs.io/core/Entity')), + (rdflib.URIRef('http://example.com/me/entity#4321'), rdflib.RDF.type, rdflib.URIRef('https://schema.bsfs.io/core/Entity')), (rdflib.URIRef('http://example.com/me/entity#1234'), rdflib.URIRef(self.t_created.uri), rdflib.Literal(t_ent_created, datatype=rdflib.XSD.integer)), (rdflib.URIRef('http://example.com/me/entity#4321'), rdflib.URIRef(self.t_created.uri), rdflib.Literal(t_ent_created, datatype=rdflib.XSD.integer)), # new triples - (rdflib.URIRef('http://example.com/me/tag#1234'), rdflib.RDF.type, rdflib.URIRef('http://bsfs.ai/schema/Tag')), - (rdflib.URIRef('http://example.com/me/tag#4321'), rdflib.RDF.type, rdflib.URIRef('http://bsfs.ai/schema/Tag')), + (rdflib.URIRef('http://example.com/me/tag#1234'), rdflib.RDF.type, rdflib.URIRef('https://schema.bsfs.io/core/Tag')), + (rdflib.URIRef('http://example.com/me/tag#4321'), rdflib.RDF.type, rdflib.URIRef('https://schema.bsfs.io/core/Tag')), (rdflib.URIRef('http://example.com/me/tag#1234'), rdflib.URIRef(self.t_created.uri), rdflib.Literal(t_tag_created, datatype=rdflib.XSD.integer)), (rdflib.URIRef('http://example.com/me/tag#4321'), rdflib.URIRef(self.t_created.uri), rdflib.Literal(t_tag_created, datatype=rdflib.XSD.integer)), }) @@ -254,8 +256,8 @@ class TestNodes(unittest.TestCase): # verify triples self.assertSetEqual(set(self.backend._graph), self.schema_triples | { # entity definitions - (rdflib.URIRef('http://example.com/me/entity#1234'), rdflib.RDF.type, rdflib.URIRef('http://bsfs.ai/schema/Entity')), - (rdflib.URIRef('http://example.com/me/entity#4321'), rdflib.RDF.type, rdflib.URIRef('http://bsfs.ai/schema/Entity')), + (rdflib.URIRef('http://example.com/me/entity#1234'), rdflib.RDF.type, rdflib.URIRef('https://schema.bsfs.io/core/Entity')), + (rdflib.URIRef('http://example.com/me/entity#4321'), rdflib.RDF.type, rdflib.URIRef('https://schema.bsfs.io/core/Entity')), # bookkeeping (rdflib.URIRef('http://example.com/me/entity#1234'), rdflib.URIRef(self.t_created.uri), rdflib.Literal(t_ent_created, datatype=rdflib.XSD.integer)), (rdflib.URIRef('http://example.com/me/entity#4321'), rdflib.URIRef(self.t_created.uri), rdflib.Literal(t_ent_created, datatype=rdflib.XSD.integer)), @@ -273,15 +275,15 @@ class TestNodes(unittest.TestCase): # verify triples self.assertSetEqual(set(self.backend._graph), self.schema_triples | { # previous values - (rdflib.URIRef('http://example.com/me/entity#1234'), rdflib.RDF.type, rdflib.URIRef('http://bsfs.ai/schema/Entity')), - (rdflib.URIRef('http://example.com/me/entity#4321'), rdflib.RDF.type, rdflib.URIRef('http://bsfs.ai/schema/Entity')), + (rdflib.URIRef('http://example.com/me/entity#1234'), rdflib.RDF.type, rdflib.URIRef('https://schema.bsfs.io/core/Entity')), + (rdflib.URIRef('http://example.com/me/entity#4321'), rdflib.RDF.type, rdflib.URIRef('https://schema.bsfs.io/core/Entity')), (rdflib.URIRef('http://example.com/me/entity#1234'), rdflib.URIRef(self.p_filesize.uri), rdflib.Literal(1234, datatype=rdflib.XSD.integer)), (rdflib.URIRef('http://example.com/me/entity#4321'), rdflib.URIRef(self.p_filesize.uri), rdflib.Literal(1234, datatype=rdflib.XSD.integer)), (rdflib.URIRef('http://example.com/me/entity#1234'), rdflib.URIRef(self.t_created.uri), rdflib.Literal(t_ent_created, datatype=rdflib.XSD.integer)), (rdflib.URIRef('http://example.com/me/entity#4321'), rdflib.URIRef(self.t_created.uri), rdflib.Literal(t_ent_created, datatype=rdflib.XSD.integer)), # tag definitions - (rdflib.URIRef('http://example.com/me/tag#1234'), rdflib.RDF.type, rdflib.URIRef('http://bsfs.ai/schema/Tag')), - (rdflib.URIRef('http://example.com/me/tag#4321'), rdflib.RDF.type, rdflib.URIRef('http://bsfs.ai/schema/Tag')), + (rdflib.URIRef('http://example.com/me/tag#1234'), rdflib.RDF.type, rdflib.URIRef('https://schema.bsfs.io/core/Tag')), + (rdflib.URIRef('http://example.com/me/tag#4321'), rdflib.RDF.type, rdflib.URIRef('https://schema.bsfs.io/core/Tag')), # tag bookkeeping (rdflib.URIRef('http://example.com/me/tag#1234'), rdflib.URIRef(self.t_created.uri), rdflib.Literal(t_tag_created, datatype=rdflib.XSD.integer)), (rdflib.URIRef('http://example.com/me/tag#4321'), rdflib.URIRef(self.t_created.uri), rdflib.Literal(t_tag_created, datatype=rdflib.XSD.integer)), @@ -306,8 +308,8 @@ class TestNodes(unittest.TestCase): self.assertEqual(nodes, nodes.set(self.p_filesize.uri, 1234)) self.assertTrue(set(self.backend._graph).issuperset({ # nodes exist - (rdflib.URIRef('http://example.com/me/entity#1234'), rdflib.RDF.type, rdflib.URIRef('http://bsfs.ai/schema/Entity')), - (rdflib.URIRef('http://example.com/me/entity#4321'), rdflib.RDF.type, rdflib.URIRef('http://bsfs.ai/schema/Entity')), + (rdflib.URIRef('http://example.com/me/entity#1234'), rdflib.RDF.type, rdflib.URIRef('https://schema.bsfs.io/core/Entity')), + (rdflib.URIRef('http://example.com/me/entity#4321'), rdflib.RDF.type, rdflib.URIRef('https://schema.bsfs.io/core/Entity')), # links exist (rdflib.URIRef('http://example.com/me/entity#1234'), rdflib.URIRef(self.p_filesize.uri), rdflib.Literal(1234, datatype=rdflib.XSD.integer)), (rdflib.URIRef('http://example.com/me/entity#4321'), rdflib.URIRef(self.p_filesize.uri), rdflib.Literal(1234, datatype=rdflib.XSD.integer)), @@ -316,10 +318,10 @@ class TestNodes(unittest.TestCase): self.assertEqual(nodes, nodes.set(self.p_tag.uri, Nodes(self.backend, self.ac, self.tag_type, self.tag_ids))) self.assertTrue(set(self.backend._graph).issuperset({ # nodes exist - (rdflib.URIRef('http://example.com/me/entity#1234'), rdflib.RDF.type, rdflib.URIRef('http://bsfs.ai/schema/Entity')), - (rdflib.URIRef('http://example.com/me/entity#4321'), rdflib.RDF.type, rdflib.URIRef('http://bsfs.ai/schema/Entity')), - (rdflib.URIRef('http://example.com/me/tag#1234'), rdflib.RDF.type, rdflib.URIRef('http://bsfs.ai/schema/Tag')), - (rdflib.URIRef('http://example.com/me/tag#4321'), rdflib.RDF.type, rdflib.URIRef('http://bsfs.ai/schema/Tag')), + (rdflib.URIRef('http://example.com/me/entity#1234'), rdflib.RDF.type, rdflib.URIRef('https://schema.bsfs.io/core/Entity')), + (rdflib.URIRef('http://example.com/me/entity#4321'), rdflib.RDF.type, rdflib.URIRef('https://schema.bsfs.io/core/Entity')), + (rdflib.URIRef('http://example.com/me/tag#1234'), rdflib.RDF.type, rdflib.URIRef('https://schema.bsfs.io/core/Tag')), + (rdflib.URIRef('http://example.com/me/tag#4321'), rdflib.RDF.type, rdflib.URIRef('https://schema.bsfs.io/core/Tag')), # links exist (rdflib.URIRef('http://example.com/me/entity#1234'), rdflib.URIRef(self.p_tag.uri), rdflib.URIRef('http://example.com/me/tag#1234')), (rdflib.URIRef('http://example.com/me/entity#1234'), rdflib.URIRef(self.p_tag.uri), rdflib.URIRef('http://example.com/me/tag#4321')), @@ -360,10 +362,10 @@ class TestNodes(unittest.TestCase): }.items())) self.assertTrue(set(self.backend._graph).issuperset({ # nodes exist - (rdflib.URIRef('http://example.com/me/entity#1234'), rdflib.RDF.type, rdflib.URIRef('http://bsfs.ai/schema/Entity')), - (rdflib.URIRef('http://example.com/me/entity#4321'), rdflib.RDF.type, rdflib.URIRef('http://bsfs.ai/schema/Entity')), - (rdflib.URIRef('http://example.com/me/tag#1234'), rdflib.RDF.type, rdflib.URIRef('http://bsfs.ai/schema/Tag')), - (rdflib.URIRef('http://example.com/me/tag#4321'), rdflib.RDF.type, rdflib.URIRef('http://bsfs.ai/schema/Tag')), + (rdflib.URIRef('http://example.com/me/entity#1234'), rdflib.RDF.type, rdflib.URIRef('https://schema.bsfs.io/core/Entity')), + (rdflib.URIRef('http://example.com/me/entity#4321'), rdflib.RDF.type, rdflib.URIRef('https://schema.bsfs.io/core/Entity')), + (rdflib.URIRef('http://example.com/me/tag#1234'), rdflib.RDF.type, rdflib.URIRef('https://schema.bsfs.io/core/Tag')), + (rdflib.URIRef('http://example.com/me/tag#4321'), rdflib.RDF.type, rdflib.URIRef('https://schema.bsfs.io/core/Tag')), # links exist (rdflib.URIRef('http://example.com/me/entity#1234'), rdflib.URIRef(self.p_filesize.uri), rdflib.Literal(1234, datatype=rdflib.XSD.integer)), (rdflib.URIRef('http://example.com/me/entity#4321'), rdflib.URIRef(self.p_filesize.uri), rdflib.Literal(1234, datatype=rdflib.XSD.integer)), @@ -406,9 +408,9 @@ class TestNodes(unittest.TestCase): .set(ns.bse.filesize, 4321) \ .set(ns.bse.tag, Nodes(self.backend, self.ac, self.tag_type, {'http://example.com/me/tag#4321'})) Nodes(self.backend, self.ac, self.tag_type, {'http://example.com/me/tag#1234'}) \ - .set(bst.label, 'tag_label_1234') + .set(ns.bst.label, 'tag_label_1234') Nodes(self.backend, self.ac, self.tag_type, {'http://example.com/me/tag#4321'}) \ - .set(bst.label, 'tag_label_4321') + .set(ns.bst.label, 'tag_label_4321') # setup: get nodes instance nodes = Nodes(self.backend, self.ac, self.ent_type, self.ent_ids) @@ -424,18 +426,18 @@ class TestNodes(unittest.TestCase): Nodes(self.backend, self.ac, self.ent_type, {'http://example.com/me/entity#4321'}): 4321, }) # can pass path as sequence of URI - self.assertDictEqual(nodes.get((ns.bse.tag, bst.label)), { + self.assertDictEqual(nodes.get((ns.bse.tag, ns.bst.label)), { Nodes(self.backend, self.ac, self.ent_type, {'http://example.com/me/entity#1234'}): {'tag_label_1234'}, Nodes(self.backend, self.ac, self.ent_type, {'http://example.com/me/entity#4321'}): {'tag_label_4321'}, }) # get returns the same path that was passed - self.assertCountEqual(list(nodes.get((ns.bse.tag, bst.label), path=True, view=list)), [ - (Nodes(self.backend, self.ac, self.ent_type, {'http://example.com/me/entity#1234'}), (ns.bse.tag, bst.label), 'tag_label_1234'), - (Nodes(self.backend, self.ac, self.ent_type, {'http://example.com/me/entity#4321'}), (ns.bse.tag, bst.label), 'tag_label_4321'), + self.assertCountEqual(list(nodes.get((ns.bse.tag, ns.bst.label), path=True, view=list)), [ + (Nodes(self.backend, self.ac, self.ent_type, {'http://example.com/me/entity#1234'}), (ns.bse.tag, ns.bst.label), 'tag_label_1234'), + (Nodes(self.backend, self.ac, self.ent_type, {'http://example.com/me/entity#4321'}), (ns.bse.tag, ns.bst.label), 'tag_label_4321'), ]) - self.assertCountEqual(list(nodes.get([ns.bse.tag, bst.label], path=True, view=list)), [ - (Nodes(self.backend, self.ac, self.ent_type, {'http://example.com/me/entity#1234'}), [ns.bse.tag, bst.label], 'tag_label_1234'), - (Nodes(self.backend, self.ac, self.ent_type, {'http://example.com/me/entity#4321'}), [ns.bse.tag, bst.label], 'tag_label_4321'), + self.assertCountEqual(list(nodes.get([ns.bse.tag, ns.bst.label], path=True, view=list)), [ + (Nodes(self.backend, self.ac, self.ent_type, {'http://example.com/me/entity#1234'}), [ns.bse.tag, ns.bst.label], 'tag_label_1234'), + (Nodes(self.backend, self.ac, self.ent_type, {'http://example.com/me/entity#4321'}), [ns.bse.tag, ns.bst.label], 'tag_label_4321'), ]) # paths must be URI or sequence thereof self.assertRaises(TypeError, nodes.get, 1234) @@ -444,16 +446,16 @@ class TestNodes(unittest.TestCase): self.assertRaises(ValueError, nodes.get, 'hello world') self.assertRaises(errors.ConsistencyError, nodes.get, 'hello_world') self.assertRaises(errors.ConsistencyError, nodes.get, ns.bse.invalid) - self.assertRaises(errors.ConsistencyError, nodes.get, (ns.bse.tag, bst.invalid)) + self.assertRaises(errors.ConsistencyError, nodes.get, (ns.bse.tag, ns.bst.invalid)) # can pass multiple paths - self.assertDictEqual(nodes.get(ns.bse.filesize, (ns.bse.tag, bst.label)), { + self.assertDictEqual(nodes.get(ns.bse.filesize, (ns.bse.tag, ns.bst.label)), { Nodes(self.backend, self.ac, self.ent_type, {'http://example.com/me/entity#1234'}): { ns.bse.filesize: 1234, - (ns.bse.tag, bst.label): {'tag_label_1234'}, + (ns.bse.tag, ns.bst.label): {'tag_label_1234'}, }, Nodes(self.backend, self.ac, self.ent_type, {'http://example.com/me/entity#4321'}): { ns.bse.filesize: 4321, - (ns.bse.tag, bst.label): {'tag_label_4321'}, + (ns.bse.tag, ns.bst.label): {'tag_label_4321'}, }, }) # get respects view diff --git a/test/graph/test_resolve.py b/test/graph/test_resolve.py index accb565..e09b1cc 100644 --- a/test/graph/test_resolve.py +++ b/test/graph/test_resolve.py @@ -16,6 +16,8 @@ from bsfs.graph.resolve import Filter ## code ## +ns.bse = ns.bsfs.Entity() + class TestFilter(unittest.TestCase): """ @@ -30,18 +32,20 @@ class TestFilter(unittest.TestCase): prefix rdfs: prefix xsd: - prefix bsfs: - prefix bse: + prefix bsfs: + prefix bse: + prefix bsl: + prefix bsa: bsfs:Entity rdfs:subClassOf bsfs:Node . bsfs:Tag rdfs:subClassOf bsfs:Node . xsd:string rdfs:subClassOf bsfs:Literal . - bsfs:Number rdfs:subClassOf bsfs:Literal . - bsfs:Array rdfs:subClassOf bsfs:Literal . - bsfs:Feature rdfs:subClassOf bsfs:Array . - xsd:integer rdfs:subClassOf bsfs:Number . + bsl:Number rdfs:subClassOf bsfs:Literal . + bsl:Array rdfs:subClassOf bsfs:Literal . + bsa:Feature rdfs:subClassOf bsl:Array . + xsd:integer rdfs:subClassOf bsl:Number . - bsfs:Colors rdfs:subClassOf bsfs:Feature ; + bsfs:Colors rdfs:subClassOf bsa:Feature ; bsfs:dimension "5"^^xsd:integer . bse:colors rdfs:subClassOf bsfs:Predicate ; diff --git a/test/graph/test_result.py b/test/graph/test_result.py index 099234a..8960ef6 100644 --- a/test/graph/test_result.py +++ b/test/graph/test_result.py @@ -13,6 +13,8 @@ from bsfs.graph.result import to_list_view, to_dict_view ## code ## +ns.bse = ns.bsfs.Entity() + class TestListView(unittest.TestCase): def setUp(self): self.triples_111 = [('ent#1234', ns.bse.iso, 123)] diff --git a/test/graph/test_walk.py b/test/graph/test_walk.py index 346896b..4b844da 100644 --- a/test/graph/test_walk.py +++ b/test/graph/test_walk.py @@ -15,8 +15,8 @@ from bsfs.graph.walk import Walk ## code ## -bse = ns.bse -bst = Namespace('http://bsfs.ai/schema/Tag') +ns.bse = ns.bsfs.Entity() +ns.bst = ns.bsfs.Tag() class TestWalk(unittest.TestCase): def setUp(self): @@ -24,9 +24,9 @@ class TestWalk(unittest.TestCase): self.schema = bsc.from_string(''' prefix rdfs: prefix xsd: - prefix bsfs: - prefix bse: - prefix bst: + prefix bsfs: + prefix bse: + prefix bst: bsfs:Entity rdfs:subClassOf bsfs:Node . bsfs:Tag rdfs:subClassOf bsfs:Node . @@ -73,14 +73,14 @@ class TestWalk(unittest.TestCase): URI('http://example.com/me/tag#1234'), URI('http://example.com/me/tag#4321')}) # add some instances - self.ents.set(bse.tag, self.tags) - self.graph.node(ns.bsfs.Tag, URI('http://example.com/me/tag#1234')).set(bst.label, 'hello') - self.graph.node(ns.bsfs.Tag, URI('http://example.com/me/tag#4321')).set(bst.label, 'world') + self.ents.set(ns.bse.tag, self.tags) + self.graph.node(ns.bsfs.Tag, URI('http://example.com/me/tag#1234')).set(ns.bst.label, 'hello') + self.graph.node(ns.bsfs.Tag, URI('http://example.com/me/tag#4321')).set(ns.bst.label, 'world') def test_essentials(self): # __eq__, __hash__, __str__, __repr__ - p_author = self.schema.predicate(bse.author) - p_tag = self.schema.predicate(bse.tag) - p_main = self.schema.predicate(bst.main) + p_author = self.schema.predicate(ns.bse.author) + p_tag = self.schema.predicate(ns.bse.tag) + p_main = self.schema.predicate(ns.bst.main) # comparison self.assertEqual(Walk(self.ents, [p_tag]), Walk(self.ents, [p_tag])) self.assertEqual(hash(Walk(self.ents, [p_tag])), hash(Walk(self.ents, [p_tag]))) @@ -96,18 +96,18 @@ class TestWalk(unittest.TestCase): self.assertNotEqual(hash(Walk(self.tags, [p_author])), hash(Walk(self.tags, [p_main]))) # string conversion self.assertEqual(str(Walk(self.ents, [p_tag, p_main])), - 'Walk(@http://bsfs.ai/schema/Entity: http://bsfs.ai/schema/Entity#tag, http://bsfs.ai/schema/Tag#main)') + 'Walk(@https://schema.bsfs.io/core/Entity: https://schema.bsfs.io/core/Entity#tag, https://schema.bsfs.io/core/Tag#main)') self.assertEqual(repr(Walk(self.ents, [p_tag, p_main])), - 'Walk(http://bsfs.ai/schema/Entity, (http://bsfs.ai/schema/Entity#tag, http://bsfs.ai/schema/Tag#main))') + 'Walk(https://schema.bsfs.io/core/Entity, (https://schema.bsfs.io/core/Entity#tag, https://schema.bsfs.io/core/Tag#main))') def test_tail(self): self.assertEqual(Walk(self.ents, ( - self.schema.predicate(bse.tag), + self.schema.predicate(ns.bse.tag), )).tail, self.schema.node(ns.bsfs.Tag)) self.assertEqual(Walk(self.ents, ( - self.schema.predicate(bse.tag), - self.schema.predicate(bst.main), + self.schema.predicate(ns.bse.tag), + self.schema.predicate(ns.bst.main), )).tail, self.schema.node(ns.bsfs.Entity)) @@ -115,24 +115,24 @@ class TestWalk(unittest.TestCase): tag_type = self.schema.node(ns.bsfs.Tag) # step returns a predicate self.assertEqual(Walk.step(self.schema, tag_type, 'subTagOf'), - (self.schema.predicate(bst.subTagOf), )) + (self.schema.predicate(ns.bst.subTagOf), )) # invalid step raises an error self.assertRaises(ValueError, Walk.step, self.schema, tag_type, 'foobar') # ambiguous step raises an error self.assertRaises(ValueError, Walk.step, self.schema, tag_type, 'author') def test_getattr(self): # __getattr__ - walk = Walk(self.ents, (self.schema.predicate(bse.tag), )) + walk = Walk(self.ents, (self.schema.predicate(ns.bse.tag), )) # first step self.assertEqual(walk.subTagOf, Walk(self.ents, ( - self.schema.predicate(bse.tag), - self.schema.predicate(bst.subTagOf), + self.schema.predicate(ns.bse.tag), + self.schema.predicate(ns.bst.subTagOf), ))) # second step self.assertEqual(walk.subTagOf.main, Walk(self.ents, ( - self.schema.predicate(bse.tag), - self.schema.predicate(bst.subTagOf), - self.schema.predicate(bst.main), + self.schema.predicate(ns.bse.tag), + self.schema.predicate(ns.bst.subTagOf), + self.schema.predicate(ns.bst.main), ))) # invalid step raises an error self.assertRaises(ValueError, getattr, walk, 'foobar') @@ -140,7 +140,7 @@ class TestWalk(unittest.TestCase): self.assertRaises(ValueError, getattr, walk, 'author') def test_get(self): # get, __call__ - walk = Walk(self.ents, (self.schema.predicate(bse.tag), )) + walk = Walk(self.ents, (self.schema.predicate(ns.bse.tag), )) tags = { self.graph.node(ns.bsfs.Tag, URI('http://example.com/me/tag#1234')), self.graph.node(ns.bsfs.Tag, URI('http://example.com/me/tag#4321'))} diff --git a/test/namespace/test_namespace.py b/test/namespace/test_namespace.py index ec2f393..f7bf02a 100644 --- a/test/namespace/test_namespace.py +++ b/test/namespace/test_namespace.py @@ -7,7 +7,7 @@ import unittest from bsfs.utils import URI # objects to test -from bsfs.namespace.namespace import Namespace, ClosedNamespace +from bsfs.namespace.namespace import Namespace, FinalNamespace ## code ## @@ -15,108 +15,48 @@ from bsfs.namespace.namespace import Namespace, ClosedNamespace class TestNamespace(unittest.TestCase): def test_essentials(self): # string conversion - self.assertEqual(str(Namespace('http://example.org/')), 'http://example.org') - self.assertEqual(str(Namespace('http://example.org#')), 'http://example.org') - self.assertEqual(repr(Namespace('http://example.org/')), 'Namespace(http://example.org, #, /)') - self.assertEqual(repr(Namespace('http://example.org#')), 'Namespace(http://example.org, #, /)') - self.assertEqual(repr(Namespace('http://example.org', fsep='.')), 'Namespace(http://example.org, ., /)') - self.assertEqual(repr(Namespace('http://example.org', psep='.')), 'Namespace(http://example.org, #, .)') - # repeated separators are truncated - self.assertEqual(str(Namespace('http://example.org////')), 'http://example.org') - self.assertEqual(str(Namespace('http://example.org####')), 'http://example.org') - self.assertEqual(repr(Namespace('http://example.org///##')), 'Namespace(http://example.org, #, /)') + self.assertEqual(str(Namespace('http://example.org')), 'http://example.org') + self.assertEqual(repr(Namespace('http://example.org')), "'http://example.org'") # comparison - class Foo(Namespace): pass - self.assertEqual(Namespace('http://example.org/'), Namespace('http://example.org/')) - self.assertEqual(Namespace('http://example.org/'), Namespace('http://example.org')) - self.assertEqual(Namespace('http://example.org/'), Namespace('http://example.org#')) - self.assertNotEqual(Namespace('http://example.org'), Namespace('http://example.org', fsep='.')) - self.assertNotEqual(Namespace('http://example.org'), Namespace('http://example.org', psep='.')) - self.assertNotEqual(Namespace('http://example.org/'), Foo('http://example.org/')) - self.assertNotEqual(Foo('http://example.org/'), Namespace('http://example.org/')) - # hashing - self.assertEqual(hash(Namespace('http://example.org/')), hash(Namespace('http://example.org/'))) - self.assertEqual(hash(Namespace('http://example.org/')), hash(Namespace('http://example.org'))) - self.assertEqual(hash(Namespace('http://example.org/')), hash(Namespace('http://example.org#'))) + self.assertEqual(Namespace('http://example.org'), Namespace('http://example.org')) + self.assertEqual(hash(Namespace('http://example.org')), hash(Namespace('http://example.org'))) + # Namespace compares to string + self.assertEqual(Namespace('http://example.org'), 'http://example.org') + self.assertEqual(hash(Namespace('http://example.org')), hash('http://example.org')) + # URI must match + self.assertNotEqual(Namespace('http://example.org'), Namespace('http://example.com')) self.assertNotEqual(hash(Namespace('http://example.org')), hash(Namespace('http://example.com'))) - self.assertNotEqual(hash(Namespace('http://example.org')), hash(Namespace('http://example.org', fsep='.'))) - self.assertNotEqual(hash(Namespace('http://example.org')), hash(Namespace('http://example.org', psep='.'))) - self.assertNotEqual(hash(Namespace('http://example.org/')), hash(Foo('http://example.org/'))) - self.assertNotEqual(hash(Foo('http://example.org/')), hash(Namespace('http://example.org/'))) def test_getattr(self): - self.assertEqual(Namespace('http://example.org/').foo, 'http://example.org#foo') - self.assertEqual(Namespace('http://example.org/').bar, 'http://example.org#bar') - self.assertEqual(Namespace('http://example.org/', fsep='/').foo, 'http://example.org/foo') - self.assertEqual(Namespace('http://example.org/', fsep='/').bar, 'http://example.org/bar') - self.assertEqual(Namespace('http://example.org', fsep='/').foo, 'http://example.org/foo') - self.assertEqual(Namespace('http://example.org', fsep='/').bar, 'http://example.org/bar') - self.assertEqual(Namespace('http://example.org#', fsep='/').foo, 'http://example.org#/foo') - self.assertEqual(Namespace('http://example.org#', fsep='/').bar, 'http://example.org#/bar') - self.assertEqual(Namespace('http://example.org/me#').foo, 'http://example.org/me#foo') - self.assertEqual(Namespace('http://example.org/me#').bar, 'http://example.org/me#bar') + self.assertEqual(Namespace('http://example.org').foo, Namespace('http://example.org/foo')) + self.assertEqual(Namespace('http://example.org').bar, Namespace('http://example.org/bar')) - def test_getitem(self): - self.assertEqual(Namespace('http://example.org')['foo'], 'http://example.org#foo') - self.assertEqual(Namespace('http://example.org')['bar'], 'http://example.org#bar') - self.assertEqual(Namespace('http://example.org', fsep='/')['foo'], 'http://example.org/foo') - self.assertEqual(Namespace('http://example.org', fsep='/')['bar'], 'http://example.org/bar') - self.assertEqual(Namespace('http://example.org/me#')['foo'], 'http://example.org/me#foo') - self.assertEqual(Namespace('http://example.org/me#')['bar'], 'http://example.org/me#bar') + def test_call(self): + self.assertEqual(Namespace('http://example.org')(), FinalNamespace('http://example.org', sep='#')) + self.assertEqual(Namespace('http://example.org').foo(), FinalNamespace('http://example.org/foo', sep='#')) - def test_add(self): - self.assertEqual(Namespace('http://example.org') + 'foo', Namespace('http://example.org/foo')) - self.assertEqual(Namespace('http://example.org', psep='.') + 'foo', Namespace('http://example.org.foo', psep='.')) - self.assertEqual(Namespace('http://example.org') + 'foo' + 'bar', Namespace('http://example.org/foo/bar')) - # can add URIs - self.assertEqual(Namespace('http://example.org') + URI('foo'), Namespace('http://example.org/foo')) - # can only add strings - self.assertRaises(TypeError, operator.add, Namespace('http://example.org'), 1234) - self.assertRaises(TypeError, operator.add, Namespace('http://example.org'), Namespace('http://example.com')) - - -class TestClosedNamespace(unittest.TestCase): +class TestFinalNamespace(unittest.TestCase): def test_essentials(self): - # string conversion - self.assertEqual(str(ClosedNamespace('http://example.org/')), 'http://example.org') - self.assertEqual(str(ClosedNamespace('http://example.org#')), 'http://example.org') - self.assertEqual(repr(ClosedNamespace('http://example.org/')), 'ClosedNamespace(http://example.org, #, /)') - self.assertEqual(repr(ClosedNamespace('http://example.org#')), 'ClosedNamespace(http://example.org, #, /)') - self.assertEqual(repr(ClosedNamespace('http://example.org', fsep='.')), 'ClosedNamespace(http://example.org, ., /)') - self.assertEqual(repr(ClosedNamespace('http://example.org', psep='.')), 'ClosedNamespace(http://example.org, #, .)') + # string conversion + self.assertEqual(str(FinalNamespace('http://example.org')), 'http://example.org') + self.assertEqual(repr(FinalNamespace('http://example.org')), "'http://example.org'") # comparison - class Foo(ClosedNamespace): pass - self.assertEqual(ClosedNamespace('http://example.org'), ClosedNamespace('http://example.org#')) - self.assertEqual(ClosedNamespace('http://example.org'), ClosedNamespace('http://example.org')) - self.assertEqual(ClosedNamespace('http://example.org'), ClosedNamespace('http://example.org/')) - self.assertEqual(ClosedNamespace('http://example.org/', 'foo', 'bar'), ClosedNamespace('http://example.org/', 'foo', 'bar')) - self.assertNotEqual(ClosedNamespace('http://example.org/', 'foo'), ClosedNamespace('http://example.org/', 'bar')) - self.assertNotEqual(ClosedNamespace('http://example.org/'), Foo('http://example.org/')) - self.assertNotEqual(Foo('http://example.org/'), ClosedNamespace('http://example.org/')) - # hashing - self.assertEqual(hash(ClosedNamespace('http://example.org')), hash(ClosedNamespace('http://example.org'))) - self.assertEqual(hash(ClosedNamespace('http://example.org')), hash(ClosedNamespace('http://example.org/'))) - self.assertEqual(hash(ClosedNamespace('http://example.org')), hash(ClosedNamespace('http://example.org#'))) - self.assertEqual(hash(ClosedNamespace('http://example.org/', 'foo', 'bar')), hash(ClosedNamespace('http://example.org/', 'foo', 'bar'))) - self.assertNotEqual(hash(ClosedNamespace('http://example.org/', 'foo')), hash(ClosedNamespace('http://example.org/', 'bar'))) - self.assertNotEqual(hash(ClosedNamespace('http://example.org/')), hash(Foo('http://example.org/'))) - self.assertNotEqual(hash(Foo('http://example.org/')), hash(ClosedNamespace('http://example.org/'))) + self.assertEqual(FinalNamespace('http://example.org'), FinalNamespace('http://example.org')) + self.assertEqual(hash(FinalNamespace('http://example.org')), hash(FinalNamespace('http://example.org'))) + # FinalNamespace compares to string + self.assertEqual(FinalNamespace('http://example.org'), 'http://example.org') + self.assertEqual(hash(FinalNamespace('http://example.org')), hash('http://example.org')) + # URI must match + self.assertNotEqual(FinalNamespace('http://example.org'), FinalNamespace('http://example.com')) + self.assertNotEqual(hash(FinalNamespace('http://example.org')), hash(FinalNamespace('http://example.com'))) + # separator is ignored + self.assertEqual(FinalNamespace('http://example.org'), FinalNamespace('http://example.org', sep='/')) + self.assertEqual(hash(FinalNamespace('http://example.org')), hash(FinalNamespace('http://example.org', sep='/'))) def test_getattr(self): - self.assertEqual(ClosedNamespace('http://example.org/', 'foo', 'bar').foo, 'http://example.org#foo') - self.assertEqual(ClosedNamespace('http://example.org/', 'bar', 'bar').bar, 'http://example.org#bar') - self.assertEqual(ClosedNamespace('http://example.org/me#', 'foo', 'bar').foo, 'http://example.org/me#foo') - self.assertEqual(ClosedNamespace('http://example.org/me#', 'foo', 'bar').bar, 'http://example.org/me#bar') - self.assertRaises(KeyError, getattr, ClosedNamespace('http://example.org/', 'bar', 'bar'), 'foobar') - self.assertRaises(KeyError, getattr, ClosedNamespace('http://example.org#', 'bar', 'bar'), 'foobar') - - def test_getitem(self): - self.assertEqual(ClosedNamespace('http://example.org/', 'foo', 'bar')['foo'], 'http://example.org#foo') - self.assertEqual(ClosedNamespace('http://example.org/', 'foo', 'bar')['bar'], 'http://example.org#bar') - self.assertEqual(ClosedNamespace('http://example.org/me#', 'foo', 'bar')['foo'], 'http://example.org/me#foo') - self.assertEqual(ClosedNamespace('http://example.org/me#', 'foo', 'bar')['bar'], 'http://example.org/me#bar') - self.assertRaises(KeyError, ClosedNamespace('http://example.org/', 'bar', 'bar').__getitem__, 'foobar') - self.assertRaises(KeyError, ClosedNamespace('http://example.org#', 'bar', 'bar').__getitem__, 'foobar') + self.assertEqual(FinalNamespace('http://example.org').foo, FinalNamespace('http://example.org#foo')) + self.assertEqual(FinalNamespace('http://example.org').bar, FinalNamespace('http://example.org#bar')) + self.assertEqual(FinalNamespace('http://example.org', sep='/').bar, FinalNamespace('http://example.org/bar')) ## main ## diff --git a/test/query/test_validator.py b/test/query/test_validator.py index bbfd2e6..418463e 100644 --- a/test/query/test_validator.py +++ b/test/query/test_validator.py @@ -14,26 +14,29 @@ from bsfs.query.validator import Filter, Fetch ## code ## +ns.bse = ns.bsfs.Entity() + class TestFilter(unittest.TestCase): def setUp(self): self.schema = _schema.from_string(''' prefix rdfs: prefix xsd: - prefix bsfs: - prefix bse: + prefix bsfs: + prefix bse: + prefix bsl: bsfs:Entity rdfs:subClassOf bsfs:Node . bsfs:URI rdfs:subClassOf bsfs:Literal . bsfs:Tag rdfs:subClassOf bsfs:Node . xsd:string rdfs:subClassOf bsfs:Literal . - bsfs:Number rdfs:subClassOf bsfs:Literal . - bsfs:Array rdfs:subClassOf bsfs:Literal . - bsfs:Feature rdfs:subClassOf bsfs:Array . - xsd:integer rdfs:subClassOf bsfs:Number . + bsl:Number rdfs:subClassOf bsfs:Literal . + bsl:Array rdfs:subClassOf bsfs:Literal . + rdfs:subClassOf bsl:Array . + xsd:integer rdfs:subClassOf bsl:Number . - bsfs:Colors rdfs:subClassOf bsfs:Feature ; + bsfs:Colors rdfs:subClassOf ; bsfs:dimension "5"^^xsd:integer ; bsfs:dtype bsfs:f32 . @@ -267,10 +270,10 @@ class TestFilter(unittest.TestCase): self.assertRaises(errors.ConsistencyError, self.validate._distance, self.schema.node(ns.bsfs.Node), ast.filter.Distance([1,2,3], 1, False)) # type must be a feature - self.assertRaises(errors.ConsistencyError, self.validate._distance, self.schema.literal(ns.bsfs.Array), + self.assertRaises(errors.ConsistencyError, self.validate._distance, self.schema.literal(ns.bsl.Array), ast.filter.Distance([1,2,3], 1, False)) # type must be in the schema - self.assertRaises(errors.ConsistencyError, self.validate._distance, self.schema.literal(ns.bsfs.Feature).child(ns.bsfs.Invalid), + self.assertRaises(errors.ConsistencyError, self.validate._distance, self.schema.literal(ns.bsl.Array.Feature).child(ns.bsfs.Invalid), ast.filter.Distance([1,2,3], 1, False)) # FIXME: reference must be a numpy array # reference must have the correct dimension @@ -287,8 +290,8 @@ class TestFetch(unittest.TestCase): prefix rdfs: prefix xsd: - prefix bsfs: - prefix bse: + prefix bsfs: + prefix bse: bsfs:Entity rdfs:subClassOf bsfs:Node . bsfs:Tag rdfs:subClassOf bsfs:Node . diff --git a/test/schema/test_schema.py b/test/schema/test_schema.py index f9ddb68..f52cf95 100644 --- a/test/schema/test_schema.py +++ b/test/schema/test_schema.py @@ -14,6 +14,8 @@ from bsfs.schema.schema import Schema ## code ## +ns.bse = ns.bsfs.Entity() + class TestSchema(unittest.TestCase): def setUp(self): @@ -21,8 +23,9 @@ class TestSchema(unittest.TestCase): prefix rdfs: prefix xsd: - prefix bsfs: - prefix bse: + prefix bsfs: + prefix bse: + prefix bsl: bsfs:Entity rdfs:subClassOf bsfs:Node . bsfs:Tag rdfs:subClassOf bsfs:Node . @@ -31,7 +34,7 @@ class TestSchema(unittest.TestCase): xsd:string rdfs:subClassOf bsfs:Literal . bsfs:Number rdfs:subClassOf bsfs:Literal . - xsd:integer rdfs:subClassOf bsfs:Number . + xsd:integer rdfs:subClassOf bsl:Number . xsd:boolean rdfs:subClassOf bsfs:Literal . bse:tag rdfs:subClassOf bsfs:Predicate ; @@ -174,13 +177,13 @@ class TestSchema(unittest.TestCase): self.assertEqual(str(Schema(self.predicates, self.nodes, self.literals)), 'Schema()') # repr conversion with only default nodes, literals, and predicates n = [ns.bsfs.Node] - l = [ns.bsfs.Array, ns.bsfs.BinaryBlob, ns.bsfs.Feature, ns.bsfs.Literal, ns.bsfs.Number, ns.bsfs.Time] + l = [ns.bsfs.Literal, ns.bsl.Array, ns.bsl.Array.Feature, ns.bsl.BinaryBlob, ns.bsl.Number, ns.bsl.Time] p = [ns.bsfs.Predicate] self.assertEqual(repr(Schema()), f'Schema({n}, {l}, {p})') self.assertEqual(repr(Schema([], [], [])), f'Schema({n}, {l}, {p})') # repr conversion n = [ns.bsfs.Entity, ns.bsfs.Image, ns.bsfs.Node, ns.bsfs.Tag, ns.bsfs.Unused] - l = [ns.bsfs.Array, ns.bsfs.BinaryBlob, ns.bsfs.Feature, ns.bsfs.Literal, ns.bsfs.Number, ns.bsfs.Time, ns.xsd.boolean, ns.xsd.integer, ns.xsd.string] + l = [ns.xsd.boolean, ns.xsd.integer, ns.xsd.string, ns.bsfs.Literal, ns.bsl.Array, ns.bsl.Array.Feature, ns.bsl.BinaryBlob, ns.bsl.Number, ns.bsl.Time] p = [ns.bse.comment, ns.bse.group, ns.bse.tag, ns.bsfs.Predicate] self.assertEqual(repr(Schema(self.predicates, self.nodes, self.literals)), f'Schema({n}, {l}, {p})') diff --git a/test/schema/test_serialize.py b/test/schema/test_serialize.py index 84512e9..7d5d3ae 100644 --- a/test/schema/test_serialize.py +++ b/test/schema/test_serialize.py @@ -14,6 +14,8 @@ from bsfs.schema.serialize import from_string, to_string ## code ## +ns.bse = ns.bsfs.Entity() + class TestFromString(unittest.TestCase): def test_empty(self): @@ -25,7 +27,7 @@ class TestFromString(unittest.TestCase): # must not have circular dependencies self.assertRaises(errors.ConsistencyError, from_string, ''' prefix rdfs: - prefix bsfs: + prefix bsfs: bsfs:Entity rdfs:subClassOf bsfs:Node . # ah, a nice circular dependency bsfs:Entity rdfs:subClassOf bsfs:Document . @@ -39,8 +41,8 @@ class TestFromString(unittest.TestCase): self.assertRaises(errors.ConsistencyError, from_string, ''' prefix rdfs: prefix xsd: - prefix bsfs: - prefix bse: + prefix bsfs: + prefix bse: xsd:string rdfs:subClassOf bsfs:Literal . @@ -54,7 +56,7 @@ class TestFromString(unittest.TestCase): self.assertRaises(errors.ConsistencyError, from_string, ''' prefix rdfs: prefix xsd: - prefix bsfs: + prefix bsfs: bsfs:Entity rdfs:subClassOf bsfs:Node . bsfs:Document rdfs:subClassOf bsfs:Node . @@ -66,8 +68,8 @@ class TestFromString(unittest.TestCase): self.assertEqual(Schema({}, {n_unused}), from_string(''' prefix rdfs: prefix xsd: - prefix bsfs: - prefix bse: + prefix bsfs: + prefix bse: bsfs:unused rdfs:subClassOf bsfs:Node . # unused symbol ''')) @@ -80,8 +82,8 @@ class TestFromString(unittest.TestCase): self.assertEqual(Schema({}, {n_ent, n_tag, n_doc, n_image}), from_string(''' prefix rdfs: prefix xsd: - prefix bsfs: - prefix bse: + prefix bsfs: + prefix bse: # nodes inherit from same parent bsfs:Entity rdfs:subClassOf bsfs:Node . @@ -100,8 +102,8 @@ class TestFromString(unittest.TestCase): self.assertEqual(Schema({p_filename}), from_string(''' prefix rdfs: prefix xsd: - prefix bsfs: - prefix bse: + prefix bsfs: + prefix bse: bsfs:Entity rdfs:subClassOf bsfs:Node . xsd:string rdfs:subClassOf bsfs:Literal . @@ -116,7 +118,7 @@ class TestFromString(unittest.TestCase): self.assertDictEqual(from_string(''' prefix rdfs: prefix xsd: - prefix bsfs: + prefix bsfs: bsfs:Entity rdfs:subClassOf bsfs:Node . @@ -124,7 +126,7 @@ class TestFromString(unittest.TestCase): self.assertDictEqual(from_string(''' prefix rdfs: prefix xsd: - prefix bsfs: + prefix bsfs: bsfs:Entity rdfs:subClassOf bsfs:Node ; rdfs:label "hello world"^^xsd:string ; @@ -141,8 +143,8 @@ class TestFromString(unittest.TestCase): self.assertRaises(errors.ConsistencyError, from_string, ''' prefix rdfs: prefix xsd: - prefix bsfs: - prefix bse: + prefix bsfs: + prefix bse: bsfs:Entity rdfs:subClassOf bsfs:Node . @@ -156,7 +158,7 @@ class TestFromString(unittest.TestCase): self.assertRaises(errors.ConsistencyError, from_string, ''' prefix rdfs: prefix xsd: - prefix bsfs: + prefix bsfs: xsd:string rdfs:subClassOf bsfs:Literal . xsd:name rdfs:subClassOf bsfs:Literal . @@ -168,8 +170,8 @@ class TestFromString(unittest.TestCase): self.assertEqual(Schema({}, {}, {l_unused}), from_string(''' prefix rdfs: prefix xsd: - prefix bsfs: - prefix bse: + prefix bsfs: + prefix bse: xsd:unused rdfs:subClassOf bsfs:Literal . # unused symbol ''')) @@ -182,13 +184,14 @@ class TestFromString(unittest.TestCase): self.assertEqual(Schema({}, {}, {l_string, l_integer, l_unsigned, l_signed}), from_string(''' prefix rdfs: prefix xsd: - prefix bsfs: - prefix bse: + prefix bsfs: + prefix bse: + prefix bsl: # literals inherit from same parent xsd:string rdfs:subClassOf bsfs:Literal . - bsfs:Number rdfs:subClassOf bsfs:Literal . - xsd:integer rdfs:subClassOf bsfs:Number . + bsl:Number rdfs:subClassOf bsfs:Literal . + xsd:integer rdfs:subClassOf bsl:Number . # literals inherit from same parent xsd:unsigned rdfs:subClassOf xsd:integer . @@ -203,8 +206,8 @@ class TestFromString(unittest.TestCase): self.assertEqual(Schema({p_filename}), from_string(''' prefix rdfs: prefix xsd: - prefix bsfs: - prefix bse: + prefix bsfs: + prefix bse: bsfs:Entity rdfs:subClassOf bsfs:Node . xsd:string rdfs:subClassOf bsfs:Literal . @@ -219,7 +222,7 @@ class TestFromString(unittest.TestCase): self.assertDictEqual(from_string(''' prefix rdfs: prefix xsd: - prefix bsfs: + prefix bsfs: xsd:string rdfs:subClassOf bsfs:Literal . @@ -227,7 +230,7 @@ class TestFromString(unittest.TestCase): self.assertDictEqual(from_string(''' prefix rdfs: prefix xsd: - prefix bsfs: + prefix bsfs: xsd:string rdfs:subClassOf bsfs:Literal ; rdfs:label "hello world"^^xsd:string ; @@ -244,8 +247,8 @@ class TestFromString(unittest.TestCase): self.assertRaises(errors.ConsistencyError, from_string, ''' prefix rdfs: prefix xsd: - prefix bsfs: - prefix bse: + prefix bsfs: + prefix bse: xsd:string rdfs:subClassOf bsfs:Literal . @@ -258,8 +261,8 @@ class TestFromString(unittest.TestCase): self.assertRaises(errors.ConsistencyError, from_string, ''' prefix rdfs: prefix xsd: - prefix bsfs: - prefix bse: + prefix bsfs: + prefix bse: bsfs:Entity rdfs:subClassOf bsfs:Literal . xsd:string rdfs:subClassOf bsfs:Literal . @@ -274,8 +277,8 @@ class TestFromString(unittest.TestCase): self.assertRaises(errors.ConsistencyError, from_string, ''' prefix rdfs: prefix xsd: - prefix bsfs: - prefix bse: + prefix bsfs: + prefix bse: bsfs:Entity rdfs:subClassOf bsfs:Node . @@ -288,8 +291,8 @@ class TestFromString(unittest.TestCase): self.assertRaises(errors.ConsistencyError, from_string, ''' prefix rdfs: prefix xsd: - prefix bsfs: - prefix bse: + prefix bsfs: + prefix bse: bsfs:Entity rdfs:subClassOf bsfs:Node . @@ -302,8 +305,8 @@ class TestFromString(unittest.TestCase): self.assertRaises(errors.ConsistencyError, from_string, ''' prefix rdfs: prefix xsd: - prefix bsfs: - prefix bse: + prefix bsfs: + prefix bse: bsfs:Entity rdfs:subClassOf bsfs:Node . @@ -320,8 +323,8 @@ class TestFromString(unittest.TestCase): self.assertEqual(Schema({p_comment}), from_string(''' prefix rdfs: prefix xsd: - prefix bsfs: - prefix bse: + prefix bsfs: + prefix bse: bsfs:Entity rdfs:subClassOf bsfs:Node . xsd:string rdfs:subClassOf bsfs:Literal . @@ -340,8 +343,8 @@ class TestFromString(unittest.TestCase): self.assertEqual(Schema({p_comment}), from_string(''' prefix rdfs: prefix xsd: - prefix bsfs: - prefix bse: + prefix bsfs: + prefix bse: bsfs:Entity rdfs:subClassOf bsfs:Node . xsd:string rdfs:subClassOf bsfs:Literal . @@ -362,8 +365,8 @@ class TestFromString(unittest.TestCase): self.assertEqual(Schema({p_comment}), from_string(''' prefix rdfs: prefix xsd: - prefix bsfs: - prefix bse: + prefix bsfs: + prefix bse: bsfs:Entity rdfs:subClassOf bsfs:Node . xsd:string rdfs:subClassOf bsfs:Literal . @@ -383,8 +386,8 @@ class TestFromString(unittest.TestCase): self.assertEqual(Schema({p_foo}), from_string(''' prefix rdfs: prefix xsd: - prefix bsfs: - prefix bse: + prefix bsfs: + prefix bse: bsfs:Entity rdfs:subClassOf bsfs:Node . @@ -405,8 +408,8 @@ class TestFromString(unittest.TestCase): self.assertEqual(Schema({p_foobar}), from_string(''' prefix rdfs: prefix xsd: - prefix bsfs: - prefix bse: + prefix bsfs: + prefix bse: bsfs:Entity rdfs:subClassOf bsfs:Node . bsfs:Image rdfs:subClassOf bsfs:Entity . @@ -421,8 +424,8 @@ class TestFromString(unittest.TestCase): self.assertRaises(errors.ConsistencyError, from_string, ''' prefix rdfs: prefix xsd: - prefix bsfs: - prefix bse: + prefix bsfs: + prefix bse: bsfs:Entity rdfs:subClassOf bsfs:Node . bsfs:Image rdfs:subClassOf bsfs:Entity . @@ -444,8 +447,8 @@ class TestFromString(unittest.TestCase): self.assertEqual(Schema({p_foobar}), from_string(''' prefix rdfs: prefix xsd: - prefix bsfs: - prefix bse: + prefix bsfs: + prefix bse: bsfs:Entity rdfs:subClassOf bsfs:Node . bsfs:Image rdfs:subClassOf bsfs:Entity . @@ -460,8 +463,8 @@ class TestFromString(unittest.TestCase): self.assertRaises(errors.ConsistencyError, from_string, ''' prefix rdfs: prefix xsd: - prefix bsfs: - prefix bse: + prefix bsfs: + prefix bse: bsfs:Entity rdfs:subClassOf bsfs:Node . bsfs:Image rdfs:subClassOf bsfs:Entity . @@ -478,8 +481,8 @@ class TestFromString(unittest.TestCase): self.assertRaises(errors.ConsistencyError, from_string, ''' prefix rdfs: prefix xsd: - prefix bsfs: - prefix bse: + prefix bsfs: + prefix bse: bsfs:Entity rdfs:subClassOf bsfs:Node . bsfs:Annotation rdfs:subClassOf bsfs:Predicate . @@ -499,8 +502,8 @@ class TestFromString(unittest.TestCase): self.assertRaises(errors.ConsistencyError, from_string, ''' prefix rdfs: prefix xsd: - prefix bsfs: - prefix bse: + prefix bsfs: + prefix bse: bsfs:Entity rdfs:subClassOf bsfs:Node . @@ -516,8 +519,8 @@ class TestFromString(unittest.TestCase): self.assertRaises(errors.ConsistencyError, from_string, ''' prefix rdfs: prefix xsd: - prefix bsfs: - prefix bse: + prefix bsfs: + prefix bse: bsfs:Entity rdfs:subClassOf bsfs:Node . @@ -533,8 +536,8 @@ class TestFromString(unittest.TestCase): self.assertRaises(errors.ConsistencyError, from_string, ''' prefix rdfs: prefix xsd: - prefix bsfs: - prefix bse: + prefix bsfs: + prefix bse: bsfs:Entity rdfs:subClassOf bsfs:Node . @@ -551,8 +554,8 @@ class TestFromString(unittest.TestCase): self.assertDictEqual(from_string(''' prefix rdfs: prefix xsd: - prefix bsfs: - prefix bse: + prefix bsfs: + prefix bse: bse:comment rdfs:subClassOf bsfs:Predicate ; rdfs:range bsfs:Node . @@ -561,8 +564,8 @@ class TestFromString(unittest.TestCase): self.assertDictEqual(from_string(''' prefix rdfs: prefix xsd: - prefix bsfs: - prefix bse: + prefix bsfs: + prefix bse: bse:comment rdfs:subClassOf bsfs:Predicate ; rdfs:range bsfs:Node ; @@ -581,70 +584,78 @@ class TestFromString(unittest.TestCase): self.assertEqual(Schema(literals={f_colors}), from_string(''' prefix rdfs: prefix xsd: - prefix bsfs: - prefix bse: + prefix bsfs: + prefix bse: + prefix bsl: + prefix bsa: - bsfs:Array rdfs:subClassOf bsfs:Literal . - bsfs:Feature rdfs:subClassOf bsfs:Array. + bsl:Array rdfs:subClassOf bsfs:Literal . + bsa:Feature rdfs:subClassOf bsl:Array. - bsfs:Colors rdfs:subClassOf bsfs:Feature . + bsfs:Colors rdfs:subClassOf bsa:Feature . ''')) # features inherit properties from parents - f_colors = types.ROOT_FEATURE.child(ns.bsfs.Colors, dimension=1234, dtype=ns.bsfs.i32) - f_main_colors = f_colors.child(ns.bsfs.MainColor, distance=ns.bsfs.cosine, dtype=ns.bsfs.f16) + f_colors = types.ROOT_FEATURE.child(ns.bsfs.Colors, dimension=1234, dtype=ns.bsfs.dtype().i32) + f_main_colors = f_colors.child(ns.bsfs.MainColor, distance=ns.bsfs.cosine, dtype=ns.bsfs.dtype().f16) self.assertEqual(Schema(literals={f_colors, f_main_colors}), from_string(''' prefix rdfs: prefix xsd: - prefix bsfs: - prefix bse: + prefix bsfs: + prefix bse: + prefix bsl: + prefix bsa: - bsfs:Array rdfs:subClassOf bsfs:Literal . - bsfs:Feature rdfs:subClassOf bsfs:Array. + bsl:Array rdfs:subClassOf bsfs:Literal . + bsa:Feature rdfs:subClassOf bsl:Array. - bsfs:Colors rdfs:subClassOf bsfs:Feature ; # inherits distance from bsfs:Feature - bsfs:dimension "1234"^^xsd:integer ; # overwrites bsfs:Feature - bsfs:dtype bsfs:i32 . # overwrites bsfs:Feature + bsfs:Colors rdfs:subClassOf bsa:Feature ; # inherits distance from bsa:Feature + bsfs:dimension "1234"^^xsd:integer ; # overwrites bsa:Feature + bsfs:dtype . # overwrites bsa:Feature bsfs:MainColor rdfs:subClassOf bsfs:Colors ; # inherits dimension from bsfs:Colors - bsfs:distance bsfs:cosine ; # overwrites bsfs:Feature - bsfs:dtype bsfs:f16 . # overwrites bsfs:Colors + bsfs:distance bsfs:cosine ; # overwrites bsa:Feature + bsfs:dtype . # overwrites bsfs:Colors ''')) # feature definition can be split across multiple statements. # statements can be repeated - f_colors = types.ROOT_FEATURE.child(ns.bsfs.Colors, dimension=1234, dtype=ns.bsfs.f32) + f_colors = types.ROOT_FEATURE.child(ns.bsfs.Colors, dimension=1234, dtype=ns.bsfs.dtype().f32) self.assertEqual(Schema(literals={f_colors}), from_string(''' prefix rdfs: prefix xsd: - prefix bsfs: - prefix bse: + prefix bsfs: + prefix bse: + prefix bsl: + prefix bsa: - bsfs:Array rdfs:subClassOf bsfs:Literal . - bsfs:Feature rdfs:subClassOf bsfs:Array. + bsl:Array rdfs:subClassOf bsfs:Literal . + bsa:Feature rdfs:subClassOf bsl:Array. - bsfs:Colors rdfs:subClassOf bsfs:Feature ; + bsfs:Colors rdfs:subClassOf bsa:Feature ; bsfs:dimension "1234"^^xsd:integer . - bsfs:Colors rdfs:subClassOf bsfs:Feature ; + bsfs:Colors rdfs:subClassOf bsa:Feature ; bsfs:dimension "1234"^^xsd:integer ; # non-conflicting repetition - bsfs:dtype bsfs:f32 . + bsfs:dtype . ''')) # cannot define the same feature from multiple parents self.assertRaises(errors.ConsistencyError, from_string, ''' prefix rdfs: prefix xsd: - prefix bsfs: - prefix bse: + prefix bsfs: + prefix bse: + prefix bsl: + prefix bsa: - bsfs:Array rdfs:subClassOf bsfs:Literal . - bsfs:Feature rdfs:subClassOf bsfs:Array. - bsfs:ColorSpace rdfs:subClassOf bsfs:Feature . + bsl:Array rdfs:subClassOf bsfs:Literal . + bsa:Feature rdfs:subClassOf bsl:Array. + bsfs:ColorSpace rdfs:subClassOf bsa:Feature . - bsfs:Colors rdfs:subClassOf bsfs:Feature . + bsfs:Colors rdfs:subClassOf bsa:Feature . bsfs:Colors rdfs:subClassOf bsfs:ColorSpace . ''') @@ -652,16 +663,18 @@ class TestFromString(unittest.TestCase): self.assertRaises(errors.ConsistencyError, from_string, ''' prefix rdfs: prefix xsd: - prefix bsfs: - prefix bse: + prefix bsfs: + prefix bse: + prefix bsl: + prefix bsa: - bsfs:Array rdfs:subClassOf bsfs:Literal . - bsfs:Feature rdfs:subClassOf bsfs:Array. + bsl:Array rdfs:subClassOf bsfs:Literal . + bsa:Feature rdfs:subClassOf bsl:Array. - bsfs:Colors rdfs:subClassOf bsfs:Feature ; + bsfs:Colors rdfs:subClassOf bsa:Feature ; bsfs:dimension "1234"^^xsd:integer . - bsfs:Colors rdfs:subClassOf bsfs:Feature ; + bsfs:Colors rdfs:subClassOf bsa:Feature ; bsfs:dimension "4321"^^xsd:integer . # conflicting dimension ''') @@ -669,32 +682,36 @@ class TestFromString(unittest.TestCase): self.assertRaises(errors.ConsistencyError, from_string, ''' prefix rdfs: prefix xsd: - prefix bsfs: - prefix bse: + prefix bsfs: + prefix bse: + prefix bsl: + prefix bsa: - bsfs:Array rdfs:subClassOf bsfs:Literal . - bsfs:Feature rdfs:subClassOf bsfs:Array. + bsl:Array rdfs:subClassOf bsfs:Literal . + bsa:Feature rdfs:subClassOf bsl:Array. - bsfs:Colors rdfs:subClassOf bsfs:Feature ; - bsfs:dtype bsfs:f32 . + bsfs:Colors rdfs:subClassOf bsa:Feature ; + bsfs:dtype . - bsfs:Colors rdfs:subClassOf bsfs:Feature ; - bsfs:dtype bsfs:f16 . # conflicting dtype + bsfs:Colors rdfs:subClassOf bsa:Feature ; + bsfs:dtype . # conflicting dtype ''') # cannot assign multiple conflicting distance metrics to the same feature self.assertRaises(errors.ConsistencyError, from_string, ''' prefix rdfs: prefix xsd: - prefix bsfs: - prefix bse: + prefix bsfs: + prefix bse: + prefix bsl: + prefix bsa: - bsfs:Array rdfs:subClassOf bsfs:Literal . - bsfs:Feature rdfs:subClassOf bsfs:Array. + bsl:Array rdfs:subClassOf bsfs:Literal . + bsa:Feature rdfs:subClassOf bsl:Array. - bsfs:Colors rdfs:subClassOf bsfs:Feature ; + bsfs:Colors rdfs:subClassOf bsa:Feature ; bsfs:distance bsfs:euclidean . - bsfs:Colors rdfs:subClassOf bsfs:Feature ; + bsfs:Colors rdfs:subClassOf bsa:Feature ; bsfs:distance bsfs:cosine . # conflicting distance ''') @@ -702,26 +719,30 @@ class TestFromString(unittest.TestCase): self.assertDictEqual(from_string(''' prefix rdfs: prefix xsd: - prefix bsfs: - prefix bse: + prefix bsfs: + prefix bse: + prefix bsl: + prefix bsa: - bsfs:Array rdfs:subClassOf bsfs:Literal . - bsfs:Feature rdfs:subClassOf bsfs:Array. + bsl:Array rdfs:subClassOf bsfs:Literal . + bsa:Feature rdfs:subClassOf bsl:Array. - bsfs:Colors rdfs:subClassOf bsfs:Feature ; + bsfs:Colors rdfs:subClassOf bsa:Feature ; bsfs:dimension "1234"^^xsd:integer . ''').literal(ns.bsfs.Colors).annotations, {}) self.assertDictEqual(from_string(''' prefix rdfs: prefix xsd: - prefix bsfs: - prefix bse: + prefix bsfs: + prefix bse: + prefix bsl: + prefix bsa: - bsfs:Array rdfs:subClassOf bsfs:Literal . - bsfs:Feature rdfs:subClassOf bsfs:Array. + bsl:Array rdfs:subClassOf bsfs:Literal . + bsa:Feature rdfs:subClassOf bsl:Array. - bsfs:Colors rdfs:subClassOf bsfs:Feature ; + bsfs:Colors rdfs:subClassOf bsa:Feature ; bsfs:dimension "1234"^^xsd:integer ; rdfs:label "hello world"^^xsd:string ; bsfs:foo "1234"^^xsd:integer . @@ -748,10 +769,10 @@ class TestFromString(unittest.TestCase): p_group = p_tag.child(ns.bse.group, domain=n_image, unique=True) p_comment = p_annotation.child(ns.bse.comment, range=l_string) # features - f_colors = types.ROOT_FEATURE.child(URI('http://bsfs.ai/schema/Feature/colors_spatial'), - dtype=ns.bsfs.f16, distance=ns.bsfs.euclidean) - f_colors1234 = f_colors.child(URI('http://bsfs.ai/schema/Feature/colors_spatial#1234'), dimension=1024) - f_colors4321 = f_colors.child(URI('http://bsfs.ai/schema/Feature/colors_spatial#4321'), dimension=2048) + f_colors = types.ROOT_FEATURE.child(URI('https://schema.bsfs.io/core/Feature/colors_spatial'), + dtype=ns.bsfs.dtype().f16, distance=ns.bsfs.euclidean) + f_colors1234 = f_colors.child(URI('https://schema.bsfs.io/core/Feature/colors_spatial#1234'), dimension=1024) + f_colors4321 = f_colors.child(URI('https://schema.bsfs.io/core/Feature/colors_spatial#4321'), dimension=2048) # schema ref = Schema( {p_annotation, p_tag, p_group, p_comment}, @@ -764,8 +785,10 @@ class TestFromString(unittest.TestCase): prefix xsd: # bsfs prefixes - prefix bsfs: - prefix bse: + prefix bsfs: + prefix bse: + prefix bsl: + prefix bsa: # nodes bsfs:Entity rdfs:subClassOf bsfs:Node ; @@ -777,10 +800,10 @@ class TestFromString(unittest.TestCase): # literals xsd:string rdfs:subClassOf bsfs:Literal ; rdfs:label "A sequence of characters"^^xsd:string . - bsfs:Array rdfs:subClassOf bsfs:Literal . - bsfs:Feature rdfs:subClassOf bsfs:Array. - bsfs:Number rdfs:subClassOf bsfs:Literal . - xsd:integer rdfs:subClassOf bsfs:Number . + bsl:Array rdfs:subClassOf bsfs:Literal . + bsa:Feature rdfs:subClassOf bsl:Array. + bsl:Number rdfs:subClassOf bsfs:Literal . + xsd:integer rdfs:subClassOf bsl:Number . xsd:boolean rdfs:subClassOf bsfs:Literal . @@ -789,19 +812,19 @@ class TestFromString(unittest.TestCase): rdfs:label "node annotation"^^xsd:string . # feature instances - rdfs:subClassOf bsfs:Feature ; - bsfs:dtype bsfs:f16 ; + rdfs:subClassOf bsa:Feature ; + bsfs:dtype ; bsfs:distance bsfs:euclidean ; # annotations rdfs:label "ColorsSpatial instances. Dimension depends on instance."^^xsd:string ; bsfs:first_arg "1234"^^xsd:integer ; bsfs:second_arg "hello world"^^xsd:string . - rdfs:subClassOf ; + rdfs:subClassOf ; bsfs:dimension "1024"^^xsd:integer ; rdfs:label "Main colors spatial instance"^^xsd:string . - rdfs:subClassOf ; + rdfs:subClassOf ; bsfs:dimension "2048"^^xsd:integer . # predicate instances @@ -829,19 +852,19 @@ class TestFromString(unittest.TestCase): self.assertDictEqual(gen.node(ns.bsfs.Tag).annotations, {ns.rdfs.label: 'Tag'}) self.assertDictEqual(gen.literal(ns.xsd.string).annotations, {ns.rdfs.label: 'A sequence of characters'}) self.assertDictEqual(gen.predicate(ns.bsfs.Annotation).annotations, {ns.rdfs.label: 'node annotation'}) - self.assertDictEqual(gen.literal(URI('http://bsfs.ai/schema/Feature/colors_spatial')).annotations, { + self.assertDictEqual(gen.literal(URI('https://schema.bsfs.io/core/Feature/colors_spatial')).annotations, { ns.rdfs.label: 'ColorsSpatial instances. Dimension depends on instance.', ns.bsfs.first_arg: 1234, ns.bsfs.second_arg: 'hello world', }) - self.assertDictEqual(gen.literal(URI('http://bsfs.ai/schema/Feature/colors_spatial#1234')).annotations, { + self.assertDictEqual(gen.literal(URI('https://schema.bsfs.io/core/Feature/colors_spatial#1234')).annotations, { ns.rdfs.label: 'Main colors spatial instance'}) self.assertDictEqual(gen.predicate(ns.bse.tag).annotations, {ns.rdfs.label: 'connect entity to a tag'}) # blank nodes result in an error self.assertRaises(errors.BackendError, from_string, ''' prefix rdfs: - prefix bsfs: + prefix bsfs: bsfs:Entity rdfs:subClassOf bsfs:Node ; bsfs:foo _:bar . ''') @@ -976,29 +999,29 @@ class TestToString(unittest.TestCase): def test_feature(self): # root features - f_colors = types.ROOT_FEATURE.child(URI('http://bsfs.ai/schema/Feature/colors'), + f_colors = types.ROOT_FEATURE.child(URI('https://schema.bsfs.io/core/Feature/colors'), distance=ns.bsfs.cosine) # derived features - f_colors1234 = f_colors.child(URI('http://bsfs.ai/schema/Feature/colors#1234'), + f_colors1234 = f_colors.child(URI('https://schema.bsfs.io/core/Feature/colors#1234'), dimension=1024) # inherits dtype, distance - f_colors4321 = f_colors.child(URI('http://bsfs.ai/schema/Feature/colors#4321'), + f_colors4321 = f_colors.child(URI('https://schema.bsfs.io/core/Feature/colors#4321'), dimension=2048, distance=ns.bsfs.euclidean) # inherits dtype # create schema schema = Schema(literals={f_colors, f_colors1234, f_colors4321}) schema_str = to_string(schema) # all symbols are serialized - self.assertIn('bsfs:Array', schema_str) - self.assertIn('[^\.]*bsfs:dimension[^\.]', schema_str)) - self.assertIsNone(re.search(r'[^\.]*bsfs:dtype[^\.]', schema_str)) - self.assertIsNone(re.search(r'[^\.]*bsfs:distance[^\.]', schema_str)) - self.assertIsNotNone(re.search(r'[^\.]*bsfs:dimension[^\.]', schema_str)) - self.assertIsNotNone(re.search(r'[^\.]*bsfs:distance[^\.]', schema_str)) - self.assertIsNone(re.search(r'[^\.]*bsfs:dtype[^\.]', schema_str)) + self.assertIsNotNone(re.search(r'.*[^\.]*bsfs:dimension[^\.]', schema_str)) + self.assertIsNone(re.search(r'.*[^\.]*bsfs:dtype[^\.]', schema_str)) + self.assertIsNone(re.search(r'.*[^\.]*bsfs:distance[^\.]', schema_str)) + self.assertIsNotNone(re.search(r'.*[^\.]*bsfs:dimension[^\.]', schema_str)) + self.assertIsNotNone(re.search(r'.*[^\.]*bsfs:distance[^\.]', schema_str)) + self.assertIsNone(re.search(r'.*[^\.]*bsfs:dtype[^\.]', schema_str)) # unserialize yields the original schema self.assertEqual(schema, from_string(schema_str)) @@ -1009,12 +1032,12 @@ class TestToString(unittest.TestCase): ns.bsfs.foo: 1234, ns.bsfs.bar: False, } - f_colors = types.ROOT_FEATURE.child(URI('http://bsfs.ai/schema/Feature/colors'), - dtype=ns.bsfs.f16, distance=ns.bsfs.euclidean, + f_colors = types.ROOT_FEATURE.child(URI('https://schema.bsfs.io/core/Feature/colors'), + dtype=ns.bsfs.dtype().f16, distance=ns.bsfs.euclidean, **annotations) self.assertDictEqual( annotations, - from_string(to_string(Schema(literals={f_colors}))).literal(URI('http://bsfs.ai/schema/Feature/colors')).annotations) + from_string(to_string(Schema(literals={f_colors}))).literal(URI('https://schema.bsfs.io/core/Feature/colors')).annotations) ## main ## diff --git a/test/schema/test_types.py b/test/schema/test_types.py index f87d857..9bfa8c5 100644 --- a/test/schema/test_types.py +++ b/test/schema/test_types.py @@ -14,6 +14,8 @@ from bsfs.schema.types import _Type, Vertex, Node, Literal, Predicate, Feature ## code ## +ns.bse = ns.bsfs.Entity() + class TestType(unittest.TestCase): def test_parents(self): diff --git a/test/triple_store/sparql/test_parse_fetch.py b/test/triple_store/sparql/test_parse_fetch.py index 9284608..1d793e7 100644 --- a/test/triple_store/sparql/test_parse_fetch.py +++ b/test/triple_store/sparql/test_parse_fetch.py @@ -15,10 +15,9 @@ from bsfs.triple_store.sparql.parse_fetch import Fetch ## code ## -bsfs = Namespace('http://bsfs.ai/schema', fsep='/') -bse = Namespace('http://bsfs.ai/schema/Entity') -bst = Namespace('http://bsfs.ai/schema/Tag') -bsc = Namespace('http://bsfs.ai/schema/Collection') +ns.bse = ns.bsfs.Entity() +ns.bst = ns.bsfs.Tag() +ns.bsc = ns.bsfs.Collection() class TestParseFetch(unittest.TestCase): @@ -27,10 +26,10 @@ class TestParseFetch(unittest.TestCase): prefix rdfs: prefix xsd: - prefix bsfs: - prefix bse: - prefix bst: - prefix bsc: + prefix bsfs: + prefix bse: + prefix bst: + prefix bsc: # nodes bsfs:Entity rdfs:subClassOf bsfs:Node . @@ -83,43 +82,43 @@ class TestParseFetch(unittest.TestCase): # graph to test queries self.graph = rdflib.Graph() # schema hierarchies - self.graph.add((rdflib.URIRef('http://bsfs.ai/schema/Entity'), rdflib.RDFS.subClassOf, rdflib.URIRef('http://bsfs.ai/schema/Node'))) - self.graph.add((rdflib.URIRef('http://bsfs.ai/schema/Collection'), rdflib.RDFS.subClassOf, rdflib.URIRef('http://bsfs.ai/schema/Node'))) - self.graph.add((rdflib.URIRef('http://bsfs.ai/schema/Tag'), rdflib.RDFS.subClassOf, rdflib.URIRef('http://bsfs.ai/schema/Node'))) + self.graph.add((rdflib.URIRef('https://schema.bsfs.io/core/Entity'), rdflib.RDFS.subClassOf, rdflib.URIRef('https://schema.bsfs.io/core/Node'))) + self.graph.add((rdflib.URIRef('https://schema.bsfs.io/core/Collection'), rdflib.RDFS.subClassOf, rdflib.URIRef('https://schema.bsfs.io/core/Node'))) + self.graph.add((rdflib.URIRef('https://schema.bsfs.io/core/Tag'), rdflib.RDFS.subClassOf, rdflib.URIRef('https://schema.bsfs.io/core/Node'))) # entities - self.graph.add((rdflib.URIRef('http://example.com/entity#1234'), rdflib.RDF.type, rdflib.URIRef('http://bsfs.ai/schema/Entity'))) - self.graph.add((rdflib.URIRef('http://example.com/entity#4321'), rdflib.RDF.type, rdflib.URIRef('http://bsfs.ai/schema/Entity'))) + self.graph.add((rdflib.URIRef('http://example.com/entity#1234'), rdflib.RDF.type, rdflib.URIRef('https://schema.bsfs.io/core/Entity'))) + self.graph.add((rdflib.URIRef('http://example.com/entity#4321'), rdflib.RDF.type, rdflib.URIRef('https://schema.bsfs.io/core/Entity'))) # tags - self.graph.add((rdflib.URIRef('http://example.com/tag#1234'), rdflib.RDF.type, rdflib.URIRef('http://bsfs.ai/schema/Tag'))) - self.graph.add((rdflib.URIRef('http://example.com/tag#4321'), rdflib.RDF.type, rdflib.URIRef('http://bsfs.ai/schema/Tag'))) + self.graph.add((rdflib.URIRef('http://example.com/tag#1234'), rdflib.RDF.type, rdflib.URIRef('https://schema.bsfs.io/core/Tag'))) + self.graph.add((rdflib.URIRef('http://example.com/tag#4321'), rdflib.RDF.type, rdflib.URIRef('https://schema.bsfs.io/core/Tag'))) # collections - self.graph.add((rdflib.URIRef('http://example.com/collection#1234'), rdflib.RDF.type, rdflib.URIRef('http://bsfs.ai/schema/Collection'))) - self.graph.add((rdflib.URIRef('http://example.com/collection#4321'), rdflib.RDF.type, rdflib.URIRef('http://bsfs.ai/schema/Collection'))) + self.graph.add((rdflib.URIRef('http://example.com/collection#1234'), rdflib.RDF.type, rdflib.URIRef('https://schema.bsfs.io/core/Collection'))) + self.graph.add((rdflib.URIRef('http://example.com/collection#4321'), rdflib.RDF.type, rdflib.URIRef('https://schema.bsfs.io/core/Collection'))) # entity literals - self.graph.add((rdflib.URIRef('http://example.com/entity#1234'), rdflib.URIRef(bse.rank), rdflib.Literal('1234', datatype=rdflib.XSD.integer))) - self.graph.add((rdflib.URIRef('http://example.com/entity#1234'), rdflib.URIRef(bse.filename), rdflib.Literal('filename_1234', datatype=rdflib.XSD.string))) - #self.graph.add((rdflib.URIRef('http://example.com/entity#4321'), rdflib.URIRef(bse.rank), rdflib.Literal('4321', datatype=rdflib.XSD.integer))) - self.graph.add((rdflib.URIRef('http://example.com/entity#4321'), rdflib.URIRef(bse.filename), rdflib.Literal('filename_4321', datatype=rdflib.XSD.string))) + self.graph.add((rdflib.URIRef('http://example.com/entity#1234'), rdflib.URIRef(ns.bse.rank), rdflib.Literal('1234', datatype=rdflib.XSD.integer))) + self.graph.add((rdflib.URIRef('http://example.com/entity#1234'), rdflib.URIRef(ns.bse.filename), rdflib.Literal('filename_1234', datatype=rdflib.XSD.string))) + #self.graph.add((rdflib.URIRef('http://example.com/entity#4321'), rdflib.URIRef(ns.bse.rank), rdflib.Literal('4321', datatype=rdflib.XSD.integer))) + self.graph.add((rdflib.URIRef('http://example.com/entity#4321'), rdflib.URIRef(ns.bse.filename), rdflib.Literal('filename_4321', datatype=rdflib.XSD.string))) # tag literals - self.graph.add((rdflib.URIRef('http://example.com/tag#1234'), rdflib.URIRef(bst.label), rdflib.Literal('tag_label_1234', datatype=rdflib.XSD.string))) - self.graph.add((rdflib.URIRef('http://example.com/tag#4321'), rdflib.URIRef(bst.label), rdflib.Literal('tag_label_4321', datatype=rdflib.XSD.string))) + self.graph.add((rdflib.URIRef('http://example.com/tag#1234'), rdflib.URIRef(ns.bst.label), rdflib.Literal('tag_label_1234', datatype=rdflib.XSD.string))) + self.graph.add((rdflib.URIRef('http://example.com/tag#4321'), rdflib.URIRef(ns.bst.label), rdflib.Literal('tag_label_4321', datatype=rdflib.XSD.string))) # collection literals - self.graph.add((rdflib.URIRef('http://example.com/collection#1234'), rdflib.URIRef(bsc.label), rdflib.Literal('collection_label_1234', datatype=rdflib.XSD.string))) - self.graph.add((rdflib.URIRef('http://example.com/collection#1234'), rdflib.URIRef(bsc.rating), rdflib.Literal('1234', datatype=rdflib.XSD.integer))) - self.graph.add((rdflib.URIRef('http://example.com/collection#4321'), rdflib.URIRef(bsc.label), rdflib.Literal('collection_label_4321', datatype=rdflib.XSD.string))) - self.graph.add((rdflib.URIRef('http://example.com/collection#4321'), rdflib.URIRef(bsc.rating), rdflib.Literal('4321', datatype=rdflib.XSD.integer))) + self.graph.add((rdflib.URIRef('http://example.com/collection#1234'), rdflib.URIRef(ns.bsc.label), rdflib.Literal('collection_label_1234', datatype=rdflib.XSD.string))) + self.graph.add((rdflib.URIRef('http://example.com/collection#1234'), rdflib.URIRef(ns.bsc.rating), rdflib.Literal('1234', datatype=rdflib.XSD.integer))) + self.graph.add((rdflib.URIRef('http://example.com/collection#4321'), rdflib.URIRef(ns.bsc.label), rdflib.Literal('collection_label_4321', datatype=rdflib.XSD.string))) + self.graph.add((rdflib.URIRef('http://example.com/collection#4321'), rdflib.URIRef(ns.bsc.rating), rdflib.Literal('4321', datatype=rdflib.XSD.integer))) # entity-tag links - self.graph.add((rdflib.URIRef('http://example.com/entity#1234'), rdflib.URIRef(bse.tag), rdflib.URIRef('http://example.com/tag#1234'))) - self.graph.add((rdflib.URIRef('http://example.com/entity#4321'), rdflib.URIRef(bse.tag), rdflib.URIRef('http://example.com/tag#4321'))) + self.graph.add((rdflib.URIRef('http://example.com/entity#1234'), rdflib.URIRef(ns.bse.tag), rdflib.URIRef('http://example.com/tag#1234'))) + self.graph.add((rdflib.URIRef('http://example.com/entity#4321'), rdflib.URIRef(ns.bse.tag), rdflib.URIRef('http://example.com/tag#4321'))) # entity-collection links - self.graph.add((rdflib.URIRef('http://example.com/entity#1234'), rdflib.URIRef(bse.collection), rdflib.URIRef('http://example.com/collection#1234'))) - self.graph.add((rdflib.URIRef('http://example.com/entity#4321'), rdflib.URIRef(bse.collection), rdflib.URIRef('http://example.com/collection#4321'))) + self.graph.add((rdflib.URIRef('http://example.com/entity#1234'), rdflib.URIRef(ns.bse.collection), rdflib.URIRef('http://example.com/collection#1234'))) + self.graph.add((rdflib.URIRef('http://example.com/entity#4321'), rdflib.URIRef(ns.bse.collection), rdflib.URIRef('http://example.com/collection#4321'))) # collection-tag links - self.graph.add((rdflib.URIRef('http://example.com/collection#1234'), rdflib.URIRef(bsc.tag), rdflib.URIRef('http://example.com/tag#1234'))) - self.graph.add((rdflib.URIRef('http://example.com/collection#4321'), rdflib.URIRef(bsc.tag), rdflib.URIRef('http://example.com/tag#4321'))) + self.graph.add((rdflib.URIRef('http://example.com/collection#1234'), rdflib.URIRef(ns.bsc.tag), rdflib.URIRef('http://example.com/tag#1234'))) + self.graph.add((rdflib.URIRef('http://example.com/collection#4321'), rdflib.URIRef(ns.bsc.tag), rdflib.URIRef('http://example.com/tag#4321'))) # tag-entity links # NOTE: cross-over - self.graph.add((rdflib.URIRef('http://example.com/tag#1234'), rdflib.URIRef(bst.main), rdflib.URIRef('http://example.com/entity#4321'))) - self.graph.add((rdflib.URIRef('http://example.com/tag#4321'), rdflib.URIRef(bst.main), rdflib.URIRef('http://example.com/entity#1234'))) + self.graph.add((rdflib.URIRef('http://example.com/tag#1234'), rdflib.URIRef(ns.bst.main), rdflib.URIRef('http://example.com/entity#4321'))) + self.graph.add((rdflib.URIRef('http://example.com/tag#4321'), rdflib.URIRef(ns.bst.main), rdflib.URIRef('http://example.com/entity#1234'))) # default parser self.parser = Fetch(self.schema) @@ -135,7 +134,7 @@ class TestParseFetch(unittest.TestCase): # __call__ requires a parseable root self.assertRaises(errors.BackendError, self.parser, self.ent, ast.filter.FilterExpression()) # __call__ returns an executable query - q = self.parser(self.ent, ast.fetch.Fetch(bse.tag, ast.fetch.Value(bst.label, 'label'))) + q = self.parser(self.ent, ast.fetch.Fetch(ns.bse.tag, ast.fetch.Value(ns.bst.label, 'label'))) self.assertSetEqual(set(q(self.graph)), { (rdflib.URIRef('http://example.com/entity#1234'), rdflib.Literal('tag_label_1234', datatype=rdflib.XSD.string)), (rdflib.URIRef('http://example.com/entity#4321'), rdflib.Literal('tag_label_4321', datatype=rdflib.XSD.string)), @@ -149,8 +148,8 @@ class TestParseFetch(unittest.TestCase): def test_all(self): # multiple values query q = self.parser(self.ent, ast.fetch.All( - ast.fetch.Value(bse.filename, name='filename'), - ast.fetch.Value(bse.rank, name='rank')), + ast.fetch.Value(ns.bse.filename, name='filename'), + ast.fetch.Value(ns.bse.rank, name='rank')), ) self.assertSetEqual(set(q.names), {'filename', 'rank'}) if q.names == ('filename', 'rank'): @@ -165,8 +164,8 @@ class TestParseFetch(unittest.TestCase): }) # mixed values and node query q = self.parser(self.ent, ast.fetch.All( - ast.fetch.Value(bse.filename, name='filename'), - ast.fetch.Node(bse.tag, name='tag'), + ast.fetch.Value(ns.bse.filename, name='filename'), + ast.fetch.Node(ns.bse.tag, name='tag'), )) self.assertSetEqual(set(q.names), {'filename', 'tag'}) if q.names == ('filename', 'tag'): @@ -180,9 +179,9 @@ class TestParseFetch(unittest.TestCase): (rdflib.URIRef('http://example.com/entity#4321'), rdflib.URIRef('http://example.com/tag#4321'), rdflib.Literal('filename_4321', datatype=rdflib.XSD.string)), }) # multiple values and second hop - q = self.parser(self.ent, ast.fetch.Fetch(bse.tag, ast.fetch.All( + q = self.parser(self.ent, ast.fetch.Fetch(ns.bse.tag, ast.fetch.All( ast.fetch.This(name='tag'), - ast.fetch.Value(bst.label, name='label'), + ast.fetch.Value(ns.bst.label, name='label'), ))) self.assertSetEqual(set(q.names), {'tag', 'label'}) if q.names == ('tag', 'label'): @@ -200,13 +199,13 @@ class TestParseFetch(unittest.TestCase): def test_fetch(self): # two-hop query - q = self.parser(self.ent, ast.fetch.Fetch(bse.tag, ast.fetch.Value(bst.label, 'tag_label'))) + q = self.parser(self.ent, ast.fetch.Fetch(ns.bse.tag, ast.fetch.Value(ns.bst.label, 'tag_label'))) self.assertSetEqual(set(q(self.graph)), { (rdflib.URIRef('http://example.com/entity#1234'), rdflib.Literal('tag_label_1234', datatype=rdflib.XSD.string)), (rdflib.URIRef('http://example.com/entity#4321'), rdflib.Literal('tag_label_4321', datatype=rdflib.XSD.string)), }) # three-hop-query - q = self.parser(self.ent, ast.fetch.Fetch(bse.tag, ast.fetch.Fetch(bst.main, ast.fetch.Value(bse.rank, 'entity_rank')))) + q = self.parser(self.ent, ast.fetch.Fetch(ns.bse.tag, ast.fetch.Fetch(ns.bst.main, ast.fetch.Value(ns.bse.rank, 'entity_rank')))) self.assertSetEqual(set(q(self.graph)), { (rdflib.URIRef('http://example.com/entity#1234'), None), (rdflib.URIRef('http://example.com/entity#4321'), rdflib.Literal('1234', datatype=rdflib.XSD.integer)), @@ -215,9 +214,9 @@ class TestParseFetch(unittest.TestCase): def test_node(self): # cannot use the internal hop name - self.assertRaises(errors.BackendError, self.parser, self.ent, ast.fetch.Node(bse.tag, self.parser.ngen.prefix[1:] + '123')) + self.assertRaises(errors.BackendError, self.parser, self.ent, ast.fetch.Node(ns.bse.tag, self.parser.ngen.prefix[1:] + '123')) # a simple Node statement - q = self.parser(self.ent, ast.fetch.Node(bse.tag, 'tag')) + q = self.parser(self.ent, ast.fetch.Node(ns.bse.tag, 'tag')) self.assertSetEqual(set(q.names), {'tag'}) self.assertSetEqual(set(q(self.graph)), { (rdflib.URIRef('http://example.com/entity#1234'), rdflib.URIRef('http://example.com/tag#1234')), @@ -227,9 +226,9 @@ class TestParseFetch(unittest.TestCase): def test_value(self): # cannot use the internal hop name - self.assertRaises(errors.BackendError, self.parser, self.schema.node(ns.bsfs.Entity), ast.fetch.Value(bse.filename, self.parser.ngen.prefix[1:] + '123')) + self.assertRaises(errors.BackendError, self.parser, self.schema.node(ns.bsfs.Entity), ast.fetch.Value(ns.bse.filename, self.parser.ngen.prefix[1:] + '123')) # a simple Value statement - q = self.parser(self.ent, ast.fetch.Value(bse.filename, 'filename')) + q = self.parser(self.ent, ast.fetch.Value(ns.bse.filename, 'filename')) self.assertSetEqual(set(q.names), {'filename'}) self.assertSetEqual(set(q(self.graph)), { (rdflib.URIRef('http://example.com/entity#1234'), rdflib.Literal('filename_1234', datatype=rdflib.XSD.string)), diff --git a/test/triple_store/sparql/test_parse_filter.py b/test/triple_store/sparql/test_parse_filter.py index 5b6ca8a..a45f2ef 100644 --- a/test/triple_store/sparql/test_parse_filter.py +++ b/test/triple_store/sparql/test_parse_filter.py @@ -15,6 +15,8 @@ from bsfs.triple_store.sparql.parse_filter import Filter ## code ## +ns.bse = ns.bsfs.Entity() + class TestParseFilter(unittest.TestCase): def setUp(self): # schema @@ -22,25 +24,28 @@ class TestParseFilter(unittest.TestCase): prefix rdfs: prefix xsd: - prefix bsfs: - prefix bse: + prefix bsfs: + prefix bse: + prefix bsl: + prefix bsd: + prefix bsa: - bsfs:Array rdfs:subClassOf bsfs:Literal . - bsfs:Feature rdfs:subClassOf bsfs:Array . - bsfs:Number rdfs:subClassOf bsfs:Literal . + bsl:Array rdfs:subClassOf bsfs:Literal . + bsa:Feature rdfs:subClassOf bsl:Array . + bsl:Number rdfs:subClassOf bsfs:Literal . bsfs:Entity rdfs:subClassOf bsfs:Node . bsfs:Image rdfs:subClassOf bsfs:Entity . bsfs:Tag rdfs:subClassOf bsfs:Node . xsd:string rdfs:subClassOf bsfs:Literal . - xsd:integer rdfs:subClassOf bsfs:Number . + xsd:integer rdfs:subClassOf bsl:Number . bsfs:URI rdfs:subClassOf bsfs:Literal . - bsfs:Colors rdfs:subClassOf bsfs:Feature ; + bsfs:Colors rdfs:subClassOf bsa:Feature ; bsfs:dimension "4"^^xsd:integer ; bsfs:dtype xsd:integer ; - bsfs:distance bsfs:euclidean . + bsfs:distance bsd:euclidean . bse:colors rdfs:subClassOf bsfs:Predicate ; rdfs:domain bsfs:Entity ; @@ -81,18 +86,18 @@ class TestParseFilter(unittest.TestCase): # graph to test queries self.graph = rdflib.Graph() # schema hierarchies - self.graph.add((rdflib.URIRef('http://bsfs.ai/schema/Entity'), rdflib.RDFS.subClassOf, rdflib.URIRef('http://bsfs.ai/schema/Node'))) - self.graph.add((rdflib.URIRef('http://bsfs.ai/schema/Image'), rdflib.RDFS.subClassOf, rdflib.URIRef('http://bsfs.ai/schema/Entity'))) - self.graph.add((rdflib.URIRef('http://bsfs.ai/schema/Tag'), rdflib.RDFS.subClassOf, rdflib.URIRef('http://bsfs.ai/schema/Node'))) + self.graph.add((rdflib.URIRef('https://schema.bsfs.io/core/Entity'), rdflib.RDFS.subClassOf, rdflib.URIRef('https://schema.bsfs.io/core/Node'))) + self.graph.add((rdflib.URIRef('https://schema.bsfs.io/core/Image'), rdflib.RDFS.subClassOf, rdflib.URIRef('https://schema.bsfs.io/core/Entity'))) + self.graph.add((rdflib.URIRef('https://schema.bsfs.io/core/Tag'), rdflib.RDFS.subClassOf, rdflib.URIRef('https://schema.bsfs.io/core/Node'))) # entities - self.graph.add((rdflib.URIRef('http://example.com/entity#1234'), rdflib.RDF.type, rdflib.URIRef('http://bsfs.ai/schema/Entity'))) - self.graph.add((rdflib.URIRef('http://example.com/entity#4321'), rdflib.RDF.type, rdflib.URIRef('http://bsfs.ai/schema/Entity'))) + self.graph.add((rdflib.URIRef('http://example.com/entity#1234'), rdflib.RDF.type, rdflib.URIRef('https://schema.bsfs.io/core/Entity'))) + self.graph.add((rdflib.URIRef('http://example.com/entity#4321'), rdflib.RDF.type, rdflib.URIRef('https://schema.bsfs.io/core/Entity'))) # tags - self.graph.add((rdflib.URIRef('http://example.com/tag#1234'), rdflib.RDF.type, rdflib.URIRef('http://bsfs.ai/schema/Tag'))) - self.graph.add((rdflib.URIRef('http://example.com/tag#4321'), rdflib.RDF.type, rdflib.URIRef('http://bsfs.ai/schema/Tag'))) + self.graph.add((rdflib.URIRef('http://example.com/tag#1234'), rdflib.RDF.type, rdflib.URIRef('https://schema.bsfs.io/core/Tag'))) + self.graph.add((rdflib.URIRef('http://example.com/tag#4321'), rdflib.RDF.type, rdflib.URIRef('https://schema.bsfs.io/core/Tag'))) # images - self.graph.add((rdflib.URIRef('http://example.com/image#1234'), rdflib.RDF.type, rdflib.URIRef('http://bsfs.ai/schema/Image'))) - self.graph.add((rdflib.URIRef('http://example.com/image#4321'), rdflib.RDF.type, rdflib.URIRef('http://bsfs.ai/schema/Image'))) + self.graph.add((rdflib.URIRef('http://example.com/image#1234'), rdflib.RDF.type, rdflib.URIRef('https://schema.bsfs.io/core/Image'))) + self.graph.add((rdflib.URIRef('http://example.com/image#4321'), rdflib.RDF.type, rdflib.URIRef('https://schema.bsfs.io/core/Image'))) # node comments self.graph.add((rdflib.URIRef('http://example.com/entity#1234'), rdflib.URIRef(ns.bse.comment), rdflib.Literal('Me, Myself, and I', datatype=rdflib.XSD.string))) self.graph.add((rdflib.URIRef('http://example.com/entity#1234'), rdflib.URIRef(ns.bse.comment), rdflib.Literal('hello world', datatype=rdflib.XSD.string))) diff --git a/test/triple_store/sparql/test_sparql.py b/test/triple_store/sparql/test_sparql.py index f45ca37..a7e7d37 100644 --- a/test/triple_store/sparql/test_sparql.py +++ b/test/triple_store/sparql/test_sparql.py @@ -15,22 +15,25 @@ from bsfs.triple_store.sparql.sparql import SparqlStore ## code ## +ns.bse = ns.bsfs.Entity() + class TestSparqlStore(unittest.TestCase): def setUp(self): self.schema = bsc.from_string(''' prefix rdfs: prefix xsd: - prefix bsfs: - prefix bse: + prefix bsfs: + prefix bse: + prefix bsl: bsfs:Entity rdfs:subClassOf bsfs:Node . bsfs:Tag rdfs:subClassOf bsfs:Node . bsfs:User rdfs:subClassOf bsfs:Node . xsd:string rdfs:subClassOf bsfs:Literal . - bsfs:Number rdfs:subClassOf bsfs:Literal . - bsfs:BinaryBlob rdfs:subClassOf bsfs:Literal . - xsd:integer rdfs:subClassOf bsfs:Number . + bsl:Number rdfs:subClassOf bsfs:Literal . + bsl:BinaryBlob rdfs:subClassOf bsfs:Literal . + xsd:integer rdfs:subClassOf bsl:Number . # non-unique literal bse:comment rdfs:subClassOf bsfs:Predicate ; @@ -59,7 +62,7 @@ class TestSparqlStore(unittest.TestCase): # binary range bse:asset rdfs:subClassOf bsfs:Predicate ; rdfs:domain bsfs:Entity ; - rdfs:range bsfs:BinaryBlob . + rdfs:range bsl:BinaryBlob . ''') self.schema_triples = { @@ -68,12 +71,12 @@ class TestSparqlStore(unittest.TestCase): (rdflib.URIRef(ns.bsfs.Tag), rdflib.RDFS.subClassOf, rdflib.URIRef(ns.bsfs.Node)), (rdflib.URIRef(ns.bsfs.User), rdflib.RDFS.subClassOf, rdflib.URIRef(ns.bsfs.Node)), (rdflib.URIRef(ns.xsd.string), rdflib.RDFS.subClassOf, rdflib.URIRef(ns.bsfs.Literal)), - (rdflib.URIRef(ns.bsfs.Array), rdflib.RDFS.subClassOf, rdflib.URIRef(ns.bsfs.Literal)), - (rdflib.URIRef(ns.bsfs.BinaryBlob), rdflib.RDFS.subClassOf, rdflib.URIRef(ns.bsfs.Literal)), - (rdflib.URIRef(ns.bsfs.Feature), rdflib.RDFS.subClassOf, rdflib.URIRef(ns.bsfs.Array)), - (rdflib.URIRef(ns.bsfs.Number), rdflib.RDFS.subClassOf, rdflib.URIRef(ns.bsfs.Literal)), - (rdflib.URIRef(ns.bsfs.Time), rdflib.RDFS.subClassOf, rdflib.URIRef(ns.bsfs.Literal)), - (rdflib.URIRef(ns.xsd.integer), rdflib.RDFS.subClassOf, rdflib.URIRef(ns.bsfs.Number)), + (rdflib.URIRef(ns.bsl.Array), rdflib.RDFS.subClassOf, rdflib.URIRef(ns.bsfs.Literal)), + (rdflib.URIRef(ns.bsl.BinaryBlob), rdflib.RDFS.subClassOf, rdflib.URIRef(ns.bsfs.Literal)), + (rdflib.URIRef(ns.bsl.Array.Feature), rdflib.RDFS.subClassOf, rdflib.URIRef(ns.bsl.Array)), + (rdflib.URIRef(ns.bsl.Number), rdflib.RDFS.subClassOf, rdflib.URIRef(ns.bsfs.Literal)), + (rdflib.URIRef(ns.bsl.Time), rdflib.RDFS.subClassOf, rdflib.URIRef(ns.bsfs.Literal)), + (rdflib.URIRef(ns.xsd.integer), rdflib.RDFS.subClassOf, rdflib.URIRef(ns.bsl.Number)), (rdflib.URIRef(ns.bse.comment), rdflib.RDFS.subClassOf, rdflib.URIRef(ns.bsfs.Predicate)), (rdflib.URIRef(ns.bse.filesize), rdflib.RDFS.subClassOf, rdflib.URIRef(ns.bsfs.Predicate)), (rdflib.URIRef(ns.bse.tag), rdflib.RDFS.subClassOf, rdflib.URIRef(ns.bsfs.Predicate)), @@ -101,7 +104,7 @@ class TestSparqlStore(unittest.TestCase): store.schema = bsc.from_string(''' prefix rdfs: prefix xsd: - prefix bsfs: + prefix bsfs: bsfs:Entity rdfs:subClassOf bsfs:Node . bsfs:Document rdfs:subClassOf bsfs:Entity . @@ -206,10 +209,10 @@ class TestSparqlStore(unittest.TestCase): curr = curr + bsc.from_string(''' prefix rdfs: prefix xsd: - prefix bsfs: - prefix bse: - prefix bst: - prefix bsc: + prefix bsfs: + prefix bse: + prefix bst: + prefix bsc: bsfs:Entity rdfs:subClassOf bsfs:Node . bsfs:Tag rdfs:subClassOf bsfs:Node . @@ -255,16 +258,16 @@ class TestSparqlStore(unittest.TestCase): (rdflib.URIRef(ns.xsd.boolean), rdflib.RDFS.subClassOf, rdflib.URIRef(ns.bsfs.Literal)), (rdflib.URIRef(ns.bse.shared), rdflib.RDFS.subClassOf, rdflib.URIRef(ns.bsfs.Predicate)), (rdflib.URIRef(ns.bse.partOf), rdflib.RDFS.subClassOf, rdflib.URIRef(ns.bsfs.Predicate)), - (rdflib.URIRef('http://bsfs.ai/schema/Tag#usedIn'), rdflib.RDFS.subClassOf, rdflib.URIRef(ns.bsfs.Predicate)), - (rdflib.URIRef('http://bsfs.ai/schema/Collection#tag'), rdflib.RDFS.subClassOf, rdflib.URIRef(ns.bsfs.Predicate)), - (rdflib.URIRef('http://bsfs.ai/schema/Tag#principal'), rdflib.RDFS.subClassOf, rdflib.URIRef(ns.bsfs.Predicate)), + (rdflib.URIRef('https://schema.bsfs.io/core/Tag#usedIn'), rdflib.RDFS.subClassOf, rdflib.URIRef(ns.bsfs.Predicate)), + (rdflib.URIRef('https://schema.bsfs.io/core/Collection#tag'), rdflib.RDFS.subClassOf, rdflib.URIRef(ns.bsfs.Predicate)), + (rdflib.URIRef('https://schema.bsfs.io/core/Tag#principal'), rdflib.RDFS.subClassOf, rdflib.URIRef(ns.bsfs.Predicate)), }) # add some instances of the new classes p_partOf = curr.predicate(ns.bse.partOf) p_shared = curr.predicate(ns.bse.shared) - p_usedIn = curr.predicate('http://bsfs.ai/schema/Tag#usedIn') - p_ctag = curr.predicate('http://bsfs.ai/schema/Collection#tag') - p_principal = curr.predicate('http://bsfs.ai/schema/Tag#principal') + p_usedIn = curr.predicate('https://schema.bsfs.io/core/Tag#usedIn') + p_ctag = curr.predicate('https://schema.bsfs.io/core/Collection#tag') + p_principal = curr.predicate('https://schema.bsfs.io/core/Tag#principal') store.create(curr.node(ns.bsfs.Collection), {URI('http://example.com/me/collection#1234'), URI('http://example.com/me/collection#4321')}) # add some more triples store.set(curr.node(ns.bsfs.Entity), ent_ids, p_shared, {True}) @@ -283,9 +286,9 @@ class TestSparqlStore(unittest.TestCase): (rdflib.URIRef(ns.xsd.boolean), rdflib.RDFS.subClassOf, rdflib.URIRef(ns.bsfs.Literal)), (rdflib.URIRef(ns.bse.shared), rdflib.RDFS.subClassOf, rdflib.URIRef(ns.bsfs.Predicate)), (rdflib.URIRef(ns.bse.partOf), rdflib.RDFS.subClassOf, rdflib.URIRef(ns.bsfs.Predicate)), - (rdflib.URIRef('http://bsfs.ai/schema/Tag#usedIn'), rdflib.RDFS.subClassOf, rdflib.URIRef(ns.bsfs.Predicate)), - (rdflib.URIRef('http://bsfs.ai/schema/Collection#tag'), rdflib.RDFS.subClassOf, rdflib.URIRef(ns.bsfs.Predicate)), - (rdflib.URIRef('http://bsfs.ai/schema/Tag#principal'), rdflib.RDFS.subClassOf, rdflib.URIRef(ns.bsfs.Predicate)), + (rdflib.URIRef('https://schema.bsfs.io/core/Tag#usedIn'), rdflib.RDFS.subClassOf, rdflib.URIRef(ns.bsfs.Predicate)), + (rdflib.URIRef('https://schema.bsfs.io/core/Collection#tag'), rdflib.RDFS.subClassOf, rdflib.URIRef(ns.bsfs.Predicate)), + (rdflib.URIRef('https://schema.bsfs.io/core/Tag#principal'), rdflib.RDFS.subClassOf, rdflib.URIRef(ns.bsfs.Predicate)), # collections (rdflib.URIRef('http://example.com/me/collection#1234'), rdflib.RDF.type, rdflib.URIRef(ns.bsfs.Collection)), (rdflib.URIRef('http://example.com/me/collection#4321'), rdflib.RDF.type, rdflib.URIRef(ns.bsfs.Collection)), @@ -309,17 +312,18 @@ class TestSparqlStore(unittest.TestCase): curr = bsc.from_string(''' prefix rdfs: prefix xsd: - prefix bsfs: - prefix bse: - prefix bst: + prefix bsfs: + prefix bse: + prefix bst: + prefix bsl: bsfs:Entity rdfs:subClassOf bsfs:Node . bsfs:Tag rdfs:subClassOf bsfs:Node . bsfs:User rdfs:subClassOf bsfs:Node . xsd:boolean rdfs:subClassOf bsfs:Literal . - bsfs:Number rdfs:subClassOf bsfs:Literal . - xsd:integer rdfs:subClassOf bsfs:Number . + bsl:Number rdfs:subClassOf bsfs:Literal . + xsd:integer rdfs:subClassOf bsl:Number . bse:filesize rdfs:subClassOf bsfs:Predicate ; rdfs:domain bsfs:Entity ; @@ -360,16 +364,16 @@ class TestSparqlStore(unittest.TestCase): (rdflib.URIRef(ns.bsfs.Tag), rdflib.RDFS.subClassOf, rdflib.URIRef(ns.bsfs.Node)), (rdflib.URIRef(ns.bsfs.User), rdflib.RDFS.subClassOf, rdflib.URIRef(ns.bsfs.Node)), (rdflib.URIRef(ns.xsd.boolean), rdflib.RDFS.subClassOf, rdflib.URIRef(ns.bsfs.Literal)), - (rdflib.URIRef(ns.bsfs.Array), rdflib.RDFS.subClassOf, rdflib.URIRef(ns.bsfs.Literal)), - (rdflib.URIRef(ns.bsfs.BinaryBlob), rdflib.RDFS.subClassOf, rdflib.URIRef(ns.bsfs.Literal)), - (rdflib.URIRef(ns.bsfs.Feature), rdflib.RDFS.subClassOf, rdflib.URIRef(ns.bsfs.Array)), - (rdflib.URIRef(ns.bsfs.Number), rdflib.RDFS.subClassOf, rdflib.URIRef(ns.bsfs.Literal)), - (rdflib.URIRef(ns.bsfs.Time), rdflib.RDFS.subClassOf, rdflib.URIRef(ns.bsfs.Literal)), - (rdflib.URIRef(ns.xsd.integer), rdflib.RDFS.subClassOf, rdflib.URIRef(ns.bsfs.Number)), + (rdflib.URIRef(ns.bsl.Array), rdflib.RDFS.subClassOf, rdflib.URIRef(ns.bsfs.Literal)), + (rdflib.URIRef(ns.bsl.BinaryBlob), rdflib.RDFS.subClassOf, rdflib.URIRef(ns.bsfs.Literal)), + (rdflib.URIRef(ns.bsl.Array.Feature), rdflib.RDFS.subClassOf, rdflib.URIRef(ns.bsl.Array)), + (rdflib.URIRef(ns.bsl.Number), rdflib.RDFS.subClassOf, rdflib.URIRef(ns.bsfs.Literal)), + (rdflib.URIRef(ns.bsl.Time), rdflib.RDFS.subClassOf, rdflib.URIRef(ns.bsfs.Literal)), + (rdflib.URIRef(ns.xsd.integer), rdflib.RDFS.subClassOf, rdflib.URIRef(ns.bsl.Number)), (rdflib.URIRef(ns.bse.shared), rdflib.RDFS.subClassOf, rdflib.URIRef(ns.bsfs.Predicate)), (rdflib.URIRef(ns.bse.tag), rdflib.RDFS.subClassOf, rdflib.URIRef(ns.bsfs.Predicate)), (rdflib.URIRef(ns.bse.filesize), rdflib.RDFS.subClassOf, rdflib.URIRef(ns.bsfs.Predicate)), - (rdflib.URIRef('http://bsfs.ai/schema/Tag#principal'), rdflib.RDFS.subClassOf, rdflib.URIRef(ns.bsfs.Predicate)), + (rdflib.URIRef('https://schema.bsfs.io/core/Tag#principal'), rdflib.RDFS.subClassOf, rdflib.URIRef(ns.bsfs.Predicate)), # node instances (rdflib.URIRef('http://example.com/me/entity#1234'), rdflib.RDF.type, rdflib.URIRef(ns.bsfs.Entity)), (rdflib.URIRef('http://example.com/me/entity#4321'), rdflib.RDF.type, rdflib.URIRef(ns.bsfs.Entity)), @@ -400,13 +404,15 @@ class TestSparqlStore(unittest.TestCase): invalid = bsc.from_string(''' prefix rdfs: prefix xsd: - prefix bsfs: - prefix bse: + prefix bsfs: + prefix bse: + prefix bsl: + prefix bsa: - bsfs:Array rdfs:subClassOf bsfs:Literal . - bsfs:Feature rdfs:subClassOf bsfs:Array . + bsl:Array rdfs:subClassOf bsfs:Literal . + bsa:Feature rdfs:subClassOf bsl:Array . - bsfs:Colors rdfs:subClassOf bsfs:Feature ; + bsfs:Colors rdfs:subClassOf bsa:Feature ; bsfs:dimension "4"^^xsd:integer ; bsfs:distance bsfs:foobar . @@ -417,8 +423,8 @@ class TestSparqlStore(unittest.TestCase): invalid = bsc.from_string(''' prefix rdfs: prefix xsd: - prefix bsfs: - prefix bse: + prefix bsfs: + prefix bse: bsfs:Entity rdfs:subClassOf bsfs:Node . bsfs:Tag rdfs:subClassOf bsfs:Entity . # inconsistent with previous tag definition @@ -433,8 +439,8 @@ class TestSparqlStore(unittest.TestCase): invalid = bsc.from_string(''' prefix rdfs: prefix xsd: - prefix bsfs: - prefix bse: + prefix bsfs: + prefix bse: bsfs:Entity rdfs:subClassOf bsfs:Node . bsfs:User rdfs:subClassOf bsfs:Node . @@ -945,9 +951,9 @@ class TestSparqlStore(unittest.TestCase): p_asset = store.schema.predicate(ns.bse.asset) store.set(ent_type, ent_ids, p_asset, {bytes(range(128)), bytes(range(128, 256))}) blob1 = rdflib.Literal('AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6e3x9fn8=', - datatype=rdflib.URIRef(ns.bsfs.BinaryBlob)) + datatype=rdflib.URIRef(ns.bsl.BinaryBlob)) blob2 = rdflib.Literal('gIGCg4SFhoeIiYqLjI2Oj5CRkpOUlZaXmJmam5ydnp+goaKjpKWmp6ipqqusra6vsLGys7S1tre4ubq7vL2+v8DBwsPExcbHyMnKy8zNzs/Q0dLT1NXW19jZ2tvc3d7f4OHi4+Tl5ufo6err7O3u7/Dx8vP09fb3+Pn6+/z9/v8=', - datatype=rdflib.URIRef(ns.bsfs.BinaryBlob)) + datatype=rdflib.URIRef(ns.bsl.BinaryBlob)) self.assertTrue(set(store._graph).issuperset({ (rdflib.URIRef('http://example.com/me/entity#1234'), rdflib.URIRef(p_asset.uri), blob1), (rdflib.URIRef('http://example.com/me/entity#1234'), rdflib.URIRef(p_asset.uri), blob2), diff --git a/test/triple_store/sparql/test_utils.py b/test/triple_store/sparql/test_utils.py index 8f894bb..44a1299 100644 --- a/test/triple_store/sparql/test_utils.py +++ b/test/triple_store/sparql/test_utils.py @@ -16,6 +16,8 @@ from bsfs.triple_store.sparql.utils import GenHopName, Query ## code ## +ns.bse = ns.bsfs.Entity() + class TestGenHopName(unittest.TestCase): def test_next(self): # baseline @@ -40,7 +42,7 @@ class TestGenHopName(unittest.TestCase): class TestQuery(unittest.TestCase): def setUp(self): - self.root_type = 'http://bsfs.ai/schema/Entity' + self.root_type = 'https://schema.bsfs.io/core/Entity' self.root_head = '?root' self.select = (('?head', 'name'), ) self.where = f'?root <{ns.bse.tag}> ?head' @@ -56,8 +58,8 @@ class TestQuery(unittest.TestCase): self.assertEqual(q, Query(self.root_type, self.root_head, self.select, self.where)) self.assertEqual(hash(q), hash(Query(self.root_type, self.root_head, self.select, self.where))) # comparison respects root_type - self.assertNotEqual(q, Query('http://bsfs.ai/schema/Tag', self.root_head, self.select, self.where)) - self.assertNotEqual(hash(q), hash(Query('http://bsfs.ai/schema/Tag', self.root_head, self.select, self.where))) + self.assertNotEqual(q, Query('https://schema.bsfs.io/core/Tag', self.root_head, self.select, self.where)) + self.assertNotEqual(hash(q), hash(Query('https://schema.bsfs.io/core/Tag', self.root_head, self.select, self.where))) # comparison respects root_head self.assertNotEqual(q, Query(self.root_type, '?foo', self.select, self.where)) self.assertNotEqual(hash(q), hash(Query(self.root_type, '?foo', self.select, self.where))) @@ -69,7 +71,7 @@ class TestQuery(unittest.TestCase): self.assertNotEqual(hash(q), hash(Query(self.root_type, self.root_head, self.select, '?root bse:filename ?head'))) # string conversion self.assertEqual(str(q), q.query) - self.assertEqual(repr(q), "Query(http://bsfs.ai/schema/Entity, ?root, (('?head', 'name'),), ?root ?head)") + self.assertEqual(repr(q), "Query(https://schema.bsfs.io/core/Entity, ?root, (('?head', 'name'),), ?root ?head)") def test_add(self): q = Query(self.root_type, self.root_head, self.select, self.where) @@ -77,7 +79,7 @@ class TestQuery(unittest.TestCase): self.assertRaises(TypeError, operator.add, q, 1234) self.assertRaises(TypeError, operator.add, q, 'foobar') # root type and head must match - self.assertRaises(ValueError, operator.add, q, Query('http://bsfs.ai/schema/Tag', self.root_head)) + self.assertRaises(ValueError, operator.add, q, Query('https://schema.bsfs.io/core/Node/Tag', self.root_head)) self.assertRaises(ValueError, operator.add, q, Query(self.root_type, '?foobar')) # select and were are combined combo = q + Query(self.root_type, self.root_head, (('?foo', 'bar'), ), f'?root <{ns.bse.filename}> ?foo') @@ -113,23 +115,23 @@ class TestQuery(unittest.TestCase): return value # query composes a valid query q = Query(self.root_type, self.root_head, self.select, self.where) - self.assertEqual(normalize(q.query), normalize(f'select distinct ?root (?head as ?name) where {{ ?root <{ns.rdf.type}>/<{ns.rdfs.subClassOf}>* . ?root <{ns.bse.tag}> ?head }} order by str(?root)')) + self.assertEqual(normalize(q.query), normalize(f'select distinct ?root (?head as ?name) where {{ ?root <{ns.rdf.type}>/<{ns.rdfs.subClassOf}>* . ?root <{ns.bse.tag}> ?head }} order by str(?root)')) # select and where are optional q = Query(self.root_type, self.root_head) - self.assertEqual(normalize(q.query), normalize(f'select distinct ?root where {{ ?root <{ns.rdf.type}>/<{ns.rdfs.subClassOf}>* . }} order by str(?root)')) + self.assertEqual(normalize(q.query), normalize(f'select distinct ?root where {{ ?root <{ns.rdf.type}>/<{ns.rdfs.subClassOf}>* . }} order by str(?root)')) # select and where need not to correspond q = Query(self.root_type, self.root_head, (('?head', 'name'), )) - self.assertEqual(normalize(q.query), normalize(f'select distinct ?root (?head as ?name) where {{ ?root <{ns.rdf.type}>/<{ns.rdfs.subClassOf}>* . }} order by str(?root)')) + self.assertEqual(normalize(q.query), normalize(f'select distinct ?root (?head as ?name) where {{ ?root <{ns.rdf.type}>/<{ns.rdfs.subClassOf}>* . }} order by str(?root)')) # query is used for string representation self.assertEqual(str(q), q.query) def test_call(self): graph = rdflib.Graph() # schema - graph.add((rdflib.URIRef('http://bsfs.ai/schema/Document'), rdflib.URIRef(ns.rdfs.subClassOf), rdflib.URIRef('http://bsfs.ai/schema/Entity'))) + graph.add((rdflib.URIRef('https://schema.bsfs.io/core/Document'), rdflib.URIRef(ns.rdfs.subClassOf), rdflib.URIRef('https://schema.bsfs.io/core/Entity'))) # nodes - graph.add((rdflib.URIRef('http://example.com/entity#1234'), rdflib.URIRef(ns.rdf.type), rdflib.URIRef('http://bsfs.ai/schema/Entity'))) - graph.add((rdflib.URIRef('http://example.com/doc#1234'), rdflib.URIRef(ns.rdf.type), rdflib.URIRef('http://bsfs.ai/schema/Document'))) + graph.add((rdflib.URIRef('http://example.com/entity#1234'), rdflib.URIRef(ns.rdf.type), rdflib.URIRef('https://schema.bsfs.io/core/Entity'))) + graph.add((rdflib.URIRef('http://example.com/doc#1234'), rdflib.URIRef(ns.rdf.type), rdflib.URIRef('https://schema.bsfs.io/core/Document'))) # links graph.add((rdflib.URIRef('http://example.com/entity#1234'), rdflib.URIRef(ns.bse.tag), rdflib.Literal('tag#1234', datatype=rdflib.XSD.string))) graph.add((rdflib.URIRef('http://example.com/doc#1234'), rdflib.URIRef(ns.bse.tag), rdflib.Literal('tag#1234', datatype=rdflib.XSD.string))) -- cgit v1.2.3