Completed
Push — master ( e4617d...4b6b67 )
by Jasper
10s
created

JsonFileTest   B

Complexity

Total Complexity 36

Size/Duplication

Total Lines 258
Duplicated Lines 35.66 %

Importance

Changes 6
Bugs 0 Features 0
Metric Value
dl 92
loc 258
rs 8.8
c 6
b 0
f 0
wmc 36

22 Methods

Rating   Name   Duplication   Size   Complexity  
A imageWithProvenance() 0 6 2
A test_byLocation() 0 9 1
A test_knowsByLocation() 0 7 1
A test_Add() 12 12 1
A test_byLocation_works_for_file_in_series() 0 9 1
A assertion() 0 2 1
A setUp() 0 3 1
A test_Update() 13 13 1
A test_updateApproval() 0 12 2
A test_Will_tell_PictureCache_to_persist_known_Snapshot() 0 6 1
A test_stats() 0 13 3
A test_Query() 0 17 2
A test_byParents() 0 13 2
A test_latest() 0 12 1
A test_stats_transient_file() 0 14 3
A test_byLocations() 13 13 2
A test_byId() 10 10 1
A test_Search_looks_through_multiple_fields() 0 23 2
A test_Search_sorts_results_by_number_of_matches() 12 12 2
A test_Search_only_returns_objects_which_have_needle() 10 10 2
A test_Search_distinguishes_words_as_OR_search() 11 11 2
A test_Search_returns_max_20_results() 11 11 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
    def test_latest(self):
88
        from niprov.jsonfile import JsonFile
89
        repo = JsonFile(self.dependencies)
90
        img1 = self.imageWithProvenance({'added':datetime.datetime(1982, 1, 5)})
91
        img2 = self.imageWithProvenance({'added':datetime.datetime(1982, 2, 5)})
92
        img3 = self.imageWithProvenance({'added':datetime.datetime(1982, 3, 5)})
93
        img4 = self.imageWithProvenance({'added':datetime.datetime(1982, 4, 5)})
94
        img5 = self.imageWithProvenance({'added':datetime.datetime(1982, 5, 5)})
95
        repo.all = Mock()
96
        repo.all.return_value = [img1,img2,img3,img4,img5]
97
        out = repo.latest(3)
98
        self.assertEqual([img5, img4, img3], out)
99
100
    def test_stats(self):
101
        from niprov.jsonfile import JsonFile
102
        repo = JsonFile(self.dependencies)
103
        repo.all = Mock()
104
        records = [{},{},{},{},{},{},{},{},{},{},{}]
105
        totalsize = 0
106
        for r in records:
107
            r['size'] = random.randint(1,1000)
108
            totalsize += r['size']
109
        repo.all.return_value = [self.imageWithProvenance(r) for r in records]
110
        out = repo.statistics()
111
        self.assertEqual(11, out['count'])
112
        self.assertEqual(totalsize, out['totalsize'])
113
114
    def test_stats_transient_file(self):
115
        from niprov.jsonfile import JsonFile
116
        repo = JsonFile(self.dependencies)
117
        repo.all = Mock()
118
        records = [{},{},{},{},{},{},{},{},{},{},{}]
119
        totalsize = 0
120
        for r in records:
121
            r['size'] = random.randint(1,1000)
122
            totalsize += r['size']
123
        totalsize -= records[3]['size']
124
        del records[3]['size']
125
        repo.all.return_value = [self.imageWithProvenance(r) for r in records]
126
        out = repo.statistics()
127
        self.assertEqual(totalsize, out['totalsize'])
128
        
129 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...
130
        from niprov.jsonfile import JsonFile
131
        repo = JsonFile(self.dependencies)
132
        img1 = self.imageWithProvenance({'id':'1'})
133
        img2 = self.imageWithProvenance({'id':'2'})
134
        img3 = self.imageWithProvenance({'id':'3'})
135
        repo.all = Mock()
136
        repo.all.return_value = [img1, img2, img3]
137
        out = repo.byId('2')
138
        self.assertEqual(img2, out)
