Completed
Push — master ( 6d6a7d...e4617d )
by Jasper
8s
created

JsonFileTest.test_byApproval()   A

Complexity

Conditions 1

Size

Total Lines 10

Duplication

Lines 10
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
dl 10
loc 10
rs 9.4285
c 0
b 0
f 0
1
from mock import Mock
2
from tests.ditest import DependencyInjectionTestBase
3
import datetime, random
4
5
6
class JsonFileTest(DependencyInjectionTestBase):
7
8
    def setUp(self):
9
        super(JsonFileTest, self).setUp()
10
        self.dependencies.getConfiguration().database_url = ''
11
12
    def imageWithProvenance(self, prov):
13
        img = Mock()
14
        img.provenance = prov
15
        if 'location' in prov:
16
            img.location.toString.return_value = prov['location']
17
        return img
18
19 View Code Duplication
    def test_Add(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
20
        from niprov.jsonfile import JsonFile
21
        repo = JsonFile(self.dependencies)
22
        img1 = self.imageWithProvenance({'location':'1','foo':'baz'})
23
        repo.all = Mock()
24
        repo.all.return_value = [img1]
25
        image = self.imageWithProvenance({'location': '2','foo':'bar'})
26
        repo.add(image)
27
        self.serializer.serializeList.assert_called_with(
28
            [img1, image])
29
        self.filesys.write.assert_called_with(repo.datafile, 
30
            self.serializer.serializeList())
31
32 View Code Duplication
    def test_Update(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
33
        from niprov.jsonfile import JsonFile
34
        repo = JsonFile(self.dependencies)
35
        img1 = self.imageWithProvenance({'location':'1','path':'a'})
36
        img2 = self.imageWithProvenance({'location':'2','path':'b'})
37
        repo.all = Mock()
38
        repo.all.return_value = [img1, img2]
39
        image = self.imageWithProvenance({'location': '2','foo':'bar'})
40
        repo.update(image)
41
        self.serializer.serializeList.assert_called_with(
42
            [img1,image])
43
        self.filesys.write.assert_called_with(repo.datafile, 
44
            self.serializer.serializeList())
45
46
    def test_knowsByLocation(self):
47
        from niprov.jsonfile import JsonFile
48
        repo = JsonFile(self.dependencies)
49
        repo.byLocation = Mock()
50
        self.assertTrue(repo.knowsByLocation(''))
51
        repo.byLocation.side_effect = IndexError
52
        self.assertFalse(repo.knowsByLocation(''))
53
54
    def test_byLocation(self):
55
        from niprov.jsonfile import JsonFile
56
        repo = JsonFile(self.dependencies)
57
        img1 = self.imageWithProvenance({'location':'1','path':'a'})
58
        img2 = self.imageWithProvenance({'location':'2','path':'b'})
59
        repo.all = Mock()
60
        repo.all.return_value = [img1, img2]
61
        out = repo.byLocation('2')
62
        self.assertEqual(img2, out)
63
64
    def test_byLocation_works_for_file_in_series(self):
65
        from niprov.jsonfile import JsonFile
66
        repo = JsonFile(self.dependencies)
67
        img1 = self.imageWithProvenance({'location':'1','path':'a'})
68
        img3 = self.imageWithProvenance({'location':'3','filesInSeries':['boo','bah']})
69
        repo.all = Mock()
70
        repo.all.return_value = [img1, img3]
71
        out = repo.byLocation('boo')
72
        self.assertEqual(img3, out)
73
74
    def test_updateApproval(self):
75
        from niprov.jsonfile import JsonFile
76
        repo = JsonFile(self.dependencies)
77
        img = Mock()
78
        img.provenance = {'fiz':'baf','approval':'notsure'}
79
        def assertion(img):
80
            self.assertEqual({'fiz':'baf','approval':'excellent!'}, img.provenance)
81
        repo.byLocation = Mock()
82
        repo.byLocation.return_value = img
83
        repo.update = Mock()
84
        repo.update.side_effect = assertion
85
        repo.updateApproval('/p/f1','excellent!')
86
87
    def test_latest(self):
88
        from niprov.jsonfile import JsonFile
89
        repo = JsonFile(self.dependencies)
90
        img1 = self.imageWithProvenance({'added':datetime.datetime(1982, 1, 5)})
91
        img2 = self.imageWithProvenance({'added':datetime.datetime(1982, 2, 5)})
92
        img3 = self.imageWithProvenance({'added':datetime.datetime(1982, 3, 5)})
93
        img4 = self.imageWithProvenance({'added':datetime.datetime(1982, 4, 5)})
94
        img5 = self.imageWithProvenance({'added':datetime.datetime(1982, 5, 5)})
95
        repo.all = Mock()
96
        repo.all.return_value = [img1,img2,img3,img4,img5]
97
        out = repo.latest(3)
98
        self.assertEqual([img5, img4, img3], out)
99
100
    def test_stats(self):
101
        from niprov.jsonfile import JsonFile
102
        repo = JsonFile(self.dependencies)
103
        repo.all = Mock()
104
        records = [{},{},{},{},{},{},{},{},{},{},{}]
105
        totalsize = 0
106
        for r in records:
107
            r['size'] = random.randint(1,1000)
108
            totalsize += r['size']
109
        repo.all.return_value = [self.imageWithProvenance(r) for r in records]
110
        out = repo.statistics()
111
        self.assertEqual(11, out['count'])
112
        self.assertEqual(totalsize, out['totalsize'])
113
114
    def test_stats_transient_file(self):
115
        from niprov.jsonfile import JsonFile
116
        repo = JsonFile(self.dependencies)
117
        repo.all = Mock()
118
        records = [{},{},{},{},{},{},{},{},{},{},{}]
119
        totalsize = 0
120
        for r in records:
121
            r['size'] = random.randint(1,1000)
122
            totalsize += r['size']
123
        totalsize -= records[3]['size']
124
        del records[3]['size']
125
        repo.all.return_value = [self.imageWithProvenance(r) for r in records]
126
        out = repo.statistics()
127
        self.assertEqual(totalsize, out['totalsize'])
128
        
129
    def test_byId(self):
130
        from niprov.jsonfile import JsonFile
131
        repo = JsonFile(self.dependencies)
132
        img1 = self.imageWithProvenance({'id':'1'})
133
        img2 = self.imageWithProvenance({'id':'2'})
134
        img3 = self.imageWithProvenance({'id':'3'})
135
        repo.all = Mock()
136
        repo.all.return_value = [img1, img2, img3]
137
        out = repo.byId('2')
138
        self.assertEqual(img2, out)
139
140
    def test_byLocations(self):
141
        self.fileFactory.fromProvenance.side_effect = lambda p: 'img_'+p['a']
142
        from niprov.jsonfile import JsonFile
143
        repo = JsonFile(self.dependencies)
144
        img1 = self.imageWithProvenance({'location':'i'})
145
        img2 = self.imageWithProvenance({'location':'j'})
146
        img3 = self.imageWithProvenance({'location':'m'})
147
        img4 = self.imageWithProvenance({'location':'f'})
148
        img5 = self.imageWithProvenance({'location':'x'})
149
        repo.all = Mock()
150
        repo.all.return_value = [img1,img2,img3,img4,img5]
151
        out = repo.byLocations(['j','f','k'])
152
        self.assertEqual([img2, img4], out)
153
154
    def test_byParents(self):
155
        self.fileFactory.fromProvenance.side_effect = lambda p: 'img_'+p['l']
156
        from niprov.jsonfile import JsonFile
157
        repo = JsonFile(self.dependencies)
158
        img1 = self.imageWithProvenance({'parents':[],'l':'a'})
159
        img2 = self.imageWithProvenance({'parents':['x','a'],'l':'b'})
160
        img3 = self.imageWithProvenance({'parents':['b'],'l':'b'})
161
        img4 = self.imageWithProvenance({'parents':['c','y'],'l':'d'})
162
        img5 = self.imageWithProvenance({'parents':['d'],'l':'e'})
163
        repo.all = Mock()
164
        repo.all.return_value = [img1, img2, img3, img4, img5]
165
        out = repo.byParents(['x','y'])
166
        self.assertEqual([img2, img4], out)
167
168
    def test_Will_tell_PictureCache_to_persist_known_Snapshot(self):
169
        from niprov.jsonfile import JsonFile
170
        repo = JsonFile(self.dependencies)
171
        img = self.imageWithProvenance({'location':'1','foo':'baz'})
172
        repo.add(img)
173
        self.pictureCache.saveToDisk.assert_called_with(for_=img)
174
175
    def test_Query(self):
176
        self.fileFactory.fromProvenance.side_effect = lambda p: 'img_'+p['l']
177
        from niprov.jsonfile import JsonFile
178
        repo = JsonFile(self.dependencies)
179
        img1 = self.imageWithProvenance({'a':'b'})
180
        img2 = self.imageWithProvenance({'color':'red','a':'d'})
181
        img3 = self.imageWithProvenance({'color':'blue','a':'f'})
182
        img4 = self.imageWithProvenance({'color':'red','a':'d'})
183
        repo.all = Mock()
184
        repo.all.return_value = [img1, img2, img3, img4]
185
        q = Mock()
186
        field1 = Mock()
187
        field1.name = 'color'
188
        field1.value = 'red'
189
        q.getFields.return_value = [field1]
190
        out = repo.inquire(q)
191
        self.assertEqual([img2, img4], out)
192
193