Completed
Pull Request — master (#125)
by Jasper
01:04
created

tests.MongoRepoTests.test_knowsByLocation()   A

Complexity

Conditions 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 1
dl 0
loc 8
rs 9.4285
1
import unittest
2
from mock import Mock, patch, sentinel
3
from datetime import timedelta
4
5
6
class MongoRepoTests(unittest.TestCase):
7
8
    def setUp(self):
9
        self.dependencies = Mock()
10
        self.config = Mock()
11
        self.factory = Mock()
12
        self.dependencies.getConfiguration.return_value = self.config
13
        self.dependencies.getFileFactory.return_value = self.factory
14
        self.db = Mock()
15
        self.db.provenance.find_one.return_value = {}
16
        self.db.provenance.find.return_value = {}
17
18
    def setupRepo(self):
19
        from niprov.mongo import MongoRepository
20
        with patch('niprov.mongo.pymongo') as pymongo:
21
            self.repo = MongoRepository(dependencies=self.dependencies)
22
        self.repo.db = self.db
23
24
    def test_Connection(self):
25
        from niprov.mongo import MongoRepository
26
        with patch('niprov.mongo.pymongo') as pymongo:
27
            repo = MongoRepository(dependencies=self.dependencies)
28
        pymongo.MongoClient.assert_called_with(self.config.database_url)
29
        self.assertEqual(pymongo.MongoClient().get_default_database(), repo.db)
30
31
    def test_knowsByLocation(self):
32
        self.setupRepo()
33
        p = '/p/f1'
34
        self.db.provenance.find_one.return_value = None
35
        self.assertFalse(self.repo.knowsByLocation(p))
36
        self.db.provenance.find_one.assert_called_with({'location':p})
37
        self.db.provenance.find_one.return_value = 1
38
        self.assertTrue(self.repo.knowsByLocation(p))
39
40
    def test_byLocation_returns_img_from_record_with_path(self):
41
        self.setupRepo()
42
        p = '/p/f1'
43
        out = self.repo.byLocation(p)
44
        self.db.provenance.find_one.assert_called_with({'location':p})
45
        self.factory.fromProvenance.assert_called_with(
46
            self.db.provenance.find_one())
47
        self.assertEqual(self.factory.fromProvenance(), out)
48
49
    def test_getSeries(self):
50
        self.setupRepo()
51
        img = Mock()
52
        out = self.repo.getSeries(img)
53
        self.db.provenance.find_one.assert_called_with(
54
            {'seriesuid':img.getSeriesId()})
55
        self.factory.fromProvenance.assert_called_with(
56
            self.db.provenance.find_one())
57
        self.assertEqual(self.factory.fromProvenance(), out)
58
59
    def test_knowsSeries_returns_False_if_no_series_id(self):
60
        self.setupRepo()
61
        img = Mock()
62
        img.getSeriesId.return_value = None
63
        self.assertFalse(self.repo.knowsSeries(img))
64
65
    def test_knowsSeries(self):
66
        self.setupRepo()
67
        img = Mock()
68
        self.assertTrue(self.repo.knowsSeries(img))
69
        self.db.provenance.find_one.return_value = None
70
        self.assertFalse(self.repo.knowsSeries(img))
71
72
    def test_Add(self):
73
        self.setupRepo()
74
        img = Mock()
75
        img.provenance = {'a':1, 'b':2}
76
        self.repo.add(img)
77
        self.db.provenance.insert_one.assert_called_with({'a':1, 'b':2})
78
79
    def test_update(self):
80
        self.setupRepo()
81
        img = Mock()
82
        img.provenance = {'a':1, 'b':2}
83
        self.repo.update(img)
84
        self.db.provenance.update.assert_called_with(
85
            {'location':img.location.toString()}, {'a':1, 'b':2})
86
87
    def test_all(self):
88
        self.factory.fromProvenance.side_effect = lambda p: 'img_'+p
89
        self.db.provenance.find.return_value = ['p1', 'p2']
90
        self.setupRepo()
91
        out = self.repo.all()
92
        self.db.provenance.find.assert_called_with()
93
        self.factory.fromProvenance.assert_any_call('p1')
94
        self.factory.fromProvenance.assert_any_call('p2')
95
        self.assertEqual(['img_p1', 'img_p2'], out)
96
97
    def test_bySubject(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
98
        self.factory.fromProvenance.side_effect = lambda p: 'img_'+p
99
        self.db.provenance.find.return_value = ['p1', 'p2']
100
        self.setupRepo()
101
        s = 'Brambo'
102
        out = self.repo.bySubject(s)
103
        self.db.provenance.find.assert_called_with({'subject':s})
104
        self.factory.fromProvenance.assert_any_call('p1')
105
        self.factory.fromProvenance.assert_any_call('p2')
106
        self.assertEqual(['img_p1', 'img_p2'], out)
107
108
    def test_byApproval(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
109
        self.factory.fromProvenance.side_effect = lambda p: 'img_'+p
110
        self.db.provenance.find.return_value = ['p1', 'p2']
111
        self.setupRepo()
112
        a = 'AOk'
113
        out = self.repo.byApproval(a)
114
        self.db.provenance.find.assert_called_with({'approval':a})
115
        self.factory.fromProvenance.assert_any_call('p1')
116
        self.factory.fromProvenance.assert_any_call('p2')
117
        self.assertEqual(['img_p1', 'img_p2'], out)
118
119
    def test_updateApproval(self):
120
        self.setupRepo()
121
        img = Mock()
122
        p = '/p/f1'
123
        newStatus = 'oh-oh'
124
        self.repo.updateApproval(p, newStatus)
125
        self.db.provenance.update.assert_called_with(
126
            {'location':p}, {'$set': {'approval': newStatus}})
127
128
    def test_latest(self):
129
        self.factory.fromProvenance.side_effect = lambda p: 'img_'+p
130
        self.db.provenance.find.return_value = Mock()
131
        self.db.provenance.find.return_value.sort.return_value.limit.return_value = ['px','py']
132
        self.setupRepo()
133
        out = self.repo.latest()
134
        self.db.provenance.find.assert_called_with()
135
        self.db.provenance.find().sort.assert_called_with('added', -1)
136
        self.db.provenance.find().sort().limit.assert_called_with(20)
137
        self.factory.fromProvenance.assert_any_call('px')
138
        self.factory.fromProvenance.assert_any_call('py')
139
        self.assertEqual(['img_px', 'img_py'], out)
140
141
    def test_statistics(self):
142
        self.db.provenance.aggregate.return_value = [sentinel.stats,]
143
        self.setupRepo()
144
        out = self.repo.statistics()
145
        self.db.provenance.aggregate.assert_called_with(
146
           [{'$group':
147
                 {
148
                   '_id': None,
149
                   'totalsize': { '$sum': '$size' },
150
                   'count': { '$sum': 1 }
151
                 }
152
            }])
153
        self.assertEqual(sentinel.stats, out)
154
155
    def test_statistics_if_no_records(self):
156
        self.db.provenance.aggregate.return_value = []
157
        self.setupRepo()
158
        out = self.repo.statistics()
159
        self.assertEqual({'count':0}, out)
160
161
    def test_byId(self):
162
        self.setupRepo()
163
        ID = 'abc123'
164
        out = self.repo.byId(ID)
165
        self.db.provenance.find_one.assert_called_with({'id':ID})
166
        self.factory.fromProvenance.assert_called_with(
167
            self.db.provenance.find_one())
168
        self.assertEqual(self.factory.fromProvenance(), out)
169
170
    def test_byLocations(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
171
        self.factory.fromProvenance.side_effect = lambda p: 'img_'+p
172
        self.db.provenance.find.return_value = ['p1', 'p2']
173
        self.setupRepo()
174
        out = self.repo.byLocations(['l1','l2'])
175
        self.db.provenance.find.assert_called_with({'location':{'$in':['l1','l2']}})
176
        self.factory.fromProvenance.assert_any_call('p1')
177
        self.factory.fromProvenance.assert_any_call('p2')
178
        self.assertEqual(['img_p1', 'img_p2'], out)
179
180
    def test_byParents(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
181
        self.factory.fromProvenance.side_effect = lambda p: 'img_'+p
182
        self.db.provenance.find.return_value = ['p1', 'p2']
183
        self.setupRepo()
184
        out = self.repo.byParents(['x1','x2'])
185
        self.db.provenance.find.assert_called_with({'parents':{'$in':['x1','x2']}})
186
        self.factory.fromProvenance.assert_any_call('p1')
187
        self.factory.fromProvenance.assert_any_call('p2')
188
        self.assertEqual(['img_p1', 'img_p2'], out)
189
190
    def test_Converts_timedelta_to_float_when_serializing(self):
191
        self.setupRepo()
192
        img = Mock()
193
        img.provenance = {'a':1, 'duration':timedelta(seconds=67.89)}
194
        self.repo.add(img)
195
        self.db.provenance.insert_one.assert_called_with(
196
            {'a':1, 'duration':67.89})
197
198
    def test_Converts_duration_to_timedelta_when_deserializing(self):
199
        self.setupRepo()
200
        self.db.provenance.find_one.return_value = {'a':3, 'duration':89.01}
201
        out = self.repo.byLocation('/p/f1')
202
        self.factory.fromProvenance.assert_called_with(
203
            {'a':3, 'duration':timedelta(seconds=89.01)})
204
        
205
206
207