-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_camup.py
289 lines (226 loc) · 10.1 KB
/
test_camup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
import __builtin__
import unittest
from mock import MagicMock,patch, mock_open
import os
from kafka.common import *
import camup
import settings
class BaseTest(unittest.TestCase):
def setUp(self):
self.SAVE_FOLDER='./test/data'
class test_read_dir(BaseTest):
@patch("os.listdir")
def test_no_files(self,osmock):
"""
check that it doesn't find files
"""
osmock.return_value=[]
camup.read_dir(self.SAVE_FOLDER)
assert osmock.called is True
@patch("os.listdir")
def test_with_files(self,osmock):
""" check with a few files
"""
osmock.return_value=["foo","bar"]
camup.read_dir(self.SAVE_FOLDER)
assert osmock.called is True
@patch("os.listdir")
def test_raising(self,osmock):
""" what if the disk is not there
"""
osmock.return_value="No folder"
osmock.side_effect=OSError("No nono noooooo")
self.assertRaises(OSError, camup.read_dir,self.SAVE_FOLDER)
class test_first_picture(BaseTest):
def test_none_is_none(self):
with self.assertRaises(ValueError):
camup.get_picture_from_storage(None)
def test_no_list_no_result(self):
filelist = []
file = camup.get_picture_from_storage(filelist)
assert file is None
@patch("camup.is_locked")
def test_a_normal_list(self,mocklock):
mocklock.return_value = False
filelist = ['20150905_1225_29_cam0.jpg', '20150905_1225_33_cam0.jpg', '20150905_1225_26_cam0.jpg']
file = camup.get_picture_from_storage(filelist)
self.assertEqual( file , '20150905_1225_26_cam0.jpg')
@patch("camup.is_locked")
def test_getting_the_latest_picture(self,mocklock):
mocklock.return_value = False
filelist = ['20150905_1225_29_cam0.jpg', '20150905_1225_33_cam0.jpg', '20150905_1225_26_cam0.jpg']
file = camup.get_picture_from_storage(filelist, reverse=True)
self.assertEqual( file , '20150905_1225_33_cam0.jpg')
@patch("camup.is_locked")
def test_one_jpg_one_pic(self,mocklock):
mocklock.return_value = False
filelist = ["foo.jpg"]
file = camup.get_picture_from_storage(filelist)
assert file == "foo.jpg"
@patch("camup.is_locked")
def test_caps_are_the_same(self,mocklock):
mocklock.return_value = False
filelist = ["foo.JpG"]
file = camup.get_picture_from_storage(filelist)
assert file == "foo.JpG"
@patch("camup.is_locked")
def test_a_locked_picture_returns_next(self,mocklock):
mocklock.side_effect = [True, False] # this is a special call to only lock the 1st file
filelist = ["pic1.jpeg","pic1.jpeg.lock","pic2.jpeg"]
file = camup.get_picture_from_storage(filelist)
assert file == "pic2.jpeg"
def test_non_images_are_ignored(self):
filelist = ["foo.txt","bar.jpog","thumbs.db"]
file = camup.get_picture_from_storage(filelist)
assert file is None
class test_clean_files(BaseTest):
@patch("os.remove")
def test_os_exception(self,osmock):
osmock.side_effect=OSError("the filesystem can not perform the operation")
@patch("os.remove")
def test_everything_allright(self,osmock):
camup.clean_files(["samplefile"])
osmock.assert_called_with("samplefile")
@patch("os.remove")
def test_not_a_list_raises(self,osmock):
assert osmock.called is False
self.assertRaises(AssertionError,camup.clean_files,"rubis")
class test_build_message(BaseTest):
@patch("uuid.uuid4")
def test_basic_input(self, mockuu):
mockuu.return_value ="this-is-a-uuid"
result = camup.build_message("foo","bar")
expectation = '{"pictureName": "foo", "image": "YmFy\\n", "barcode": "bull-seat", "ride": "chewit", "id": "this-is-a-uuid"}'
self.assertEqual(expectation, result)
def test_noinput(self):
self.assertRaises(TypeError, camup.build_message)
def test_fail_on_binary_passed_as_id(self):
bigbin = "aoeuidrtns" * 1000
self.assertRaises(AssertionError, camup.build_message, bigbin, "foo","bar")
@patch("uuid.uuid4")
def test_adding_a_barcode(self,mockuu):
mockuu.return_value = "this-is-a-uuid"
result = camup.build_message("foo","bar","barcode")
expectation = '{"pictureName": "foo", "image": "YmFy\\n", "barcode": "barcode", "ride": "chewit", "id": "this-is-a-uuid"}'
self.assertEqual(expectation,result)
class test_send_message(BaseTest):
def setUp(self):
pass
def test_nomessage(self):
self.assertRaises(TypeError,camup.send_message)
@patch("camup.SimpleProducer")
@patch("camup.KafkaClient")
def test_general_error(self, mockclient,kafkamock):
kafkamock.side_effect=IOError("Kafka doesn't want you msg anymore")
self.assertRaises(IOError, camup.send_message,"foo")
@patch("camup.SimpleProducer")
@patch("camup.KafkaClient")
def test_kafka_error(self,mockclient,kafkamock):
kafkamock.side_effect=InvalidFetchRequestError("foo")
self.assertRaises(InvalidFetchRequestError, camup.send_message, "fo")
#TODO: This test is of little value when we removed custom exception behaviour. delete it or improve it.
@patch("camup.SimpleProducer")
@patch("camup.KafkaClient")
def test_kafka_kafkian_exception(self,mockclient,mockkafka):
mockkafka.side_effect=MessageSizeTooLargeError("foo")
self.assertRaises(MessageSizeTooLargeError, camup.send_message,"fo")
class test_publish(BaseTest):
@patch("camup.raw_input")
@patch("camup.send_message")
def test_noinput(self,mockinput,mocksend):
mockinput.return_value = "yes"
mocksend.return_value = "Ok"
self.assertRaises(TypeError, camup.publish)
#assert mockinput.called is True
#assert mocksend.called is True
# Idon't really like it, but when you call assertRaises in unittest, creates a different scope, so you can not really access the mock from here. :(
@patch("camup.raw_input")
@patch("camup.send_message")
def test_normal_input(self,mocksend,mockinput):
mockinput.return_value = "yes"
mocksend.return_value = "Ok"
camup.publish("topic","message")
#assert mockinput.called is True
mocksend.assert_called_with("message")
@patch("camup.raw_input")
@patch("camup.send_message")
def test_dont_send(self,mocksend,mockinput):
mockinput.return_value = "NONONONONONONONO MOTHERFUCKER"
mocksend.return_value = "yes"
camup.publish("topic","message")
#assert mockinput.called is True
#assert mocksend.called is False
@patch("camup.raw_input")
@patch("camup.send_message")
def test_false_yes(self,mocksend,mockinput):
""" this shouldn't send the message"""
mockinput.return_value = "this is not a yes"
mocksend.return_value = "Ok"
camup.publish("topic","message")
#assert mockinput.called is True
#assert mocksend.called is False
class test_lock_file(BaseTest):
def setUp(self):
self.SVFD = settings.SAVE_FOLDER
@patch("os.utime")
@patch("camup.open")
def test_oserror(self,mockopen,mockos):
"""If there is an issue in the filesystem"""
mockos.side_effect=OSError("the filesystem is Fake!!")
self.assertRaises(OSError, camup.lock_picture,"anyfile")
assert mockos.called is True
@patch("os.utime")
@patch("camup.open")
def test_osworks(self,mockopen,mockos):
"""creating a lock returns the name of the lock"""
mockos.return_value = "oh yeah"
result = camup.lock_picture("anyfile")
assert mockos.called is True
self.assertEqual( result , self.SVFD + "/" + "anyfile.lock")
class test_acquire_a_picture(BaseTest):
"""
this class requires a bit more of work, due to the patching of open.
that's why we need to import builtin (for python 2 only)
and use patch.object instead of just patch.
"""
@patch("camup.get_picture_from_storage")
@patch("camup.lock_picture")
@patch.object(__builtin__,"open", mock_open(read_data="datastream"))
def test_alright(self,mocklock,mockgetfirst):
""" if everything goes well, return the image stream and the img_id"""
mockgetfirst.return_value = "thiscouldbea.jpg"
result = camup.acquire_a_picture()
mocklock.assert_called_with("thiscouldbea.jpg")
assert mockgetfirst.called is True
self.assertEqual(("datastream","thiscouldbea"), result)
@patch("camup.get_picture_from_storage")
@patch("camup.lock_picture")
@patch.object(__builtin__,"open", mock_open(read_data="datastream"))
def test_we_dont_lock_if_we_dont_want_to(self,mocklock,mockgetfirst):
""" if everything goes well, return the image stream and the img_id"""
mockgetfirst.return_value = "thiscouldbea.jpg"
result = camup.acquire_a_picture(lockit=False)
assert mockgetfirst.called is True
assert mocklock.called is False
self.assertEqual(("datastream","thiscouldbea"), result)
class test_is_locked(BaseTest):
def test_the_picture_is_locked_returns_true(self):
filelist = ["pic1.jpg","pic2.jpeg", "pic1.jpg.lock"]
assert camup.is_locked("pic1.jpg",filelist) is True
def test_passing_a_filelist(self):
filelist = ["pic1.jpg","pic2.jpeg", "pic2.jpeg.lock"]
assert camup.is_locked("pic1.jpg",filelist) is False
@patch("os.listdir")
def test_another_picture_not_locked_return_false(self,mockos):
mockos.return_value = ["pic1.jpg","pic2.jpeg", "pic2.jpeg.lock"]
assert camup.is_locked("pic1.jpg",path="crap") is False
@patch("os.listdir")
def test_passing_a_path(self,mockos):
mockos.return_value = ["pic1.jpg","pic2.jpeg", "pic2.jpeg.lock"]
assert camup.is_locked("pic1.jpg",path = "bullshitpath") is False
@patch("os.listdir")
def test_passing_a_path_and_failing_to_read(self,mockos):
mockos.side_effect = OSError("oh son, the filesystem is borked")
self.assertRaises(OSError, camup.is_locked,"pic1.jpg",path = "bullshitpath")
if __name__=="__main__":
unittest.main()