ApiTests   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 92
Duplicated Lines 9.78 %

Importance

Changes 0
Metric Value
dl 9
loc 92
rs 10
c 0
b 0
f 0
wmc 19

11 Methods

Rating   Name   Duplication   Size   Complexity  
A tearDown() 0 4 2
A setUp() 0 5 2
A test_Narrative_file() 0 7 1
A createExtensionlessFiles() 0 10 4
A test_Log() 9 9 1
A clearDicomfiles() 0 2 1
A test_Export_terminal() 0 6 1
A touch() 0 3 2
A test_bySubject() 0 5 1
A test_Rename() 0 9 3
A test_Discover() 0 7 1

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
import unittest
2
import os, shutil
3
4
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
    def test_Discover(self):
19
        import niprov
20
        niprov.discover('testdata')
21
        img = niprov.ProvenanceContext().get().byLocation(os.path.abspath('testdata/dicom/T1.dcm'))
22
        self.assertEqual(img.provenance['dimensions'], [80, 80, 10])
23
        img = niprov.ProvenanceContext().get().byLocation(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
        pth = os.path.abspath('testdata/dicom/T1.dcm')
30
        provenance = niprov.ProvenanceContext().get().byLocation(pth)
31
        niprov.print_(provenance)
32
33 View Code Duplication
    def test_Log(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
34
        import niprov
35
        niprov.discover('testdata')
36
        newfile = os.path.abspath('temp/smoothed.test')
37
        self.touch(newfile)
38
        niprov.log(newfile, 'test', os.path.abspath('testdata/eeg/stub.cnt'))
39
        img = niprov.ProvenanceContext().get().byLocation(newfile)
40
        self.assertEqual(img.provenance['subject'], 'Jane Doe')
41
        self.assertEqual(img.provenance['size'], os.path.getsize(newfile))
42
43
    def test_Narrative_file(self):
44
        import niprov
45
        niprov.discover('testdata')
46
        pth = os.path.abspath('testdata/dicom/T1.dcm')
47
        provenance = niprov.ProvenanceContext().get().byLocation(pth)
48
        text = niprov.export(provenance, 'direct','narrated')
49
        self.assertEqual(text, ("This is a T1 image. It was recorded August 5, " 
50
            "2014. The participant's name is 05aug14test. It is 158KB in size. "))
51
52
#    def test_Narrative_pipeline(self):
53
#        import niprov
54
#        niprov.discover('testdata')
55
#        self.touch('temp/smoothed.test')
56
#        niprov.log('temp/smoothed.test','spatial smoothing',
57
#            'testdata/parrec/T2.PAR')
58
#        self.touch('temp/stats.test')
59
#        niprov.log('temp/stats.test','t-test','temp/smoothed.test')
60
#        text = niprov.export(format='narrative', forPipeline='temp/stats.test')
61
#        self.assertEqual(text, ("A T2 image was recorded. A spatial smoothing "
62
#            "was then applied. A t-test was then applied."))
63
64
    def test_Rename(self):
65
        try:
66
            import niprov
67
            files, directory = self.createExtensionlessFiles()
68
            niprov.renameDicoms(directory)
69
            assert not os.path.isfile(files[0])
70
            assert os.path.isfile(files[0]+'.dcm')
71
        finally:
72
            self.clearDicomfiles()
73
74
    def test_bySubject(self):
75
        import niprov
76
        niprov.discover('testdata')
77
        provenance = niprov.ProvenanceContext().get().bySubject('05aug14test')
78
        niprov.print_(provenance)
79
80
    def createExtensionlessFiles(self):
81
        if not os.path.exists('dicomdir'):
82
            os.makedirs('dicomdir')
83
        files = []
84
        for n in range(5):
85
            f = os.path.join('dicomdir',str(n))
86
            files.append(f)
87
            with open(f,'w') as fhandle:
88
                fhandle.write('x')
89
        return files, 'dicomdir'
90
91
    def clearDicomfiles(self):
92
        shutil.rmtree('dicomdir')
93
94
    def touch(self, path):
95
        with open(path,'w') as tempfile:
96
            tempfile.write('0')
97
98
99