Completed
Push — master ( edb1ff...6d0d49 )
by Jasper
9s
created

ContextApiTests.test_Relative_paths()   A

Complexity

Conditions 1

Size

Total Lines 6

Duplication

Lines 6
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
dl 6
loc 6
rs 9.4285
c 1
b 0
f 0
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_Relative_paths(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
        self.provenance.log(newfile, 'test', 'testdata/eeg/stub.cnt')
30
        img = self.provenance.get(forFile=newfile)
31
32
    def test_Log(self):
33
        self.provenance.discover('testdata')
34
        newfile = 'temp/smoothed.test'
35
        self.touch(newfile)
36 View Code Duplication
        parent = os.path.abspath('testdata/eeg/stub.cnt')
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
37
        self.provenance.log(newfile, 'test', parent)
38
        testfpath = os.path.abspath(newfile)
39
        img = self.provenance.get(forFile=testfpath)
40
        self.assertEqual(img.provenance['subject'], 'Jane Doe')
41
        self.assertEqual(img.provenance['size'], os.path.getsize(newfile))
42
43
    def test_Record_with_user(self):
44
        self.provenance.discover('testdata')
45
        newfile = 'temp/recorded.test'
46
        self.touch(newfile)
47
        parent = os.path.abspath('testdata/eeg/stub.cnt')
48
        self.provenance.record('echo hallo', newfile, parent, user='007')
49
        testfpath = os.path.abspath(newfile)
50
        img = self.provenance.get(forFile=testfpath)
51
        self.assertEqual(img.provenance['user'], '007')
52
53
    def test_Export_Import(self):
54
        from niprov.exceptions import UnknownFileError
55
        self.provenance.discover('testdata')
56
        discoveredFile = os.path.abspath('testdata/eeg/stub.cnt')
57
        self.assertIsNotNone(self.provenance.get(forFile=discoveredFile))
58
        backupFilepath = self.provenance.backup()
59
        os.remove(self.dbpath) # get rid of existing data.
60
        with self.assertRaises(UnknownFileError):
61
            self.provenance.get(forFile=discoveredFile)
62
        self.provenance.importp(backupFilepath)
63
        self.assertIsNotNone(self.provenance.get(forFile=discoveredFile))
64
65
    @unittest.skip("Doesn't work on Travis right now.")
66
    def test_Attach_provenance_string_in_file_based_on_config(self):
67
        import nibabel
68
        self.provenance.config.attach = True
69
        newfile = os.path.abspath('temp/fileX.nii.gz')
70
        shutil.copy('testdata/nifti/fieldmap.nii.gz', newfile)
71
        self.provenance.add(newfile)
72
        img = nibabel.load(newfile)
73
        self.assertEqual(img.get_header().extensions.count('comment'), 1)
74
        self.assertEqual(img.get_header().extensions[0].get_code(), 6)
75
        content = img.get_header().extensions[0].get_content()
76
        self.assertIn('"path": "{0}"'.format(newfile), content)
77
78
    def test_Approval(self):
79
        self.provenance.discover('testdata')
80
        self.provenance.markForApproval([os.path.abspath('testdata/dicom/T1.dcm'),
81
                                os.path.abspath('testdata/dicom/T2.dcm')])
82
        imgs = self.provenance.markedForApproval()
83
        self.provenance.approve(os.path.abspath('testdata/dicom/T1.dcm'))
84
        imgs = self.provenance.markedForApproval()
85
86
    def test_Comparison(self):
87
        # Given two PARREC images' provenance records
88
        par1 = self.provenance.add(abspath('testdata/parrec/T1.PAR'))[0]
89
        par2 = self.provenance.add(abspath('testdata/parrec/T2.PAR'))[0]
90
        # Comparing them returns a Diff object with methods testing equality
91
        self.assertFalse(self.provenance.compare(par1, par2).areEqual())
92
        # Compare() can also be called as a method on the objects themselves,
93
        # and the Diff object has assert..() methods that raise AssertionErrors
94
        msgRegExp = ".*echo-time.*2\.08.*80\.0.*"
95
        with self.assertRaisesRegexp(AssertionError, msgRegExp):
96
            par1.compare(par2).assertEqualProtocol()
97
98
99
if __name__ == '__main__':
100
    unittest.main()
101
102