|
1 | | -# litestart not yet supported by 3.13 |
2 | | -# from contextlib import asynccontextmanager |
3 | | -# |
4 | | -# from litestar import Litestar, get |
5 | | -# from litestar.datastructures import State |
6 | | -# from litestar.testing import TestClient |
7 | | -# |
8 | | -# from journey_tests.journeys import AsyncJourneys, SyncJourneys |
9 | | -# |
10 | | -# class MyState(State): |
11 | | -# sync: SyncJourneys |
12 | | -# async_: AsyncJourneys |
13 | | -# |
14 | | -# |
15 | | -# @asynccontextmanager |
16 | | -# async def lifespan(app: Litestar): |
17 | | -# sync = SyncJourneys.use() |
18 | | -# async_ = await AsyncJourneys.use() |
19 | | -# app.state.sync = sync |
20 | | -# app.state.async_ = async_ |
21 | | -# try: |
22 | | -# yield |
23 | | -# finally: |
24 | | -# sync.close() |
25 | | -# await async_.close() |
26 | | -# |
27 | | -# |
28 | | -# @get("/sync-in-sync", sync_to_thread=True) |
29 | | -# def sync_in_sync(state: MyState) -> dict: |
30 | | -# return state.sync.simple() |
31 | | -# |
32 | | -# |
33 | | -# @get("/sync-in-async", sync_to_thread=True) |
34 | | -# async def sync_in_async(state: MyState) -> dict: |
35 | | -# return state.sync.simple() |
36 | | -# |
37 | | -# |
38 | | -# @get("/async-in-async") |
39 | | -# async def async_in_async(state: MyState) -> dict: |
40 | | -# return await state.async_.simple() |
41 | | -# |
42 | | -# |
43 | | -# app = Litestar(route_handlers=[sync_in_sync, sync_in_async, async_in_async], lifespan=[lifespan]) |
44 | | -# |
45 | | -# |
46 | | -# def test_sync_in_sync() -> None: |
47 | | -# with TestClient(app=app) as client: |
48 | | -# res = client.get("/sync-in-sync") |
49 | | -# assert res.status_code == 200 |
50 | | -# assert len(res.json()) == 100 |
51 | | -# |
52 | | -# |
53 | | -# def test_sync_in_async() -> None: |
54 | | -# with TestClient(app=app) as client: |
55 | | -# res = client.get("/sync-in-async") |
56 | | -# assert res.status_code == 200 |
57 | | -# assert len(res.json()) == 100 |
58 | | -# |
59 | | -# |
60 | | -# def test_async_in_async() -> None: |
61 | | -# with TestClient(app=app) as client: |
62 | | -# res = client.get("/async-in-async") |
63 | | -# assert res.status_code == 200 |
64 | | -# assert len(res.json()) == 100 |
| 1 | +from contextlib import asynccontextmanager |
| 2 | +from typing import List |
| 3 | + |
| 4 | +from litestar import Litestar, get |
| 5 | +from litestar.datastructures import State |
| 6 | +from litestar.testing import TestClient |
| 7 | + |
| 8 | +from journey_tests.journeys import AsyncJourneys, SyncJourneys |
| 9 | + |
| 10 | + |
| 11 | +class MyState(State): |
| 12 | + sync: SyncJourneys |
| 13 | + async_: AsyncJourneys |
| 14 | + |
| 15 | + |
| 16 | +@asynccontextmanager |
| 17 | +async def lifespan(app: Litestar): |
| 18 | + sync = SyncJourneys.use() |
| 19 | + async_ = await AsyncJourneys.use() |
| 20 | + app.state.sync = sync |
| 21 | + app.state.async_ = async_ |
| 22 | + try: |
| 23 | + yield |
| 24 | + finally: |
| 25 | + sync.close() |
| 26 | + await async_.close() |
| 27 | + |
| 28 | + |
| 29 | +@get("/sync-in-sync", sync_to_thread=True) |
| 30 | +def sync_in_sync(state: MyState) -> List[dict]: |
| 31 | + return state.sync.simple() |
| 32 | + |
| 33 | + |
| 34 | +@get("/sync-in-async", sync_to_thread=True) |
| 35 | +async def sync_in_async(state: MyState) -> List[dict]: |
| 36 | + return state.sync.simple() |
| 37 | + |
| 38 | + |
| 39 | +@get("/async-in-async") |
| 40 | +async def async_in_async(state: MyState) -> List[dict]: |
| 41 | + return await state.async_.simple() |
| 42 | + |
| 43 | + |
| 44 | +app = Litestar(route_handlers=[sync_in_sync, sync_in_async, async_in_async], lifespan=[lifespan]) |
| 45 | + |
| 46 | + |
| 47 | +def test_sync_in_sync() -> None: |
| 48 | + with TestClient(app=app) as client: |
| 49 | + res = client.get("/sync-in-sync") |
| 50 | + assert res.status_code == 200 |
| 51 | + assert len(res.json()) == 100 |
| 52 | + |
| 53 | + |
| 54 | +def test_sync_in_async() -> None: |
| 55 | + with TestClient(app=app) as client: |
| 56 | + res = client.get("/sync-in-async") |
| 57 | + assert res.status_code == 200 |
| 58 | + assert len(res.json()) == 100 |
| 59 | + |
| 60 | + |
| 61 | +def test_async_in_async() -> None: |
| 62 | + with TestClient(app=app) as client: |
| 63 | + res = client.get("/async-in-async") |
| 64 | + assert res.status_code == 200 |
| 65 | + assert len(res.json()) == 100 |
0 commit comments