Coverage for tests/test_tutorial/test_fastapi/test_delete/test_tutorial001_py39.py: 100%

45 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2024-09-09 00:02 +0000

1from dirty_equals import IsDict 1jefghi

2from fastapi.testclient import TestClient 1jefghi

3from sqlmodel import create_engine 1jefghi

4from sqlmodel.pool import StaticPool 1jefghi

5 

6from ....conftest import needs_py39 1jefghi

7 

8 

9@needs_py39 1jefghi

10def test_tutorial(clear_sqlmodel): 1efghi

11 from docs_src.tutorial.fastapi.delete import tutorial001_py39 as mod 1abcd

12 

13 mod.sqlite_url = "sqlite://" 1abcd

14 mod.engine = create_engine( 1abcd

15 mod.sqlite_url, connect_args=mod.connect_args, poolclass=StaticPool 

16 ) 

17 

18 with TestClient(mod.app) as client: 1abcd

19 hero1_data = {"name": "Deadpond", "secret_name": "Dive Wilson"} 1abcd

20 hero2_data = { 1abcd

21 "name": "Spider-Boy", 

22 "secret_name": "Pedro Parqueador", 

23 "id": 9000, 

24 } 

25 hero3_data = { 1abcd

26 "name": "Rusty-Man", 

27 "secret_name": "Tommy Sharp", 

28 "age": 48, 

29 } 

30 response = client.post("/heroes/", json=hero1_data) 1abcd

31 assert response.status_code == 200, response.text 1abcd

32 response = client.post("/heroes/", json=hero2_data) 1abcd

33 assert response.status_code == 200, response.text 1abcd

34 hero2 = response.json() 1abcd

35 hero2_id = hero2["id"] 1abcd

36 response = client.post("/heroes/", json=hero3_data) 1abcd

37 assert response.status_code == 200, response.text 1abcd

38 response = client.get(f"/heroes/{hero2_id}") 1abcd

39 assert response.status_code == 200, response.text 1abcd

40 response = client.get("/heroes/9000") 1abcd

41 assert response.status_code == 404, response.text 1abcd

42 response = client.get("/heroes/") 1abcd

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

44 data = response.json() 1abcd

45 assert len(data) == 3 1abcd

46 response = client.patch( 1abcd

47 f"/heroes/{hero2_id}", json={"secret_name": "Spider-Youngster"} 

48 ) 

49 assert response.status_code == 200, response.text 1abcd

50 response = client.patch("/heroes/9001", json={"name": "Dragon Cube X"}) 1abcd

51 assert response.status_code == 404, response.text 1abcd

52 

53 response = client.delete(f"/heroes/{hero2_id}") 1abcd

54 assert response.status_code == 200, response.text 1abcd

55 response = client.get("/heroes/") 1abcd

56 assert response.status_code == 200, response.text 1abcd

57 data = response.json() 1abcd

58 assert len(data) == 2 1abcd

59 

60 response = client.delete("/heroes/9000") 1abcd

61 assert response.status_code == 404, response.text 1abcd

62 

63 response = client.get("/openapi.json") 1abcd

64 assert response.status_code == 200, response.text 1abcd

