Coverage for tests/test_tutorial/test_body_updates/test_tutorial001.py: 100%

25 statements  

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

1import pytest 1abcde

2from fastapi.testclient import TestClient 1abcde

3 

4from ...utils import needs_pydanticv1, needs_pydanticv2 1abcde

5 

6 

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

8def get_client(): 1abcde

9 from docs_src.body_updates.tutorial001 import app 1abcde

10 

11 client = TestClient(app) 1abcde

12 return client 1abcde

13 

14 

15def test_get(client: TestClient): 1abcde

16 response = client.get("/items/baz") 1abcde

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

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

19 "name": "Baz", 

20 "description": None, 

21 "price": 50.2, 

22 "tax": 10.5, 

23 "tags": [], 

24 } 

25 

26 

27def test_put(client: TestClient): 1abcde

28 response = client.put( 1abcde

29 "/items/bar", json={"name": "Barz", "price": 3, "description": None} 

30 ) 

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

32 "name": "Barz", 

33 "description": None, 

34 "price": 3, 

35 "tax": 10.5, 

36 "tags": [], 

37 } 

38 

39 

40@needs_pydanticv2 1abcde

41def test_openapi_schema(client: TestClient): 1abcde

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

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

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

45 "openapi": "3.1.0", 

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

47 "paths": { 

48 "/items/{item_id}": { 

49 "get": { 

50 "responses": { 

51 "200": { 

52 "description": "Successful Response", 

53 "content": { 

54 "application/json": { 

55 "schema": {"$ref": "#/components/schemas/Item"} 

56 } 

57 }, 

58 }, 

59 "422": { 

60 "description": "Validation Error", 

61 "content": { 

62 "application/json": { 

63 "schema": { 

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

65 } 

66 } 

67 }, 

68 }, 

69 }, 

70 "summary": "Read Item", 

71 "operationId": "read_item_items__item_id__get", 

72 "parameters": [ 

73 { 

74 "required": True, 

75 "schema": {"title": "Item Id", "type": "string"}, 

76 "name": "item_id", 

77 "in": "path", 

78 } 

79 ], 

80 }, 

81 "put": { 

82 "responses": { 

83 "200": { 

84 "description": "Successful Response", 

85 "content": { 

86 "application/json": { 

87 "schema": {"$ref": "#/components/schemas/Item"} 

88 } 

89 }, 

90 }, 

91 "422": { 

92 "description": "Validation Error", 

93 "content": { 

94 "application/json": { 

95 "schema": { 

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

97 } 

98 } 

99 }, 

100 }, 

101 }, 

102 "summary": "Update Item", 

103 "operationId": "update_item_items__item_id__put", 

104 "parameters": [ 

105 { 

106 "required": True, 

107 "schema": {"title": "Item Id", "type": "string"}, 

108 "name": "item_id", 

109 "in": "path", 

110 } 

111 ], 

112 "requestBody": { 

113 "content": { 

114 "application/json": { 

115 "schema": {"$ref": "#/components/schemas/Item"} 

116 } 

117 }, 

118 "required": True, 

119 }, 

120 }, 

121 } 

122 }, 

123 "components": { 

124 "schemas": { 

125 "Item": { 

126 "type": "object", 

127 "title": "Item", 

128 "properties": { 

129 "name": { 

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

131 "title": "Name", 

132 }, 

133 "description": { 

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

135 "title": "Description", 

136 }, 

137 "price": { 

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

139 "title": "Price", 

140 }, 

141 "tax": {"title": "Tax", "type": "number", "default": 10.5}, 

142 "tags": { 

143 "title": "Tags", 

144 "type": "array", 

145 "items": {"type": "string"}, 

146 "default": [], 

147 }, 

148 }, 

149 }, 

150 "ValidationError": { 

151 "title": "ValidationError", 

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

153 "type": "object", 

154 "properties": { 

155 "loc": { 

156 "title": "Location", 

157 "type": "array", 

158 "items": { 

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

160 }, 

161 }, 

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

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

164 }, 

165 }, 

166 "HTTPValidationError": { 

167 "title": "HTTPValidationError", 

168 "type": "object", 

169 "properties": { 

170 "detail": { 

171 "title": "Detail", 

172 "type": "array", 

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

174 } 

175 }, 

176 }, 

177 } 

178 }, 

179 } 

180 

181 

182# TODO: remove when deprecating Pydantic v1 

183@needs_pydanticv1 1abcde

184def test_openapi_schema_pv1(client: TestClient): 1abcde

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

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

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

188 "openapi": "3.1.0", 

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

190 "paths": { 

191 "/items/{item_id}": { 

192 "get": { 

193 "responses": { 

194 "200": { 

195 "description": "Successful Response", 

196 "content": { 

197 "application/json": { 

198 "schema": {"$ref": "#/components/schemas/Item"} 

199 } 

200 }, 

201 }, 

202 "422": { 

203 "description": "Validation Error", 

204 "content": { 

205 "application/json": { 

206 "schema": { 

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

208 } 

209 } 

210 }, 

211 }, 

212 }, 

213 "summary": "Read Item", 

214 "operationId": "read_item_items__item_id__get", 

215 "parameters": [ 

216 { 

217 "required": True, 

218 "schema": {"title": "Item Id", "type": "string"}, 

219 "name": "item_id", 

220 "in": "path", 

221 } 

222 ], 

223 }, 

224 "put": { 

225 "responses": { 

226 "200": { 

227 "description": "Successful Response", 

228 "content": { 

229 "application/json": { 

230 "schema": {"$ref": "#/components/schemas/Item"} 

231 } 

232 }, 

233 }, 

234 "422": { 

235 "description": "Validation Error", 

236 "content": { 

237 "application/json": { 

238 "schema": { 

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

240 } 

241 } 

242 }, 

243 }, 

244 }, 

245 "summary": "Update Item", 

246 "operationId": "update_item_items__item_id__put", 

247 "parameters": [ 

248 { 

249 "required": True, 

250 "schema": {"title": "Item Id", "type": "string"}, 

251 "name": "item_id", 

252 "in": "path", 

253 } 

254 ], 

255 "requestBody": { 

256 "content": { 

257 "application/json": { 

258 "schema": {"$ref": "#/components/schemas/Item"} 

259 } 

260 }, 

261 "required": True, 

262 }, 

263 }, 

264 } 

265 }, 

266 "components": { 

267 "schemas": { 

268 "Item": { 

269 "title": "Item", 

270 "type": "object", 

271 "properties": { 

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

273 "description": {"title": "Description", "type": "string"}, 

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

275 "tax": {"title": "Tax", "type": "number", "default": 10.5}, 

276 "tags": { 

277 "title": "Tags", 

278 "type": "array", 

279 "items": {"type": "string"}, 

280 "default": [], 

281 }, 

282 }, 

283 }, 

284 "ValidationError": { 

285 "title": "ValidationError", 

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

287 "type": "object", 

288 "properties": { 

289 "loc": { 

290 "title": "Location", 

291 "type": "array", 

292 "items": { 

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

294 }, 

295 }, 

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

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

298 }, 

299 }, 

300 "HTTPValidationError": { 

301 "title": "HTTPValidationError", 

302 "type": "object", 

303 "properties": { 

304 "detail": { 

305 "title": "Detail", 

306 "type": "array", 

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

308 } 

309 }, 

310 }, 

311 } 

312 }, 

313 }