|
1 | 1 | import collections.abc |
| 2 | +import dataclasses |
2 | 3 | import http |
| 4 | +import typing |
3 | 5 |
|
4 | 6 | import modern_di |
5 | 7 | from aiohttp import web |
6 | | -from aiohttp.test_utils import TestClient |
| 8 | +from aiohttp.test_utils import TestClient, TestServer |
| 9 | +from modern_di import providers |
7 | 10 |
|
8 | | -from modern_di_aiohttp import fetch_di_container, setup_di |
| 11 | +from modern_di_aiohttp import FromDI, fetch_di_container, inject, setup_di |
9 | 12 | from modern_di_aiohttp.main import _on_cleanup, _on_startup |
10 | 13 | from tests.dependencies import Dependencies |
11 | 14 |
|
@@ -42,3 +45,75 @@ async def test_cleanup_closes_root() -> None: |
42 | 45 | await _on_cleanup(app) |
43 | 46 | assert container.closed |
44 | 47 | assert fetch_di_container(app) is container |
| 48 | + |
| 49 | + |
| 50 | +@dataclasses.dataclass(kw_only=True, slots=True) |
| 51 | +class Label: |
| 52 | + name: str |
| 53 | + |
| 54 | + |
| 55 | +def build_unmanaged_app(finalized: list[str]) -> tuple[web.Application, modern_di.Container]: |
| 56 | + class LabelDependencies(modern_di.Group): |
| 57 | + app_label = providers.Factory( |
| 58 | + creator=lambda: Label(name="app"), |
| 59 | + bound_type=None, |
| 60 | + cache=providers.CacheSettings(finalizer=lambda label: finalized.append(label.name)), |
| 61 | + ) |
| 62 | + request_label = providers.Factory( |
| 63 | + scope=modern_di.Scope.REQUEST, |
| 64 | + creator=lambda: Label(name="request"), |
| 65 | + bound_type=None, |
| 66 | + cache=providers.CacheSettings(finalizer=lambda label: finalized.append(label.name)), |
| 67 | + ) |
| 68 | + |
| 69 | + @inject |
| 70 | + async def labels( |
| 71 | + request: web.Request, # noqa: ARG001 |
| 72 | + app_label: typing.Annotated[Label, FromDI(LabelDependencies.app_label)], |
| 73 | + request_label: typing.Annotated[Label, FromDI(LabelDependencies.request_label)], |
| 74 | + ) -> web.Response: |
| 75 | + return web.Response(text=f"{app_label.name},{request_label.name}") |
| 76 | + |
| 77 | + app = web.Application() |
| 78 | + container = modern_di.Container(groups=[LabelDependencies]) |
| 79 | + setup_di(app, container, manage_lifespan=False) |
| 80 | + app.router.add_get("/", labels) |
| 81 | + return app, container |
| 82 | + |
| 83 | + |
| 84 | +async def test_manage_lifespan_false_leaves_the_root_alone() -> None: |
| 85 | + finalized: list[str] = [] |
| 86 | + app, container = build_unmanaged_app(finalized) |
| 87 | + |
| 88 | + async with TestClient(TestServer(app)) as client: |
| 89 | + response = await client.get("/") |
| 90 | + assert await response.text() == "app,request" |
| 91 | + assert finalized == ["request"] |
| 92 | + |
| 93 | + assert not container.closed |
| 94 | + assert finalized == ["request"] |
| 95 | + |
| 96 | + |
| 97 | +async def test_manage_lifespan_false_does_not_reopen_a_closed_root() -> None: |
| 98 | + app, container = build_unmanaged_app([]) |
| 99 | + container.close_sync() |
| 100 | + |
| 101 | + async with TestClient(TestServer(app)) as client: |
| 102 | + response = await client.get("/") |
| 103 | + assert response.status == http.HTTPStatus.INTERNAL_SERVER_ERROR |
| 104 | + |
| 105 | + assert container.closed |
| 106 | + |
| 107 | + |
| 108 | +async def test_user_owned_root_runs_app_finalizer_once_at_exit() -> None: |
| 109 | + finalized: list[str] = [] |
| 110 | + app, container = build_unmanaged_app(finalized) |
| 111 | + |
| 112 | + async with container: |
| 113 | + async with TestClient(TestServer(app)) as client: |
| 114 | + first = await client.get("/") |
| 115 | + second = await client.get("/") |
| 116 | + assert (await first.text(), await second.text()) == ("app,request", "app,request") |
| 117 | + assert finalized == ["request", "request"] |
| 118 | + |
| 119 | + assert finalized == ["request", "request", "app"] |
0 commit comments