Completed
Pull Request — master (#143)
by Jasper
01:31
created

JsonFileTest.test_Query()   A

Complexity

Conditions 2

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
dl 0
loc 17
rs 9.4285
c 1
b 0
f 0
1
from mock import Mock
2
from tests.ditest import DependencyInjectionTestBase
3
import datetime, random
4
5
6
class JsonFileTest(DependencyInjectionTestBase):
7
8
    def setUp(self):
9
        super(JsonFileTest, self).setUp()
10
        self.dependencies.getConfiguration().database_url = ''
11
12
    def imageWithProvenance(self, prov):
13
        img = Mock()
14
        img.provenance = prov
15
        if 'location' in prov:
16
            img.location.toString.return_value = prov['location']
17
        return img
18
19 View Code Duplication
    def test_Add(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
20
        from niprov.jsonfile import JsonFile
21
        repo = JsonFile(self.dependencies)
22
        img1 = self.imageWithProvenance({'location':'1','foo':'baz'})
23
        repo.all = Mock()
24
        repo.all.return_value = [img1]
25
        image = self.imageWithProvenance({'location': '2','foo':'bar'})
26
        repo.add(image)
27
        self.serializer.serializeList.assert_called_with(
28
            [img1, image])
29
        self.filesys.write.assert_called_with(repo.datafile, 
30
            self.serializer.serializeList())
31
32 View Code Duplication
    def test_Update(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
33
        from niprov.jsonfile import JsonFile
34
        repo = JsonFile(self.dependencies)
35
        img1 = self.imageWithProvenance({'location':'1','path':'a'})
36
        img2 = self.imageWithProvenance({'location':'2','path':'b'})
37
        repo.all = Mock()
38
        repo.all.return_value = [img1, img2]
39
        image = self.imageWithProvenance({'location': '2','foo':'bar'})
40
        repo.update(image)
41
        self.serializer.serializeList.assert_called_with(
42
            [img1,image])
43
        self.filesys.write.assert_called_with(repo.datafile, 
44
            self.serializer.serializeList())
45
46
    def test_knowsByLocation(self):
47
        from niprov.jsonfile import JsonFile
48
        repo = JsonFile(self.dependencies)
49
        repo.byLocation = Mock()
50
        self.assertTrue(repo.knowsByLocation(''))
51
        repo.byLocation.side_effect = IndexError
52
        self.assertFalse(repo.knowsByLocation(''))
53
54
    def test_byLocation(self):
55
        from niprov.jsonfile import JsonFile
56
        repo = JsonFile(self.dependencies)
57
        img1 = self.imageWithProvenance({'location':'1','path':'a'})
58
        img2 = self.imageWithProvenance({'location':'2','path':'b'})
59
        repo.all = Mock()
60
        repo.all.return_value = [img1, img2]
61
        out = repo.byLocation('2')
62
        self.assertEqual(img2, out)
63
64
    def test_byLocation_works_for_file_in_series(self):
65
        from niprov.jsonfile import JsonFile
66
        repo = JsonFile(self.dependencies)
67
        img1 = self.imageWithProvenance({'location':'1','path':'a'})
68
        img3 = self.imageWithProvenance({'location':'3','filesInSeries':['boo','bah']})
69
        repo.all = Mock()
70
        repo.all.return_value = [img1, img3]
71
        out = repo.byLocation('boo')
72
        self.assertEqual(img3, out)
73
74
    def test_updateApproval(self):
75
        from niprov.jsonfile import JsonFile
76
        repo = JsonFile(self.dependencies)
77
        img = Mock()
78
        img.provenance = {'fiz':'baf','approval':'notsure'}
79
        def assertion(img):
80
            self.assertEqual({'fiz':'baf','approval':'excellent!'}, img.provenance)
81
        repo.byLocation = Mock()
82
        repo.byLocation.return_value = img
83
        repo.update = Mock()
84
        repo.update.side_effect = assertion
85
        repo.updateApproval('/p/f1','excellent!')
86
87 View Code Duplication
    def test_bySubject(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
88
        from niprov.jsonfile import JsonFile
89
        repo = JsonFile(self.dependencies)
90
        img1 = self.imageWithProvenance({'subject':'john','a':'b'})
91
        img2 = self.imageWithProvenance({'subject':'tim','a':'d'})
92
        img3 = self.imageWithProvenance({'subject':'john','a':'f'})
93
        repo.all = Mock()
94
        repo.all.return_value = [img1, img2, img3]
95
        out = repo.bySubject('john')
96
        self.assertEqual([img1, img3], out)
97
98 View Code Duplication
    def test_byApproval(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
99
        from niprov.jsonfile import JsonFile
100
        repo = JsonFile(self.dependencies)
101
        img1 = self.imageWithProvenance({'approval':'y','a':'b'})
102
        img2 = self.imageWithProvenance({'approval':'x','a':'d'})
103
        img3 = self.imageWithProvenance({'approval':'x','a':'f'})
104
        repo.all = Mock()
105
        repo.all.return_value = [img1, img2, img3]
106
        out = repo.byApproval('x')
107
        self.assertEqual([img2, img3], out)
108
109
    def test_latest(self):
110
        from niprov.jsonfile import JsonFile
111
        repo = JsonFile(self.dependencies)
112
        img1 = self.imageWithProvenance({'added':datetime.datetime(1982, 1, 5)})
113
        img2 = self.imageWithProvenance({'added':datetime.datetime(1982, 2, 5)})
114
        img3 = self.imageWithProvenance({'added':datetime.datetime(1982, 3, 5)})
115
        img4 = self.imageWithProvenance({'added':datetime.datetime(1982, 4, 5)})
116
        img5 = self.imageWithProvenance({'added':datetime.datetime(1982, 5, 5)})
117
        repo.all = Mock()
118
        repo.all.return_value = [img1,img2,img3,img4,img5]
119
        out = repo.latest(3)
120
        self.assertEqual([img5, img4, img3], out)
121
122
    def test_stats(self):
123
        from niprov.jsonfile import JsonFile
124
        repo = JsonFile(self.dependencies)
125
        repo.all = Mock()
126
        records = [{},{},{},{},{},{},{},{},{},{},{}]
127
        totalsize = 0
128
        for r in records:
129
            r['size'] = random.randint(1,1000)
130
            totalsize += r['size']
131
        repo.all.return_value = [self.imageWithProvenance(r) for r in records]
132
        out = repo.statistics()
133
        self.assertEqual(11, out['count'])
134
        self.assertEqual(totalsize, out['totalsize'])
135
136
    def test_stats_transient_file(self):
137
        from niprov.jsonfile import JsonFile
138
        repo = JsonFile(self.dependencies)
139
        repo.all = Mock()
140
        records = [{},{},{},{},{},{},{},{},{},{},{}]
141
        totalsize = 0
142
        for r in records:
143
            r['size'] = random.randint(1,1000)
144
            totalsize += r['size']
145
        totalsize -= records[3]['size']
146
        del records[3]['size']
147
        repo.all.return_value = [self.imageWithProvenance(r) for r in records]
148
        out = repo.statistics()
149
        self.assertEqual(totalsize, out['totalsize'])
150
        
151 View Code Duplication
    def test_byId(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
152
        from niprov.jsonfile import JsonFile
153
        repo = JsonFile(self.dependencies)
154
        img1 = self.imageWithProvenance({'id':'1'})
155
        img2 = self.imageWithProvenance({'id':'2'})
156
        img3 = self.imageWithProvenance({'id':'3'})
157
        repo.all = Mock()
158
        repo.all.return_value = [img1, img2, img3]
159
        out = repo.byId('2')
160
        self.assertEqual(img2, out)
161
162
    def test_bySubject_doesnt_balk_if_no_subject_field(self):
163
        from niprov.jsonfile import JsonFile
164
        repo = JsonFile(self.dependencies)
165
        img1 = self.imageWithProvenance({'a':'b'})
166
        img2 = self.imageWithProvenance({'subject':'tim','a':'d'})
167
        img3 = self.imageWithProvenance({'subject':'john','a':'f'})
168
        repo.all = Mock()
169
        repo.all.return_value = [img1, img2, img3]
170
        out = repo.bySubject('john')
171
172
    def test_byLocations(self):
173
        self.fileFactory.fromProvenance.side_effect = lambda p: 'img_'+p['a']
174
        from niprov.jsonfile import JsonFile
175
        repo = JsonFile(self.dependencies)
176
        img1 = self.imageWithProvenance({'location':'i'})
177
        img2 = self.imageWithProvenance({'location':'j'})
178
        img3 = self.imageWithProvenance({'location':'m'})
179
        img4 = self.imageWithProvenance({'location':'f'})
180
        img5 = self.imageWithProvenance({'location':'x'})
181
        repo.all = Mock()
182
        repo.all.return_value = [img1,img2,img3,img4,img5]
183
        out = repo.byLocations(['j','f','k'])
184
        self.assertEqual([img2, img4], out)
185
186
    def test_byParents(self):
187
        self.fileFactory.fromProvenance.side_effect = lambda p: 'img_'+p['l']
188
        from niprov.jsonfile import JsonFile
189
        repo = JsonFile(self.dependencies)
190
        img1 = self.imageWithProvenance({'parents':[],'l':'a'})
191
        img2 = self.imageWithProvenance({'parents':['x','a'],'l':'b'})
192
        img3 = self.imageWithProvenance({'parents':['b'],'l':'b'})
193
        img4 = self.imageWithProvenance({'parents':['c','y'],'l':'d'})
194
        img5 = self.imageWithProvenance({'parents':['d'],'l':'e'})
195
        repo.all = Mock()
196
        repo.all.return_value = [img1, img2, img3, img4, img5]
197
        out = repo.byParents(['x','y'])
198
        self.assertEqual([img2, img4], out)
199
200
    def test_Will_tell_PictureCache_to_persist_known_Snapshot(self):
201
        from niprov.jsonfile import JsonFile
202
        repo = JsonFile(self.dependencies)
203
        img = self.imageWithProvenance({'location':'1','foo':'baz'})
204
        repo.add(img)
205
        self.pictureCache.saveToDisk.assert_called_with(for_=img)
206
207
    def test_Query(self):
208
        self.fileFactory.fromProvenance.side_effect = lambda p: 'img_'+p['l']
209
        from niprov.jsonfile import JsonFile
210
        repo = JsonFile(self.dependencies)
211
        img1 = self.imageWithProvenance({'a':'b'})
212
        img2 = self.imageWithProvenance({'color':'red','a':'d'})
213
        img3 = self.imageWithProvenance({'color':'blue','a':'f'})
214
        img4 = self.imageWithProvenance({'color':'red','a':'d'})
215
        repo.all = Mock()
216
        repo.all.return_value = [img1, img2, img3, img4]
217
        q = Mock()
218
        field1 = Mock()
219
        field1.name = 'color'
220
        field1.value = 'red'
221
        q.getFields.return_value = [field1]
222
        out = repo.inquire(q)
223
        self.assertEqual([img2, img4], out)
224
225