-
Notifications
You must be signed in to change notification settings - Fork 119
Expand file tree
/
Copy pathtest_litestar.py
More file actions
221 lines (195 loc) · 5.79 KB
/
Copy pathtest_litestar.py
File metadata and controls
221 lines (195 loc) · 5.79 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
import importlib.util
from contextlib import asynccontextmanager
from unittest.mock import Mock
import litestar
import pytest
from asgi_lifespan import LifespanManager
from litestar import Request, get, websocket_listener
from litestar.contrib.htmx.request import HTMXRequest
from litestar.testing import TestClient
try:
importlib.util.find_spec("litestar.plugins.InitPlugin")
HAS_PLUGINS = True
except ImportError:
HAS_PLUGINS = False
from dishka import make_async_container
from dishka.integrations.litestar import (
DishkaRouter,
FromDishka,
inject,
setup_dishka,
)
from ..common import (
APP_DEP_VALUE,
REQUEST_DEP_VALUE,
AppDep,
AppProvider,
RequestDep,
)
@asynccontextmanager
async def dishka_app(
view,
provider,
request_class: type[Request] = Request,
) -> TestClient:
app = litestar.Litestar(request_class=request_class)
app.register(get("/")(inject(view)))
app.register(websocket_listener("/ws")(websocket_handler))
container = make_async_container(provider)
setup_dishka(container, app)
async with LifespanManager(app):
yield TestClient(app)
await container.close()
@asynccontextmanager
async def dishka_plugin_app(
view,
provider,
request_class: type[Request] = Request,
) -> TestClient:
from dishka.integrations.litestar import DishkaPlugin
container = make_async_container(provider)
app = litestar.Litestar(request_class=request_class,
plugins=[DishkaPlugin(container)])
app.register(get("/")(inject(view)))
app.register(websocket_listener("/ws")(websocket_handler))
async with LifespanManager(app):
yield TestClient(app)
await container.close()
@asynccontextmanager
async def dishka_auto_app(
view,
provider,
request_class: type[Request] = Request,
) -> TestClient:
router = DishkaRouter("", route_handlers=[])
router.register(get("/")(view))
router.register(websocket_listener("/ws")(websocket_handler))
app = litestar.Litestar([router], request_class=request_class)
container = make_async_container(provider)
setup_dishka(container, app)
async with LifespanManager(app):
yield TestClient(app)
await container.close()
async def websocket_handler(data: str):
pass
def get_with_app(request_class: type[Request]):
async def handler(
request: request_class,
a: FromDishka[AppDep],
mock: FromDishka[Mock],
) -> None:
mock(a)
return handler
def get_with_request(request_class: type[Request]):
async def handler(
request: request_class,
a: FromDishka[RequestDep],
mock: FromDishka[Mock],
) -> None:
mock(a)
return handler
@pytest.mark.parametrize(
("request_class", "app_factory"),
[
(Request, dishka_app),
(HTMXRequest, dishka_app),
(Request, dishka_auto_app),
(HTMXRequest, dishka_auto_app),
*((
(Request, dishka_plugin_app),
(HTMXRequest, dishka_plugin_app),
) if HAS_PLUGINS else ()),
],
)
@pytest.mark.asyncio
async def test_app_dependency(
request_class,
app_factory,
app_provider: AppProvider,
):
async with app_factory(
get_with_app(request_class),
app_provider,
request_class,
) as client:
client.get("/")
app_provider.mock.assert_called_with(APP_DEP_VALUE)
app_provider.app_released.assert_not_called()
app_provider.app_released.assert_called()
@pytest.mark.parametrize(
("request_class", "app_factory"),
[
(Request, dishka_app),
(HTMXRequest, dishka_app),
(Request, dishka_auto_app),
(HTMXRequest, dishka_auto_app),
*((
(Request, dishka_plugin_app),
(HTMXRequest, dishka_plugin_app),
) if HAS_PLUGINS else ()),
],
)
@pytest.mark.asyncio
async def test_request_dependency(
request_class,
app_factory,
app_provider: AppProvider,
):
async with app_factory(
get_with_request(request_class),
app_provider,
request_class,
) as client:
client.get("/")
app_provider.mock.assert_called_with(REQUEST_DEP_VALUE)
app_provider.request_released.assert_called_once()
@pytest.mark.parametrize(
("request_class", "app_factory"),
[
(Request, dishka_app),
(HTMXRequest, dishka_app),
(Request, dishka_auto_app),
(HTMXRequest, dishka_auto_app),
*((
(Request, dishka_plugin_app),
(HTMXRequest, dishka_plugin_app),
) if HAS_PLUGINS else ()),
],
)
@pytest.mark.asyncio
async def test_request_dependency2(
request_class,
app_factory,
app_provider: AppProvider,
):
async with app_factory(
get_with_request(request_class),
app_provider,
request_class,
) as client:
client.get("/")
app_provider.mock.assert_called_with(REQUEST_DEP_VALUE)
app_provider.mock.reset_mock()
app_provider.request_released.assert_called_once()
app_provider.request_released.reset_mock()
client.get("/")
app_provider.mock.assert_called_with(REQUEST_DEP_VALUE)
app_provider.request_released.assert_called_once()
@pytest.mark.asyncio
async def test_request_middleware(app_provider: AppProvider):
async with dishka_app(
get_with_request(Request),
app_provider,
Request,
) as client:
with client.websocket_connect("/ws") as websocket:
websocket.send("test")
@pytest.mark.asyncio
async def test_auto_request_middleware(app_provider: AppProvider):
async with dishka_auto_app(
get_with_request(Request),
app_provider,
Request,
) as client:
with client.websocket_connect("/ws") as websocket:
websocket.send("test")