65 assert response.json() == { 1abcd

66 "openapi": "3.1.0", 

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

68 "paths": { 

69 "/heroes/": { 

70 "get": { 

71 "summary": "Read Heroes", 

72 "operationId": "read_heroes_heroes__get", 

73 "parameters": [ 

74 { 

75 "required": False, 

76 "schema": { 

77 "title": "Offset", 

78 "type": "integer", 

79 "default": 0, 

80 }, 

81 "name": "offset", 

82 "in": "query", 

83 }, 

84 { 

85 "required": False, 

86 "schema": { 

87 "title": "Limit", 

88 "maximum": 100.0, 

89 "type": "integer", 

90 "default": 100, 

91 }, 

92 "name": "limit", 

93 "in": "query", 

94 }, 

95 ], 

96 "responses": { 

97 "200": { 

98 "description": "Successful Response", 

99 "content": { 

100 "application/json": { 

101 "schema": { 

102 "title": "Response Read Heroes Heroes Get", 

103 "type": "array", 

104 "items": { 

105 "$ref": "#/components/schemas/HeroPublic" 

106 }, 

107 } 

108 } 

109 }, 

110 }, 

111 "422": { 

112 "description": "Validation Error", 

113 "content": { 

114 "application/json": { 

115 "schema": { 

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

117 } 

118 } 

119 }, 

120 }, 

121 }, 

122 }, 

123 "post": { 

124 "summary": "Create Hero", 

125 "operationId": "create_hero_heroes__post", 

126 "requestBody": { 

127 "content": { 

128 "application/json": { 

129 "schema": { 

130 "$ref": "#/components/schemas/HeroCreate" 

131 } 

132 } 

133 }, 

134 "required": True, 

135 }, 

136 "responses": { 

137 "200": { 

138 "description": "Successful Response", 

139 "content": { 

140 "application/json": { 

141 "schema": { 

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

143 } 

144 } 

145 }, 

146 }, 

147 "422": { 

148 "description": "Validation Error", 

149 "content": { 

150 "application/json": { 

151 "schema": { 

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

153 } 

154 } 

155 }, 

156 }, 

157 }, 

158 }, 

159 }, 

160 "/heroes/{hero_id}": { 

161 "get": { 

162 "summary": "Read Hero", 

163 "operationId": "read_hero_heroes__hero_id__get", 

164 "parameters": [ 

165 { 

166 "required": True, 

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

168 "name": "hero_id", 

169 "in": "path", 

170 } 

171 ], 

172 "responses": { 

173 "200": { 

174 "description": "Successful Response", 

175 "content": { 

176 "application/json": { 

177 "schema": { 

178 "$ref": "#/components/schemas/HeroPublic" 

179 } 

180 } 

181 }, 

182 }, 

183 "422": { 

184 "description": "Validation Error", 

185 "content": { 

186 "application/json": { 

187 "schema": { 

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

189 } 

190 } 

191 }, 

192 }, 

193 }, 

194 }, 

195 "delete": { 

196 "summary": "Delete Hero", 

197 "operationId": "delete_hero_heroes__hero_id__delete", 

198 "parameters": [ 

199 { 

200 "required": True, 

201 "schema": {"title": "Hero Id", "type": "integer"}, 

202 "name": "hero_id", 

203 "in": "path", 

204 } 

205 ], 

206 "responses": { 

207 "200": { 

208 "description": "Successful Response", 

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

210 }, 

211 "422": { 

212 "description": "Validation Error", 

213 "content": { 

214 "application/json": { 

215 "schema": { 

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

217 } 

218 } 

219 }, 

220 }, 

221 }, 

222 }, 

223 "patch": { 

224 "summary": "Update Hero", 

225 "operationId": "update_hero_heroes__hero_id__patch", 

226 "parameters": [ 

227 { 

228 "required": True, 

229 "schema": {"title": "Hero Id", "type": "integer"}, 

230 "name": "hero_id", 

231 "in": "path", 

232 } 

233 ], 

234 "requestBody": { 

235 "content": { 

236 "application/json": { 

237 "schema": { 

238 "$ref": "#/components/schemas/HeroUpdate" 

239 } 

240 } 

241 }, 

242 "required": True, 

243 }, 

244 "responses": { 

245 "200": { 

246 "description": "Successful Response", 

247 "content": { 

248 "application/json": { 

249 "schema": { 

250 "$ref": "#/components/schemas/HeroPublic" 

251 } 

252 } 

253 }, 

254 }, 

255 "422": { 

256 "description": "Validation Error", 

257 "content": { 

258 "application/json": { 

259 "schema": { 

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

261 } 

262 } 

263 }, 

264 }, 

265 }, 

266 }, 

267 }, 

268 }, 

269 "components": { 

270 "schemas": { 

271 "HTTPValidationError": { 

272 "title": "HTTPValidationError", 

273 "type": "object", 

274 "properties": { 

275 "detail": { 

276 "title": "Detail", 

277 "type": "array", 

278 "items": { 

279 "$ref": "#/components/schemas/ValidationError" 

280 }, 

281 } 

282 }, 

283 }, 

284 "HeroCreate": { 

285 "title": "HeroCreate", 

286 "required": ["name", "secret_name"], 

287 "type": "object", 

288 "properties": { 

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

290 "secret_name": {"title": "Secret Name", "type": "string"}, 

291 "age": IsDict( 

292 { 

293 "title": "Age", 

294 "anyOf": [{"type": "integer"}, {"type": "null"}], 

295 } 

296 ) 

297 | IsDict( 

298 # TODO: remove when deprecating Pydantic v1 

299 {"title": "Age", "type": "integer"} 

300 ), 

301 }, 

302 }, 

303 "HeroPublic": { 

304 "title": "HeroPublic", 

305 "required": ["name", "secret_name", "id"], 

306 "type": "object", 

307 "properties": { 

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

309 "secret_name": {"title": "Secret Name", "type": "string"}, 

310 "age": IsDict( 

311 { 

312 "title": "Age", 

313 "anyOf": [{"type": "integer"}, {"type": "null"}], 

314 } 

315 ) 

316 | IsDict( 

317 # TODO: remove when deprecating Pydantic v1 

318 {"title": "Age", "type": "integer"} 

319 ), 

320 "id": {"title": "Id", "type": "integer"}, 

321 }, 

322 }, 

323 "HeroUpdate": { 

324 "title": "HeroUpdate", 

325 "type": "object", 

326 "properties": { 

327 "name": IsDict( 

328 { 

329 "title": "Name", 

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

331 } 

332 ) 

333 | IsDict( 

334 # TODO: remove when deprecating Pydantic v1 

335 {"title": "Name", "type": "string"} 

336 ), 

337 "secret_name": IsDict( 

338 { 

339 "title": "Secret Name", 

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

341 } 

342 ) 

343 | IsDict( 

344 # TODO: remove when deprecating Pydantic v1 

345 {"title": "Secret Name", "type": "string"} 

346 ), 

347 "age": IsDict( 

348 { 

349 "title": "Age", 

350 "anyOf": [{"type": "integer"}, {"type": "null"}], 

351 } 

352 ) 

353 | IsDict( 

354 # TODO: remove when deprecating Pydantic v1 

355 {"title": "Age", "type": "integer"} 

356 ), 

357 }, 

358 }, 

359 "ValidationError": { 

360 "title": "ValidationError", 

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

362 "type": "object", 

363 "properties": { 

364 "loc": { 

365 "title": "Location", 

366 "type": "array", 

367 "items": { 

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

369 }, 

370 }, 

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

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

373 }, 

374 }, 

375 } 

376 }, 

377 }