Skip to content

Commit

Permalink
Merge pull request #1310 from weaviate/tests/add-sync-in-async-api-jo…
Browse files Browse the repository at this point in the history
…urney-tests

Add tests for sync client used in async API routes
  • Loading branch information
tsmith023 authored Sep 19, 2024
2 parents 5c5cb07 + 969fcec commit feb0cfd
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 23 deletions.
28 changes: 20 additions & 8 deletions journey_tests/test_fastapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
23 changes: 17 additions & 6 deletions journey_tests/test_flask.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
30 changes: 21 additions & 9 deletions journey_tests/test_litestar.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit feb0cfd

Please sign in to comment.