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
|
from fastapi.dependencies.models import Dependant
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from typing import AsyncGenerator
|
from typing import AsyncGenerator, Any
|
||||||
from itertools import combinations
|
from itertools import combinations
|
||||||
from fastapi.routing import APIRoute, iter_route_contexts
|
from fastapi.routing import APIRoute, iter_route_contexts
|
||||||
from httpx import AsyncClient, ASGITransport
|
from httpx import AsyncClient, ASGITransport
|
||||||
|
|
@ -308,6 +308,42 @@ async def route_data():
|
||||||
return routes
|
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():
|
def get_testable_routes():
|
||||||
routes = []
|
routes = []
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -8,8 +8,7 @@ from typing import Any
|
||||||
import pytest
|
import pytest
|
||||||
from httpx import AsyncClient
|
from httpx import AsyncClient
|
||||||
|
|
||||||
from .conftest import generate_query_and_status
|
from .conftest import generate_query_and_status, standard_test
|
||||||
|
|
||||||
|
|
||||||
pytestmark = [
|
pytestmark = [
|
||||||
pytest.mark.user_module,
|
pytest.mark.user_module,
|
||||||
|
|
@ -154,13 +153,14 @@ async def test_get_self_orgs_success(default_client: AsyncClient):
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.anyio
|
@pytest.mark.anyio
|
||||||
async def test_get_self_orgs_dynamic(
|
async def test_get_self_orgs_standard(
|
||||||
default_client: AsyncClient, route_data: dict[str, dict[str, Any]]
|
default_client: AsyncClient, route_data: dict[str, dict[str, Any]]
|
||||||
):
|
):
|
||||||
method = "GET"
|
method = "GET"
|
||||||
path = "/user/self/orgs"
|
path = "/user/self/orgs"
|
||||||
auth_level = "User"
|
auth_level = "User"
|
||||||
query = ""
|
query = ""
|
||||||
|
body = {}
|
||||||
expected_data = {
|
expected_data = {
|
||||||
"organisations": [
|
"organisations": [
|
||||||
{
|
{
|
||||||
|
|
@ -183,24 +183,6 @@ async def test_get_self_orgs_dynamic(
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
# req_func = getattr(default_client, method); resp = await req_func(url=path, json=body)
|
await standard_test(
|
||||||
resp = await default_client.get(f"{path}{query}")
|
default_client, method, path, auth_level, query, body, expected_data, route_data
|
||||||
|
)
|
||||||
route = route_data.get(f"{method.upper()}{path}")
|
|
||||||
assert route is not None
|
|
||||||
|
|
||||||
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
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue