1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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 ##
|