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

79 statements  

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

1from dirty_equals import IsDict 1idefgh

2from fastapi.testclient import TestClient 1idefgh

3from sqlmodel import create_engine 1idefgh

4from sqlmodel.pool import StaticPool 1idefgh

5 

6from ....conftest import needs_py310 1idefgh

7 

8 

9@needs_py310 1idefgh

10def test_tutorial(clear_sqlmodel): 1defgh

11 from docs_src.tutorial.fastapi.teams import tutorial001_py310 as mod 1abc

12 

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

14 mod.engine = create_engine( 1abc

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

16 ) 

17 

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

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

20 hero2_data = { 1abc

21 "name": "Spider-Boy", 

22 "secret_name": "Pedro Parqueador", 

23 "id": 9000, 

24 } 

25 hero3_data = { 1abc

26 "name": "Rusty-Man", 

27 "secret_name": "Tommy Sharp", 

28 "age": 48, 

29 } 

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

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

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

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

34 hero2 = response.json() 1abc

35 hero2_id = hero2["id"] 1abc

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

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

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

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

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

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

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

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

44 data = response.json() 1abc

45 assert len(data) == 3 1abc

46 response = client.patch( 1abc

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

48 ) 

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

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

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

52 response = client.delete(f"/heroes/{hero2_id}") 1abc

53 assert response.status_code == 200, response.text 1abc

54 response = client.get("/heroes/") 1abc

55 assert response.status_code == 200, response.text 1abc

56 data = response.json() 1abc

57 assert len(data) == 2 1abc

58 response = client.delete("/heroes/9000") 1abc

59 assert response.status_code == 404, response.text 1abc

60 

61 team_preventers = {"name": "Preventers", "headquarters": "Sharp Tower"} 1abc

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

63 response = client.post("/teams/", json=team_preventers) 1abc

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

65 team_preventers_data = response.json() 1abc

66 team_preventers_id = team_preventers_data["id"] 1abc

67 response = client.post("/teams/", json=team_z_force) 1abc

68 assert response.status_code == 200, response.text 1abc

69 team_z_force_data = response.json() 1abc

70 team_z_force_data["id"] 1abc

71 response = client.get("/teams/") 1abc

72 data = response.json() 1abc

73 assert len(data) == 2 1abc

74 response = client.get(f"/teams/{team_preventers_id}") 1abc

75 data = response.json() 1abc

76 assert response.status_code == 200, response.text 1abc

77 assert data == team_preventers_data 1abc

78 response = client.get("/teams/9000") 1abc

79 assert response.status_code == 404, response.text 1abc

80 response = client.patch( 1abc

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

82 ) 

83 data = response.json() 1abc

84 assert response.status_code == 200, response.text 1abc

85 assert data["name"] == team_preventers["name"] 1abc

86 assert data["headquarters"] == "Preventers Tower" 1abc

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

88 assert response.status_code == 404, response.text 1abc

89 response = client.delete(f"/teams/{team_preventers_id}") 1abc

90 assert response.status_code == 200, response.text 1abc

91 response = client.delete("/teams/9000") 1abc

92 assert response.status_code == 404, response.text 1abc

93 response = client.get("/teams/") 1abc

94 assert response.status_code == 200, response.text 1abc

95 data = response.json() 1abc

96 assert len(data) == 1 1abc

97 

98 response = client.get("/openapi.json") 1abc

99 assert response.status_code == 200, response.text 1abc

