|
| 1 | +from collections.abc import Generator |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from django_subatomic import _utils as utils |
| 6 | + |
| 7 | + |
| 8 | +class _AnError(Exception): |
| 9 | + pass |
| 10 | + |
| 11 | + |
| 12 | +class TestContextManager: |
| 13 | + """ |
| 14 | + Tests for the `contextmanager` decorator. |
| 15 | + """ |
| 16 | + |
| 17 | + def test_context_manager(self) -> None: |
| 18 | + """ |
| 19 | + Generators that yield once can be used as context managers. |
| 20 | + """ |
| 21 | + |
| 22 | + @utils.contextmanager |
| 23 | + def good_example(steps: list[str]) -> Generator[str, None, None]: |
| 24 | + steps.append("enter") |
| 25 | + yield "yielded" |
| 26 | + steps.append("exit") |
| 27 | + |
| 28 | + steps: list[str] = [] |
| 29 | + with good_example(steps) as yielded: |
| 30 | + steps.append("body") |
| 31 | + |
| 32 | + assert steps == ["enter", "body", "exit"] |
| 33 | + assert yielded == "yielded" |
| 34 | + |
| 35 | + def test_context_body_raises_exception(self) -> None: |
| 36 | + """ |
| 37 | + Exceptions raised in the context body are propagated. |
| 38 | + """ |
| 39 | + |
| 40 | + @utils.contextmanager |
| 41 | + def basic_context() -> Generator[None, None, None]: |
| 42 | + yield |
| 43 | + |
| 44 | + with pytest.raises(_AnError): |
| 45 | + with basic_context(): |
| 46 | + raise _AnError |
| 47 | + |
| 48 | + def test_context_manager_handles_exception(self) -> None: |
| 49 | + """ |
| 50 | + Context managers may handle exceptions raised in the context body. |
| 51 | + """ |
| 52 | + |
| 53 | + @utils.contextmanager |
| 54 | + def handle_exception() -> Generator[None, None, None]: |
| 55 | + # Make sure the error is raised, but don't propagate it. |
| 56 | + with pytest.raises(_AnError): |
| 57 | + yield |
| 58 | + |
| 59 | + with handle_exception(): |
| 60 | + raise _AnError |
| 61 | + |
| 62 | + def test_context_manager_handles_exception_but_then_yields(self) -> None: |
| 63 | + """ |
| 64 | + A second yield must not happen after an exception is handled. |
| 65 | + """ |
| 66 | + |
| 67 | + @utils.contextmanager |
| 68 | + def handle_exception_but_yield_twice() -> Generator[None, None, None]: |
| 69 | + try: |
| 70 | + yield |
| 71 | + except _AnError: |
| 72 | + yield |
| 73 | + |
| 74 | + with pytest.raises(utils.UnexpectedSecondYield): |
| 75 | + with handle_exception_but_yield_twice(): |
| 76 | + raise _AnError |
| 77 | + |
| 78 | + def test_fails_without_yield(self) -> None: |
| 79 | + """ |
| 80 | + Decorated functions must yield once. |
| 81 | + """ |
| 82 | + |
| 83 | + @utils.contextmanager |
| 84 | + def yield_not_run() -> Generator[None, None, None]: |
| 85 | + if False: |
| 86 | + # Yield makes this a generator, but the yield is never run. |
| 87 | + yield # type: ignore[unreachable] |
| 88 | + |
| 89 | + with pytest.raises(utils.DidNotYield): |
| 90 | + with yield_not_run(): |
| 91 | + ... |
| 92 | + |
| 93 | + def test_fails_on_multiple_yields(self) -> None: |
| 94 | + """ |
| 95 | + Decorated functions must not yield multiple times. |
| 96 | + """ |
| 97 | + |
| 98 | + @utils.contextmanager |
| 99 | + def yields_twice(steps: list[str]) -> Generator[None, None, None]: |
| 100 | + steps.append("enter") |
| 101 | + yield |
| 102 | + steps.append("between") |
| 103 | + yield # This line causes the failure. |
| 104 | + # The following line will never be reached. |
| 105 | + steps.append("after") # pragma: no cover |
| 106 | + |
| 107 | + steps: list[str] = [] |
| 108 | + with pytest.raises(utils.UnexpectedSecondYield): |
| 109 | + with yields_twice(steps): |
| 110 | + steps.append("body") |
| 111 | + |
| 112 | + assert steps == ["enter", "body", "between"] |
| 113 | + |
| 114 | + def test_does_not_work_as_a_decorator(self) -> None: |
| 115 | + """ |
| 116 | + Functions decorated with `contextmanager` cannot be used as decorators. |
| 117 | + """ |
| 118 | + |
| 119 | + @utils.contextmanager |
| 120 | + def not_a_decorator() -> Generator[None, None, None]: |
| 121 | + yield # pragma: no cover |
| 122 | + |
| 123 | + with pytest.raises( |
| 124 | + TypeError, match="takes 0 positional arguments but 1 was given" |
| 125 | + ): |
| 126 | + # This doesn't work as a decorator. |
| 127 | + @not_a_decorator # type: ignore[call-arg] |
| 128 | + def not_reached() -> None: ... |
0 commit comments