Coverage for tests/test_tutorial/test_fastapi/test_teams/test_tutorial001.py: 100%

77 statements  

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

1from dirty_equals import IsDict 1ghijkl

2from fastapi.testclient import TestClient 1ghijkl

3from sqlmodel import create_engine 1ghijkl

4from sqlmodel.pool import StaticPool 1ghijkl

5 

6 

7def test_tutorial(clear_sqlmodel): 1ghijkl

8 from docs_src.tutorial.fastapi.teams import tutorial001 as mod 1fabcde

9 

10 mod.sqlite_url = "sqlite://" 1fabcde

11 mod.engine = create_engine( 1fabcde

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

13 ) 

14 

15 with TestClient(mod.app) as client: 1fabcde

16 hero1_data = {"name": "Deadpond", "secret_name": "Dive Wilson"} 1fabcde

17 hero2_data = { 1abcde

18 "name": "Spider-Boy", 

19 "secret_name": "Pedro Parqueador", 

20 "id": 9000, 

21 } 

22 hero3_data = { 1abcde

23 "name": "Rusty-Man", 

24 "secret_name": "Tommy Sharp", 

25 "age": 48, 

26 } 

27 response = client.post("/heroes/", json=hero1_data) 1fabcde

28 assert response.status_code == 200, response.text 1fabcde

29 response = client.post("/heroes/", json=hero2_data) 1fabcde

30 assert response.status_code == 200, response.text 1fabcde

31 hero2 = response.json() 1fabcde

32 hero2_id = hero2["id"] 1fabcde

33 response = client.post("/heroes/", json=hero3_data) 1fabcde

34 assert response.status_code == 200, response.text 1fabcde

35 response = client.get(f"/heroes/{hero2_id}") 1fabcde

36 assert response.status_code == 200, response.text 1fabcde

37 response = client.get("/heroes/9000") 1fabcde

38 assert response.status_code == 404, response.text 1fabcde

39 response = client.get("/heroes/") 1fabcde

40 assert response.status_code == 200, response.text 1fabcde

41 data = response.json() 1fabcde

42 assert len(data) == 3 1fabcde

43 response = client.patch( 1fabcde

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

45 ) 

46 assert response.status_code == 200, response.text 1fabcde

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

48 assert response.status_code == 404, response.text 1fabcde

49 response = client.delete(f"/heroes/{hero2_id}") 1fabcde

50 assert response.status_code == 200, response.text 1fabcde

51 response = client.get("/heroes/") 1fabcde

52 assert response.status_code == 200, response.text 1fabcde

53 data = response.json() 1fabcde

54 assert len(data) == 2 1fabcde

55 response = client.delete("/heroes/9000") 1fabcde

56 assert response.status_code == 404, response.text 1fabcde

57 

58 team_preventers = {"name": "Preventers", "headquarters": "Sharp Tower"} 1fabcde

59 team_z_force = {"name": "Z-Force", "headquarters": "Sister Margaret's Bar"} 1fabcde

60 response = client.post("/teams/", json=team_preventers) 1fabcde

61 assert response.status_code == 200, response.text 1fabcde

62 team_preventers_data = response.json() 1fabcde

63 team_preventers_id = team_preventers_data["id"] 1fabcde

64 response = client.post("/teams/", json=team_z_force) 1fabcde

65 assert response.status_code == 200, response.text 1fabcde

66 team_z_force_data = response.json() 1fabcde

67 team_z_force_data["id"] 1fabcde

68 response = client.get("/teams/") 1fabcde

69 data = response.json() 1fabcde

70 assert len(data) == 2 1fabcde

71 response = client.get(f"/teams/{team_preventers_id}") 1fabcde

72 data = response.json() 1fabcde

73 assert response.status_code == 200, response.text 1fabcde

74 assert data == team_preventers_data 1fabcde

75 response = client.get("/teams/9000") 1fabcde

76 assert response.status_code == 404, response.text 1fabcde

77 response = client.patch( 1fabcde

78 f"/teams/{team_preventers_id}", json={"headquarters": "Preventers Tower"} 

79 ) 

80 data = response.json() 1fabcde

81 assert response.status_code == 200, response.text 1fabcde

82 assert data["name"] == team_preventers["name"] 1fabcde

83 assert data["headquarters"] == "Preventers Tower" 1fabcde

84 response = client.patch("/teams/9000", json={"name": "Freedom League"}) 1fabcde

85 assert response.status_code == 404, response.text 1fabcde

86 response = client.delete(f"/teams/{team_preventers_id}") 1fabcde

