| 24 | |
| 25 | |
| 26 | def test_read_items(): |
| 27 | # Before the lifespan starts, "items" is still empty |
| 28 | assert items == {} |
| 29 | |
| 30 | with TestClient(app) as client: |
| 31 | # Inside the "with TestClient" block, the lifespan starts and items added |
| 32 | assert items == {"foo": {"name": "Fighters"}, "bar": {"name": "Tenders"}} |
| 33 | |
| 34 | response = client.get("/items/foo") |
| 35 | assert response.status_code == 200 |
| 36 | assert response.json() == {"name": "Fighters"} |
| 37 | |
| 38 | # After the requests is done, the items are still there |
| 39 | assert items == {"foo": {"name": "Fighters"}, "bar": {"name": "Tenders"}} |
| 40 | |
| 41 | # The end of the "with TestClient" block simulates terminating the app, so |
| 42 | # the lifespan ends and items are cleaned up |
| 43 | assert items == {} |