tests: dynamic test standardised
The dynamic test structure should now be able applicable to all endpoints and the bulk of its logic has been split into a new function.
This commit is contained in:
parent
ed01e2515f
commit
41df580a1a
2 changed files with 43 additions and 25 deletions
|
|
@ -1,7 +1,7 @@
|
|||
from fastapi.dependencies.models import Dependant
|
||||
import pytest
|
||||
|
||||
from typing import AsyncGenerator
|
||||
from typing import AsyncGenerator, Any
|
||||
from itertools import combinations
|
||||
from fastapi.routing import APIRoute, iter_route_contexts
|
||||
from httpx import AsyncClient, ASGITransport
|
||||
|
|
@ -308,6 +308,42 @@ async def route_data():
|
|||
return routes
|
||||
|
||||
|
||||
async def standard_test(
|
||||
client: AsyncClient,
|
||||
method: str,
|
||||
path: str,
|
||||
auth_level: str,
|
||||
query: str,
|
||||
body: dict,
|
||||
expected_data: dict,
|
||||
route_data: dict[str, dict[str, Any]],
|
||||
):
|
||||
route = route_data.get(f"{method.upper()}{path}")
|
||||
assert route is not None
|
||||
|
||||
req_func = getattr(client, method.lower())
|
||||
if method in ["GET", "DELETE"]:
|
||||
resp = await req_func(url=f"{path}{query}")
|
||||
else:
|
||||
resp = await req_func(url=f"{path}{query}", json=body)
|
||||
|
||||
assert route["auth_level"] == auth_level
|
||||
|
||||
assert resp.status_code == route["status_code"]
|
||||
if route["status_code"] == 204:
|
||||
return
|
||||
|
||||
expected_response_schema = route["response_model"]
|
||||
data = resp.json()
|
||||
|
||||
response_model = expected_response_schema(**data)
|
||||
assert isinstance(response_model, expected_response_schema)
|
||||
|
||||
expected_response_model = expected_response_schema(**expected_data)
|
||||
|
||||
assert response_model == expected_response_model
|
||||
|
||||
|
||||
def get_testable_routes():
|
||||
routes = []
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue