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