Repository navigation
Expand file tree
/
Copy pathtest_testing.py
More file actions
354 lines (299 loc) · 11.7 KB
/
Copy pathtest_testing.py
File metadata and controls
354 lines (299 loc) · 11.7 KB
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
"""
Tests for the effect.testing module.
"""
from testtools import TestCase
from testtools.matchers import (MatchesListwise, Equals, MatchesException,
raises)
from . import (
Constant,
Effect,
base_dispatcher,
parallel,
sync_perform)
from .testing import (
ESConstant,
ESError,
ESFunc,
EQDispatcher,
EQFDispatcher,
SequenceDispatcher,
fail_effect,
resolve_effect,
resolve_stubs)
class ResolveEffectTests(TestCase):
def test_basic_resolution(self):
"""
When no callbacks are attached to the effect, the result argument is
returned directly.
"""
eff = Effect(None)
self.assertEqual(resolve_effect(eff, "blegh"), "blegh")
def test_invoke_callbacks(self):
"""
Callbacks of the effect are invoked to calculate the final result.
"""
def add1(n):
return n + 1
eff = Effect(None).on(success=add1).on(success=add1)
self.assertEqual(resolve_effect(eff, 0), 2)
def test_callback_returning_effect(self):
"""
When a callback returns an effect, that effect is returned.
"""
stub_effect = Effect('inner')
eff = Effect(None).on(success=lambda r: stub_effect)
result = resolve_effect(eff, 'foo')
self.assertEqual(result.intent, 'inner')
self.assertEqual(resolve_effect(result, 'next-result'),
'next-result')
def test_intermediate_callback_returning_effect(self):
"""
When a callback returns an effect, and that outer callback has
callbacks that come after it, the remaining callbacks will be wrapped
around the returned effect.
"""
nested_effect = Effect("nested")
def a(r):
return nested_effect
def b(r):
return ("b-result", r)
eff = Effect("orig").on(success=a).on(success=b)
result = resolve_effect(eff, "foo")
self.assertEqual(
resolve_effect(result, "next-result"),
('b-result', 'next-result'))
def test_maintain_intermediate_effect_callbacks(self):
"""
When a nested effect is returned from a callback, and that nested
effect has callbacks itself, they are flattened along with the
callbacks remaining on the outer effect.
"""
nested_effect = Effect("nested")
def a(r):
return nested_effect.on(success=nested_b)
def nested_b(r):
return ("nested-b-result", r)
def c(r):
return ("c-result", r)
eff = Effect("orig").on(success=a).on(success=c)
result = resolve_effect(eff, "foo")
self.assertEqual(resolve_effect(result, 'next-result'),
('c-result', ('nested-b-result', 'next-result')))
def test_resolve_effect_cb_exception(self):
"""
When a callback raises an exception, the next error handler is called
with the exception info.
"""
self.assertThat(
resolve_effect(
Effect("orig")
.on(success=lambda r: 1 / 0)
.on(error=lambda exc: ('handled', exc)),
'result'),
MatchesListwise([
Equals('handled'),
MatchesException(ZeroDivisionError)]))
def test_raise_if_final_result_is_error(self):
"""
If the last callback raises an error, that error is raised from
resolve_effect.
"""
self.assertThat(
lambda:
resolve_effect(
Effect('orig').on(
success=lambda r: _raise(ValueError('oh goodness'))),
'result'),
raises(ValueError('oh goodness')))
def test_fail_effect(self):
"""
fail_effect allows failing an effect directly, so its first error
handler is invoked.
"""
self.assertThat(
lambda:
fail_effect(
Effect('orig'),
ValueError('oh deary me')),
raises(ValueError('oh deary me')))
def test_skip_callbacks(self):
"""
Intermediate callbacks of the wrong type are skipped.
"""
eff = (Effect('foo')
.on(error=lambda f: 1)
.on(success=lambda x: ('succeeded', x)))
self.assertEqual(resolve_effect(eff, 'foo'), ('succeeded', 'foo'))
class ResolveStubsTests(TestCase):
"""Tests for resolve_stubs."""
def test_resolve_stubs(self):
"""
resolve_stubs automatically performs intents that are wrapped in
:class:`Stub`.
"""
eff = ESConstant("foo").on(
success=lambda r: ESError(RuntimeError("foo")).on(
error=lambda e: ESFunc(lambda: "heyo")))
self.assertEqual(resolve_stubs(base_dispatcher, eff), "heyo")
def test_non_test_intent(self):
"""
When a non-stub effect intent is found, the effect is returned.
"""
bare_effect = Effect(object())
eff = ESConstant("foo").on(success=lambda r: bare_effect)
result_eff = resolve_stubs(base_dispatcher, eff)
self.assertIs(result_eff.intent, bare_effect.intent)
self.assertEqual(result_eff.callbacks, [])
def test_type_error(self):
"""
TypeErrors in callbacks (or otherwise performed intents) are propagated
resolve_stubs.
(This only exists because the initial implementation was done stupidly,
and had to be fixed.)
"""
eff = ESConstant("foo").on(success=lambda r: None["foo"])
self.assertRaises(TypeError, resolve_stubs, base_dispatcher, eff)
def test_resolve_stubs_callbacks_only_invoked_once(self):
"""
Callbacks are run only once.
This is a regression test for a really dumb bug.
"""
eff = ESConstant("foo").on(success=lambda r: ("got it", r))
self.assertEqual(resolve_stubs(base_dispatcher, eff),
("got it", "foo"))
def test_outer_callbacks_after_intermediate_effect(self):
"""
When a callback returns an effect, and the outer effect has further
callbacks, the remaining callbacks will be wrapped around the returned
effect.
"""
eff = ESConstant("foo").on(
success=lambda r: Effect("something")
).on(
lambda r: ("callbacked", r))
result = resolve_stubs(base_dispatcher, eff)
self.assertIs(type(result), Effect)
self.assertEqual(result.intent, "something")
result2 = resolve_effect(result, "bar")
self.assertEqual(result2, ("callbacked", "bar"))
def test_parallel_stubs(self):
"""Parallel effects are recursively resolved."""
p_eff = parallel([ESConstant(1), ESConstant(2)])
self.assertEqual(resolve_stubs(base_dispatcher, p_eff), [1, 2])
def test_parallel_non_stubs(self):
"""
If a parallel effect contains a non-stub, the parallel effect is
returned as-is.
"""
p_eff = parallel(
[ESConstant(1), Effect(Constant(2))]
).on(lambda x: 0)
self.assertEqual(resolve_stubs(base_dispatcher, p_eff), p_eff)
def test_parallel_stubs_with_callbacks(self):
"""
resolve_stubs runs callbacks of parallel effects.
(bugfix test)
"""
p_eff = parallel([ESConstant(1), ESConstant(2)]).on(lambda r: r[0])
self.assertEqual(resolve_stubs(base_dispatcher, p_eff), 1)
def test_parallel_stubs_with_callbacks_returning_effects(self):
"""
resolve_stubs further processes effects that are returned from
callbacks of parallel effects.
"""
p_eff = parallel([ESConstant(1), ESConstant(2)]).on(
lambda r: ESConstant(r[0] + 1))
self.assertEqual(resolve_stubs(base_dispatcher, p_eff), 2)
def test_parallel_stubs_with_element_callbacks_returning_non_stubs(self):
"""
When an element of a parallel effect returns a non-stub effect, it will
NOT be performed.
"""
p_eff = parallel([ESConstant(1).on(lambda r: Effect(Constant(2)))])
self.assertEqual(resolve_stubs(base_dispatcher, p_eff),
[Effect(Constant(2))])
def _raise(e):
raise e
class EQDispatcherTests(TestCase):
"""Tests for :obj:`EQDispatcher`."""
def test_no_intent(self):
"""When the dispatcher can't match the intent, it returns None."""
d = EQDispatcher([])
self.assertIs(d('foo'), None)
def test_perform(self):
"""When an intent matches, performing it returns the canned result."""
d = EQDispatcher([('hello', 'there')])
self.assertEqual(sync_perform(d, Effect('hello')), 'there')
class EQFDispatcherTests(TestCase):
"""Tests for :obj:`EQFDispatcher`."""
def test_no_intent(self):
"""When the dispatcher can't match the intent, it returns None."""
d = EQFDispatcher([])
self.assertIs(d('foo'), None)
def test_perform(self):
"""When an intent matches, performing it returns the canned result."""
d = EQFDispatcher([('hello', lambda i: (i, 'there'))])
self.assertEqual(sync_perform(d, Effect('hello')), ('hello', 'there'))
class SequenceDispatcherTests(TestCase):
"""Tests for :obj:`SequenceDispatcher`."""
def test_mismatch(self):
"""
When an intent isn't expected, a None is returned.
"""
d = SequenceDispatcher([('foo', lambda i: 1 / 0)])
self.assertEqual(d('hello'), None)
def test_success(self):
"""
Each intent is performed in sequence with the provided functions, as
long as the intents match.
"""
d = SequenceDispatcher([
('foo', lambda i: ('performfoo', i)),
('bar', lambda i: ('performbar', i)),
])
eff = Effect('foo').on(lambda r: Effect('bar').on(lambda r2: (r, r2)))
self.assertEqual(
sync_perform(d, eff),
(('performfoo', 'foo'), ('performbar', 'bar')))
def test_ran_out(self):
"""When there are no more items left, None is returned."""
d = SequenceDispatcher([])
self.assertEqual(d('foo'), None)
def test_out_of_order(self):
"""Order of items in the sequence matters."""
d = SequenceDispatcher([
('bar', lambda i: ('performbar', i)),
('foo', lambda i: ('performfoo', i)),
])
self.assertEqual(d('foo'), None)
def test_consumed(self):
"""`consumed` returns True if there are no more elements."""
d = SequenceDispatcher([])
self.assertTrue(d.consumed())
def test_consumed_honors_changes(self):
"""
`consumed` returns True if there are no more elements after performing
some..
"""
d = SequenceDispatcher([('foo', lambda i: 'bar')])
sync_perform(d, Effect('foo'))
self.assertTrue(d.consumed())
def test_not_consumed(self):
"""
`consumed` returns False if there are more elements.
"""
d = SequenceDispatcher([('foo', lambda i: 'bar')])
self.assertFalse(d.consumed())
def test_consume_good(self):
"""``consume`` doesn't raise an error if all elements are consumed."""
d = SequenceDispatcher([('foo', lambda i: 'bar')])
with d.consume():
sync_perform(d, Effect('foo'))
def test_consume_raises(self):
"""``consume`` raises an error if not all elements are consumed."""
d = SequenceDispatcher([('foo', None)])
def failer():
with d.consume():
pass
e = self.assertRaises(AssertionError, failer)
self.assertEqual(str(e), "Not all intents were performed: ['foo']")