| Total Complexity | 19 |
| Total Lines | 89 |
| Duplicated Lines | 17.98 % |
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 | import unittest |
||
| 5 | class ApiTests(unittest.TestCase): |
||
| 6 | |||
| 7 | def setUp(self): |
||
| 8 | self.dbpath = os.path.expanduser(os.path.join('~','provenance.json')) |
||
| 9 | if os.path.exists(self.dbpath): |
||
| 10 | shutil.move(self.dbpath, self.dbpath.replace('.json','.backup.json')) |
||
| 11 | os.mkdir('temp') |
||
| 12 | |||
| 13 | def tearDown(self): |
||
| 14 | if os.path.exists(self.dbpath): |
||
| 15 | shutil.move(self.dbpath, self.dbpath.replace('.json','.test.json')) |
||
| 16 | shutil.rmtree('temp') |
||
| 17 | |||
| 18 | View Code Duplication | def test_Discover(self): |
|
|
|
|||
| 19 | import niprov |
||
| 20 | niprov.discover('testdata') |
||
| 21 | img = niprov.get(forFile=os.path.abspath('testdata/dicom/T1.dcm')) |
||
| 22 | self.assertEqual(img.provenance['dimensions'], [80, 80, 10]) |
||
| 23 | img = niprov.get(forFile=os.path.abspath('testdata/eeg/stub.cnt')) |
||
| 24 | self.assertEqual(img.provenance['subject'], 'Jane Doe') |
||
| 25 | |||
| 26 | def test_Export_terminal(self): |
||
| 27 | import niprov |
||
| 28 | niprov.discover('testdata') |
||
| 29 | niprov.print_() |
||
| 30 | niprov.print_(forFile=os.path.abspath('testdata/dicom/T1.dcm')) |
||
| 31 | |||
| 32 | View Code Duplication | def test_Log(self): |
|
| 33 | import niprov |
||
| 34 | niprov.discover('testdata') |
||
| 35 | newfile = os.path.abspath('temp/smoothed.test') |
||
| 36 | self.touch(newfile) |
||
| 37 | niprov.log(newfile, 'test', os.path.abspath('testdata/eeg/stub.cnt')) |
||
| 38 | img = niprov.get(forFile=newfile) |
||
| 39 | self.assertEqual(img.provenance['subject'], 'Jane Doe') |
||
| 40 | self.assertEqual(img.provenance['size'], os.path.getsize(newfile)) |
||
| 41 | |||
| 42 | def test_Narrative_file(self): |
||
| 43 | import niprov |
||
| 44 | niprov.discover('testdata') |
||
| 45 | text = niprov.export('direct','narrated', |
||
| 46 | forFile=os.path.abspath('testdata/dicom/T1.dcm')) |
||
| 47 | self.assertEqual(text, ("This is a T1 image. It was recorded August 5, " |
||
| 48 | "2014. The participant's name is 05aug14test. It is 158KB in size. ")) |
||
| 49 | |||
| 50 | # def test_Narrative_pipeline(self): |
||
| 51 | # import niprov |
||
| 52 | # niprov.discover('testdata') |
||
| 53 | # self.touch('temp/smoothed.test') |
||
| 54 | # niprov.log('temp/smoothed.test','spatial smoothing', |
||
| 55 | # 'testdata/parrec/T2.PAR') |
||
| 56 | # self.touch('temp/stats.test') |
||
| 57 | # niprov.log('temp/stats.test','t-test','temp/smoothed.test') |
||
| 58 | # text = niprov.export(format='narrative', forPipeline='temp/stats.test') |
||
| 59 | # self.assertEqual(text, ("A T2 image was recorded. A spatial smoothing " |
||
| 60 | # "was then applied. A t-test was then applied.")) |
||
| 61 | |||
| 62 | def test_Rename(self): |
||
| 63 | try: |
||
| 64 | import niprov |
||
| 65 | files, directory = self.createExtensionlessFiles() |
||
| 66 | niprov.renameDicoms(directory) |
||
| 67 | assert not os.path.isfile(files[0]) |
||
| 68 | assert os.path.isfile(files[0]+'.dcm') |
||
| 69 | finally: |
||
| 70 | self.clearDicomfiles() |
||
| 71 | |||
| 72 | def test_bySubject(self): |
||
| 73 | import niprov |
||
| 74 | niprov.discover('testdata') |
||
| 75 | niprov.print_(forSubject='05aug14test') |
||
| 76 | |||
| 77 | def createExtensionlessFiles(self): |
||
| 78 | if not os.path.exists('dicomdir'): |
||
| 79 | os.makedirs('dicomdir') |
||
| 80 | files = [] |
||
| 81 | for n in range(5): |
||
| 82 | f = os.path.join('dicomdir',str(n)) |
||
| 83 | files.append(f) |
||
| 84 | with open(f,'w') as fhandle: |
||
| 85 | fhandle.write('x') |
||
| 86 | return files, 'dicomdir' |
||
| 87 | |||
| 88 | def clearDicomfiles(self): |
||
| 89 | shutil.rmtree('dicomdir') |
||
| 90 | |||
| 91 | def touch(self, path): |
||
| 92 | with open(path,'w') as tempfile: |
||
| 93 | tempfile.write('0') |
||
| 94 | |||
| 96 |