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
|
"""
Part of the bsie test suite.
A copy of the license is provided with the project.
Author: Matthias Baumgartner, 2022
"""
# standard imports
import unittest
# bsie imports
from bsie.utils import ns, errors
from bsie.utils.bsfs import URI
from bsie.utils.node import Node
# objects to test
from bsie.lib.naming_policy import NamingPolicy, NamingPolicyIterator, DefaultNamingPolicy
## code ##
class TestDefaultNamingPolicy(unittest.TestCase):
def test_handle_node(self):
# setup
policy = DefaultNamingPolicy('http://example.com', 'me')
# handle_node doesn't modify existing uris
self.assertEqual(policy.handle_node(
Node(ns.bsfs.Entity, uri='http://example.com/you/foo#bar')).uri,
URI('http://example.com/you/foo#bar'))
# processes bsfs:File
self.assertEqual(policy.handle_node(
Node(ns.bsfs.File, ucid='abc123cba')).uri,
URI('http://example.com/me/file#abc123cba'))
# raises an exception on unknown types
self.assertRaises(errors.ProgrammingError, policy.handle_node,
Node(ns.bsfs.Entity, ucid='abc123cba', size=123))
def test_name_file(self):
# setup
policy = DefaultNamingPolicy('http://example.com', 'me')
# name_file uses ucid
self.assertEqual(policy.name_file(
Node(ns.bsfs.File, ucid='123abc321')).uri,
URI('http://example.com/me/file#123abc321'))
# name_file falls back to a random guid
self.assertTrue(policy.name_file(
Node(ns.bsfs.File)).uri.startswith('http://example.com/me/file#'))
class TestNamingPolicyIterator(unittest.TestCase):
def test_call(self): # NOTE: We test NamingPolicy.__call__ here
# setup
policy = DefaultNamingPolicy('http://example.com', 'me')
# call accepts list
triples = [('node', 'pred', 'value'), ('node', 'pred', 'value')]
it = policy(triples)
self.assertIsInstance(it, NamingPolicyIterator)
self.assertEqual(it._iterable, triples)
self.assertEqual(it._policy, policy)
# call accepts iterator
triples = iter([('node', 'pred', 'value'), ('node', 'pred', 'value')])
it = policy(triples)
self.assertIsInstance(it, NamingPolicyIterator)
self.assertEqual(it._iterable, triples)
self.assertEqual(it._policy, policy)
def test_iter(self):
# setup
policy = DefaultNamingPolicy('http://example.com', 'me')
triples = [
(Node(ns.bsfs.File, ucid='foo'), 'predA', 'hello'),
]
# handles nodes, handles values, ignores predicate
self.assertListEqual(list(policy(triples)), [
(Node(ns.bsfs.File, uri='http://example.com/me/file#foo'), 'predA', 'hello'),
])
## main ##
if __name__ == '__main__':
unittest.main()
## EOF ##
|