Skip to content

Commit

Permalink
fix fastapi tests
Browse files Browse the repository at this point in the history
  • Loading branch information
roman-right committed Oct 14, 2023
1 parent a8bc9c8 commit db68162
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 20 deletions.
2 changes: 1 addition & 1 deletion tests/fastapi/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@


@app.on_event("startup")
def app_init():
async def app_init():
# CREATE MOTOR CLIENT
client = MongoClient(Settings().mongodb_dsn)

Expand Down
6 changes: 3 additions & 3 deletions tests/fastapi/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
10 changes: 5 additions & 5 deletions tests/fastapi/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)])
)
Expand All @@ -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
22 changes: 11 additions & 11 deletions tests/fastapi/test_api.py
Original file line number Diff line number Diff line change
@@ -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"]}

0 comments on commit db68162

Please sign in to comment.