Coverage for tests / test_custom_route_class.py: 100%

46 statements  

« prev     ^ index     » next       coverage.py v7.13.3, created at 2026-02-12 18:15 +0000

1import pytest 1abcd

2from fastapi import APIRouter, FastAPI 1abcd

3from fastapi.routing import APIRoute 1abcd

4from fastapi.testclient import TestClient 1abcd

5from inline_snapshot import snapshot 1abcd

6from starlette.routing import Route 1abcd

7 

8app = FastAPI() 1abcd

9 

10 

11class APIRouteA(APIRoute): 1abcd

12 x_type = "A" 1abcd

13 

14 

15class APIRouteB(APIRoute): 1abcd

16 x_type = "B" 1abcd

17 

18 

19class APIRouteC(APIRoute): 1abcd

20 x_type = "C" 1abcd

21 

22 

23router_a = APIRouter(route_class=APIRouteA) 1abcd

24router_b = APIRouter(route_class=APIRouteB) 1abcd

25router_c = APIRouter(route_class=APIRouteC) 1abcd

26 

27 

28@router_a.get("/") 1abcd

29def get_a(): 1abcd

30 return {"msg": "A"} 1hij

31 

32 

33@router_b.get("/") 1abcd

34def get_b(): 1abcd

35 return {"msg": "B"} 1hij

36 

37 

38@router_c.get("/") 1abcd

39def get_c(): 1abcd

40 return {"msg": "C"} 1hij

41 

42 

43router_b.include_router(router=router_c, prefix="/c") 1abcd

44router_a.include_router(router=router_b, prefix="/b") 1abcd

45app.include_router(router=router_a, prefix="/a") 1abcd

46 

47 

48client = TestClient(app) 1abcd

49 

50 

51@pytest.mark.parametrize( 1abcd

52 "path,expected_status,expected_response", 

53 [ 

54 ("/a", 200, {"msg": "A"}), 

55 ("/a/b", 200, {"msg": "B"}), 

56 ("/a/b/c", 200, {"msg": "C"}), 

57 ], 

58) 

59def test_get_path(path, expected_status, expected_response): 1abcd

60 response = client.get(path) 1hij

61 assert response.status_code == expected_status 1hij

62 assert response.json() == expected_response 1hij

63 

64 

65def test_route_classes(): 1abcd

66 routes = {} 1efg

67 for r in app.router.routes: 1efg

68 assert isinstance(r, Route) 1efg

69 routes[r.path] = r 1efg

70 assert getattr(routes["/a/"], "x_type") == "A" # noqa: B009 1efg

71 assert getattr(routes["/a/b/"], "x_type") == "B" # noqa: B009 1efg

72 assert getattr(routes["/a/b/c/"], "x_type") == "C" # noqa: B009 1efg

73 

74 

75def test_openapi_schema(): 1abcd

76 response = client.get("/openapi.json") 1klm

77 assert response.status_code == 200, response.text 1klm

78 assert response.json() == snapshot( 1klm

79 { 

80 "openapi": "3.1.0", 

81 "info": {"title": "FastAPI", "version": "0.1.0"}, 

82 "paths": { 

83 "/a/": { 

84 "get": { 

85 "responses": { 

86 "200": { 

87 "description": "Successful Response", 

88 "content": {"application/json": {"schema": {}}}, 

89 } 

90 }, 

91 "summary": "Get A", 

92 "operationId": "get_a_a__get", 

93 } 

94 }, 

95 "/a/b/": { 

96 "get": { 

97 "responses": { 

98 "200": { 

99 "description": "Successful Response", 

100 "content": {"application/json": {"schema": {}}}, 

101 } 

102 }, 

103 "summary": "Get B", 

104 "operationId": "get_b_a_b__get", 

105 } 

106 }, 

107 "/a/b/c/": { 

108 "get": { 

109 "responses": { 

110 "200": { 

111 "description": "Successful Response", 

112 "content": {"application/json": {"schema": {}}}, 

113 } 

114 }, 

115 "summary": "Get C", 

116 "operationId": "get_c_a_b_c__get", 

117 } 

118 }, 

119 }, 

120 } 

121 )