139
140 View Code Duplication
    def test_byLocations(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
141
        self.fileFactory.fromProvenance.side_effect = lambda p: 'img_'+p['a']
142
        from niprov.jsonfile import JsonFile
143
        repo = JsonFile(self.dependencies)
144
        img1 = self.imageWithProvenance({'location':'i'})
145
        img2 = self.imageWithProvenance({'location':'j'})
146
        img3 = self.imageWithProvenance({'location':'m'})
147
        img4 = self.imageWithProvenance({'location':'f'})
148
        img5 = self.imageWithProvenance({'location':'x'})
149
        repo.all = Mock()
150
        repo.all.return_value = [img1,img2,img3,img4,img5]
151
        out = repo.byLocations(['j','f','k'])
152
        self.assertEqual([img2, img4], out)
153
154
    def test_byParents(self):
155
        self.fileFactory.fromProvenance.side_effect = lambda p: 'img_'+p['l']
156
        from niprov.jsonfile import JsonFile
157
        repo = JsonFile(self.dependencies)
158
        img1 = self.imageWithProvenance({'parents':[],'l':'a'})
159
        img2 = self.imageWithProvenance({'parents':['x','a'],'l':'b'})
160
        img3 = self.imageWithProvenance({'parents':['b'],'l':'b'})
161
        img4 = self.imageWithProvenance({'parents':['c','y'],'l':'d'})
162
        img5 = self.imageWithProvenance({'parents':['d'],'l':'e'})
163
        repo.all = Mock()
164
        repo.all.return_value = [img1, img2, img3, img4, img5]
165
        out = repo.byParents(['x','y'])
166
        self.assertEqual([img2, img4], out)
167
168
    def test_Will_tell_PictureCache_to_persist_known_Snapshot(self):
169
        from niprov.jsonfile import JsonFile
170
        repo = JsonFile(self.dependencies)
171
        img = self.imageWithProvenance({'location':'1','foo':'baz'})
172
        repo.add(img)
173
        self.pictureCache.saveToDisk.assert_called_with(for_=img)
174
175
    def test_Query(self):
176
        self.fileFactory.fromProvenance.side_effect = lambda p: 'img_'+p['l']
177
        from niprov.jsonfile import JsonFile
178
        repo = JsonFile(self.dependencies)
179
        img1 = self.imageWithProvenance({'a':'b'})
180
        img2 = self.imageWithProvenance({'color':'red','a':'d'})
181
        img3 = self.imageWithProvenance({'color':'blue','a':'f'})
182
        img4 = self.imageWithProvenance({'color':'red','a':'d'})
183
        repo.all = Mock()
184
        repo.all.return_value = [img1, img2, img3, img4]
185
        q = Mock()
186
        field1 = Mock()
187
        field1.name = 'color'
188
        field1.value = 'red'
189
        q.getFields.return_value = [field1]
190
        out = repo.inquire(q)
191
        self.assertEqual([img2, img4], out)
192
193 View Code Duplication
    def test_Search_only_returns_objects_which_have_needle(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
194
        self.fileFactory.fromProvenance.side_effect = lambda p: 'img_'+p['l']
195
        from niprov.jsonfile import JsonFile
196
        repo = JsonFile(self.dependencies)
197
        img1 = self.imageWithProvenance({'transformation':'green blue'})
198
        img2 = self.imageWithProvenance({'transformation':'yellow red'})
199
        repo.all = Mock()
200
        repo.all.return_value = [img1, img2]
201
        out = repo.search('red')
202
        self.assertEqual([img2], out)
203
204 View Code Duplication
    def test_Search_sorts_results_by_number_of_matches(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
205
        self.fileFactory.fromProvenance.side_effect = lambda p: 'img_'+p['l']
206
        from niprov.jsonfile import JsonFile
207
        repo = JsonFile(self.dependencies)
208
        img1 = self.imageWithProvenance({'transformation':'red bluered'})
209
        img2 = self.imageWithProvenance({'transformation':'red and green'})
210
        img3 = self.imageWithProvenance({'transformation':'red pruple red red'})
211
        img4 = self.imageWithProvenance({'transformation':'nuthin'})
212
        repo.all = Mock()
213
        repo.all.return_value = [img1, img2, img3, img4]
214
        out = repo.search('red')
215
        self.assertEqual([img3, img1, img2], out)
216
217
    def test_Search_looks_through_multiple_fields(self):
218
        self.fileFactory.fromProvenance.side_effect = lambda p: 'img_'+p['l']
219
        from niprov.jsonfile import JsonFile
220
        repo = JsonFile(self.dependencies)
221
        i1 = self.imageWithProvenance({'color':'red'})
222
        i2 = self.imageWithProvenance({'location':'redis'})
223
        i3 = self.imageWithProvenance({'user':'reddit'})
224
        i4 = self.imageWithProvenance({'subject':'red bastard'})
225
        i5 = self.imageWithProvenance({'protocol':'reddish'})
226
        i6 = self.imageWithProvenance({'transformation':'zoomed red'})
227
        i7 = self.imageWithProvenance({'technique':'redshift'})
228
        i8 = self.imageWithProvenance({'modality':'red'})
229
        repo.all = Mock()
230
        repo.all.return_value = [i1,i2,i3,i4,i5,i6,i7,i8]
231
        out = repo.search('red')
232
        self.assertNotIn(i1, out)
233
        self.assertIn(i2, out)
234
        self.assertIn(i3, out)
235
        self.assertIn(i4, out)
236
        self.assertIn(i5, out)
237
        self.assertIn(i6, out)
238
        self.assertIn(i7, out)
239
        self.assertIn(i8, out)
240
241 View Code Duplication
    def test_Search_distinguishes_words_as_OR_search(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
242
        self.fileFactory.fromProvenance.side_effect = lambda p: 'img_'+p['l']
243
        from niprov.jsonfile import JsonFile
244
        repo = JsonFile(self.dependencies)
245
        i1 = self.imageWithProvenance({'transformation':'blue red red'})       #2
246
        i2 = self.imageWithProvenance({'transformation':'red blue green red'}) #3
247
        i3 = self.imageWithProvenance({'transformation':'green blue'})         #1
248
        repo.all = Mock()
249
        repo.all.return_value = [i1,i2,i3]
250
        out = repo.search('red green')
251
        self.assertEqual([i2, i1, i3], out)
252
253 View Code Duplication
    def test_Search_returns_max_20_results(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
254
        self.fileFactory.fromProvenance.side_effect = lambda p: 'img_'+p['l']
255
        from niprov.jsonfile import JsonFile
256
        repo = JsonFile(self.dependencies)
257
        recs = []
258
        for i in range(35):
259
            recs.append(self.imageWithProvenance({'transformation':'red'}))
260
        repo.all = Mock()
261
        repo.all.return_value = recs
262
        out = repo.search('red')
263
        self.assertEqual(20, len(out))
264
265