|
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.byLocation.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 = {'parentfield':location} |
|
32
|
|
|
return self.newimg |
|
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
|
|
|
import niprov.logging |
|
41
|
|
|
with patch('niprov.logging.inheritFrom') as self.inheritFrom: |
|
42
|
|
|
self.inheritFrom.side_effect = lambda p, p2: p |
|
43
|
|
|
return niprov.logging.log(*args, dependencies=self.dependencies, opts=self.opts, |
|
44
|
|
|
**kwargs) |
|
45
|
|
|
|
|
46
|
|
|
def test_Returns_img(self): |
|
47
|
|
|
parents = ['/p/f1'] |
|
48
|
|
|
new = '/p/f2' |
|
49
|
|
|
trans = 'Something cool' |
|
50
|
|
|
out = self.log(new, trans, parents) |
|
51
|
|
|
self.assertEqual(out, self.newimg) |
|
52
|
|
|
|
|
53
|
|
|
def test_Adds_code_or_logtext(self): |
|
54
|
|
|
self.log('new', 'trans', 'old', code='abc', logtext='def') |
|
55
|
|
|
self.assertEqual(self.provenancesAdded[0]['code'],'abc') |
|
56
|
|
|
self.assertEqual(self.provenancesAdded[0]['logtext'],'def') |
|
57
|
|
|
|
|
58
|
|
|
def test_Determines_user_and_logs_them(self): |
|
59
|
|
|
self.users.determineUser.return_value = 'paprika' |
|
60
|
|
|
self.log('new', 'trans', 'old', user='mononoko') |
|
61
|
|
|
self.users.determineUser.assert_called_with('mononoko') |
|
62
|
|
|
self.assertEqual(self.provenancesAdded[0]['user'],'paprika') |
|
63
|
|
|
|
|
64
|
|
|
def test_Script_added_to_provenance(self): |
|
65
|
|
|
parents = ['/p/f1'] |
|
66
|
|
|
new = '/p/f2' |
|
67
|
|
|
trans = 'Something cool' |
|
68
|
|
|
script = '/p/test.py' |
|
69
|
|
|
self.log(new, trans, parents, script=script) |
|
70
|
|
|
self.assertEqual(self.provenancesAdded[0]['script'], script) |
|
71
|
|
|
|
|
72
|
|
|
def test_Accepts_and_processes_custom_provenance(self): |
|
73
|
|
|
parents = ['/p/f1'] |
|
74
|
|
|
new = '/p/f2' |
|
75
|
|
|
trans = 'Something cool' |
|
76
|
|
|
p = {'akey':'avalue'} |
|
77
|
|
|
self.log(new, trans, parents, provenance=p) |
|
78
|
|
|
self.assertEqual(self.provenancesAdded[0]['akey'], 'avalue') |
|
79
|
|
|
|
|
80
|
|
|
def test_Calls_reconfigureOrGetConfiguration_on_dependencies(self): |
|
81
|
|
|
outOpts = Mock() |
|
82
|
|
|
outOpts.dryrun = True |
|
83
|
|
|
self.dependencies.reconfigureOrGetConfiguration.return_value = outOpts |
|
84
|
|
|
provenance = self.log(['/p/f1'], 'bla', ['/p/f2'], transient=True) |
|
85
|
|
|
self.dependencies.reconfigureOrGetConfiguration.assert_called_with( |
|
86
|
|
|
self.opts) |
|
87
|
|
|
|
|
88
|
|
|
def test_Can_pass_multiple_new_files(self): |
|
89
|
|
|
parents = ['p1','p2'] |
|
90
|
|
|
new = ['/p/f2','/p/f3'] |
|
91
|
|
|
trans = 'Something cool' |
|
92
|
|
|
self.locationFactory.completeString.side_effect = lambda p: 'l:'+p |
|
93
|
|
|
self.log(new, trans, parents) |
|
94
|
|
|
self.assertEqual(self.provenancesAdded[0]['parents'], ['l:p1','l:p2']) |
|
95
|
|
|
self.assertEqual(self.provenancesAdded[1]['parents'], ['l:p1','l:p2']) |
|
96
|
|
|
|
|
97
|
|
|
def test_Add_parent_if_parent_unknown(self): |
|
98
|
|
|
self.repo.byLocation.return_value = None |
|
99
|
|
|
self.locationFactory.completeString.side_effect = lambda p: 'l:'+p |
|
100
|
|
|
provenance = self.log('new', 'trans', 'parentpath') |
|
101
|
|
|
self.add.assert_any_call('l:parentpath') |
|
102
|
|
|
self.listener.addUnknownParent.assert_called_with('l:parentpath') |
|
103
|
|
|
|
|
104
|
|
|
def test_Uses_validated_location_for_parent_lookup(self): |
|
105
|
|
|
self.locationFactory.completeString.side_effect = lambda p: 'l:'+p |
|
106
|
|
|
provenance = self.log('new', 'trans', 'parentpath') |
|
107
|
|
|
self.repo.byLocation.assert_called_with('l:parentpath') |
|
108
|
|
|
|
|
109
|
|
|
def test_Inherits_from_parent(self): |
|
110
|
|
|
parent = Mock() |
|
111
|
|
|
parent.provenance = {'x':123} |
|
112
|
|
|
inprov = {'a':89} |
|
113
|
|
|
self.repo.byLocation.return_value = parent |
|
114
|
|
|
self.locationFactory.completeString.side_effect = lambda p: 'l:'+p |
|
115
|
|
|
img = self.log('new', 'trans', 'parentpath', provenance=inprov) |
|
116
|
|
|
self.inheritFrom.assert_called_with(inprov, parent.provenance) |
|
117
|
|
|
|
|
118
|
|
|
def test_If_parent_unknown_uses_add_return_value(self): |
|
119
|
|
|
"""In this case the parent provenance should be obtained from |
|
120
|
|
|
what add() returns. |
|
121
|
|
|
""" |
|
122
|
|
|
self.locationFactory.completeString.side_effect = lambda p: 'l:'+p |
|
123
|
|
|
inprov = {'a':89} |
|
124
|
|
|
self.repo.byLocation.return_value = None |
|
125
|
|
|
img = self.log('new', 'trans', 'parentpath', provenance=inprov) |
|
126
|
|
|
## add function is mocked to set 'acquired' field to location passed |
|
127
|
|
|
self.inheritFrom.assert_called_with(inprov, {'parentfield':'l:parentpath'}) |
|
128
|
|
|
|
|
129
|
|
|
|
|
130
|
|
|
|
|
131
|
|
|
|