fix: fastapi 0.137 router.route changes
This commit is contained in:
parent
ee47186c5a
commit
4b3ab92d2a
2 changed files with 24 additions and 18 deletions
|
|
@ -2,7 +2,7 @@ import pytest
|
||||||
|
|
||||||
from typing import AsyncGenerator
|
from typing import AsyncGenerator
|
||||||
from itertools import combinations
|
from itertools import combinations
|
||||||
from fastapi.routing import APIRoute
|
from fastapi.routing import APIRoute, iter_route_contexts
|
||||||
from httpx import AsyncClient, ASGITransport
|
from httpx import AsyncClient, ASGITransport
|
||||||
from sqlalchemy.orm import sessionmaker
|
from sqlalchemy.orm import sessionmaker
|
||||||
|
|
||||||
|
|
@ -258,12 +258,13 @@ def generate_body_and_status(params: dict[str, str]) -> list[tuple[dict, int]]:
|
||||||
def get_testable_routes():
|
def get_testable_routes():
|
||||||
routes = []
|
routes = []
|
||||||
|
|
||||||
for route in app.routes:
|
contexts = list(iter_route_contexts(app.routes))
|
||||||
if not isinstance(route, APIRoute):
|
|
||||||
continue
|
for route in contexts:
|
||||||
if not route.methods:
|
if not route.methods:
|
||||||
continue
|
continue
|
||||||
|
if not isinstance(route.route, APIRoute):
|
||||||
|
continue
|
||||||
for method in route.methods:
|
for method in route.methods:
|
||||||
if method in {"HEAD", "OPTIONS"}:
|
if method in {"HEAD", "OPTIONS"}:
|
||||||
continue
|
continue
|
||||||
|
|
@ -271,10 +272,10 @@ def get_testable_routes():
|
||||||
routes.append(
|
routes.append(
|
||||||
(
|
(
|
||||||
method,
|
method,
|
||||||
route.path,
|
route.route.path,
|
||||||
route.status_code,
|
route.route.status_code,
|
||||||
route.response_model,
|
route.route.response_model,
|
||||||
route.summary,
|
route.route.summary,
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -282,11 +283,11 @@ def get_testable_routes():
|
||||||
|
|
||||||
|
|
||||||
# with open("endpoints.txt", "w") as f:
|
# with open("endpoints.txt", "w") as f:
|
||||||
# for ep in get_testable_routes():
|
# for ep in get_testable_routes():
|
||||||
# f.write(f"[{ep[0]}]({ep[1]}) -> {ep[2]}: {ep[3]}\n")
|
# f.write(f"[{ep[0]}]({ep[1]}) -> {ep[2]}: {ep[3]}\n")
|
||||||
#
|
#
|
||||||
#
|
#
|
||||||
### Docstring formatted output ###
|
### Docstring formatted output ###
|
||||||
# with open("endpoints.txt", "w") as f:
|
with open("endpoints.txt", "w") as f:
|
||||||
# for ep in get_testable_routes():
|
for ep in get_testable_routes():
|
||||||
# f.write(f"- [{ep[0]}]({ep[1]}): []: {ep[4]}\n")
|
f.write(f"- [{ep[0]}]({ep[1]}): []: {ep[4]}\n")
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,7 @@
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
from httpx import AsyncClient
|
from httpx import AsyncClient
|
||||||
from fastapi.routing import APIRoute
|
from fastapi.routing import APIRoute, iter_route_contexts
|
||||||
|
|
||||||
from .conftest import generate_query_and_status
|
from .conftest import generate_query_and_status
|
||||||
|
|
||||||
|
|
@ -180,10 +180,15 @@ async def test_get_self_orgs_dynamic(default_client: AsyncClient):
|
||||||
|
|
||||||
resp = await default_client.get(path)
|
resp = await default_client.get(path)
|
||||||
|
|
||||||
|
contexts = list(iter_route_contexts(default_client._transport.app.routes)) # ty:ignore[unresolved-attribute]
|
||||||
|
|
||||||
route = next(
|
route = next(
|
||||||
route
|
route.route
|
||||||
for route in default_client._transport.app.routes # ty:ignore[unresolved-attribute]
|
for route in contexts
|
||||||
if isinstance(route, APIRoute) and path in route.path and method in route.methods
|
if isinstance(route.route, APIRoute)
|
||||||
|
and path in route.route.path
|
||||||
|
and isinstance(route.methods, set)
|
||||||
|
and method in route.methods
|
||||||
)
|
)
|
||||||
|
|
||||||
assert resp.status_code == route.status_code
|
assert resp.status_code == route.status_code
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue