Completed
Pull Request — master (#118)
by Jasper
01:09
created

acceptance.ContextApiTests   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 74
Duplicated Lines 0 %
Metric Value
dl 0
loc 74
rs 10
wmc 12

8 Methods

Rating   Name   Duplication   Size   Complexity  
A test_Approval() 0 7 1
A tearDown() 0 2 1
A test_Attach_provenance_string_in_file_based_on_config() 0 12 1
A test_Log() 0 10 1
A test_Export_Import() 0 11 2
A touch() 0 3 2
A test_Comparison() 0 11 2
A setUp() 0 9 2
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
    def test_Log(self):
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
    def test_Export_Import(self):
37
        from niprov.exceptions import UnknownFileError
38
        self.provenance.discover('testdata')
39
        discoveredFile = os.path.abspath('testdata/eeg/stub.cnt')
40
        self.assertIsNotNone(self.provenance.get(forFile=discoveredFile))
41
        backupFilepath = self.provenance.backup()
42
        os.remove(self.dbpath) # get rid of existing data.
43
        with self.assertRaises(UnknownFileError):
44
            self.provenance.get(forFile=discoveredFile)
45
        self.provenance.importp(backupFilepath)
46
        self.assertIsNotNone(self.provenance.get(forFile=discoveredFile))
47
48
    @unittest.skip("Doesn't work on Travis right now.")
49
    def test_Attach_provenance_string_in_file_based_on_config(self):
50
        import nibabel
51
        self.provenance.config.attach = True
52
        newfile = os.path.abspath('temp/fileX.nii.gz')
53
        shutil.copy('testdata/nifti/fieldmap.nii.gz', newfile)
54
        self.provenance.add(newfile)
55
        img = nibabel.load(newfile)
56
        self.assertEqual(img.get_header().extensions.count('comment'), 1)
57
        self.assertEqual(img.get_header().extensions[0].get_code(), 6)
58
        content = img.get_header().extensions[0].get_content()
59
        self.assertIn('"path": "{0}"'.format(newfile), content)
60
61
    def test_Approval(self):
62
        self.provenance.discover('testdata')
63
        self.provenance.markForApproval([os.path.abspath('testdata/dicom/T1.dcm'),
64
                                os.path.abspath('testdata/dicom/T2.dcm')])
65
        imgs = self.provenance.markedForApproval()
66
        self.provenance.approve(os.path.abspath('testdata/dicom/T1.dcm'))
67
        imgs = self.provenance.markedForApproval()
68
69
    def test_Comparison(self):
70
        # Given two PARREC images' provenance records
71
        par1 = self.provenance.add(abspath('testdata/parrec/T1.PAR'))[0]
72
        par2 = self.provenance.add(abspath('testdata/parrec/T2.PAR'))[0]
73
        # Comparing them returns a Diff object with methods testing equality
74
        self.assertFalse(self.provenance.compare(par1, par2).areEqual())
75
        # Compare() can also be called as a method on the objects themselves,
76
        # and the Diff object has assert..() methods that raise AssertionErrors
77
        msgRegExp = ".*echo-time.*2\.08.*80\.0.*"
78
        with self.assertRaisesRegexp(AssertionError, msgRegExp):
79
            par1.compare(par2).assertEqualProtocol()
80
81
82
if __name__ == '__main__':
83
    unittest.main()
84
85