aboutsummaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
authorMatthias Baumgartner <dev@igsor.net>2023-03-02 12:19:58 +0100
committerMatthias Baumgartner <dev@igsor.net>2023-03-02 12:19:58 +0100
commit87f437380c1dd8f420437cddc028c0f3174ee1c9 (patch)
treeacebd2de636477cf6b498256a67f6863abd5a9c1 /test
parentd70e78bbdd9d9b5727f18a82fce08f20bdbbba19 (diff)
downloadbsfs-87f437380c1dd8f420437cddc028c0f3174ee1c9.tar.gz
bsfs-87f437380c1dd8f420437cddc028c0f3174ee1c9.tar.bz2
bsfs-87f437380c1dd8f420437cddc028c0f3174ee1c9.zip
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
Diffstat (limited to 'test')
-rw-r--r--test/graph/ac/test_null.py3
-rw-r--r--test/graph/test_graph.py95
-rw-r--r--test/graph/test_resolve.py3
3 files changed, 90 insertions, 11 deletions
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: <http://www.w3.org/2000/01/rdf-schema#>
+ prefix xsd: <http://www.w3.org/2001/XMLSchema#>
+ prefix bsfs: <http://bsfs.ai/schema/>
+ prefix bse: <http://bsfs.ai/schema/Entity#>
+
+ 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)),