Completed
Push — master ( 1ed88c...6b2da2 )
by Jasper
7s
created

ContextApiTests.test_Record_with_user()   A

Complexity

Conditions 1

Size

Total Lines 9

Duplication

Lines 9
Ratio 100 %
Metric Value
cc 1
dl 9
loc 9
rs 9.6666
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