| Total Complexity | 41 |
| Total Lines | 303 |
| Duplicated Lines | 33.99 % |
| Changes | 9 | ||
| Bugs | 0 | Features | 0 |
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:
Complex classes like JsonFileTest often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | from mock import Mock |
||
| 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): |
|
|
|
|||
| 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): |
|
| 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): |
|
| 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): |
|
| 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_with_value_field(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 | field1.all = False |
||
| 190 | q.getFields.return_value = [field1] |
||
| 191 | out = repo.inquire(q) |
||
| 192 | self.assertEqual([img2, img4], out) |
||
| 193 | View Code Duplication | ||
| 194 | def test_Search_only_returns_objects_which_have_needle(self): |
||
| 195 | self.fileFactory.fromProvenance.side_effect = lambda p: 'img_'+p['l'] |
||
| 196 | from niprov.jsonfile import JsonFile |
||
| 197 | repo = JsonFile(self.dependencies) |
||
| 198 | img1 = self.imageWithProvenance({'transformation':'green blue'}) |
||
| 199 | img2 = self.imageWithProvenance({'transformation':'yellow red'}) |
||
| 200 | repo.all = Mock() |
||
| 201 | repo.all.return_value = [img1, img2] |
||
| 202 | out = repo.search('red') |
||
| 203 | self.assertEqual([img2], out) |
||
| 204 | View Code Duplication | ||
| 205 | def test_Search_sorts_results_by_number_of_matches(self): |
||
| 206 | self.fileFactory.fromProvenance.side_effect = lambda p: 'img_'+p['l'] |
||
| 207 | from niprov.jsonfile import JsonFile |
||
| 208 | repo = JsonFile(self.dependencies) |
||
| 209 | img1 = self.imageWithProvenance({'transformation':'red bluered'}) |
||
| 210 | img2 = self.imageWithProvenance({'transformation':'red and green'}) |
||
| 211 | img3 = self.imageWithProvenance({'transformation':'red pruple red red'}) |
||
| 212 | img4 = self.imageWithProvenance({'transformation':'nuthin'}) |
||
| 213 | repo.all = Mock() |
||
| 214 | repo.all.return_value = [img1, img2, img3, img4] |
||
| 215 | out = repo.search('red') |
||
| 216 | self.assertEqual([img3, img1, img2], out) |
||
| 217 | |||
| 218 | def test_Search_looks_through_multiple_fields(self): |
||
| 219 | self.fileFactory.fromProvenance.side_effect = lambda p: 'img_'+p['l'] |
||
| 220 | from niprov.jsonfile import JsonFile |
||
| 221 | repo = JsonFile(self.dependencies) |
||
| 222 | i1 = self.imageWithProvenance({'color':'red'}) |
||
| 223 | i2 = self.imageWithProvenance({'location':'redis'}) |
||
| 224 | i3 = self.imageWithProvenance({'user':'reddit'}) |
||
| 225 | i4 = self.imageWithProvenance({'subject':'red bastard'}) |
||
| 226 | i5 = self.imageWithProvenance({'protocol':'reddish'}) |
||
| 227 | i6 = self.imageWithProvenance({'transformation':'zoomed red'}) |
||
| 228 | i7 = self.imageWithProvenance({'technique':'redshift'}) |
||
| 229 | i8 = self.imageWithProvenance({'modality':'red'}) |
||
| 230 | repo.all = Mock() |
||
| 231 | repo.all.return_value = [i1,i2,i3,i4,i5,i6,i7,i8] |
||
| 232 | out = repo.search('red') |
||
| 233 | self.assertNotIn(i1, out) |
||
| 234 | self.assertIn(i2, out) |
||
| 235 | self.assertIn(i3, out) |
||
| 236 | self.assertIn(i4, out) |
||
| 237 | self.assertIn(i5, out) |
||
| 238 | self.assertIn(i6, out) |
||
| 239 | self.assertIn(i7, out) |
||
| 240 | self.assertIn(i8, out) |
||
| 241 | View Code Duplication | ||
| 242 | def test_Search_distinguishes_words_as_OR_search(self): |
||
| 243 | self.fileFactory.fromProvenance.side_effect = lambda p: 'img_'+p['l'] |
||
| 244 | from niprov.jsonfile import JsonFile |
||
| 245 | repo = JsonFile(self.dependencies) |
||
| 246 | i1 = self.imageWithProvenance({'transformation':'blue red red'}) #2 |
||
| 247 | i2 = self.imageWithProvenance({'transformation':'red blue green red'}) #3 |
||
| 248 | i3 = self.imageWithProvenance({'transformation':'green blue'}) #1 |
||
| 249 | repo.all = Mock() |
||
| 250 | repo.all.return_value = [i1,i2,i3] |
||
| 251 | out = repo.search('red green') |
||
| 252 | self.assertEqual([i2, i1, i3], out) |
||
| 253 | View Code Duplication | ||
| 254 | def test_Search_returns_max_20_results(self): |
||
| 255 | self.fileFactory.fromProvenance.side_effect = lambda p: 'img_'+p['l'] |
||
| 256 | from niprov.jsonfile import JsonFile |
||
| 257 | repo = JsonFile(self.dependencies) |
||
| 258 | recs = [] |
||
| 259 | for i in range(35): |
||
| 260 | recs.append(self.imageWithProvenance({'transformation':'red'})) |
||
| 261 | repo.all = Mock() |
||
| 262 | repo.all.return_value = recs |
||
| 263 | out = repo.search('red') |
||
| 264 | self.assertEqual(20, len(out)) |
||
| 265 | |||
| 266 | def test_Query_with_ALL_field(self): |
||
| 267 | self.fileFactory.fromProvenance.side_effect = lambda p: 'img_'+p['l'] |
||
| 268 | from niprov.jsonfile import JsonFile |
||
| 269 | repo = JsonFile(self.dependencies) |
||
| 270 | img1 = self.imageWithProvenance({'a':'b'}) |
||
| 271 | img2 = self.imageWithProvenance({'color':'red','a':'d'}) |
||
| 272 | img3 = self.imageWithProvenance({'color':'blue','a':'f'}) |
||
| 273 | img4 = self.imageWithProvenance({'color':'green','a':'d'}) |
||
| 274 | img5 = self.imageWithProvenance({'color':'blue','a':'g'}) |
||
| 275 | repo.all = Mock() |
||
| 276 | repo.all.return_value = [img1, img2, img3, img4, img5] |
||
| 277 | q = Mock() |
||
| 278 | field1 = Mock() |
||
| 279 | field1.name = 'color' |
||
| 280 | field1.all = True |
||
| 281 | q.getFields.return_value = [field1] |
||
| 282 | out = repo.inquire(q) |
||
| 283 | self.assertIn('red', out) |
||
| 284 | self.assertIn('green', out) |
||
| 285 | self.assertIn('blue', out) |
||
| 286 | self.assertEqual(3, len(out)) |
||
| 287 | |||
| 288 | View Code Duplication | def test_getSeries(self): |
|
| 289 | from niprov.jsonfile import JsonFile |
||
| 290 | repo = JsonFile(self.dependencies) |
||
| 291 | img = Mock() |
||
| 292 | img.getSeriesId.return_value = '2' |
||
| 293 | img1 = self.imageWithProvenance({'seriesuid':'1','path':'a'}) |
||
| 294 | img2 = self.imageWithProvenance({'seriesuid':'2','path':'b'}) |
||
| 295 | repo.all = Mock() |
||
| 296 | repo.all.return_value = [img1, img2] |
||
| 297 | out = repo.getSeries(img) |
||
| 298 | self.assertEqual(img2, out) |
||
| 299 | |||
| 300 | def test_getSeries_returns_None_right_away_if_no_series_id(self): |
||
| 301 | from niprov.jsonfile import JsonFile |
||
| 302 | repo = JsonFile(self.dependencies) |
||
| 303 | img = Mock() |
||
| 304 | img.getSeriesId.return_value = None |
||
| 305 | repo.all = Mock() |
||
| 306 | out = repo.getSeries(img) |
||
| 307 | assert not repo.all.called, "Should not be called if no series id" |
||
| 308 | self.assertEqual(None, out) |
||
| 309 | |||
| 310 |