aboutsummaryrefslogtreecommitdiffstats
path: root/test/graph/test_walk.py
diff options
context:
space:
mode:
Diffstat (limited to 'test/graph/test_walk.py')
-rw-r--r--test/graph/test_walk.py170
1 files changed, 170 insertions, 0 deletions
diff --git a/test/graph/test_walk.py b/test/graph/test_walk.py
new file mode 100644
index 0000000..4b844da
--- /dev/null
+++ b/test/graph/test_walk.py
@@ -0,0 +1,170 @@
+
+# imports
+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
+
+# symbol to test
+from bsfs.graph.walk import Walk
+
+## code ##
+
+ns.bse = ns.bsfs.Entity()
+ns.bst = ns.bsfs.Tag()
+
+class TestWalk(unittest.TestCase):
+ def setUp(self):
+ # backend setup
+ self.schema = bsc.from_string('''
+ prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
+ prefix xsd: <http://www.w3.org/2001/XMLSchema#>
+ prefix bsfs: <https://schema.bsfs.io/core/>
+ prefix bse: <https://schema.bsfs.io/core/Entity#>
+ prefix bst: <https://schema.bsfs.io/core/Tag#>
+
+ bsfs:Entity rdfs:subClassOf bsfs:Node .
+ bsfs:Tag rdfs:subClassOf bsfs:Node .
+ bsfs:User rdfs:subClassOf bsfs:Node .
+ xsd:string rdfs:subClassOf bsfs:Literal .
+
+ bse:author rdfs:subClassOf bsfs:Predicate ;
+ rdfs:domain bsfs:Node ;
+ rdfs:range bsfs:User .
+
+ bse:tag rdfs:subClassOf bsfs:Predicate ;
+ rdfs:domain bsfs:Entity ;
+ rdfs:range bsfs:Tag .
+
+ bst:label rdfs:subClassOf bsfs:Predicate ;
+ rdfs:domain bsfs:Tag ;
+ rdfs:range xsd:string ;
+ bsfs:unique "true"^^xsd:boolean .
+
+ bst:subTagOf rdfs:subClassOf bsfs:Predicate ;
+ rdfs:domain bsfs:Tag ;
+ rdfs:range bsfs:Tag .
+
+ bst:main rdfs:subClassOf bsfs:Predicate ;
+ rdfs:domain bsfs:Tag ;
+ rdfs:range bsfs:Entity .
+
+ bst:author rdfs:subClassOf bsfs:Predicate ;
+ rdfs:domain bsfs:Tag ;
+ rdfs:range xsd:string .
+
+ ''')
+ self.backend = SparqlStore.Open()
+ self.user = URI('http://example.com/me')
+ self.ac = NullAC(self.backend, self.user)
+ self.graph = Graph(self.backend, self.ac)
+ self.graph.migrate(self.schema)
+
+ # nodes setup
+ self.ents = self.graph.nodes(ns.bsfs.Entity, {
+ URI('http://example.com/me/entity#1234'),
+ URI('http://example.com/me/entity#4321')})
+ self.tags = self.graph.nodes(ns.bsfs.Tag, {
+ URI('http://example.com/me/tag#1234'),
+ URI('http://example.com/me/tag#4321')})
+ # add some instances
+ 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(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])))
+ # comparison respects type
+ class Foo(Walk): pass
+ self.assertNotEqual(Walk(self.ents, [p_tag]), Foo(self.ents, [p_tag]))
+ self.assertNotEqual(hash(Walk(self.ents, [p_tag])), hash(Foo(self.ents, [p_tag])))
+ # comparison respects root
+ self.assertNotEqual(Walk(self.ents, [p_author]), Walk(self.tags, [p_author]))
+ self.assertNotEqual(hash(Walk(self.ents, [p_author])), hash(Walk(self.tags, [p_author])))
+ # comparison respects path
+ self.assertNotEqual(Walk(self.tags, [p_author]), Walk(self.tags, [p_main]))
+ 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(@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(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(ns.bse.tag),
+ )).tail,
+ self.schema.node(ns.bsfs.Tag))
+ self.assertEqual(Walk(self.ents, (
+ self.schema.predicate(ns.bse.tag),
+ self.schema.predicate(ns.bst.main),
+ )).tail,
+ self.schema.node(ns.bsfs.Entity))
+
+ def test_step(self):
+ tag_type = self.schema.node(ns.bsfs.Tag)
+ # step returns a predicate
+ self.assertEqual(Walk.step(self.schema, tag_type, '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(ns.bse.tag), ))
+ # first step
+ self.assertEqual(walk.subTagOf, Walk(self.ents, (
+ 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(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')
+ # ambiguous step raises an error
+ self.assertRaises(ValueError, getattr, walk, 'author')
+
+ def test_get(self): # get, __call__
+ 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'))}
+ # get returns from Nodes.get
+ self.assertDictEqual(walk.get(), {
+ self.graph.node(ns.bsfs.Entity, URI('http://example.com/me/entity#1234')): tags,
+ self.graph.node(ns.bsfs.Entity, URI('http://example.com/me/entity#4321')): tags,
+ })
+ self.assertDictEqual(walk(), {
+ self.graph.node(ns.bsfs.Entity, URI('http://example.com/me/entity#1234')): tags,
+ self.graph.node(ns.bsfs.Entity, URI('http://example.com/me/entity#4321')): tags,
+ })
+ # get passes kwargs to Nodes.get
+ self.assertSetEqual(tags, walk.get(node=False))
+ self.assertSetEqual(tags, walk(node=False))
+ self.assertSetEqual(tags, set(walk.get(view=list, node=False)))
+ self.assertSetEqual(tags, set(walk(view=list, node=False)))
+ # get returns values if need be
+ self.assertSetEqual(walk.label(node=False), {'hello', 'world'})
+
+
+## main ##
+
+if __name__ == '__main__':
+ unittest.main()
+
+## EOF ##