Coverage for tests/test_tutorial/test_body_multiple_params/test_tutorial003_an_py310.py: 100%

29 statements  

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

1import pytest 1deabc

2from dirty_equals import IsDict 1deabc

3from fastapi.testclient import TestClient 1deabc

4 

5from ...utils import needs_py310 1deabc

6 

7 

8@pytest.fixture(name="client") 1deabc

9def get_client(): 1deabc

10 from docs_src.body_multiple_params.tutorial003_an_py310 import app 1abc

11 

12 client = TestClient(app) 1abc

13 return client 1abc

14 

15 

16@needs_py310 1deabc

17def test_post_body_valid(client: TestClient): 1deabc

18 response = client.put( 1abc

19 "/items/5", 

20 json={ 

21 "importance": 2, 

22 "item": {"name": "Foo", "price": 50.5}, 

23 "user": {"username": "Dave"}, 

24 }, 

25 ) 

26 assert response.status_code == 200 1abc

27 assert response.json() == { 1abc

28 "item_id": 5, 

29 "importance": 2, 

30 "item": { 

31 "name": "Foo", 

32 "price": 50.5, 

33 "description": None, 

34 "tax": None, 

35 }, 

36 "user": {"username": "Dave", "full_name": None}, 

37 } 

38 

39 

40@needs_py310 1deabc

41def test_post_body_no_data(client: TestClient): 1deabc

42 response = client.put("/items/5", json=None) 1abc

43 assert response.status_code == 422 1abc

44 assert response.json() == IsDict( 1abc

45 { 

46 "detail": [ 

47 { 

48 "type": "missing", 

49 "loc": ["body", "item"], 

50 "msg": "Field required", 

51 "input": None, 

52 }, 

53 { 

54 "type": "missing", 

55 "loc": ["body", "user"], 

56 "msg": "Field required", 

57 "input": None, 

58 }, 

59 { 

60 "type": "missing", 

61 "loc": ["body", "importance"], 

62 "msg": "Field required", 

63 "input": None, 

64 }, 

65 ] 

66 } 

67 ) | IsDict( 

68 # TODO: remove when deprecating Pydantic v1 

69 { 

70 "detail": [ 

71 { 

72 "loc": ["body", "item"], 

73 "msg": "field required", 

74 "type": "value_error.missing", 

75 }, 

76 { 

77 "loc": ["body", "user"], 

78 "msg": "field required", 

79 "type": "value_error.missing", 

80 }, 

81 { 

82 "loc": ["body", "importance"], 

83 "msg": "field required", 

84 "type": "value_error.missing", 

85 }, 

86 ] 

87 } 

88 ) 

89 

90 

91@needs_py310 1deabc

92def test_post_body_empty_list(client: TestClient): 1deabc

93 response = client.put("/items/5", json=[]) 1abc

94 assert response.status_code == 422 1abc

95 assert response.json() == IsDict( 1abc

96 { 

97 "detail": [ 

98 { 

99 "type": "missing", 

100 "loc": ["body", "item"], 

101 "msg": "Field required", 

102 "input": None, 

103 }, 

104 { 

105 "type": "missing", 

106 "loc": ["body", "user"], 

107 "msg": "Field required", 

108 "input": None, 

109 }, 

110 { 

111 "type": "missing", 

112 "loc": ["body", "importance"], 

113 "msg": "Field required", 

114 "input": None, 

115 }, 

116 ] 

117 } 

118 ) | IsDict( 

119 # TODO: remove when deprecating Pydantic v1 

120 { 

121 "detail": [ 

122 { 

123 "loc": ["body", "item"], 

124 "msg": "field required", 

125 "type": "value_error.missing", 

126 }, 

127 { 

128 "loc": ["body", "user"], 

129 "msg": "field required", 

130 "type": "value_error.missing", 

131 }, 

132 { 

133 "loc": ["body", "importance"], 

134 "msg": "field required", 

135 "type": "value_error.missing", 

136 }, 

137 ] 

138 } 

139 ) 

140 

141 

142@needs_py310 1deabc

143def test_openapi_schema(client: TestClient): 1deabc

