|
| 1 | +"""Wiring discovery against Cython-compiled user modules.""" |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +pytest.importorskip("Cython") |
| 6 | + |
| 7 | +import pyximport # noqa: E402 |
| 8 | + |
| 9 | +pyximport.install(language_level=3) |
| 10 | + |
| 11 | +cythonmodule = pytest.importorskip( |
| 12 | + "samples.wiringcython.cythonmodule", |
| 13 | + reason="Cython fixture not built (Cython / C toolchain missing)", |
| 14 | +) |
| 15 | + |
| 16 | +from samples.wiringcython.container import Container, Service # noqa: E402 |
| 17 | + |
| 18 | +from dependency_injector import providers # noqa: E402 |
| 19 | +from dependency_injector.wiring import ( # noqa: E402 |
| 20 | + _is_cyfunction, |
| 21 | + _is_function_like, |
| 22 | + _patched_registry, |
| 23 | +) |
| 24 | + |
| 25 | + |
| 26 | +@pytest.fixture |
| 27 | +def container(): |
| 28 | + c = Container() |
| 29 | + c.wire(modules=[cythonmodule]) |
| 30 | + yield c |
| 31 | + c.unwire() |
| 32 | + |
| 33 | + |
| 34 | +def _pure_python_fn(): |
| 35 | + pass |
| 36 | + |
| 37 | + |
| 38 | +@pytest.mark.parametrize( |
| 39 | + "obj,is_cy,is_func_like", |
| 40 | + [ |
| 41 | + pytest.param(lambda: cythonmodule.sync_handler, True, True, id="cython-sync"), |
| 42 | + pytest.param(lambda: cythonmodule.async_handler, True, True, id="cython-async"), |
| 43 | + pytest.param( |
| 44 | + lambda: cythonmodule.async_gen_handler, True, True, id="cython-async-gen" |
| 45 | + ), |
| 46 | + pytest.param( |
| 47 | + lambda: cythonmodule.HandlerClass.__call__, |
| 48 | + True, |
| 49 | + True, |
| 50 | + id="cython-class-call", |
| 51 | + ), |
| 52 | + pytest.param(lambda: _pure_python_fn, False, True, id="pure-python"), |
| 53 | + ], |
| 54 | +) |
| 55 | +def test_function_like_predicate(obj, is_cy, is_func_like): |
| 56 | + target = obj() |
| 57 | + assert _is_cyfunction(target) is is_cy |
| 58 | + assert _is_function_like(target) is is_func_like |
| 59 | + |
| 60 | + |
| 61 | +def test_sync_handler_wired(container): |
| 62 | + assert cythonmodule.sync_handler() == "injected" |
| 63 | + |
| 64 | + |
| 65 | +@pytest.mark.asyncio |
| 66 | +async def test_async_handler_wired(container): |
| 67 | + assert await cythonmodule.async_handler() == "injected" |
| 68 | + |
| 69 | + |
| 70 | +@pytest.mark.asyncio |
| 71 | +async def test_async_gen_handler_wired(container): |
| 72 | + results = [v async for v in cythonmodule.async_gen_handler()] |
| 73 | + assert results == ["injected", "injected_2"] |
| 74 | + |
| 75 | + |
| 76 | +@pytest.mark.asyncio |
| 77 | +async def test_class_method_wired(container): |
| 78 | + handler = cythonmodule.HandlerClass() |
| 79 | + assert await handler() == "injected" |
| 80 | + |
| 81 | + |
| 82 | +def test_sync_handler_respects_provider_override(container): |
| 83 | + with container.service.override(providers.Object(Service(value="overridden"))): |
| 84 | + assert cythonmodule.sync_handler() == "overridden" |
| 85 | + assert cythonmodule.sync_handler() == "injected" |
| 86 | + |
| 87 | + |
| 88 | +def test_unwire_clears_injection_bindings_on_compiled_module(): |
| 89 | + c = Container() |
| 90 | + c.wire(modules=[cythonmodule]) |
| 91 | + |
| 92 | + wrapper = cythonmodule.sync_handler |
| 93 | + patched = _patched_registry.get_callable(wrapper) |
| 94 | + |
| 95 | + assert patched is not None |
| 96 | + assert patched.reference_injections |
| 97 | + assert patched.injections |
| 98 | + |
| 99 | + c.unwire() |
| 100 | + |
| 101 | + assert patched.injections == {} |
| 102 | + assert patched.reference_injections |
0 commit comments