| 1 |
import importlib |
| 2 |
|
| 3 |
import pytest |
| 4 |
from fastapi.testclient import TestClient |
| 5 |
|
| 6 |
|
| 7 |
@pytest.fixture( |
| 8 |
name="client", |
| 9 |
params=[ |
| 10 |
pytest.param("tutorial008b_py310"), |
| 11 |
pytest.param("tutorial008b_an_py310"), |
| 12 |
], |
| 13 |
) |
| 14 |
def get_client(request: pytest.FixtureRequest): |
| 15 |
mod = importlib.import_module(f"docs_src.dependencies.{request.param}") |
| 16 |
|
| 17 |
client = TestClient(mod.app) |
| 18 |
return client |
| 19 |
|
| 20 |
|
| 21 |
def test_get_no_item(client: TestClient): |
| 22 |
response = client.get("/items/foo") |
| 23 |
assert response.status_code == 404, response.text |
| 24 |
assert response.json() == {"detail": "Item not found"} |
| 25 |
|
| 26 |
|
| 27 |
def test_owner_error(client: TestClient): |
| 28 |
response = client.get("/items/plumbus") |
| 29 |
assert response.status_code == 400, response.text |
| 30 |
assert response.json() == {"detail": "Owner error: Rick"} |
| 31 |
|
| 32 |
|
| 33 |
def test_get_item(client: TestClient): |
| 34 |
response = client.get("/items/portal-gun") |
| 35 |
assert response.status_code == 200, response.text |
| 36 |
assert response.json() == {"description": "Gun to create portals", "owner": "Rick"} |