144 response = client.get("/openapi.json") 1abc

145 assert response.status_code == 200, response.text 1abc

146 assert response.json() == { 1abc

147 "openapi": "3.1.0", 

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

149 "paths": { 

150 "/items/{item_id}": { 

151 "put": { 

152 "responses": { 

153 "200": { 

154 "description": "Successful Response", 

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

156 }, 

157 "422": { 

158 "description": "Validation Error", 

159 "content": { 

160 "application/json": { 

161 "schema": { 

162 "$ref": "#/components/schemas/HTTPValidationError" 

163 } 

164 } 

165 }, 

166 }, 

167 }, 

168 "summary": "Update Item", 

169 "operationId": "update_item_items__item_id__put", 

170 "parameters": [ 

171 { 

172 "required": True, 

173 "schema": {"title": "Item Id", "type": "integer"}, 

174 "name": "item_id", 

175 "in": "path", 

176 } 

177 ], 

178 "requestBody": { 

179 "content": { 

180 "application/json": { 

181 "schema": { 

182 "$ref": "#/components/schemas/Body_update_item_items__item_id__put" 

183 } 

184 } 

185 }, 

186 "required": True, 

187 }, 

188 } 

189 } 

190 }, 

191 "components": { 

192 "schemas": { 

193 "Item": { 

194 "title": "Item", 

195 "required": ["name", "price"], 

196 "type": "object", 

197 "properties": { 

198 "name": {"title": "Name", "type": "string"}, 

199 "description": IsDict( 

200 { 

201 "title": "Description", 

202 "anyOf": [{"type": "string"}, {"type": "null"}], 

203 } 

204 ) 

205 | IsDict( 

206 # TODO: remove when deprecating Pydantic v1 

207 {"title": "Description", "type": "string"} 

208 ), 

209 "price": {"title": "Price", "type": "number"}, 

210 "tax": IsDict( 

211 { 

212 "title": "Tax", 

213 "anyOf": [{"type": "number"}, {"type": "null"}], 

214 } 

215 ) 

216 | IsDict( 

217 # TODO: remove when deprecating Pydantic v1 

218 {"title": "Tax", "type": "number"} 

219 ), 

220 }, 

221 }, 

222 "User": { 

223 "title": "User", 

224 "required": ["username"], 

225 "type": "object", 

226 "properties": { 

227 "username": {"title": "Username", "type": "string"}, 

228 "full_name": IsDict( 

229 { 

230 "title": "Full Name", 

231 "anyOf": [{"type": "string"}, {"type": "null"}], 

232 } 

233 ) 

234 | IsDict( 

235 # TODO: remove when deprecating Pydantic v1 

236 {"title": "Full Name", "type": "string"} 

237 ), 

238 }, 

239 }, 

240 "Body_update_item_items__item_id__put": { 

241 "title": "Body_update_item_items__item_id__put", 

242 "required": ["item", "user", "importance"], 

243 "type": "object", 

244 "properties": { 

245 "item": {"$ref": "#/components/schemas/Item"}, 

246 "user": {"$ref": "#/components/schemas/User"}, 

247 "importance": {"title": "Importance", "type": "integer"}, 

248 }, 

249 }, 

250 "ValidationError": { 

251 "title": "ValidationError", 

252 "required": ["loc", "msg", "type"], 

253 "type": "object", 

254 "properties": { 

255 "loc": { 

256 "title": "Location", 

257 "type": "array", 

258 "items": { 

259 "anyOf": [{"type": "string"}, {"type": "integer"}] 

260 }, 

261 }, 

262 "msg": {"title": "Message", "type": "string"}, 

263 "type": {"title": "Error Type", "type": "string"}, 

264 }, 

265 }, 

266 "HTTPValidationError": { 

267 "title": "HTTPValidationError", 

268 "type": "object", 

269 "properties": { 

270 "detail": { 

271 "title": "Detail", 

272 "type": "array", 

273 "items": {"$ref": "#/components/schemas/ValidationError"}, 

274 } 

275 }, 

276 }, 

277 } 

278 }, 

279 }