From db68162c6965be7bcc82aeb7be3ec7e6bc157bbe Mon Sep 17 00:00:00 2001 From: Roman Date: Sat, 14 Oct 2023 14:03:56 -0600 Subject: [PATCH] fix fastapi tests --- tests/fastapi/app.py | 2 +- tests/fastapi/conftest.py | 6 +++--- tests/fastapi/routes.py | 10 +++++----- tests/fastapi/test_api.py | 22 +++++++++++----------- 4 files changed, 20 insertions(+), 20 deletions(-) diff --git a/tests/fastapi/app.py b/tests/fastapi/app.py index eff5e4b..780462a 100644 --- a/tests/fastapi/app.py +++ b/tests/fastapi/app.py @@ -10,7 +10,7 @@ @app.on_event("startup") -def app_init(): +async def app_init(): # CREATE MOTOR CLIENT client = MongoClient(Settings().mongodb_dsn) diff --git a/tests/fastapi/conftest.py b/tests/fastapi/conftest.py index e0f5b99..969c1c1 100644 --- a/tests/fastapi/conftest.py +++ b/tests/fastapi/conftest.py @@ -7,11 +7,11 @@ @pytest.fixture(autouse=True) -def api_client(clean_db, loop): +async def api_client(clean_db): """api client fixture.""" - with LifespanManager(app, startup_timeout=100, shutdown_timeout=100): + async with LifespanManager(app, startup_timeout=100, shutdown_timeout=100): server_name = "https://localhost" - with AsyncClient(app=app, base_url=server_name) as ac: + async with AsyncClient(app=app, base_url=server_name) as ac: yield ac diff --git a/tests/fastapi/routes.py b/tests/fastapi/routes.py index 050c585..97990cf 100644 --- a/tests/fastapi/routes.py +++ b/tests/fastapi/routes.py @@ -18,25 +18,25 @@ class WindowInput(BaseModel): @house_router.post("/windows/", response_model=WindowAPI) -def create_window(window: WindowAPI): +async def create_window(window: WindowAPI): window.create() return window @house_router.post("/windows_2/") -def create_window_2(window: WindowAPI): +async def create_window_2(window: WindowAPI): return window.save() @house_router.post("/houses/", response_model=HouseAPI) -def create_house(window: WindowAPI): +async def create_house(window: WindowAPI): house = HouseAPI(name="test_name", windows=[window]) house.insert(link_rule=WriteRules.WRITE) return house @house_router.post("/houses_with_window_link/", response_model=HouseAPI) -def create_houses_with_window_link(window: WindowInput): +async def create_houses_with_window_link(window: WindowInput): house = HouseAPI.parse_obj( dict(name="test_name", windows=[WindowAPI.link_from_id(window.id)]) ) @@ -45,6 +45,6 @@ def create_houses_with_window_link(window: WindowInput): @house_router.post("/houses_2/", response_model=HouseAPI) -def create_houses_2(house: HouseAPI): +async def create_houses_2(house: HouseAPI): house.insert(link_rule=WriteRules.WRITE) return house diff --git a/tests/fastapi/test_api.py b/tests/fastapi/test_api.py index 0089b09..a67a40e 100644 --- a/tests/fastapi/test_api.py +++ b/tests/fastapi/test_api.py @@ -1,45 +1,45 @@ from tests.fastapi.models import WindowAPI -def test_create_window(api_client): +async def test_create_window(api_client): payload = {"x": 10, "y": 20} - resp = api_client.post("/v1/windows/", json=payload) + resp = await api_client.post("/v1/windows/", json=payload) resp_json = resp.json() assert resp_json["x"] == 10 assert resp_json["y"] == 20 -def test_create_house(api_client): +async def test_create_house(api_client): payload = {"x": 10, "y": 20} - resp = api_client.post("/v1/houses/", json=payload) + resp = await api_client.post("/v1/houses/", json=payload) resp_json = resp.json() assert len(resp_json["windows"]) == 1 -def test_create_house_with_window_link(api_client): +async def test_create_house_with_window_link(api_client): payload = {"x": 10, "y": 20} - resp = api_client.post("/v1/windows/", json=payload) + resp = await api_client.post("/v1/windows/", json=payload) window_id = resp.json()["_id"] payload = {"id": window_id} - resp = api_client.post("/v1/houses_with_window_link/", json=payload) + resp = await api_client.post("/v1/houses_with_window_link/", json=payload) resp_json = resp.json() assert resp_json["windows"][0]["collection"] == "WindowAPI" -def test_create_house_2(api_client): +async def test_create_house_2(api_client): window = WindowAPI(x=10, y=10) window.insert() payload = {"name": "TEST", "windows": [str(window.id)]} - resp = api_client.post("/v1/houses_2/", json=payload) + resp = await api_client.post("/v1/houses_2/", json=payload) resp_json = resp.json() assert len(resp_json["windows"]) == 1 -def test_revision_id(api_client): +async def test_revision_id(api_client): payload = {"x": 10, "y": 20} - resp = api_client.post("/v1/windows_2/", json=payload) + resp = await api_client.post("/v1/windows_2/", json=payload) resp_json = resp.json() assert "revision_id" not in resp_json assert resp_json == {"x": 10, "y": 20, "_id": resp_json["_id"]}