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
|
# 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 ##
|