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

34 statements  

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

1import importlib 1abdc

2 

3import pytest 1abdc

4from fastapi.testclient import TestClient 1abdc

5from inline_snapshot import snapshot 1abdc

6 

7from ...utils import needs_py310 1abdc

8 

9 

10@pytest.fixture( 1abdc

11 name="client", 

12 params=[ 

13 pytest.param("tutorial005_py310", marks=needs_py310), 

14 pytest.param("tutorial005_an_py310", marks=needs_py310), 

15 ], 

16) 

17def get_client(request: pytest.FixtureRequest): 1abdc

18 mod = importlib.import_module(f"docs_src.body_multiple_params.{request.param}") 1abc

19 

20 client = TestClient(mod.app) 1abc

21 return client 1abc

22 

23 

24def test_post_all(client: TestClient): 1abdc

25 response = client.put( 1efg

26 "/items/5", 

27 json={ 

28 "item": { 

29 "name": "Foo", 

30 "price": 50.5, 

31 "description": "Some Foo", 

32 "tax": 0.1, 

33 }, 

34 }, 

35 ) 

36 assert response.status_code == 200 1efg

37 assert response.json() == { 1efg

38 "item_id": 5, 

39 "item": { 

40 "name": "Foo", 

41 "price": 50.5, 

42 "description": "Some Foo", 

43 "tax": 0.1, 

44 }, 

45 } 

46 

47 

48def test_post_required(client: TestClient): 1abdc

49 response = client.put( 1hij

50 "/items/5", 

51 json={ 

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

53 }, 

54 ) 

55 assert response.status_code == 200 1hij

56 assert response.json() == { 1hij

57 "item_id": 5, 

58 "item": { 

59 "name": "Foo", 

60 "price": 50.5, 

61 "description": None, 

62 "tax": None, 

63 }, 

64 } 

65 

66 

67def test_post_no_body(client: TestClient): 1abdc

68 response = client.put("/items/5", json=None) 1klm

69 assert response.status_code == 422 1klm

70 assert response.json() == { 1klm

71 "detail": [ 

72 { 

73 "input": None, 

74 "loc": [ 

75 "body", 

76 "item", 

77 ], 

78 "msg": "Field required", 

79 "type": "missing", 

80 }, 

81 ], 

82 } 

83 

84 

85def test_post_like_not_embeded(client: TestClient): 1abdc

86 response = client.put( 1nop

87 "/items/5", 

88 json={ 

89 "name": "Foo", 

90 "price": 50.5, 

91 }, 

92 ) 

93 assert response.status_code == 422 1nop

94 assert response.json() == { 1nop

95 "detail": [ 

96 { 

97 "input": None, 

98 "loc": [ 

99 "body", 

100 "item", 

101 ], 

102 "msg": "Field required", 

103 "type": "missing", 

104 }, 

105 ], 

106 } 

107 

108 

109def test_post_missing_required_field_in_item(client: TestClient): 1abdc

110 response = client.put( 1qrs

111 "/items/5", json={"item": {"name": "Foo"}, "user": {"username": "johndoe"}} 

112 ) 

113 assert response.status_code == 422 1qrs

114 assert response.json() == { 1qrs

115 "detail": [ 

116 { 

117 "input": {"name": "Foo"}, 

118 "loc": [ 

119 "body", 

120 "item", 

121 "price", 

122 ], 

123 "msg": "Field required", 

124 "type": "missing", 

125 }, 

126 ], 

127 } 

128 

129 

130def test_openapi_schema(client: TestClient): 1abdc

131 response = client.get("/openapi.json") 1tuv

132 assert response.status_code == 200, response.text 1tuv

133 assert response.json() == snapshot( 1tuv

134 { 

135 "info": { 

136 "title": "FastAPI", 

137 "version": "0.1.0", 

138 }, 

139 "openapi": "3.1.0", 

140 "paths": { 

141 "/items/{item_id}": { 

142 "put": { 

143 "operationId": "update_item_items__item_id__put", 

144 "parameters": [ 

145 { 

146 "in": "path", 

147 "name": "item_id", 

148 "required": True, 

149 "schema": { 

150 "title": "Item Id", 

151 "type": "integer", 

152 }, 

153 }, 

154 ], 

155 "requestBody": { 

156 "content": { 

157 "application/json": { 

158 "schema": { 

159 "$ref": "#/components/schemas/Body_update_item_items__item_id__put", 

160 }, 

161 }, 

162 }, 

163 "required": True, 

164 }, 

165 "responses": { 

166 "200": { 

167 "content": { 

168 "application/json": { 

169 "schema": {}, 

170 }, 

171 }, 

172 "description": "Successful Response", 

173 }, 

174 "422": { 

175 "content": { 

176 "application/json": { 

177 "schema": { 

178 "$ref": "#/components/schemas/HTTPValidationError", 

179 }, 

180 }, 

181 }, 

182 "description": "Validation Error", 

183 }, 

184 }, 

185 "summary": "Update Item", 

186 }, 

187 }, 

188 }, 

189 "components": { 

190 "schemas": { 

191 "Body_update_item_items__item_id__put": { 

192 "properties": { 

193 "item": { 

194 "$ref": "#/components/schemas/Item", 

195 }, 

196 }, 

197 "required": ["item"], 

198 "title": "Body_update_item_items__item_id__put", 

199 "type": "object", 

200 }, 

201 "HTTPValidationError": { 

202 "properties": { 

203 "detail": { 

204 "items": { 

205 "$ref": "#/components/schemas/ValidationError", 

206 }, 

207 "title": "Detail", 

208 "type": "array", 

209 }, 

210 }, 

211 "title": "HTTPValidationError", 

212 "type": "object", 

213 }, 

214 "Item": { 

215 "properties": { 

216 "name": { 

217 "title": "Name", 

218 "type": "string", 

219 }, 

220 "description": { 

221 "title": "Description", 

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

223 }, 

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

225 "tax": { 

226 "title": "Tax", 

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

228 }, 

229 }, 

230 "required": [ 

231 "name", 

232 "price", 

233 ], 

234 "title": "Item", 

235 "type": "object", 

236 }, 

237 "ValidationError": { 

238 "properties": { 

239 "ctx": {"title": "Context", "type": "object"}, 

240 "input": {"title": "Input"}, 

241 "loc": { 

242 "items": { 

243 "anyOf": [ 

244 { 

245 "type": "string", 

246 }, 

247 { 

248 "type": "integer", 

249 }, 

250 ], 

251 }, 

252 "title": "Location", 

253 "type": "array", 

254 }, 

255 "msg": { 

256 "title": "Message", 

257 "type": "string", 

258 }, 

259 "type": { 

260 "title": "Error Type", 

261 "type": "string", 

262 }, 

263 }, 

264 "required": [ 

265 "loc", 

266 "msg", 

267 "type", 

268 ], 

269 "title": "ValidationError", 

270 "type": "object", 

271 }, 

272 }, 

273 }, 

274 } 

275 )