87 assert response.status_code == 200, response.text 1fabcde

88 response = client.delete("/teams/9000") 1fabcde

89 assert response.status_code == 404, response.text 1fabcde

90 response = client.get("/teams/") 1fabcde

91 assert response.status_code == 200, response.text 1fabcde

92 data = response.json() 1fabcde

93 assert len(data) == 1 1fabcde

94 

95 response = client.get("/openapi.json") 1fabcde

96 assert response.status_code == 200, response.text 1fabcde

97 assert response.json() == { 1fabcde

98 "openapi": "3.1.0", 

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

100 "paths": { 

101 "/heroes/": { 

102 "get": { 

103 "summary": "Read Heroes", 

104 "operationId": "read_heroes_heroes__get", 

105 "parameters": [ 

106 { 

107 "required": False, 

108 "schema": { 

109 "title": "Offset", 

110 "type": "integer", 

111 "default": 0, 

112 }, 

113 "name": "offset", 

114 "in": "query", 

115 }, 

116 { 

117 "required": False, 

118 "schema": { 

119 "title": "Limit", 

120 "maximum": 100.0, 

121 "type": "integer", 

122 "default": 100, 

123 }, 

124 "name": "limit", 

125 "in": "query", 

126 }, 

127 ], 

128 "responses": { 

129 "200": { 

130 "description": "Successful Response", 

131 "content": { 

132 "application/json": { 

133 "schema": { 

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

135 "type": "array", 

136 "items": { 

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

138 }, 

139 } 

140 } 

141 }, 

142 }, 

143 "422": { 

144 "description": "Validation Error", 

145 "content": { 

146 "application/json": { 

147 "schema": { 

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

149 } 

150 } 

151 }, 

152 }, 

153 }, 

154 }, 

155 "post": { 

156 "summary": "Create Hero", 

157 "operationId": "create_hero_heroes__post", 

158 "requestBody": { 

159 "content": { 

160 "application/json": { 

161 "schema": { 

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

163 } 

164 } 

165 }, 

166 "required": True, 

167 }, 

168 "responses": { 

169 "200": { 

170 "description": "Successful Response", 

171 "content": { 

172 "application/json": { 

173 "schema": { 

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

175 } 

176 } 

177 }, 

178 }, 

179 "422": { 

180 "description": "Validation Error", 

181 "content": { 

182 "application/json": { 

183 "schema": { 

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

185 } 

186 } 

187 }, 

188 }, 

189 }, 

190 }, 

191 }, 

192 "/heroes/{hero_id}": { 

193 "get": { 

194 "summary": "Read Hero", 

195 "operationId": "read_hero_heroes__hero_id__get", 

196 "parameters": [ 

197 { 

198 "required": True, 

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

200 "name": "hero_id", 

201 "in": "path", 

202 } 

203 ], 

204 "responses": { 

205 "200": { 

206 "description": "Successful Response", 

207 "content": { 

208 "application/json": { 

209 "schema": { 

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

211 } 

212 } 

213 }, 

214 }, 

215 "422": { 

216 "description": "Validation Error", 

217 "content": { 

218 "application/json": { 

219 "schema": { 

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

221 } 

222 } 

223 }, 

224 }, 

225 }, 

226 }, 

227 "delete": { 

228 "summary": "Delete Hero", 

229 "operationId": "delete_hero_heroes__hero_id__delete", 

230 "parameters": [ 

231 { 

232 "required": True, 

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

234 "name": "hero_id", 

235 "in": "path", 

236 } 

237 ], 

238 "responses": { 

239 "200": { 

240 "description": "Successful Response", 

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

242 }, 

243 "422": { 

244 "description": "Validation Error", 

245 "content": { 

246 "application/json": { 

247 "schema": { 

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

249 } 

250 } 

251 }, 

252 }, 

253 }, 

254 }, 

255 "patch": { 

256 "summary": "Update Hero", 

257 "operationId": "update_hero_heroes__hero_id__patch", 

258 "parameters": [ 

259 { 

260 "required": True, 

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

262 "name": "hero_id", 

263 "in": "path", 

264 } 

265 ], 

266 "requestBody": { 

267 "content": { 

268 "application/json": { 

269 "schema": { 

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

271 } 

272 } 

273 }, 

274 "required": True, 

275 }, 

276 "responses": { 

277 "200": { 

278 "description": "Successful Response", 

279 "content": { 

280 "application/json": { 

281 "schema": { 

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

283 } 

284 } 

285 }, 

286 }, 

287 "422": { 

288 "description": "Validation Error", 

289 "content": { 

290 "application/json": { 

291 "schema": { 

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

293 } 

294 } 

295 }, 

296 }, 

297 }, 

298 }, 

299 }, 

300 "/teams/": { 

301 "get": { 

302 "summary": "Read Teams", 

303 "operationId": "read_teams_teams__get", 

304 "parameters": [ 

305 { 

306 "required": False, 

307 "schema": { 

308 "title": "Offset", 

309 "type": "integer", 

310 "default": 0, 

311 }, 

312 "name": "offset", 

313 "in": "query", 

314 }, 

315 { 

316 "required": False, 

317 "schema": { 

318 "title": "Limit", 

319 "maximum": 100.0, 

320 "type": "integer", 

321 "default": 100, 

322 }, 

323 "name": "limit", 

324 "in": "query", 

325 }, 

326 ], 

327 "responses": { 

328 "200": { 

329 "description": "Successful Response", 

330 "content": { 

331 "application/json": { 

332 "schema": { 

333 "title": "Response Read Teams Teams Get", 

334 "type": "array", 

335 "items": { 

336 "$ref": "#/components/schemas/TeamPublic" 

337 }, 

338 } 

339 } 

340 }, 

341 }, 

342 "422": { 

343 "description": "Validation Error", 

344 "content": { 

345 "application/json": { 

346 "schema": { 

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

348 } 

349 } 

350 }, 

351 }, 

352 }, 

353 }, 

354 "post": { 

355 "summary": "Create Team", 

356 "operationId": "create_team_teams__post", 

357 "requestBody": { 

358 "content": { 

359 "application/json": { 

360 "schema": { 

361 "$ref": "#/components/schemas/TeamCreate" 

362 } 

363 } 

364 }, 

365 "required": True, 

366 }, 

367 "responses": { 

368 "200": { 

369 "description": "Successful Response", 

370 "content": { 

371 "application/json": { 

372 "schema": { 

373 "$ref": "#/components/schemas/TeamPublic" 

374 } 

375 } 

376 }, 

377 }, 

378 "422": { 

379 "description": "Validation Error", 

380 "content": { 

381 "application/json": { 

382 "schema": { 

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

384 } 

385 } 

386 }, 

387 }, 

388 }, 

389 }, 

390 }, 

391 "/teams/{team_id}": { 

392 "get": { 

393 "summary": "Read Team", 

394 "operationId": "read_team_teams__team_id__get", 

395 "parameters": [ 

396 { 

397 "required": True, 

398 "schema": {"title": "Team Id", "type": "integer"}, 

399 "name": "team_id", 

400 "in": "path", 

401 } 

402 ], 

403 "responses": { 

404 "200": { 

405 "description": "Successful Response", 

406 "content": { 

407 "application/json": { 

408 "schema": { 

409 "$ref": "#/components/schemas/TeamPublic" 

410 } 

411 } 

412 }, 

413 }, 

414 "422": { 

415 "description": "Validation Error", 

416 "content": { 

417 "application/json": { 

418 "schema": { 

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

420 } 

421 } 

422 }, 

423 }, 

424 }, 

425 }, 

426 "delete": { 

427 "summary": "Delete Team", 

428 "operationId": "delete_team_teams__team_id__delete", 

429 "parameters": [ 

430 { 

431 "required": True, 

432 "schema": {"title": "Team Id", "type": "integer"}, 

433 "name": "team_id", 

434 "in": "path", 

435 } 

436 ], 

437 "responses": { 

438 "200": { 

439 "description": "Successful Response", 

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

441 }, 

442 "422": { 

443 "description": "Validation Error", 

444 "content": { 

445 "application/json": { 

446 "schema": { 

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

448 } 

449 } 

450 }, 

451 }, 

452 }, 

453 }, 

454 "patch": { 

455 "summary": "Update Team", 

456 "operationId": "update_team_teams__team_id__patch", 

457 "parameters": [ 

458 { 

459 "required": True, 

460 "schema": {"title": "Team Id", "type": "integer"}, 

461 "name": "team_id", 

462 "in": "path", 

463 } 

464 ], 

465 "requestBody": { 

466 "content": { 

467 "application/json": { 

468 "schema": { 

469 "$ref": "#/components/schemas/TeamUpdate" 

470 } 

471 } 

472 }, 

473 "required": True, 

474 }, 

475 "responses": { 

476 "200": { 

477 "description": "Successful Response", 

478 "content": { 

479 "application/json": { 

480 "schema": { 

481 "$ref": "#/components/schemas/TeamPublic" 

482 } 

483 } 

484 }, 

485 }, 

486 "422": { 

487 "description": "Validation Error", 

488 "content": { 

489 "application/json": { 

490 "schema": { 

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

492 } 

493 } 

494 }, 

495 }, 

496 }, 

497 }, 

498 }, 

499 }, 

500 "components": { 

501 "schemas": { 

502 "HTTPValidationError": { 

503 "title": "HTTPValidationError", 

504 "type": "object", 

505 "properties": { 

506 "detail": { 

507 "title": "Detail", 

508 "type": "array", 

509 "items": { 

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

511 }, 

512 } 

513 }, 

514 }, 

515 "HeroCreate": { 

516 "title": "HeroCreate", 

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

518 "type": "object", 

519 "properties": { 

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

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

522 "age": IsDict( 

523 { 

524 "title": "Age", 

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

526 } 

527 ) 

528 | IsDict( 

529 # TODO: remove when deprecating Pydantic v1 

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

531 ), 

532 "team_id": IsDict( 

533 { 

534 "title": "Team Id", 

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

536 } 

537 ) 

538 | IsDict( 

539 # TODO: remove when deprecating Pydantic v1 

540 {"title": "Team Id", "type": "integer"} 

541 ), 

542 }, 

543 }, 

544 "HeroPublic": { 

545 "title": "HeroPublic", 

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

547 "type": "object", 

548 "properties": { 

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

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

551 "age": IsDict( 

552 { 

553 "title": "Age", 

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

555 } 

556 ) 

557 | IsDict( 

558 # TODO: remove when deprecating Pydantic v1 

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

560 ), 

561 "team_id": IsDict( 

562 { 

563 "title": "Team Id", 

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

565 } 

566 ) 

567 | IsDict( 

568 # TODO: remove when deprecating Pydantic v1 

569 {"title": "Team Id", "type": "integer"} 

570 ), 

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

572 }, 

573 }, 

574 "HeroUpdate": { 

575 "title": "HeroUpdate", 

576 "type": "object", 

577 "properties": { 

578 "name": IsDict( 

579 { 

580 "title": "Name", 

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

582 } 

583 ) 

584 | IsDict( 

585 # TODO: remove when deprecating Pydantic v1 

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

587 ), 

588 "secret_name": IsDict( 

589 { 

590 "title": "Secret Name", 

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

592 } 

593 ) 

594 | IsDict( 

595 # TODO: remove when deprecating Pydantic v1 

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

597 ), 

598 "age": IsDict( 

599 { 

600 "title": "Age", 

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

602 } 

603 ) 

604 | IsDict( 

605 # TODO: remove when deprecating Pydantic v1 

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

607 ), 

608 "team_id": IsDict( 

609 { 

610 "title": "Team Id", 

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

612 } 

613 ) 

614 | IsDict( 

615 # TODO: remove when deprecating Pydantic v1 

616 {"title": "Team Id", "type": "integer"} 

617 ), 

618 }, 

619 }, 

620 "TeamCreate": { 

621 "title": "TeamCreate", 

622 "required": ["name", "headquarters"], 

623 "type": "object", 

624 "properties": { 

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

626 "headquarters": {"title": "Headquarters", "type": "string"}, 

627 }, 

628 }, 

629 "TeamPublic": { 

630 "title": "TeamPublic", 

631 "required": ["name", "headquarters", "id"], 

632 "type": "object", 

633 "properties": { 

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

635 "headquarters": {"title": "Headquarters", "type": "string"}, 

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

637 }, 

638 }, 

639 "TeamUpdate": { 

640 "title": "TeamUpdate", 

641 "type": "object", 

642 "properties": { 

643 "name": IsDict( 

644 { 

645 "title": "Name", 

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

647 } 

648 ) 

649 | IsDict( 

650 # TODO: remove when deprecating Pydantic v1 

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

652 ), 

653 "headquarters": IsDict( 

654 { 

655 "title": "Headquarters", 

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

657 } 

658 ) 

659 | IsDict( 

660 # TODO: remove when deprecating Pydantic v1 

661 {"title": "Headquarters", "type": "string"} 

662 ), 

663 }, 

664 }, 

665 "ValidationError": { 

666 "title": "ValidationError", 

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

668 "type": "object", 

669 "properties": { 

670 "loc": { 

671 "title": "Location", 

672 "type": "array", 

673 "items": { 

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

675 }, 

676 }, 

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

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

679 }, 

680 }, 

681 } 

682 }, 

683 }