-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathtest_testing.py
More file actions
551 lines (458 loc) · 16.3 KB
/
Copy pathtest_testing.py
File metadata and controls
551 lines (458 loc) · 16.3 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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
"""
Tests for the effect.testing module.
"""
import attr
import pytest
from testtools import TestCase
from testtools.matchers import MatchesListwise, Equals, raises
from . import (
ComposedDispatcher,
Constant,
Effect,
base_dispatcher,
parallel,
sync_perform,
sync_performer,
)
from .do import do
from .fold import FoldError, sequence
from .testing import (
_ANY,
ESConstant,
ESError,
ESFunc,
EQDispatcher,
EQFDispatcher,
SequenceDispatcher,
const,
conste,
fail_effect,
intent_func,
nested_sequence,
parallel_sequence,
perform_sequence,
resolve_effect,
resolve_stubs,
)
from ._test_utils import MatchesException
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("division by zero")),
]
),
)
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']")
@attr.s
class MyIntent(object):
val = attr.ib()
@attr.s
class OtherIntent(object):
val = attr.ib()
def test_perform_sequence():
"""perform_sequence pretty much acts like SequenceDispatcher by default."""
@do
def code_under_test():
r = yield Effect(MyIntent("a"))
r2 = yield Effect(OtherIntent("b"))
return (r, r2)
seq = [
(MyIntent("a"), lambda i: "result1"),
(OtherIntent("b"), lambda i: "result2"),
]
eff = code_under_test()
assert perform_sequence(seq, eff) == ("result1", "result2")
def test_perform_sequence_log():
"""
When an intent isn't found, a useful log of intents is included in the
exception message.
"""
@do
def code_under_test():
r = yield Effect(MyIntent("a"))
r2 = yield Effect(OtherIntent("b"))
return (r, r2)
seq = [(MyIntent("a"), lambda i: "result1")]
with pytest.raises(AssertionError) as exc:
perform_sequence(seq, code_under_test())
expected = "sequence: MyIntent(val='a')\n" "NOT FOUND: OtherIntent(val='b')"
assert expected in str(exc.value)
def test_parallel_sequence():
"""
Ensures that all parallel effects are found in the given intents, in
order, and returns the results associated with those intents.
"""
seq = [
parallel_sequence(
[
[(1, lambda i: "one!")],
[(2, lambda i: "two!")],
[(3, lambda i: "three!")],
]
)
]
p = parallel([Effect(1), Effect(2), Effect(3)])
assert perform_sequence(seq, p) == ["one!", "two!", "three!"]
def test_parallel_sequence_fallback():
"""
Accepts a ``fallback`` dispatcher that will be used when the sequence
doesn't contain an intent.
"""
def dispatch_2(intent):
if intent == 2:
return sync_performer(lambda d, i: "two!")
fallback = ComposedDispatcher([dispatch_2, base_dispatcher])
seq = [
parallel_sequence(
[
[(1, lambda i: "one!")],
[], # only implicit effects in this slot
[(3, lambda i: "three!")],
],
fallback_dispatcher=fallback,
),
]
p = parallel([Effect(1), Effect(2), Effect(3)])
assert perform_sequence(seq, p) == ["one!", "two!", "three!"]
def test_parallel_sequence_must_be_parallel():
"""
If the sequences aren't run in parallel, the parallel_sequence won't
match and a FoldError of NoPerformerFoundError will be raised.
"""
seq = [
parallel_sequence(
[
[(1, lambda i: "one!")],
[(2, lambda i: "two!")],
[(3, lambda i: "three!")],
]
)
]
p = sequence([Effect(1), Effect(2), Effect(3)])
with pytest.raises(FoldError) as excinfo:
perform_sequence(seq, p)
assert type(excinfo.value.wrapped_exception) is AssertionError
def test_nested_sequence():
"""
:func:`nested_sequence` returns sequence performer function for an intent
that wraps an effect.
"""
@attr.s
class WrappedIntent(object):
effect = attr.ib()
value = attr.ib()
@do
def internal():
yield Effect(1)
yield Effect(2)
return "wrap"
@do
def code_under_test():
r = yield Effect(WrappedIntent(internal(), "field"))
r2 = yield Effect(MyIntent("a"))
return (r, r2)
seq = [
(
WrappedIntent(_ANY, "field"),
nested_sequence([(1, const("r1")), (2, const("r2"))]),
),
(MyIntent("a"), const("result2")),
]
eff = code_under_test()
assert perform_sequence(seq, eff) == ("wrap", "result2")
def test_const():
"""
:func:`const` takes an argument but returns fixed value
"""
assert const(2)(MyIntent("whatever")) == 2
assert const("text")(OtherIntent("else")) == "text"
def test_conste():
"""
:func:`conste` takes an argument but always raises given exception
"""
func = conste(ValueError("boo"))
with pytest.raises(ValueError):
func(MyIntent("yo"))
def test_intent_func():
"""
:func:`intent_func` returns function that returns Effect of tuple of passed arg
and its args.
"""
func = intent_func("myfunc")
assert func(2, 3) == Effect(("myfunc", 2, 3))
assert func("text", 3, None) == Effect(("myfunc", "text", 3, None))