100 assert response.json() == { 1abc

101 "openapi": "3.1.0", 

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

103 "paths": { 

104 "/heroes/": { 

105 "get": { 

106 "summary": "Read Heroes", 

107 "operationId": "read_heroes_heroes__get", 

108 "parameters": [ 

109 { 

110 "required": False, 

111 "schema": { 

112 "title": "Offset", 

113 "type": "integer", 

114 "default": 0, 

115 }, 

116 "name": "offset", 

117 "in": "query", 

118 }, 

119 { 

120 "required": False, 

121 "schema": { 

122 "title": "Limit", 

123 "maximum": 100.0, 

124 "type": "integer", 

125 "default": 100, 

126 }, 

127 "name": "limit", 

128 "in": "query", 

129 }, 

130 ], 

131 "responses": { 

132 "200": { 

133 "description": "Successful Response", 

134 "content": { 

135 "application/json": { 

136 "schema": { 

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

138 "type": "array", 

139 "items": { 

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

141 }, 

142 } 

143 } 

144 }, 

145 }, 

146 "422": { 

147 "description": "Validation Error", 

148 "content": { 

149 "application/json": { 

150 "schema": { 

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

152 } 

153 } 

154 }, 

155 }, 

156 }, 

157 }, 

158 "post": { 

159 "summary": "Create Hero", 

160 "operationId": "create_hero_heroes__post", 

161 "requestBody": { 

162 "content": { 

163 "application/json": { 

164 "schema": { 

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

166 } 

167 } 

168 }, 

169 "required": True, 

170 }, 

171 "responses": { 

172 "200": { 

173 "description": "Successful Response", 

174 "content": { 

175 "application/json": { 

176 "schema": { 

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

178 } 

179 } 

180 }, 

181 }, 

182 "422": { 

183 "description": "Validation Error", 

184 "content": { 

185 "application/json": { 

186 "schema": { 

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

188 } 

189 } 

190 }, 

191 }, 

192 }, 

193 }, 

194 }, 

195 "/heroes/{hero_id}": { 

196 "get": { 

197 "summary": "Read Hero", 

198 "operationId": "read_hero_heroes__hero_id__get", 

199 "parameters": [ 

200 { 

201 "required": True, 

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

203 "name": "hero_id", 

204 "in": "path", 

205 } 

206 ], 

207 "responses": { 

208 "200": { 

209 "description": "Successful Response", 

210 "content": { 

211 "application/json": { 

212 "schema": { 

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

214 } 

215 } 

216 }, 

217 }, 

218 "422": { 

219 "description": "Validation Error", 

220 "content": { 

221 "application/json": { 

222 "schema": { 

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

224 } 

225 } 

226 }, 

227 }, 

228 }, 

229 }, 

230 "delete": { 

231 "summary": "Delete Hero", 

232 "operationId": "delete_hero_heroes__hero_id__delete", 

233 "parameters": [ 

234 { 

235 "required": True, 

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

237 "name": "hero_id", 

238 "in": "path", 

239 } 

240 ], 

241 "responses": { 

242 "200": { 

243 "description": "Successful Response", 

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

245 }, 

246 "422": { 

247 "description": "Validation Error", 

248 "content": { 

249 "application/json": { 

250 "schema": { 

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

252 } 

253 } 

254 }, 

255 }, 

256 }, 

257 }, 

258 "patch": { 

259 "summary": "Update Hero", 

260 "operationId": "update_hero_heroes__hero_id__patch", 

261 "parameters": [ 

262 { 

263 "required": True, 

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

265 "name": "hero_id", 

266 "in": "path", 

267 } 

268 ], 

269 "requestBody": { 

270 "content": { 

271 "application/json": { 

272 "schema": { 

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

274 } 

275 } 

276 }, 

277 "required": True, 

278 }, 

279 "responses": { 

280 "200": { 

281 "description": "Successful Response", 

282 "content": { 

283 "application/json": { 

284 "schema": { 

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

286 } 

287 } 

288 }, 

289 }, 

290 "422": { 

291 "description": "Validation Error", 

292 "content": { 

293 "application/json": { 

294 "schema": { 

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

296 } 

297 } 

298 }, 

299 }, 

300 }, 

301 }, 

302 }, 

303 "/teams/": { 

304 "get": { 

305 "summary": "Read Teams", 

306 "operationId": "read_teams_teams__get", 

307 "parameters": [ 

308 { 

309 "required": False, 

310 "schema": { 

311 "title": "Offset", 

312 "type": "integer", 

313 "default": 0, 

314 }, 

315 "name": "offset", 

316 "in": "query", 

317 }, 

318 { 

319 "required": False, 

320 "schema": { 

321 "title": "Limit", 

322 "maximum": 100.0, 

323 "type": "integer", 

324 "default": 100, 

325 }, 

326 "name": "limit", 

327 "in": "query", 

328 }, 

329 ], 

330 "responses": { 

331 "200": { 

332 "description": "Successful Response", 

333 "content": { 

334 "application/json": { 

335 "schema": { 

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

337 "type": "array", 

338 "items": { 

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

340 }, 

341 } 

342 } 

343 }, 

344 }, 

345 "422": { 

346 "description": "Validation Error", 

347 "content": { 

348 "application/json": { 

349 "schema": { 

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

351 } 

352 } 

353 }, 

354 }, 

355 }, 

356 }, 

357 "post": { 

358 "summary": "Create Team", 

359 "operationId": "create_team_teams__post", 

360 "requestBody": { 

361 "content": { 

362 "application/json": { 

363 "schema": { 

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

365 } 

366 } 

367 }, 

368 "required": True, 

369 }, 

370 "responses": { 

371 "200": { 

372 "description": "Successful Response", 

373 "content": { 

374 "application/json": { 

375 "schema": { 

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

377 } 

378 } 

379 }, 

380 }, 

381 "422": { 

382 "description": "Validation Error", 

383 "content": { 

384 "application/json": { 

385 "schema": { 

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

387 } 

388 } 

389 }, 

390 }, 

391 }, 

392 }, 

393 }, 

394 "/teams/{team_id}": { 

395 "get": { 

396 "summary": "Read Team", 

397 "operationId": "read_team_teams__team_id__get", 

398 "parameters": [ 

399 { 

400 "required": True, 

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

402 "name": "team_id", 

403 "in": "path", 

404 } 

405 ], 

406 "responses": { 

407 "200": { 

408 "description": "Successful Response", 

409 "content": { 

410 "application/json": { 

411 "schema": { 

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

413 } 

414 } 

415 }, 

416 }, 

417 "422": { 

418 "description": "Validation Error", 

419 "content": { 

420 "application/json": { 

421 "schema": { 

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

423 } 

424 } 

425 }, 

426 }, 

427 }, 

428 }, 

429 "delete": { 

430 "summary": "Delete Team", 

431 "operationId": "delete_team_teams__team_id__delete", 

432 "parameters": [ 

433 { 

434 "required": True, 

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

436 "name": "team_id", 

437 "in": "path", 

438 } 

439 ], 

440 "responses": { 

441 "200": { 

442 "description": "Successful Response", 

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

444 }, 

445 "422": { 

446 "description": "Validation Error", 

447 "content": { 

448 "application/json": { 

449 "schema": { 

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

451 } 

452 } 

453 }, 

454 }, 

455 }, 

456 }, 

457 "patch": { 

458 "summary": "Update Team", 

459 "operationId": "update_team_teams__team_id__patch", 

460 "parameters": [ 

461 { 

462 "required": True, 

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

464 "name": "team_id", 

465 "in": "path", 

466 } 

467 ], 

468 "requestBody": { 

469 "content": { 

470 "application/json": { 

471 "schema": { 

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

473 } 

474 } 

475 }, 

476 "required": True, 

477 }, 

478 "responses": { 

479 "200": { 

480 "description": "Successful Response", 

481 "content": { 

482 "application/json": { 

483 "schema": { 

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

485 } 

486 } 

487 }, 

488 }, 

489 "422": { 

490 "description": "Validation Error", 

491 "content": { 

492 "application/json": { 

493 "schema": { 

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

495 } 

496 } 

497 }, 

498 }, 

499 }, 

500 }, 

501 }, 

502 }, 

503 "components": { 

504 "schemas": { 

505 "HTTPValidationError": { 

506 "title": "HTTPValidationError", 

507 "type": "object", 

508 "properties": { 

509 "detail": { 

510 "title": "Detail", 

511 "type": "array", 

512 "items": { 

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

514 }, 

515 } 

516 }, 

517 }, 

518 "HeroCreate": { 

519 "title": "HeroCreate", 

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

521 "type": "object", 

522 "properties": { 

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

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

525 "age": IsDict( 

526 { 

527 "title": "Age", 

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

529 } 

530 ) 

531 | IsDict( 

532 # TODO: remove when deprecating Pydantic v1 

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

534 ), 

535 "team_id": IsDict( 

536 { 

537 "title": "Team Id", 

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

539 } 

540 ) 

541 | IsDict( 

542 # TODO: remove when deprecating Pydantic v1 

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

544 ), 

545 }, 

546 }, 

547 "HeroPublic": { 

548 "title": "HeroPublic", 

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

550 "type": "object", 

551 "properties": { 

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

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

554 "age": IsDict( 

555 { 

556 "title": "Age", 

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

558 } 

559 ) 

560 | IsDict( 

561 # TODO: remove when deprecating Pydantic v1 

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

563 ), 

564 "team_id": IsDict( 

565 { 

566 "title": "Team Id", 

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

568 } 

569 ) 

570 | IsDict( 

571 # TODO: remove when deprecating Pydantic v1 

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

573 ), 

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

575 }, 

576 }, 

577 "HeroUpdate": { 

578 "title": "HeroUpdate", 

579 "type": "object", 

580 "properties": { 

581 "name": IsDict( 

582 { 

583 "title": "Name", 

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

585 } 

586 ) 

587 | IsDict( 

588 # TODO: remove when deprecating Pydantic v1 

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

590 ), 

591 "secret_name": IsDict( 

592 { 

593 "title": "Secret Name", 

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

595 } 

596 ) 

597 | IsDict( 

598 # TODO: remove when deprecating Pydantic v1 

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

600 ), 

601 "age": IsDict( 

602 { 

603 "title": "Age", 

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

605 } 

606 ) 

607 | IsDict( 

608 # TODO: remove when deprecating Pydantic v1 

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

610 ), 

611 "team_id": IsDict( 

612 { 

613 "title": "Team Id", 

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

615 } 

616 ) 

617 | IsDict( 

618 # TODO: remove when deprecating Pydantic v1 

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

620 ), 

621 }, 

622 }, 

623 "TeamCreate": { 

624 "title": "TeamCreate", 

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

626 "type": "object", 

627 "properties": { 

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

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

630 }, 

631 }, 

632 "TeamPublic": { 

633 "title": "TeamPublic", 

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

635 "type": "object", 

636 "properties": { 

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

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

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

640 }, 

641 }, 

642 "TeamUpdate": { 

643 "title": "TeamUpdate", 

644 "type": "object", 

645 "properties": { 

646 "name": IsDict( 

647 { 

648 "title": "Name", 

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

650 } 

651 ) 

652 | IsDict( 

653 # TODO: remove when deprecating Pydantic v1 

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

655 ), 

656 "headquarters": IsDict( 

657 { 

658 "title": "Headquarters", 

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

660 } 

661 ) 

662 | IsDict( 

663 # TODO: remove when deprecating Pydantic v1 

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

665 ), 

666 }, 

667 }, 

668 "ValidationError": { 

669 "title": "ValidationError", 

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

671 "type": "object", 

672 "properties": { 

673 "loc": { 

674 "title": "Location", 

675 "type": "array", 

676 "items": { 

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

678 }, 

679 }, 

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

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

682 }, 

683 }, 

684 } 

685 }, 

686 }