Coverage for tests/test_custom_route_class.py: 100%

45 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2024-08-08 03:53 +0000

1import pytest 1abcde

2from fastapi import APIRouter, FastAPI 1abcde

3from fastapi.routing import APIRoute 1abcde

4from fastapi.testclient import TestClient 1abcde

5from starlette.routing import Route 1abcde

6 

7app = FastAPI() 1abcde

8 

9 

10class APIRouteA(APIRoute): 1abcde

11 x_type = "A" 1abcde

12 

13 

14class APIRouteB(APIRoute): 1abcde

15 x_type = "B" 1abcde

16 

17 

18class APIRouteC(APIRoute): 1abcde

19 x_type = "C" 1abcde

20 

21 

22router_a = APIRouter(route_class=APIRouteA) 1abcde

23router_b = APIRouter(route_class=APIRouteB) 1abcde

24router_c = APIRouter(route_class=APIRouteC) 1abcde

25 

26 

27@router_a.get("/") 1abcde

28def get_a(): 1abcde

29 return {"msg": "A"} 1abcde

30 

31 

32@router_b.get("/") 1abcde

33def get_b(): 1abcde

34 return {"msg": "B"} 1abcde

35 

36 

37@router_c.get("/") 1abcde

38def get_c(): 1abcde

39 return {"msg": "C"} 1abcde

40 

41 

42router_b.include_router(router=router_c, prefix="/c") 1abcde

43router_a.include_router(router=router_b, prefix="/b") 1abcde

44app.include_router(router=router_a, prefix="/a") 1abcde

45 

46 

47client = TestClient(app) 1abcde

48 

49 

50@pytest.mark.parametrize( 1abcde

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): 1abcde

59 response = client.get(path) 1abcde

60 assert response.status_code == expected_status 1abcde

61 assert response.json() == expected_response 1abcde

62 

63 

64def test_route_classes(): 1abcde

65 routes = {} 1abcde

66 for r in app.router.routes: 1abcde

67 assert isinstance(r, Route) 1abcde

68 routes[r.path] = r 1abcde

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

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

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

72 

73 

74def test_openapi_schema(): 1abcde

75 response = client.get("/openapi.json") 1abcde

76 assert response.status_code == 200, response.text 1abcde

77 assert response.json() == { 1abcde

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 }