From 969fcec29bd4e4f319ecdda826d11bf1dcfb4c7f Mon Sep 17 00:00:00 2001 From: Tommy Smith Date: Wed, 18 Sep 2024 19:27:12 +0100 Subject: [PATCH] Add tests for sync client used in async API routes --- journey_tests/test_fastapi.py | 28 ++++++++++++++++++++-------- journey_tests/test_flask.py | 23 +++++++++++++++++------ journey_tests/test_litestar.py | 30 +++++++++++++++++++++--------- 3 files changed, 58 insertions(+), 23 deletions(-) diff --git a/journey_tests/test_fastapi.py b/journey_tests/test_fastapi.py index a838ebf79..36d8f7f04 100644 --- a/journey_tests/test_fastapi.py +++ b/journey_tests/test_fastapi.py @@ -30,25 +30,37 @@ async def lifespan(app: FastAPI): app = FastAPI(lifespan=lifespan) -@app.get("/sync") -def sync() -> JSONResponse: +@app.get("/sync-in-sync") +def sync_in_sync() -> JSONResponse: return JSONResponse(content=journeys["sync"].simple()) -@app.get("/async") -async def async_() -> JSONResponse: +@app.get("/sync-in-async") +async def sync_in_async() -> JSONResponse: + return JSONResponse(content=journeys["sync"].simple()) + + +@app.get("/async-in-async") +async def async_in_async() -> JSONResponse: return JSONResponse(content=await journeys["async_"].simple()) -def test_sync() -> None: +def test_sync_in_sync() -> None: + with TestClient(app) as client: + res = client.get("/sync-in-sync") + assert res.status_code == 200 + assert len(res.json()) == 100 + + +def test_sync_in_async() -> None: with TestClient(app) as client: - res = client.get("/sync") + res = client.get("/sync-in-async") assert res.status_code == 200 assert len(res.json()) == 100 -def test_async() -> None: +def test_async_in_async() -> None: with TestClient(app) as client: - res = client.get("/async") + res = client.get("/async-in-async") assert res.status_code == 200 assert len(res.json()) == 100 diff --git a/journey_tests/test_flask.py b/journey_tests/test_flask.py index 6db45b98f..de158128e 100644 --- a/journey_tests/test_flask.py +++ b/journey_tests/test_flask.py @@ -17,23 +17,34 @@ async def get_async_client() -> AsyncJourneys: return g.async_ -@app.route("/sync") +@app.route("/sync-in-sync") def sync() -> dict: return get_sync_client().simple() -@app.route("/async") +@app.route("/sync-in-async") +async def sync_() -> dict: + return get_sync_client().simple() + + +@app.route("/async-in-async") async def async_() -> dict: return await (await get_async_client()).simple() -def test_sync() -> None: - res = app.test_client().get("/sync") +def test_sync_in_sync() -> None: + res = app.test_client().get("/sync-in-sync") + assert res.status_code == 200 + assert len(res.json) == 100 + + +def test_sync_in_async() -> None: + res = app.test_client().get("/sync-in-async") assert res.status_code == 200 assert len(res.json) == 100 -def test_async() -> None: - res = app.test_client().get("/async") +def test_async_in_async() -> None: + res = app.test_client().get("/async-in-async") assert res.status_code == 200 assert len(res.json) == 100 diff --git a/journey_tests/test_litestar.py b/journey_tests/test_litestar.py index 93ca5b6d1..e1ce0222e 100644 --- a/journey_tests/test_litestar.py +++ b/journey_tests/test_litestar.py @@ -25,28 +25,40 @@ async def lifespan(app: Litestar): await async_.close() -@get("/sync", sync_to_thread=True) -def sync(state: MyState) -> dict: +@get("/sync-in-sync", sync_to_thread=True) +def sync_in_sync(state: MyState) -> dict: return state.sync.simple() -@get("/async") -async def async_(state: MyState) -> dict: +@get("/sync-in-async", sync_to_thread=True) +async def sync_in_async(state: MyState) -> dict: + return state.sync.simple() + + +@get("/async-in-async") +async def async_in_async(state: MyState) -> dict: return await state.async_.simple() -app = Litestar(route_handlers=[sync, async_], lifespan=[lifespan]) +app = Litestar(route_handlers=[sync_in_sync, sync_in_async, async_in_async], lifespan=[lifespan]) + + +def test_sync_in_sync() -> None: + with TestClient(app=app) as client: + res = client.get("/sync-in-sync") + assert res.status_code == 200 + assert len(res.json()) == 100 -def test_sync() -> None: +def test_sync_in_async() -> None: with TestClient(app=app) as client: - res = client.get("/sync") + res = client.get("/sync-in-async") assert res.status_code == 200 assert len(res.json()) == 100 -def test_async() -> None: +def test_async_in_async() -> None: with TestClient(app=app) as client: - res = client.get("/async") + res = client.get("/async-in-async") assert res.status_code == 200 assert len(res.json()) == 100