Completed
Pull Request — master (#112)
by Jasper
01:12
created

ContextApiTests   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 84
Duplicated Lines 22.62 %
Metric Value
dl 19
loc 84
rs 10
wmc 13

9 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 9 2
A touch() 0 3 2
A tearDown() 0 2 1
A test_Approval() 0 7 1
A test_Export_Import() 0 11 2
A test_Comparison() 0 11 2
A test_Log() 10 10 1
A test_Record_with_user() 9 9 1
A test_Attach_provenance_string_in_file_based_on_config() 0 12 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
from os.path import abspath
4
5
6
class ContextApiTests(unittest.TestCase):
7
8
    def setUp(self):
9
        self.dbpath = os.path.expanduser(os.path.join('~','provenance_test.json'))
10
        if os.path.exists(self.dbpath):
11
            os.remove(self.dbpath)
12
        os.mkdir('temp')
13
        from niprov import Context
14
        self.provenance = Context()
15
        self.provenance.config.database_type = 'file'
16
        self.provenance.config.database_url = self.dbpath
17
18
    def tearDown(self):
19
        shutil.rmtree('temp')
20
21
    def touch(self, path):
22
        with open(path,'w') as tempfile:
23
            tempfile.write('0')
24
25 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...
26
        self.provenance.discover('testdata')
27
        newfile = 'temp/smoothed.test'
28
        self.touch(newfile)
29
        parent = os.path.abspath('testdata/eeg/stub.cnt')
30
        self.provenance.log(newfile, 'test', parent)
31
        testfpath = os.path.abspath(newfile)
32
        img = self.provenance.get(forFile=testfpath)
33
        self.assertEqual(img.provenance['subject'], 'Jane Doe')
34
        self.assertEqual(img.provenance['size'], os.path.getsize(newfile))
35
36 View Code Duplication
    def test_Record_with_user(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
37
        self.provenance.discover('testdata')
38
        newfile = 'temp/recorded.test'
39
        self.touch(newfile)
40
        parent = os.path.abspath('testdata/eeg/stub.cnt')
41
        self.provenance.record('echo hallo', newfile, parent, user='007')
42
        testfpath = os.path.abspath(newfile)
43
        img = self.provenance.get(forFile=testfpath)
44
        self.assertEqual(img.provenance['user'], 007)
45
46
    def test_Export_Import(self):
47
        from niprov.exceptions import UnknownFileError
48
        self.provenance.discover('testdata')
49
        discoveredFile = os.path.abspath('testdata/eeg/stub.cnt')
50
        self.assertIsNotNone(self.provenance.get(forFile=discoveredFile))
51
        backupFilepath = self.provenance.backup()
52
        os.remove(self.dbpath) # get rid of existing data.
53
        with self.assertRaises(UnknownFileError):
54
            self.provenance.get(forFile=discoveredFile)
55
        self.provenance.importp(backupFilepath)
56
        self.assertIsNotNone(self.provenance.get(forFile=discoveredFile))
57
58
    @unittest.skip("Doesn't work on Travis right now.")
59
    def test_Attach_provenance_string_in_file_based_on_config(self):
60
        import nibabel
61
        self.provenance.config.attach = True
62
        newfile = os.path.abspath('temp/fileX.nii.gz')
63
        shutil.copy('testdata/nifti/fieldmap.nii.gz', newfile)
64
        self.provenance.add(newfile)
65
        img = nibabel.load(newfile)
66
        self.assertEqual(img.get_header().extensions.count('comment'), 1)
67
        self.assertEqual(img.get_header().extensions[0].get_code(), 6)
68
        content = img.get_header().extensions[0].get_content()
69
        self.assertIn('"path": "{0}"'.format(newfile), content)
70
71
    def test_Approval(self):
72
        self.provenance.discover('testdata')
73
        self.provenance.markForApproval([os.path.abspath('testdata/dicom/T1.dcm'),
74
                                os.path.abspath('testdata/dicom/T2.dcm')])
75
        imgs = self.provenance.markedForApproval()
76
        self.provenance.approve(os.path.abspath('testdata/dicom/T1.dcm'))
77
        imgs = self.provenance.markedForApproval()
78
79
    def test_Comparison(self):
80
        # Given two PARREC images' provenance records
81
        par1 = self.provenance.add(abspath('testdata/parrec/T1.PAR'))[0]
82
        par2 = self.provenance.add(abspath('testdata/parrec/T2.PAR'))[0]
83
        # Comparing them returns a Diff object with methods testing equality
84
        self.assertFalse(self.provenance.compare(par1, par2).areEqual())
85
        # Compare() can also be called as a method on the objects themselves,
86
        # and the Diff object has assert..() methods that raise AssertionErrors
87
        msgRegExp = ".*echo-time.*2\.08.*80\.0.*"
88
        with self.assertRaisesRegexp(AssertionError, msgRegExp):
89
            par1.compare(par2).assertEqualProtocol()
90
91
92
if __name__ == '__main__':
93
    unittest.main()
94
95