|
1
|
|
|
from mock import Mock, patch |
|
2
|
|
|
from datetime import datetime as dt |
|
3
|
|
|
from tests.ditest import DependencyInjectionTestBase |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
class LoggingTests(DependencyInjectionTestBase): |
|
7
|
|
|
|
|
8
|
|
|
def setUp(self): |
|
9
|
|
|
super(LoggingTests, self).setUp() |
|
10
|
|
|
self.repo.knowsByLocation.return_value = True |
|
11
|
|
|
self.opts = Mock() |
|
12
|
|
|
self.opts.dryrun = False |
|
13
|
|
|
img = Mock() |
|
14
|
|
|
img.provenance = {'acquired':dt.now(), |
|
15
|
|
|
'subject':'JD', |
|
16
|
|
|
'protocol':'X', |
|
17
|
|
|
'technique':'abc', |
|
18
|
|
|
'repetition-time':1.0, |
|
19
|
|
|
'epi-factor':1.0, |
|
20
|
|
|
'magnetization-transfer-contrast':True, |
|
21
|
|
|
'diffusion':True, |
|
22
|
|
|
'echo-time':123, |
|
23
|
|
|
'flip-angle':892, |
|
24
|
|
|
'inversion-time':123} |
|
25
|
|
|
self.locationFactory.completeString.side_effect = lambda p: p |
|
26
|
|
|
self.repo.byLocation.return_value = img |
|
27
|
|
|
self.provenancesAdded = [] |
|
28
|
|
|
def wrapProv(location, transient=False, provenance=False, dependencies=None): |
|
29
|
|
|
self.provenancesAdded.append(provenance) |
|
30
|
|
|
self.newimg = Mock() |
|
31
|
|
|
self.newimg.provenance = {'acquired':location} |
|
32
|
|
|
return (self.newimg, 'test') |
|
33
|
|
|
self.dependencies.reconfigureOrGetConfiguration.return_value = self.opts |
|
34
|
|
|
patcher = patch('niprov.logging.add') |
|
35
|
|
|
self.add = patcher.start() |
|
36
|
|
|
self.add.side_effect = wrapProv |
|
37
|
|
|
self.addCleanup(patcher.stop) |
|
38
|
|
|
|
|
39
|
|
|
def log(self, *args, **kwargs): |
|
40
|
|
|
from niprov.logging import log |
|
41
|
|
|
return log(*args, dependencies=self.dependencies, opts=self.opts, |
|
42
|
|
|
**kwargs) |
|
43
|
|
|
|
|
44
|
|
|
def test_Returns_img(self): |
|
45
|
|
|
parents = ['/p/f1'] |
|
46
|
|
|
new = '/p/f2' |
|
47
|
|
|
trans = 'Something cool' |
|
48
|
|
|
out = self.log(new, trans, parents) |
|
49
|
|
|
self.assertEqual(out, self.newimg) |
|
50
|
|
|
|
|
51
|
|
|
def test_Copies_fields_from_known_parent(self): |
|
52
|
|
|
self.locationFactory.completeString.side_effect = lambda p: p |
|
53
|
|
|
parent = '/p/f1' |
|
54
|
|
|
parents = [parent] |
|
55
|
|
|
parentProv = {'acquired':dt.now(), |
|
56
|
|
|
'subject':'JD', |
|
57
|
|
|
'protocol':'X', |
|
58
|
|
|
'technique':'abc', |
|
59
|
|
|
'repetition-time':1.0, |
|
60
|
|
|
'epi-factor':1.0, |
|
61
|
|
|
'magnetization-transfer-contrast':True, |
|
62
|
|
|
'diffusion':True, |
|
63
|
|
|
'echo-time':123, |
|
64
|
|
|
'flip-angle':892, |
|
65
|
|
|
'inversion-time':123} |
|
66
|
|
|
parentImg = Mock() |
|
67
|
|
|
parentImg.provenance = parentProv |
|
68
|
|
|
self.repo.byLocation.side_effect = lambda x: {parent:parentImg}[x] |
|
69
|
|
|
self.log('new', 'trans', parents) |
|
70
|
|
|
self.assertEqual(self.provenancesAdded[0]['acquired'], parentProv['acquired']) |
|
71
|
|
|
self.assertEqual(self.provenancesAdded[0]['subject'], parentProv['subject']) |
|
72
|
|
|
self.assertEqual(self.provenancesAdded[0]['protocol'], parentProv['protocol']) |
|
73
|
|
|
self.assertEqual(self.provenancesAdded[0]['technique'], parentProv['technique']) |
|
74
|
|
|
self.assertEqual(self.provenancesAdded[0]['repetition-time'], parentProv['repetition-time']) |
|
75
|
|
|
self.assertEqual(self.provenancesAdded[0]['epi-factor'], parentProv['epi-factor']) |
|
76
|
|
|
self.assertEqual(self.provenancesAdded[0]['magnetization-transfer-contrast'], |
|
77
|
|
|
parentProv['magnetization-transfer-contrast']) |
|
78
|
|
|
self.assertEqual(self.provenancesAdded[0]['diffusion'], parentProv['diffusion']) |
|
79
|
|
|
self.assertEqual(self.provenancesAdded[0]['echo-time'], parentProv['echo-time']) |
|
80
|
|
|
self.assertEqual(self.provenancesAdded[0]['flip-angle'], parentProv['flip-angle']) |
|
81
|
|
|
self.assertEqual(self.provenancesAdded[0]['inversion-time'], parentProv['inversion-time']) |
|
82
|
|
|
|
|
83
|
|
|
def test_Adds_code_or_logtext(self): |
|
84
|
|
|
self.log('new', 'trans', 'old', code='abc', logtext='def') |
|
85
|
|
|
self.assertEqual(self.provenancesAdded[0]['code'],'abc') |
|
86
|
|
|
self.assertEqual(self.provenancesAdded[0]['logtext'],'def') |
|
87
|
|
|
|
|
88
|
|
|
def test_Determines_user_and_logs_them(self): |
|
89
|
|
|
self.users.determineUser.return_value = 'paprika' |
|
90
|
|
|
self.log('new', 'trans', 'old', user='mononoko') |
|
91
|
|
|
self.users.determineUser.assert_called_with('mononoko') |
|
92
|
|
|
self.assertEqual(self.provenancesAdded[0]['user'],'paprika') |
|
93
|
|
|
|
|
94
|
|
|
def test_Script_added_to_provenance(self): |
|
95
|
|
|
parents = ['/p/f1'] |
|
96
|
|
|
new = '/p/f2' |
|
97
|
|
|
trans = 'Something cool' |
|
98
|
|
|
script = '/p/test.py' |
|
99
|
|
|
self.log(new, trans, parents, script=script) |
|
100
|
|
|
self.assertEqual(self.provenancesAdded[0]['script'], script) |
|
101
|
|
|
|
|
102
|
|
|
def test_Accepts_and_processes_custom_provenance(self): |
|
103
|
|
|
parents = ['/p/f1'] |
|
104
|
|
|
new = '/p/f2' |
|
105
|
|
|
trans = 'Something cool' |
|
106
|
|
|
p = {'akey':'avalue'} |
|
107
|
|
|
self.log(new, trans, parents, provenance=p) |
|
108
|
|
|
self.assertEqual(self.provenancesAdded[0]['akey'], 'avalue') |
|
109
|
|
|
|
|
110
|
|
|
def test_Doesnt_complain_if_parent_is_missing_basic_fields(self): |
|
111
|
|
|
img = Mock() |
|
112
|
|
|
img.provenance = {'acquired':dt.now()} #missing subject |
|
113
|
|
|
self.repo.byLocation.return_value = img |
|
114
|
|
|
provenance = self.log('new', 'trans', ['/p/f1parent']) |
|
115
|
|
|
self.assertNotIn('subject', self.provenancesAdded[0]) |
|
116
|
|
|
|
|
117
|
|
|
def test_Calls_reconfigureOrGetConfiguration_on_dependencies(self): |
|
118
|
|
|
outOpts = Mock() |
|
119
|
|
|
outOpts.dryrun = True |
|
120
|
|
|
self.dependencies.reconfigureOrGetConfiguration.return_value = outOpts |
|
121
|
|
|
provenance = self.log(['/p/f1'], 'bla', ['/p/f2'], transient=True) |
|
122
|
|
|
self.dependencies.reconfigureOrGetConfiguration.assert_called_with( |
|
123
|
|
|
self.opts) |
|
124
|
|
|
|
|
125
|
|
|
def test_Can_pass_multiple_new_files(self): |
|
126
|
|
|
parents = ['p1','p2'] |
|
127
|
|
|
new = ['/p/f2','/p/f3'] |
|
128
|
|
|
trans = 'Something cool' |
|
129
|
|
|
self.locationFactory.completeString.side_effect = lambda p: 'l:'+p |
|
130
|
|
|
self.log(new, trans, parents) |
|
131
|
|
|
self.assertEqual(self.provenancesAdded[0]['parents'], ['l:p1','l:p2']) |
|
132
|
|
|
self.assertEqual(self.provenancesAdded[1]['parents'], ['l:p1','l:p2']) |
|
133
|
|
|
|
|
134
|
|
|
def test_Add_parent_if_parent_unknown(self): |
|
135
|
|
|
self.repo.knowsByLocation.return_value = False |
|
136
|
|
|
self.locationFactory.completeString.side_effect = lambda p: 'l:'+p |
|
137
|
|
|
provenance = self.log('new', 'trans', 'parentpath') |
|
138
|
|
|
self.add.assert_any_call('l:parentpath') |
|
139
|
|
|
self.listener.addUnknownParent.assert_called_with('l:parentpath') |
|
140
|
|
|
|
|
141
|
|
|
def test_Uses_validated_location_for_parent_lookup(self): |
|
142
|
|
|
self.locationFactory.completeString.side_effect = lambda p: 'l:'+p |
|
143
|
|
|
provenance = self.log('new', 'trans', 'parentpath') |
|
144
|
|
|
self.repo.knowsByLocation.assert_called_with('l:parentpath') |
|
145
|
|
|
self.repo.byLocation.assert_called_with('l:parentpath') |
|
146
|
|
|
|
|
147
|
|
|
def test_If_parent_unknown_uses_add_return_value(self): |
|
148
|
|
|
"""In this case the parent provenance should be obtained from |
|
149
|
|
|
what add() returns. |
|
150
|
|
|
""" |
|
151
|
|
|
self.repo.knowsByLocation.return_value = False |
|
152
|
|
|
parent = 'unknown/parent/path.f' |
|
153
|
|
|
provenance = self.log('new', 'trans', [parent]) |
|
154
|
|
|
assert not self.repo.byLocation.called |
|
155
|
|
|
loggedFileProv = self.provenancesAdded[1] # [0]: parent [1]: newly logged |
|
156
|
|
|
## add function is mocked to set 'acquired' field to location passed |
|
157
|
|
|
## since acquired is an inheritable field, it should show up in kid: |
|
158
|
|
|
self.assertEqual(loggedFileProv['acquired'], 'unknown/parent/path.f') |
|
159
|
|
|
|
|
160
|
|
|
|
|
161
|
|
|
|