diff options
Diffstat (limited to 'test/matcher/test_default_matcher.py')
-rw-r--r-- | test/matcher/test_default_matcher.py | 104 |
1 files changed, 104 insertions, 0 deletions
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 ## |