Coverage for tests/test_additional_responses_router.py: 100%

41 statements  

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

1from fastapi import APIRouter, FastAPI 1abcde

2from fastapi.testclient import TestClient 1abcde

3from pydantic import BaseModel 1abcde

4 

5 

6class ResponseModel(BaseModel): 1abcde

7 message: str 1abcde

8 

9 

10app = FastAPI() 1abcde

11router = APIRouter() 1abcde

12 

13 

14@router.get("/a", responses={501: {"description": "Error 1"}}) 1abcde

15async def a(): 1abcde

16 return "a" 1abcde

17 

18 

19@router.get( 1abcde

20 "/b", 

21 responses={ 

22 502: {"description": "Error 2"}, 

23 "4XX": {"description": "Error with range, upper"}, 

24 }, 

25) 

26async def b(): 1abcde

27 return "b" 1abcde

28 

29 

30@router.get( 1abcde

31 "/c", 

32 responses={ 

33 "400": {"description": "Error with str"}, 

34 "5xx": {"description": "Error with range, lower"}, 

35 "default": {"description": "A default response"}, 

36 }, 

37) 

38async def c(): 1abcde

39 return "c" 1abcde

40 

41 

42@router.get( 1abcde

43 "/d", 

44 responses={ 

45 "400": {"description": "Error with str"}, 

46 "5XX": {"model": ResponseModel}, 

47 "default": {"model": ResponseModel}, 

48 }, 

49) 

50async def d(): 1abcde

51 return "d" 1abcde

52 

53 

54app.include_router(router) 1abcde

55 

56 

57client = TestClient(app) 1abcde

58 

59 

60def test_a(): 1abcde

61 response = client.get("/a") 1abcde

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

63 assert response.json() == "a" 1abcde

64 

65 

66def test_b(): 1abcde

67 response = client.get("/b") 1abcde

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

69 assert response.json() == "b" 1abcde

70 

71 

72def test_c(): 1abcde

73 response = client.get("/c") 1abcde

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

75 assert response.json() == "c" 1abcde

76 

77 

78def test_d(): 1abcde

79 response = client.get("/d") 1abcde

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

81 assert response.json() == "d" 1abcde

82 

83 

84def test_openapi_schema(): 1abcde

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

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

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

88 "openapi": "3.1.0", 

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

90 "paths": { 

91 "/a": { 

92 "get": { 

93 "responses": { 

94 "501": {"description": "Error 1"}, 

95 "200": { 

96 "description": "Successful Response", 

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

98 }, 

99 }, 

100 "summary": "A", 

101 "operationId": "a_a_get", 

102 } 

103 }, 

104 "/b": { 

105 "get": { 

106 "responses": { 

107 "502": {"description": "Error 2"}, 

108 "4XX": {"description": "Error with range, upper"}, 

109 "200": { 

110 "description": "Successful Response", 

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

112 }, 

113 }, 

114 "summary": "B", 

115 "operationId": "b_b_get", 

116 } 

117 }, 

118 "/c": { 

119 "get": { 

120 "responses": { 

121 "400": {"description": "Error with str"}, 

122 "5XX": {"description": "Error with range, lower"}, 

123 "200": { 

124 "description": "Successful Response", 

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

126 }, 

127 "default": {"description": "A default response"}, 

128 }, 

129 "summary": "C", 

130 "operationId": "c_c_get", 

131 } 

132 }, 

133 "/d": { 

134 "get": { 

135 "responses": { 

136 "400": {"description": "Error with str"}, 

137 "5XX": { 

138 "description": "Server Error", 

139 "content": { 

140 "application/json": { 

141 "schema": { 

142 "$ref": "#/components/schemas/ResponseModel" 

143 } 

144 } 

145 }, 

146 }, 

147 "200": { 

148 "description": "Successful Response", 

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

150 }, 

151 "default": { 

152 "description": "Default Response", 

153 "content": { 

154 "application/json": { 

155 "schema": { 

156 "$ref": "#/components/schemas/ResponseModel" 

157 } 

158 } 

159 }, 

160 }, 

161 }, 

162 "summary": "D", 

163 "operationId": "d_d_get", 

164 } 

165 }, 

166 }, 

167 "components": { 

168 "schemas": { 

169 "ResponseModel": { 

170 "title": "ResponseModel", 

171 "required": ["message"], 

172 "type": "object", 

173 "properties": {"message": {"title": "Message", "type": "string"}}, 

174 } 

175 } 

176 }, 

177 }