Completed
Push — master ( b66745...9821e6 )
by Jasper
02:18 queued 58s
created

BaseFileTests.test_keepVersionsFromPrevious()   A

Complexity

Conditions 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
dl 0
loc 10
rs 9.4285
c 2
b 0
f 0
1
from tests.ditest import DependencyInjectionTestBase
2
from mock import Mock, sentinel
3
from datetime import datetime
4
5
class BaseFileTests(DependencyInjectionTestBase):
6
7
    def setUp(self):
8
        super(BaseFileTests, self).setUp()
9
        self.path = 'example.abc'
10
        self.location.toDictionary.return_value = {'path':self.path}
11
        from niprov.basefile import BaseFile
12
        self.constructor = BaseFile
13
        self.file = BaseFile(self.path, dependencies=self.dependencies)
14
15
    def test_Saves_file_path_along_with_provenance(self):
16
        out = self.file.inspect()
17
        self.assertEqual(out['path'], self.path)
18
19
    def test_Saves_filesize_along_with_provenance(self):
20
        out = self.file.inspect()
21
        self.assertEqual(out['size'], self.filesys.getsize(self.path))
22
23
    def test_Saves_file_creation_time_along_with_provenance(self):
24
        out = self.file.inspect()
25
        self.assertEqual(out['created'], self.filesys.getctime(self.path))
26
27
    def test_Asks_hasher_for_digest_of_file(self):
28
        out = self.file.inspect()
29
        self.assertEqual(out['hash'], self.hasher.digest(self.path))
30
31
    def test_Provenance_property_equals_dictionary_returned_by_inspect(self):
32
        out = self.file.inspect()
33
        self.assertEqual(out, self.file.provenance)
34
35
    def test_Attach_method(self):
36
        pass
37
38
    def test_Series_interface(self):
39
        self.assertEqual(self.file.getSeriesId(), None)
40
41
    def test_Construct_with_provenance(self):
42
        prov = {'aprop':'aval'}
43
        img = self.constructor(self.path, provenance=prov, dependencies=self.dependencies)
44
        self.assertEqual(prov, img.provenance)
45
46
    def test_Construct_with_provenance_adds_path_if_not_set(self):
47
        prov = {'aprop':'aval'}
48
        img = self.constructor(self.path, provenance=prov, dependencies=self.dependencies)
49
        self.assertIn('path', img.provenance)
50
        self.assertEqual(self.path, img.provenance['path'])
51
52
    def test_Inspect_leaves_existing_fields_updates_others(self):
53
        prov = {'aprop':'aval','size':9876}
54
        img = self.constructor(self.path, provenance=prov, 
55
            dependencies=self.dependencies)
56
        img.inspect()
57
        self.assertEqual(img.provenance['aprop'], 'aval')
58
        self.assertEqual(img.provenance['size'], self.filesys.getsize(self.path))
59
60
    def test_Uses_location_objects_to_add_location_info_to_provenance(self):
61
        self.location.toDictionary.return_value = {'newLocKey':'newLocValue',
62
            'path':'newPathVal'}
63
        img = self.constructor(self.path, dependencies=self.dependencies)
64
        self.locationFactory.fromString.assert_called_with(self.path)
65
        self.assertEqual('newLocValue', img.provenance['newLocKey'])
66
        self.assertEqual('newPathVal', img.provenance['path'])
67
        self.assertEqual(self.location, img.location)
68
69
    def test_parents_property(self):
70
        prov = {'aprop':'aval','size':9876}
71
        img = self.constructor(self.path, provenance=prov, 
72
            dependencies=self.dependencies)
73
        self.assertEqual(img.parents, [])
74
        prov = {'aprop':'aval','parents':['foo','bar']}
75
        img2 = self.constructor(self.path, provenance=prov, 
76
            dependencies=self.dependencies)
77
        self.assertEqual(img2.parents, ['foo','bar'])
78
79
    def test_getProvenance_provides_prov_serialized_in_requested_format(self):
80
        img = self.constructor(self.path, dependencies=self.dependencies)
81
        out = img.getProvenance('myformat')
82
        self.formatFactory.create.assert_called_with('myformat')
83
        self.format.serialize.assert_called_with(img)
84
        self.assertEqual(self.format.serialize(), out)
85
86
    def test_viewSnapshot_uses_ViewerMedium_and_PictureCache(self):
87
        viewer = Mock()
88
        self.mediumFactory.create.return_value = viewer
89
        self.pictureCache.getFilepath.return_value = 'snapshot filepath'
90
        img = self.constructor(self.path, dependencies=self.dependencies)
91
        img.viewSnapshot()
92
        self.pictureCache.getFilepath.assert_called_with(for_=img)
93
        viewer.export.assert_called_with('snapshot filepath')
94
95
    def test_getSnapshotFilepath(self):
96
        self.pictureCache.getFilepath.return_value = 'snapshot filepath'
97
        img = self.constructor(self.path, dependencies=self.dependencies)
98
        self.assertEqual(img.getSnapshotFilepath(), 'snapshot filepath')
99
100
    def test_Determines_modality(self):
101
        img = self.constructor(self.path, dependencies=self.dependencies)
102
        img.inspect()
103
        self.assertEqual(img.provenance['modality'], 'other')
104
105
    def test_Preserves_modality_if_inherited(self):
106
        img = self.constructor(self.path, dependencies=self.dependencies,
107
                                provenance={'modality':'magic'})
108
        img.inspect()
109
        self.assertEqual(img.provenance['modality'], 'magic')
110
111
    def test_On_construction_has_status_new(self):
112
        img = self.constructor(self.path, dependencies=self.dependencies)
113
        self.assertEqual(img.status, 'new')
114
115
    def test_keepVersionsFromPrevious(self):
116
        img = self.constructor(self.path, dependencies=self.dependencies)
117
        prev = Mock()
118
        prev.provenance = {'y':1501, '_versions':[{'y':1499},{'y':1500}]}
119
        img.keepVersionsFromPrevious(prev)
120
        self.assertEqual('new-version', img.status)
121
        self.assertEqual(3, len(img.provenance['_versions']))
122
        self.assertEqual({'y':1501}, img.provenance['_versions'][-1])
123
        self.assertEqual({'y':1500}, img.provenance['_versions'][-2])
124
        self.assertEqual({'y':1499}, img.provenance['_versions'][-3])
125
126
    def test_versions_property_access(self):
127
        img = self.constructor(self.path, dependencies=self.dependencies)
128
        img.provenance = {'_versions':sentinel.versions}
129
        self.assertEqual(img.provenance['_versions'], img.versions)
130