Coverage for tests/test_custom_route_class.py: 100%
45 statements
« prev ^ index » next coverage.py v7.6.1, created at 2025-05-05 00:03 +0000
« prev ^ index » next coverage.py v7.6.1, created at 2025-05-05 00:03 +0000
1import pytest 1abcdef
2from fastapi import APIRouter, FastAPI 1abcdef
3from fastapi.routing import APIRoute 1abcdef
4from fastapi.testclient import TestClient 1abcdef
5from starlette.routing import Route 1abcdef
7app = FastAPI() 1abcdef
10class APIRouteA(APIRoute): 1abcdef
11 x_type = "A" 1abcdef
14class APIRouteB(APIRoute): 1abcdef
15 x_type = "B" 1abcdef
18class APIRouteC(APIRoute): 1abcdef
19 x_type = "C" 1abcdef
22router_a = APIRouter(route_class=APIRouteA) 1abcdef
23router_b = APIRouter(route_class=APIRouteB) 1abcdef
24router_c = APIRouter(route_class=APIRouteC) 1abcdef
27@router_a.get("/") 1abcdef
28def get_a(): 1abcdef
29 return {"msg": "A"} 1mnopqr
32@router_b.get("/") 1abcdef
33def get_b(): 1abcdef
34 return {"msg": "B"} 1mnopqr
37@router_c.get("/") 1abcdef
38def get_c(): 1abcdef
39 return {"msg": "C"} 1mnopqr
42router_b.include_router(router=router_c, prefix="/c") 1abcdef
43router_a.include_router(router=router_b, prefix="/b") 1abcdef
44app.include_router(router=router_a, prefix="/a") 1abcdef
47client = TestClient(app) 1abcdef
50@pytest.mark.parametrize( 1abcdef
51 "path,expected_status,expected_response",
52 [
53 ("/a", 200, {"msg": "A"}),
54 ("/a/b", 200, {"msg": "B"}),
55 ("/a/b/c", 200, {"msg": "C"}),
56 ],
57)
58def test_get_path(path, expected_status, expected_response): 1abcdef
59 response = client.get(path) 1mnopqr
60 assert response.status_code == expected_status 1mnopqr
61 assert response.json() == expected_response 1mnopqr
64def test_route_classes(): 1abcdef
65 routes = {} 1ghijkl
66 for r in app.router.routes: 1ghijkl
67 assert isinstance(r, Route) 1ghijkl
68 routes[r.path] = r 1ghijkl
69 assert getattr(routes["/a/"], "x_type") == "A" # noqa: B009 1ghijkl
70 assert getattr(routes["/a/b/"], "x_type") == "B" # noqa: B009 1ghijkl
71 assert getattr(routes["/a/b/c/"], "x_type") == "C" # noqa: B009 1ghijkl
74def test_openapi_schema(): 1abcdef
75 response = client.get("/openapi.json") 1stuvwx
76 assert response.status_code == 200, response.text 1stuvwx
77 assert response.json() == { 1stuvwx
78 "openapi": "3.1.0",
79 "info": {"title": "FastAPI", "version": "0.1.0"},
80 "paths": {
81 "/a/": {
82 "get": {
83 "responses": {
84 "200": {
85 "description": "Successful Response",
86 "content": {"application/json": {"schema": {}}},
87 }
88 },
89 "summary": "Get A",
90 "operationId": "get_a_a__get",
91 }
92 },
93 "/a/b/": {
94 "get": {
95 "responses": {
96 "200": {
97 "description": "Successful Response",
98 "content": {"application/json": {"schema": {}}},
99 }
100 },
101 "summary": "Get B",
102 "operationId": "get_b_a_b__get",
103 }
104 },
105 "/a/b/c/": {
106 "get": {
107 "responses": {
108 "200": {
109 "description": "Successful Response",
110 "content": {"application/json": {"schema": {}}},
111 }
112 },
113 "summary": "Get C",
114 "operationId": "get_c_a_b_c__get",
115 }
116 },
117 },
118 }