|
| 1 | +import collections.abc |
| 2 | +import dataclasses |
| 3 | +import http |
| 4 | +import typing |
| 5 | + |
| 6 | +from aiohttp import web |
| 7 | +from aiohttp.test_utils import TestClient |
| 8 | +from modern_di import Container, Group, Scope, providers |
| 9 | +from modern_di.providers import CacheSettings |
| 10 | + |
| 11 | +from modern_di_aiohttp import FromDI, fetch_request_container, inject, setup_di |
| 12 | + |
| 13 | + |
| 14 | +AiohttpClient = collections.abc.Callable[[web.Application], collections.abc.Awaitable[TestClient]] |
| 15 | + |
| 16 | + |
| 17 | +@dataclasses.dataclass(kw_only=True, slots=True) |
| 18 | +class Label: |
| 19 | + name: str |
| 20 | + |
| 21 | + |
| 22 | +def make_group(name: str, finalized: list[str]) -> type[Group]: |
| 23 | + class AppDependencies(Group): |
| 24 | + label = providers.Factory( |
| 25 | + scope=Scope.REQUEST, |
| 26 | + creator=lambda: Label(name=name), |
| 27 | + bound_type=Label, |
| 28 | + cache=CacheSettings(finalizer=lambda label: finalized.append(label.name)), |
| 29 | + ) |
| 30 | + |
| 31 | + return AppDependencies |
| 32 | + |
| 33 | + |
| 34 | +@inject |
| 35 | +async def label_view( |
| 36 | + request: web.Request, # noqa: ARG001 |
| 37 | + label: typing.Annotated[Label, FromDI(Label)], |
| 38 | +) -> web.Response: |
| 39 | + return web.Response(text=label.name) |
| 40 | + |
| 41 | + |
| 42 | +@web.middleware |
| 43 | +async def resolve_label_middleware( |
| 44 | + request: web.Request, |
| 45 | + handler: typing.Callable[[web.Request], typing.Awaitable[web.StreamResponse]], |
| 46 | +) -> web.StreamResponse: |
| 47 | + fetch_request_container(request).resolve(Label) |
| 48 | + return await handler(request) |
| 49 | + |
| 50 | + |
| 51 | +def build_apps(*, sub_has_di: bool, finalized: list[str]) -> web.Application: |
| 52 | + sub = web.Application() |
| 53 | + sub.router.add_get("/x", label_view) |
| 54 | + if sub_has_di: |
| 55 | + setup_di(sub, Container(groups=[make_group("sub", finalized)])) |
| 56 | + |
| 57 | + parent = web.Application() |
| 58 | + parent.router.add_get("/x", label_view) |
| 59 | + setup_di(parent, Container(groups=[make_group("parent", finalized)])) |
| 60 | + parent.middlewares.append(resolve_label_middleware) |
| 61 | + parent.add_subapp("/sub", sub) |
| 62 | + return parent |
| 63 | + |
| 64 | + |
| 65 | +async def test_each_app_resolves_from_its_own_container(aiohttp_client: AiohttpClient) -> None: |
| 66 | + client = await aiohttp_client(build_apps(sub_has_di=True, finalized=[])) |
| 67 | + |
| 68 | + parent_response = await client.get("/x") |
| 69 | + assert parent_response.status == http.HTTPStatus.OK |
| 70 | + assert await parent_response.text() == "parent" |
| 71 | + |
| 72 | + sub_response = await client.get("/sub/x") |
| 73 | + assert sub_response.status == http.HTTPStatus.OK |
| 74 | + assert await sub_response.text() == "sub" |
| 75 | + |
| 76 | + |
| 77 | +async def test_sub_app_without_setup_di_resolves_from_parent(aiohttp_client: AiohttpClient) -> None: |
| 78 | + client = await aiohttp_client(build_apps(sub_has_di=False, finalized=[])) |
| 79 | + |
| 80 | + response = await client.get("/sub/x") |
| 81 | + assert response.status == http.HTTPStatus.OK |
| 82 | + assert await response.text() == "parent" |
| 83 | + |
| 84 | + |
| 85 | +async def test_both_child_containers_closed_after_sub_app_request(aiohttp_client: AiohttpClient) -> None: |
| 86 | + finalized: list[str] = [] |
| 87 | + client = await aiohttp_client(build_apps(sub_has_di=True, finalized=finalized)) |
| 88 | + |
| 89 | + assert (await client.get("/sub/x")).status == http.HTTPStatus.OK |
| 90 | + assert sorted(finalized) == ["parent", "sub"] |
0 commit comments