Repository navigation
Expand file tree
/
Copy pathtest_decorators.py
More file actions
647 lines (429 loc) · 18.6 KB
/
Copy pathtest_decorators.py
File metadata and controls
647 lines (429 loc) · 18.6 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
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
"""Tests for the decorator forms: bound() and taped()."""
import asyncio
import inspect
import os
import unittest
from typing import Any
import pytest
from wrapture import Binding, WrongModeError, binding, bound, taped, timeline
class Gateway:
def charge(self, amount: int, currency: str = "USD") -> dict[str, Any]:
return {"id": f"ch_{amount}", "amount": amount}
class Model:
status = 3
class PushClient:
async def send(self, user: str) -> str:
raise RuntimeError("no gateway in tests")
# ---------------------------------------------------------------------------
# injection and lifecycle
# ---------------------------------------------------------------------------
def test_bound_applies_around_the_call_and_injects_the_binding() -> None:
@taped()
@bound(Gateway, "charge").on_call.returns({"id": "STUB"})
def body(tape: Any, charge: Any) -> str:
assert isinstance(charge, Binding)
assert Gateway().charge(5) == {"id": "STUB"}
charge.events.assert_once()
tape.assert_order(charge)
return "done"
assert body() == "done"
assert Gateway().charge(2) == {"id": "ch_2", "amount": 2}
def test_a_bare_bound_is_observe_only() -> None:
@taped()
@bound(Gateway, "charge")
def body(tape: Any, charge: Any) -> None:
assert Gateway().charge(3) == {"id": "ch_3", "amount": 3}
charge.events.with_args(amount=3).assert_once()
body()
def test_each_call_gets_a_fresh_binding() -> None:
@taped()
@bound(Gateway, "charge").on_call.returns({"id": "STUB"})
def body(tape: Any, charge: Any) -> None:
Gateway().charge(1)
charge.events.assert_once()
body()
body()
def test_the_return_value_passes_through() -> None:
@bound(Gateway, "charge").on_call.returns({"id": "STUB"})
def body(charge: Any) -> int:
return 41
assert body() == 41
def test_injection_lands_in_var_keyword_when_no_parameter_matches() -> None:
@bound(Gateway, "charge").on_call.returns({"id": "STUB"})
def body(**kwargs: Any) -> None:
assert isinstance(kwargs["charge"], Binding)
assert Gateway().charge(1) == {"id": "STUB"}
body()
def test_phases_configure_in_the_body_through_the_handle() -> None:
@taped()
@bound(Gateway, "charge")
def body(tape: Any, charge: Any) -> None:
flaky = charge.on_call.then(after=1)
flaky.raises(TimeoutError("busy"))
flaky.then(after=1).returns({"id": "fallback"})
gw = Gateway()
assert gw.charge(1) == {"id": "ch_1", "amount": 1}
with pytest.raises(TimeoutError):
gw.charge(2)
assert gw.charge(3) == {"id": "fallback"}
body()
def test_body_created_bindings_record_onto_the_decorators_tape() -> None:
class Ledger:
def record(self, entry: str) -> None:
pass
@taped()
@bound(Gateway, "charge").on_call.returns({"id": "STUB"})
def body(tape: Any, charge: Any) -> None:
record = binding(Ledger, "record")
with record:
Gateway().charge(1)
Ledger().record("entry")
tape.assert_order(charge, record)
body()
# ---------------------------------------------------------------------------
# the chain
# ---------------------------------------------------------------------------
def test_stages_and_terminal_chain_on_one_channel() -> None:
@(
bound(Gateway, "charge")
.on_call.transforms_result(lambda r: {**r, "seen": True})
.returns({"id": "STUB"})
)
def body(charge: Any) -> None:
assert Gateway().charge(1) == {"id": "STUB", "seen": True}
body()
def test_a_repeated_terminal_in_the_chain_is_last_wins() -> None:
@bound(Gateway, "charge").on_call.returns({"id": "first"}).returns({"id": "second"})
def body(charge: Any) -> None:
assert Gateway().charge(1) == {"id": "second"}
body()
def test_an_attribute_binding_configures_its_own_channel() -> None:
@bound(Model, "status").on_get.returns(5)
def body(status: Any) -> None:
assert Model().status == 5
body()
assert Model().status == 3
def test_then_and_advance_error_at_decoration() -> None:
with pytest.raises(TypeError, match=r"then\(\) is not available"):
bound(Gateway, "charge").on_call.then(after=1)
with pytest.raises(TypeError, match=r"advance\(\) is not available"):
bound(Gateway, "charge").on_call.advance()
def test_an_unknown_verb_errors_at_decoration() -> None:
with pytest.raises(AttributeError, match="no verb 'rejects'"):
bound(Gateway, "charge").on_call.rejects()
def test_channel_use_on_a_value_binding_errors_at_decoration() -> None:
with pytest.raises(WrongModeError, match="on_call is not available"):
_ = bound(os.environ, item="API_KEY").on_call
def test_value_verbs_on_a_positional_binding_error_at_decoration() -> None:
with pytest.raises(WrongModeError, match=r"overrides\(\) is only available"):
bound(Gateway, "charge").overrides(1)
def test_value_verb_subsets_match_the_modes() -> None:
with pytest.raises(WrongModeError, match=r"updates\(\)"):
bound(os.environ, item="API_KEY").updates({"a": 1})
with pytest.raises(WrongModeError, match=r"hides\(\)"):
bound(Model, "status", mode="mapping").hides()
# ---------------------------------------------------------------------------
# value and mapping bindings
# ---------------------------------------------------------------------------
def test_a_value_binding_pins_and_restores_an_environment_variable() -> None:
assert "WRAPTURE_TEST_KEY" not in os.environ
@bound(os.environ, item="WRAPTURE_TEST_KEY").overrides("sk_test")
def body(WRAPTURE_TEST_KEY: Any) -> None:
assert os.environ["WRAPTURE_TEST_KEY"] == "sk_test"
body()
assert "WRAPTURE_TEST_KEY" not in os.environ
def test_hides_keeps_the_slot_absent() -> None:
os.environ["WRAPTURE_TEST_KEY"] = "present"
try:
@bound(os.environ, item="WRAPTURE_TEST_KEY").hides()
def body(WRAPTURE_TEST_KEY: Any) -> None:
assert "WRAPTURE_TEST_KEY" not in os.environ
body()
assert os.environ["WRAPTURE_TEST_KEY"] == "present"
finally:
del os.environ["WRAPTURE_TEST_KEY"]
def test_a_mapping_binding_updates_content_in_place() -> None:
class Config:
SETTINGS = {"currency": "USD", "tax_rate": 0.2}
holder = Config.SETTINGS
@bound(Config, "SETTINGS", mode="mapping").updates({"tax_rate": 0.0})
def body(SETTINGS: Any) -> None:
assert holder == {"currency": "USD", "tax_rate": 0.0}
body()
assert holder == {"currency": "USD", "tax_rate": 0.2}
# ---------------------------------------------------------------------------
# alias derivation
# ---------------------------------------------------------------------------
def test_the_alias_is_the_final_segment_of_the_addressing_path() -> None:
class App:
gateway = Gateway()
@taped()
@bound(App, "gateway.charge").on_call.returns({"id": "STUB"})
def body(tape: Any, charge: Any) -> None:
assert App.gateway.charge(1) == {"id": "STUB"}
body()
def test_alias_overrides_the_derived_name() -> None:
@bound(Gateway, "charge", alias="gateway_charge").on_call.returns({"id": "STUB"})
def body(gateway_charge: Any) -> None:
assert Gateway().charge(1) == {"id": "STUB"}
body()
def test_a_non_identifier_slot_name_requires_alias() -> None:
registry: dict[str, Any] = {"content-type": lambda: "text"}
with pytest.raises(TypeError, match="pass alias="):
@bound(registry, item="content-type").overrides("json")
def body(**kwargs: Any) -> None: ...
@bound(registry, item="content-type", alias="content_type").overrides("json")
def body2(content_type: Any) -> None:
assert registry["content-type"] == "json"
body2()
def test_colliding_aliases_across_decorators_error() -> None:
class Channel:
def close(self) -> None: ...
class Transport:
def close(self) -> None: ...
with pytest.raises(TypeError, match="already injected"):
@bound(Transport, "close")
@bound(Channel, "close")
def body(close: Any, **kwargs: Any) -> None: ...
@taped()
@bound(Transport, "close", alias="transport_close")
@bound(Channel, "close", alias="channel_close")
def body2(tape: Any, transport_close: Any, channel_close: Any) -> None:
Channel().close()
Transport().close()
tape.assert_order(channel_close, transport_close)
body2()
def test_a_missing_parameter_without_kwargs_errors_at_decoration() -> None:
with pytest.raises(TypeError, match="no parameter of that name"):
@bound(Gateway, "charge")
def body() -> None: ...
# ---------------------------------------------------------------------------
# signature pruning
# ---------------------------------------------------------------------------
def test_each_layer_prunes_only_its_own_alias() -> None:
@taped()
@bound(Gateway, "charge")
def body(where: str, tape: Any, charge: Any) -> None: ...
assert list(inspect.signature(body).parameters) == ["where"]
def test_var_keyword_injection_needs_no_pruning() -> None:
@bound(Gateway, "charge")
def body(where: str, **kwargs: Any) -> None: ...
assert list(inspect.signature(body).parameters) == ["where", "kwargs"]
# ---------------------------------------------------------------------------
# collapsing decorators on the same target
# ---------------------------------------------------------------------------
def test_same_target_decorators_collapse_to_one_binding() -> None:
@taped()
@bound(Model, "status").on_get.returns(5)
@bound(Model, "status").on_set.raises(AttributeError("read-only"))
def body(tape: Any, status: Any) -> None:
model = Model()
assert model.status == 5
with pytest.raises(AttributeError, match="read-only"):
model.status = 9
status.events.assert_times(2)
body()
def test_collapsed_stages_compose_in_reading_order() -> None:
class Service:
def word(self) -> Any:
return "real"
@bound(Service, "word").on_call.transforms_result(lambda r: r + "!")
@bound(Service, "word").on_call.transforms_result(str.upper).returns("ab")
def body(word: Any) -> None:
# Reading order: the suffix stage is outermost, then upper, then
# the terminal, the same as those statements on a live binding.
assert Service().word() == "AB!"
body()
def test_collapsed_terminals_are_last_in_reading_order_wins() -> None:
@bound(Gateway, "charge").on_call.returns({"id": "upper"})
@bound(Gateway, "charge").on_call.returns({"id": "lower"})
def body(charge: Any) -> None:
assert Gateway().charge(1) == {"id": "lower"}
body()
def test_distinct_aliases_do_not_collapse() -> None:
@taped()
@bound(Gateway, "charge", alias="outer")
@bound(Gateway, "charge", alias="inner").on_call.returns({"id": "STUB"})
def body(tape: Any, outer: Any, inner: Any) -> None:
assert outer is not inner
assert Gateway().charge(1) == {"id": "STUB"}
body()
def test_equivalent_but_differently_spelled_addressing_does_not_merge() -> None:
with pytest.raises(TypeError, match="already injected"):
@bound(Gateway, "charge")
@bound("tests.test_decorators", "Gateway.charge")
def body(charge: Any) -> None: ...
# ---------------------------------------------------------------------------
# convention checks
# ---------------------------------------------------------------------------
def test_generator_functions_are_rejected() -> None:
with pytest.raises(TypeError, match="generator function"):
@bound(Gateway, "charge")
def gen(charge: Any) -> Any:
yield
with pytest.raises(TypeError, match="generator function"):
@taped()
async def agen(tape: Any) -> Any:
yield
def test_a_pytest_fixture_below_the_decorator_is_rejected() -> None:
with pytest.raises(TypeError, match="fixture"):
@bound(Gateway, "charge")
@pytest.fixture
def fix(charge: Any) -> None: ...
# ---------------------------------------------------------------------------
# async tests
# ---------------------------------------------------------------------------
def test_the_binding_spans_the_await() -> None:
@taped()
@bound(PushClient, "send").on_call.returns("queued")
async def body(tape: Any, send: Any) -> None:
assert await PushClient().send("ana") == "queued"
send.events.finished().assert_once()
assert inspect.iscoroutinefunction(body)
asyncio.run(body())
def test_async_teardown_runs_after_the_body() -> None:
order: list[str] = []
@bound(Gateway, "charge").on_call.returns({"id": "STUB"})
async def body(charge: Any) -> None:
order.append("body")
assert Gateway().charge(1) == {"id": "STUB"}
asyncio.run(body())
order.append("after")
assert order == ["body", "after"]
assert Gateway().charge(1) == {"id": "ch_1", "amount": 1}
# ---------------------------------------------------------------------------
# unittest.TestCase methods
# ---------------------------------------------------------------------------
def test_testcase_methods_are_supported() -> None:
class Case(unittest.TestCase):
@taped()
@bound(Gateway, "charge").on_call.returns({"id": "STUB"})
def test_it(self, tape: Any, charge: Any) -> None:
assert isinstance(self, Case)
assert Gateway().charge(1) == {"id": "STUB"}
charge.events.assert_once()
result = unittest.TestResult()
Case("test_it").run(result)
assert result.wasSuccessful(), result.errors + result.failures
def test_isolated_asyncio_testcase_methods_are_supported() -> None:
class Case(unittest.IsolatedAsyncioTestCase):
@taped()
@bound(PushClient, "send").on_call.returns("queued")
async def test_it(self, tape: Any, send: Any) -> None:
assert await PushClient().send("ana") == "queued"
send.events.finished().assert_once()
result = unittest.TestResult()
Case("test_it").run(result)
assert result.wasSuccessful(), result.errors + result.failures
# ---------------------------------------------------------------------------
# taped()
# ---------------------------------------------------------------------------
def test_taped_applies_the_bindings_it_is_given() -> None:
charge = binding(Gateway, "charge").on_call.returns({"id": "STUB"})
@taped(charge)
def body(tape: Any) -> None:
assert Gateway().charge(1) == {"id": "STUB"}
tape.assert_order(charge)
body()
assert Gateway().charge(1) == {"id": "ch_1", "amount": 1}
def test_taped_alias_renames_the_injected_tape() -> None:
@taped(alias="inner")
def body(inner: Any) -> None:
assert inner.pending == 0
body()
def test_taped_nests_under_an_ambient_tape() -> None:
charge = binding(Gateway, "charge").on_call.returns({"id": "STUB"})
@taped()
def body(tape: Any) -> None:
Gateway().charge(1)
tape.for_binding(charge).assert_once()
with charge, timeline() as ambient:
body()
ambient.for_binding(charge).assert_once()
def test_a_spec_in_a_variable_is_reusable_across_functions() -> None:
stubbed = bound(Gateway, "charge").on_call.returns({"id": "STUB"})
@taped()
@stubbed
def one(tape: Any, charge: Any) -> None:
Gateway().charge(1)
charge.events.assert_once()
@taped()
@stubbed
def two(tape: Any, charge: Any) -> None:
Gateway().charge(2)
charge.events.assert_once()
one()
two()
# ---------------------------------------------------------------------------
# declared expectations
# ---------------------------------------------------------------------------
def test_a_chain_expectation_is_verified_at_teardown() -> None:
@taped()
@bound(Gateway, "charge").on_call.returns({"id": "STUB"}).expect_once()
def body(tape: Any, charge: Any) -> None:
Gateway().charge(1)
body()
def test_an_unmet_chain_expectation_fails_the_test() -> None:
from wrapture import ExpectationNotMetError
@taped()
@bound(Gateway, "charge").expect_once()
def body(tape: Any, charge: Any) -> None:
pass
with pytest.raises(ExpectationNotMetError, match="Gateway.charge"):
body()
def test_expect_never_guards_the_negative_path() -> None:
from wrapture import ExpectationNotMetError
@taped()
@bound(Gateway, "charge").expect_never()
def body(tape: Any, charge: Any) -> None:
Gateway().charge(1)
with pytest.raises(ExpectationNotMetError):
body()
def test_a_body_declared_expectation_is_verified_too() -> None:
from wrapture import ExpectationNotMetError
@taped()
@bound(Gateway, "charge")
def body(tape: Any, charge: Any) -> None:
charge.expect_once()
with pytest.raises(ExpectationNotMetError):
body()
def test_a_raising_body_is_not_buried_by_verification() -> None:
@taped()
@bound(Gateway, "charge").expect_once()
def body(tape: Any, charge: Any) -> None:
raise KeyError("the real failure")
with pytest.raises(KeyError, match="the real failure"):
body()
def test_expectations_with_no_tape_error_loudly() -> None:
from wrapture import ExpectationNotMetError
@bound(Gateway, "charge").expect_once()
def body(charge: Any) -> None:
Gateway().charge(1)
with pytest.raises(ExpectationNotMetError, match="nothing was recording"):
body()
def test_expectations_route_through_a_channel_recorder() -> None:
@taped()
@bound(Gateway, "charge").on_call.returns({"id": "STUB"}).expect_once()
def body(tape: Any, charge: Any) -> None:
Gateway().charge(1)
body()
def test_expectations_merge_across_collapsed_decorators() -> None:
from wrapture import ExpectationNotMetError
@taped()
@bound(Gateway, "charge").expect_once()
@bound(Gateway, "charge").on_call.returns({"id": "STUB"})
def body(tape: Any, charge: Any) -> None:
pass
with pytest.raises(ExpectationNotMetError):
body()
def test_a_value_binding_cannot_carry_an_expectation() -> None:
with pytest.raises(WrongModeError, match="cannot carry an expectation"):
bound(os.environ, item="API_KEY").expect_once()
def test_an_async_expectation_is_verified() -> None:
from wrapture import ExpectationNotMetError
@taped()
@bound(PushClient, "send").on_call.returns("queued").expect_once()
async def body(tape: Any, send: Any) -> None:
pass
with pytest.raises(ExpectationNotMetError):
asyncio.run(body())