diff options
Diffstat (limited to 'test/matcher')
-rw-r--r-- | test/matcher/__init__.py | 0 | ||||
-rw-r--r-- | test/matcher/test_default_matcher.py | 104 | ||||
-rw-r--r-- | test/matcher/test_matcher.py | 62 | ||||
-rw-r--r-- | test/matcher/test_nodes.py | 96 |
4 files changed, 262 insertions, 0 deletions
diff --git a/test/matcher/__init__.py b/test/matcher/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/test/matcher/__init__.py diff --git a/test/matcher/test_default_matcher.py b/test/matcher/test_default_matcher.py new file mode 100644 index 0000000..2ed371f --- /dev/null +++ b/test/matcher/test_default_matcher.py @@ -0,0 +1,104 @@ + +# standard imports +from dataclasses import dataclass +import unittest + +# bsie imports +from bsie.matcher import nodes +from bsie.utils import ns, errors +from bsie.utils.bsfs import URI + +# objects to test +from bsie.matcher.default_matcher import DefaultMatcher + + +## code ## + +@dataclass(kw_only=True, unsafe_hash=True) +class Invalid(nodes.Node): + ucid: str + +class TestDefaultMatcher(unittest.TestCase): + + def test_match_node(self): + # setup + matcher = DefaultMatcher('http://example.com', 'me') + # match_node doesn't modify existing uris + self.assertEqual(matcher.match_node( + nodes.Node(uri='http://example.com/you/foo#bar')).uri, + URI('http://example.com/you/foo#bar')) + # processes bsn:Entity + self.assertEqual(matcher.match_node( + nodes.Entity(ucid='abc123cba')).uri, + URI('http://example.com/me/file#abc123cba')) + # processes bsn:Preview + self.assertEqual(matcher.match_node( + nodes.Preview(ucid='abc123cba', size=123)).uri, + URI('http://example.com/me/preview#abc123cba_s123')) + # processes bsn:Tag + self.assertEqual(matcher.match_node( + nodes.Tag(label='hello')).uri, + URI('http://example.com/me/tag#hello')) + # processes bsn:Face + self.assertEqual(matcher.match_node( + nodes.Face(ucid='hello')).uri, + URI('http://example.com/me/face#hello')) + # raises an exception on unknown types + self.assertRaises(ValueError, matcher.match_node, + Invalid(ucid='abc123cba')) + + def test_match_entity(self): + # setup + matcher = DefaultMatcher('http://example.com', 'me') + # match_entity uses ucid + self.assertEqual(matcher.match_entity( + nodes.Entity(ucid='123abc321')).uri, + URI('http://example.com/me/file#123abc321')) + + def test_match_preview(self): + # setup + matcher = DefaultMatcher('http://example.com', 'me') + # match_preview uses ucid + self.assertEqual(matcher.match_preview( + nodes.Preview(ucid='123abc321', size=400)).uri, + URI('http://example.com/me/preview#123abc321_s400')) + self.assertEqual(matcher.match_preview( + nodes.Preview(ucid='321cba123', size=200)).uri, + URI('http://example.com/me/preview#321cba123_s200')) + + def test_match_tag(self): + # setup + matcher = DefaultMatcher('http://example.com', 'me') + # match_tag uses label + self.assertEqual(matcher.match_tag( + nodes.Tag(label='hello')).uri, + URI('http://example.com/me/tag#hello')) + # match_tag matches the label + self.assertEqual( + matcher.match_tag(nodes.Tag(label='world')), + matcher.match_tag(nodes.Tag(label='world')), + ) + self.assertNotEqual( + matcher.match_tag(nodes.Tag(label='hello')), + matcher.match_tag(nodes.Tag(label='world')), + ) + # label can include characters that are not valid for an uri + self.assertEqual(matcher.match_tag( + nodes.Tag(label='hello world { foo bar ] ')).uri, + URI('http://example.com/me/tag#hello%20world%20%7B%20foo%20bar%20%5D%20')) + + def test_match_face(self): + # setup + matcher = DefaultMatcher('http://example.com', 'me') + # match_face uses ucid + self.assertEqual(matcher.match_face( + nodes.Face(ucid='hello_world')).uri, + URI('http://example.com/me/face#hello_world')) + + +## main ## + +if __name__ == '__main__': + unittest.main() + +## EOF ## diff --git a/test/matcher/test_matcher.py b/test/matcher/test_matcher.py new file mode 100644 index 0000000..eaa4766 --- /dev/null +++ b/test/matcher/test_matcher.py @@ -0,0 +1,62 @@ + +# standard imports +import unittest + +# bsie imports +from bsie.matcher import nodes +from bsie.utils import ns, errors +from bsie.utils.bsfs import URI + +# objects to test +from bsie.matcher.matcher import Matcher, MatcherIterator + + +## code ## + +class StubMatcher(Matcher): + def match_node(self, node): + if node.uri is None: + node.uri = 'foo' + return node + +class TestMatcherIterator(unittest.TestCase): + + def test_call(self): + # setup + matcher = StubMatcher() + # call accepts list + triples = [('node', 'pred', 'value'), ('node', 'pred', 'value')] + it = matcher(triples) + self.assertIsInstance(it, MatcherIterator) + self.assertEqual(it._iterable, triples) + self.assertEqual(it._matcher, matcher) + # call accepts iterator + triples = iter([('node', 'pred', 'value'), ('node', 'pred', 'value')]) + it = matcher(triples) + self.assertIsInstance(it, MatcherIterator) + self.assertEqual(it._iterable, triples) + self.assertEqual(it._matcher, matcher) + + def test_iter(self): + # setup + matcher = StubMatcher() + triples = [ + (nodes.Entity(ucid='foo'), 'predA', 'hello'), + (nodes.Preview(ucid='bar', size=123), 'predB', 1234), + (nodes.Preview(ucid='hello', size=321), 'predC', nodes.Entity(ucid='world')) + ] + # handles nodes, handles values, ignores predicate + self.assertListEqual(list(matcher(triples)), [ + (nodes.Entity(uri='foo', ucid='foo'), 'predA', 'hello'), + (nodes.Preview(uri='foo', ucid='bar', size=123), 'predB', 1234), + (nodes.Preview(uri='foo', ucid='hello', size=321), 'predC', + nodes.Entity(uri='foo', ucid='world')), + ]) + + +## main ## + +if __name__ == '__main__': + unittest.main() + +## EOF ## diff --git a/test/matcher/test_nodes.py b/test/matcher/test_nodes.py new file mode 100644 index 0000000..d884132 --- /dev/null +++ b/test/matcher/test_nodes.py @@ -0,0 +1,96 @@ + +# standard imports +import unittest + +# bsie imports +from bsie.utils import bsfs, ns + +# objects to test +from bsie.matcher import nodes + + +## code ## + +class TestNode(unittest.TestCase): + def test_node(self): + # cannot pass additional kwargs + self.assertRaises(TypeError, nodes.Node, foo='bar') + # may pass uri + self.assertIsInstance(nodes.Node(uri='bar'), nodes.Node) + self.assertEqual(nodes.Node(uri='bar').uri, 'bar') + + def test_entity(self): + # must pass ucid + self.assertRaises(TypeError, nodes.Entity) + self.assertRaises(TypeError, nodes.Entity, uri='foo') + self.assertIsInstance(nodes.Entity(ucid='foo'), nodes.Entity) + self.assertIsInstance(nodes.Entity(ucid='bar'), nodes.Entity) + # cannot pass additional kwargs + self.assertRaises(TypeError, nodes.Entity, ucid='foo', foo='bar') + # may pass uri + self.assertIsInstance(nodes.Entity(ucid='foo', uri='bar'), nodes.Entity) + self.assertEqual(nodes.Entity(ucid='foo', uri='bar').uri, 'bar') + # has node_type + self.assertEqual(nodes.Entity(ucid='foo').node_type, ns.bsn.Entity) + + def test_face(self): + # must pass ucid + self.assertRaises(TypeError, nodes.Face) + self.assertRaises(TypeError, nodes.Face, uri='foo') + self.assertIsInstance(nodes.Face(ucid='foo'), nodes.Face) + self.assertIsInstance(nodes.Face(ucid='bar'), nodes.Face) + # cannot pass additional kwargs + self.assertRaises(TypeError, nodes.Face, ucid='foo', foo='bar') + # may pass uri + self.assertIsInstance(nodes.Face(ucid='foo', uri='bar'), nodes.Face) + self.assertEqual(nodes.Face(ucid='foo', uri='bar').uri, 'bar') + # has node_type + self.assertEqual(nodes.Face(ucid='foo').node_type, ns.bsn.Face) + + def test_person(self): + # cannot pass additional kwargs + self.assertRaises(TypeError, nodes.Person, foo='bar') + # may pass uri + self.assertIsInstance(nodes.Person(uri='bar'), nodes.Person) + self.assertEqual(nodes.Person(uri='bar').uri, 'bar') + # has node_type + self.assertEqual(nodes.Person().node_type, ns.bsn.Person) + + def test_preview(self): + # must pass ucid and size + self.assertRaises(TypeError, nodes.Preview) + self.assertRaises(TypeError, nodes.Preview, ucid='foo') + self.assertRaises(TypeError, nodes.Preview, size=123) + self.assertRaises(TypeError, nodes.Preview, ucid='foo', uri='foo') + self.assertRaises(TypeError, nodes.Preview, size=123, uri='foo') + self.assertIsInstance(nodes.Preview(ucid='foo', size=123), nodes.Preview) + self.assertIsInstance(nodes.Preview(ucid='bar', size=321), nodes.Preview) + # cannot pass additional kwargs + self.assertRaises(TypeError, nodes.Preview, ucid='foo', size=123, foo='bar') + # may pass uri + self.assertIsInstance(nodes.Preview(ucid='foo', size=123, uri='bar'), nodes.Preview) + self.assertEqual(nodes.Preview(ucid='foo', size=123, uri='bar').uri, 'bar') + # has node_type + self.assertEqual(nodes.Preview(ucid='foo', size=123).node_type, ns.bsn.Preview) + + def test_tag(self): + # must pass label + self.assertRaises(TypeError, nodes.Tag) + self.assertRaises(TypeError, nodes.Tag, uri='foo') + self.assertIsInstance(nodes.Tag(label='foo'), nodes.Tag) + self.assertIsInstance(nodes.Tag(label='bar'), nodes.Tag) + # cannot pass additional kwargs + self.assertRaises(TypeError, nodes.Tag, label='foo', foo='bar') + # may pass uri + self.assertIsInstance(nodes.Tag(label='foo', uri='bar'), nodes.Tag) + self.assertEqual(nodes.Tag(label='foo', uri='bar').uri, 'bar') + # has node_type + self.assertEqual(nodes.Tag(label='foo').node_type, ns.bsn.Tag) + + +## main ## + +if __name__ == '__main__': + unittest.main() + +## EOF ## |