-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
324 lines (273 loc) · 10.6 KB
/
tests.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
import hashlib
import pickle
import unittest
import mock
import pytest
import webtest
from google.appengine.api import taskqueue
from google.appengine.runtime.apiproxy_errors import DeadlineExceededError
import webapp2
import deferred
class Parent(object):
class Inner(object):
@staticmethod
def function():
pass
@classmethod
def function(cls):
pass
def top_level_function():
pass
TLF_MOCK = mock.MagicMock()
class LoadTests(unittest.TestCase):
def test_top_level(self):
func = deferred._load('tests.top_level_function')
assert func is top_level_function
def test_classmethod(self):
func = deferred._load('tests.Parent.function')
assert func.__code__.co_code == Parent.function.__code__.co_code
def test_inner_classmethod(self):
func = deferred._load('tests.Parent.Inner.function')
assert func is Parent.Inner.function
def test_no_module(self):
with pytest.raises(deferred.InvalidPath):
deferred._load('some_module')
def test_no_module_nested(self):
with pytest.raises(deferred.InvalidPath):
deferred._load('some.module.function')
def test_no_module_attribute(self):
with pytest.raises(deferred.InvalidPath):
deferred._load('tests.something_nonexistent')
def test_no_class_attribute(self):
with pytest.raises(deferred.InvalidPath):
deferred._load('tests.Parent.nonexistent')
def test_empty(self):
with pytest.raises(deferred.InvalidPath):
deferred._load('')
class GenerateHashTests(unittest.TestCase):
def test_ok(self):
args = (1, 2, 'some', 'thing')
kwargs = {
'another': 'thing',
'value': 42,
}
expected = hashlib.md5(pickle.dumps((args, kwargs))).hexdigest()
result = deferred._generate_hash(args, kwargs)
assert result == expected
@mock.patch.object(deferred.time, 'sleep', mock.MagicMock())
class ExecuteTests(unittest.TestCase):
def setUp(self):
super(ExecuteTests, self).setUp()
self.executor = mock.MagicMock()
def test_ok(self):
deferred._execute(self.executor, 1, 2, 'a', 'mama', some='thing')
self.executor.assert_called_once_with(
1,
2,
'a',
'mama',
some='thing',
)
@mock.patch.object(deferred, 'MAX_RETRIES', 5)
def test_retry(self):
self.executor.side_effect = [DeadlineExceededError] * 3 + [None]
deferred._execute(self.executor, 1, 2, 'a', 'mama', some='thing')
assert self.executor.call_count == 4
@mock.patch.object(deferred, 'MAX_RETRIES', 5)
def test_retry_raise(self):
self.executor.side_effect = [DeadlineExceededError] * 6
with pytest.raises(DeadlineExceededError):
deferred._execute(self.executor, 1, 2, 'a', 'mama', some='thing')
assert self.executor.call_count == 5
@mock.patch.object(deferred, 'MAX_RETRIES', 5)
@mock.patch.object(deferred, 'logging')
def test_already_exists(self, m_l):
error = taskqueue.TaskAlreadyExistsError
self.executor.side_effect = [DeadlineExceededError] * 3 + [error]
deferred._execute(self.executor, 1, 2, 'a', 'mama', some='thing')
assert self.executor.call_count == 4
assert m_l.warning.called
@mock.patch.object(deferred, 'MAX_RETRIES', 5)
@mock.patch.object(deferred, 'logging')
def test_tombstoned(self, m_l):
error = taskqueue.TombstonedTaskError
self.executor.side_effect = [DeadlineExceededError] * 3 + [error]
deferred._execute(self.executor, 1, 2, 'a', 'mama', some='thing')
assert self.executor.call_count == 4
assert m_l.warning.called
@mock.patch.object(deferred, 'MAX_RETRIES', 5)
@mock.patch.object(deferred, 'logging')
def test_too_large(self, m_l):
error = taskqueue.TaskTooLargeError
self.executor.side_effect = [DeadlineExceededError] * 3 + [error]
deferred._execute(self.executor, 1, 2, 'a', 'mama', some='thing')
assert self.executor.call_count == 4
assert m_l.exception.called
class PrepareTaskqueueKwargsTests(unittest.TestCase):
def test_normal(self):
payload, taskqueue_kwargs = deferred._prepare_taskqueue_kwargs(
'/some/path',
(1, 2, 'a', 'c'),
{
'some': 'thing',
'other': 'thing',
'3': 5,
},
)
assert taskqueue_kwargs == {}
assert payload == {
'path': '/some/path',
'args': (1, 2, 'a', 'c'),
'kwargs': {
'some': 'thing',
'other': 'thing',
'3': 5,
},
}
def test_taskqueue_kwargs(self):
payload, taskqueue_kwargs = deferred._prepare_taskqueue_kwargs(
'/some/path',
(1, 2, 'a', 'c'),
{
'some': 'thing',
'other': 'thing',
'3': 5,
'_some_arg': 'something',
'_queue': 'not-default',
'_target': 'mymodule',
'_countdown': 120,
},
)
assert taskqueue_kwargs == {
'_some_arg': 'something',
'queue_name': 'not-default',
'_target': 'mymodule',
'_countdown': 120,
}
assert payload == {
'path': '/some/path',
'args': (1, 2, 'a', 'c'),
'kwargs': {
'some': 'thing',
'other': 'thing',
'3': 5,
},
}
class DecoratorTests(unittest.TestCase):
def test_ok(self):
@deferred.deferred('/some/path/')
def function():
return 123
identifier = getattr(function, '__deferred_identifier')
assert identifier == '/some/path/'
assert function() == 123
class DeferTests(unittest.TestCase):
def setUp(self):
super(DeferTests, self).setUp()
self.patches = (
mock.patch.object(deferred.taskqueue, 'add'),
mock.patch.object(deferred.gae_deferred, 'defer'),
mock.patch.object(deferred, 'DEFAULT_MODULE', 'somemodule'),
mock.patch.object(deferred, 'DEFAULT_QUEUE', 'somequeue'),
)
self.taskqueue, self.defer, _, _ = [p.start() for p in self.patches]
def tearDown(self):
super(DeferTests, self).tearDown()
for patcher in self.patches:
patcher.stop()
def test_defer(self):
deferred.defer(top_level_function)
assert not self.taskqueue.called
self.defer.assert_called_once_with(
top_level_function,
_name=mock.ANY,
_queue='somequeue',
_target='somemodule',
)
def test_non_default_queue(self):
deferred.defer(top_level_function, _queue='differentqueue')
assert not self.taskqueue.called
self.defer.assert_called_once_with(
top_level_function,
_name=mock.ANY,
_queue='differentqueue',
_target='somemodule',
)
def test_non_default_target(self):
deferred.defer(top_level_function, _target='differentmodule')
assert not self.taskqueue.called
self.defer.assert_called_once_with(
top_level_function,
_name=mock.ANY,
_queue='somequeue',
_target='differentmodule',
)
def test_name_given(self):
deferred.defer(top_level_function, _name='taskname')
assert not self.taskqueue.called
self.defer.assert_called_once_with(
top_level_function,
_name='taskname',
_queue='somequeue',
_target='somemodule',
)
def test_name_random(self):
deferred.defer(top_level_function)
assert not self.taskqueue.called
name = self.defer.call_args[1]['_name']
assert name.startswith('top_level_function-')
assert len(name) == (len('top_level_function-') + 32)
@mock.patch.object(deferred, 'DEFERRED_URL', '/defer/%s')
def test_taskqueue(self):
decorated = deferred.deferred('some/path')(top_level_function)
deferred.defer(decorated)
assert not self.defer.called
assert self.taskqueue.called
kwargs = self.taskqueue.call_args[1]
assert kwargs['url'] == '/defer/some/path'
assert kwargs['method'] == 'POST'
assert kwargs['payload']
assert kwargs['queue_name'] == 'somequeue'
assert kwargs['_target'] == 'somemodule'
@mock.patch.object(deferred, 'DEFERRED_URL', '/defer/%s')
def test_taskqueue_params(self):
decorated = deferred.deferred('some/path')(top_level_function)
deferred.defer(decorated, _target='othermodule', _queue='otherqueue')
assert not self.defer.called
assert self.taskqueue.called
kwargs = self.taskqueue.call_args[1]
assert kwargs['url'] == '/defer/some/path'
assert kwargs['method'] == 'POST'
assert kwargs['payload']
assert kwargs['queue_name'] == 'otherqueue'
assert kwargs['_target'] == 'othermodule'
@mock.patch.object(deferred, 'DEFERRED_URL', '/deferred/%s')
class HandlerTests(unittest.TestCase):
def setUp(self):
super(HandlerTests, self).setUp()
app = webapp2.WSGIApplication(
routes=[deferred.ROUTE],
config={},
)
self.app = webtest.TestApp(app)
def test_missing_body(self):
self.app.post('/deferred/something', status=400)
def test_unpickleable(self):
self.app.post('/deferred/something', 'not a pickle', status=400)
def test_missing_path(self):
payload = pickle.dumps({})
self.app.post('/deferred/something', payload, status=400)
def test_invalid_path(self):
payload = pickle.dumps({'path': 'not.a.function'})
self.app.post('/deferred/something', payload, status=400)
@mock.patch.dict(globals(), {'top_level_function': TLF_MOCK})
def test_ok(self):
payload = pickle.dumps({
'path': 'tests.top_level_function',
'args': (1, 2),
'kwargs': {
'some': 'thing',
},
})
self.app.post('/deferred/something', payload)
TLF_MOCK.assert_called_once_with(1, 2, some='thing')