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

24 statements  

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

1import pytest 1abcde

2from dirty_equals import IsDict 1abcde

3from fastapi.testclient import TestClient 1abcde

4 

5 

6@pytest.fixture(name="client") 1abcde

7def get_client(): 1abcde

8 from docs_src.body_multiple_params.tutorial003_an import app 1abcde

9 

10 client = TestClient(app) 1abcde

11 return client 1abcde

12 

13 

14def test_post_body_valid(client: TestClient): 1abcde

15 response = client.put( 1abcde

16 "/items/5", 

17 json={ 

18 "importance": 2, 

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

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

21 }, 

22 ) 

23 assert response.status_code == 200 1abcde

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

25 "item_id": 5, 

26 "importance": 2, 

27 "item": { 

28 "name": "Foo", 

29 "price": 50.5, 

30 "description": None, 

31 "tax": None, 

32 }, 

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

34 } 

35 

36 

37def test_post_body_no_data(client: TestClient): 1abcde

38 response = client.put("/items/5", json=None) 1abcde

39 assert response.status_code == 422 1abcde

40 assert response.json() == IsDict( 1abcde

41 { 

42 "detail": [ 

43 { 

44 "type": "missing", 

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

46 "msg": "Field required", 

47 "input": None, 

48 }, 

49 { 

50 "type": "missing", 

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

52 "msg": "Field required", 

53 "input": None, 

54 }, 

55 { 

56 "type": "missing", 

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

58 "msg": "Field required", 

59 "input": None, 

60 }, 

61 ] 

62 } 

63 ) | IsDict( 

64 # TODO: remove when deprecating Pydantic v1 

65 { 

66 "detail": [ 

67 { 

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

69 "msg": "field required", 

70 "type": "value_error.missing", 

71 }, 

72 { 

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

74 "msg": "field required", 

75 "type": "value_error.missing", 

76 }, 

77 { 

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

79 "msg": "field required", 

80 "type": "value_error.missing", 

81 }, 

82 ] 

83 } 

84 ) 

85 

86 

87def test_post_body_empty_list(client: TestClient): 1abcde

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

89 assert response.status_code == 422 1abcde

90 assert response.json() == IsDict( 1abcde

91 { 

92 "detail": [ 

93 { 

94 "type": "missing", 

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

96 "msg": "Field required", 

97 "input": None, 

98 }, 

99 { 

100 "type": "missing", 

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

102 "msg": "Field required", 

103 "input": None, 

104 }, 

105 { 

106 "type": "missing", 

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

108 "msg": "Field required", 

109 "input": None, 

110 }, 

111 ] 

112 } 

113 ) | IsDict( 

114 # TODO: remove when deprecating Pydantic v1 

115 { 

116 "detail": [ 

117 { 

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

119 "msg": "field required", 

120 "type": "value_error.missing", 

121 }, 

122 { 

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

124 "msg": "field required", 

125 "type": "value_error.missing", 

126 }, 

127 { 

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

129 "msg": "field required", 

130 "type": "value_error.missing", 

131 }, 

132 ] 

133 } 

134 ) 

135 

136 

137def test_openapi_schema(client: TestClient): 1abcde

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

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

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

141 "openapi": "3.1.0", 

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

143 "paths": { 

144 "/items/{item_id}": { 

145 "put": { 

146 "responses": { 

147 "200": { 

148 "description": "Successful Response", 

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

150 }, 

151 "422": { 

152 "description": "Validation Error", 

153 "content": { 

154 "application/json": { 

155 "schema": { 

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

157 } 

158 } 

159 }, 

160 }, 

161 }, 

162 "summary": "Update Item", 

163 "operationId": "update_item_items__item_id__put", 

164 "parameters": [ 

165 { 

166 "required": True, 

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

168 "name": "item_id", 

169 "in": "path", 

170 } 

171 ], 

172 "requestBody": { 

173 "content": { 

174 "application/json": { 

175 "schema": { 

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

177 } 

178 } 

179 }, 

180 "required": True, 

181 }, 

182 } 

183 } 

184 }, 

185 "components": { 

186 "schemas": { 

187 "Item": { 

188 "title": "Item", 

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

190 "type": "object", 

191 "properties": { 

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

193 "description": IsDict( 

194 { 

195 "title": "Description", 

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

197 } 

198 ) 

199 | IsDict( 

200 # TODO: remove when deprecating Pydantic v1 

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

202 ), 

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

204 "tax": IsDict( 

205 { 

206 "title": "Tax", 

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

208 } 

209 ) 

210 | IsDict( 

211 # TODO: remove when deprecating Pydantic v1 

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

213 ), 

214 }, 

215 }, 

216 "User": { 

217 "title": "User", 

218 "required": ["username"], 

219 "type": "object", 

220 "properties": { 

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

222 "full_name": IsDict( 

223 { 

224 "title": "Full Name", 

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

226 } 

227 ) 

228 | IsDict( 

229 # TODO: remove when deprecating Pydantic v1 

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

231 ), 

232 }, 

233 }, 

234 "Body_update_item_items__item_id__put": { 

235 "title": "Body_update_item_items__item_id__put", 

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

237 "type": "object", 

238 "properties": { 

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

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

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

242 }, 

243 }, 

244 "ValidationError": { 

245 "title": "ValidationError", 

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

247 "type": "object", 

248 "properties": { 

249 "loc": { 

250 "title": "Location", 

251 "type": "array", 

252 "items": { 

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

254 }, 

255 }, 

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

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

258 }, 

259 }, 

260 "HTTPValidationError": { 

261 "title": "HTTPValidationError", 

262 "type": "object", 

263 "properties": { 

264 "detail": { 

265 "title": "Detail", 

266 "type": "array", 

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

268 } 

269 }, 

270 }, 

271 } 

272 }, 

273 }