diff options
author | Matthias Baumgartner <dev@igsor.net> | 2023-01-21 23:03:50 +0100 |
---|---|---|
committer | Matthias Baumgartner <dev@igsor.net> | 2023-01-21 23:03:50 +0100 |
commit | 1392951dfc82af05e7a5999baa1c0a4fc72083b8 (patch) | |
tree | ead6f9249a8f32e5709b883e9b17c42113e80906 /test/graph | |
parent | 04bb201c6162e81dbdefcb1cff9595180fa66917 (diff) | |
download | bsfs-1392951dfc82af05e7a5999baa1c0a4fc72083b8.tar.gz bsfs-1392951dfc82af05e7a5999baa1c0a4fc72083b8.tar.bz2 bsfs-1392951dfc82af05e7a5999baa1c0a4fc72083b8.zip |
Nodes magic methods for convenience
Diffstat (limited to 'test/graph')
-rw-r--r-- | test/graph/test_nodes.py | 109 |
1 files changed, 107 insertions, 2 deletions
diff --git a/test/graph/test_nodes.py b/test/graph/test_nodes.py index 4eae250..c07fa53 100644 --- a/test/graph/test_nodes.py +++ b/test/graph/test_nodes.py @@ -4,10 +4,14 @@ Part of the bsfs test suite. A copy of the license is provided with the project. Author: Matthias Baumgartner, 2022 """ -# imports -import rdflib +# standard imports +from functools import partial +import operator import unittest +# external imports +import rdflib + # bsie imports from bsfs import schema as bsc from bsfs.graph.walk import Walk @@ -476,6 +480,107 @@ class TestNodes(unittest.TestCase): # invalid step raises an error self.assertRaises(ValueError, getattr, nodes, 'foobar') + def test_schema(self): + self.assertEqual(Nodes(self.backend, self.user, 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) + nodes = gen({URI('http://example.com/me/entity#1234')}) + # add/or concatenates guids + self.assertEqual( + gen({URI('http://example.com/me/entity#1234')}) + + gen({URI('http://example.com/me/entity#4321')}), + # target + gen({ + URI('http://example.com/me/entity#1234'), + URI('http://example.com/me/entity#4321')})) + self.assertEqual( + gen({URI('http://example.com/me/entity#1234')}) | + gen({URI('http://example.com/me/entity#4321')}), + # target + gen({ + URI('http://example.com/me/entity#1234'), + URI('http://example.com/me/entity#4321')})) + # repeated guids are ignored + self.assertEqual( + gen({URI('http://example.com/me/entity#1234')}) + + gen({URI('http://example.com/me/entity#1234')}), + # target + gen({URI('http://example.com/me/entity#1234')})) + self.assertEqual( + gen({URI('http://example.com/me/entity#1234')}) | + gen({URI('http://example.com/me/entity#1234')}), + # target + gen({URI('http://example.com/me/entity#1234')})) + + # sub substracts guids + self.assertEqual( + gen({URI('http://example.com/me/entity#1234'), + URI('http://example.com/me/entity#4321')}) - + gen({URI('http://example.com/me/entity#4321')}), + # target + gen({URI('http://example.com/me/entity#1234')})) + # missing guids are ignored + self.assertEqual( + gen({URI('http://example.com/me/entity#1234')}) - + gen({URI('http://example.com/me/entity#4321')}), + # target + gen({URI('http://example.com/me/entity#1234')})) + + # and intersects guids + self.assertEqual( + gen({URI('http://example.com/me/entity#1234'), + URI('http://example.com/me/entity#4321')}) & + gen({URI('http://example.com/me/entity#4321'), + URI('http://example.com/me/entity#5678')}), + # target + gen({URI('http://example.com/me/entity#4321')})) + + for op in (operator.add, operator.or_, operator.sub, operator.and_): + # type must match + self.assertRaises(TypeError, op, nodes, 1234) + 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 + self.assertRaises(ValueError, op, nodes, + Nodes(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')})) + + def test_len(self): + self.assertEqual(1, len(Nodes(self.backend, self.user, self.ent_type, { + URI('http://example.com/me/entity#1234'), + }))) + self.assertEqual(2, len(Nodes(self.backend, self.user, 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, { + URI('http://example.com/me/entity#1234'), + URI('http://example.com/me/entity#4321'), + URI('http://example.com/me/entity#5678'), + URI('http://example.com/me/entity#8765'), + }))) + + 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, { + URI('http://example.com/me/entity#1234'), + URI('http://example.com/me/entity#4321'), + URI('http://example.com/me/entity#5678'), + URI('http://example.com/me/entity#8765'), + })), { + gen({URI('http://example.com/me/entity#1234')}), + gen({URI('http://example.com/me/entity#4321')}), + gen({URI('http://example.com/me/entity#5678')}), + gen({URI('http://example.com/me/entity#8765')}), + }) + + ## main ## if __name__ == '__main__': |