| 1 |
import importlib |
| 2 |
from types import ModuleType |
| 3 |
|
| 4 |
import pytest |
| 5 |
from fastapi.testclient import TestClient |
| 6 |
from pytest import MonkeyPatch |
| 7 |
|
| 8 |
|
| 9 |
@pytest.fixture( |
| 10 |
name="mod_path", |
| 11 |
params=[ |
| 12 |
pytest.param("app03_py310"), |
| 13 |
pytest.param("app03_an_py310"), |
| 14 |
], |
| 15 |
) |
| 16 |
def get_mod_path(request: pytest.FixtureRequest): |
| 17 |
mod_path = f"docs_src.settings.{request.param}" |
| 18 |
return mod_path |
| 19 |
|
| 20 |
|
| 21 |
@pytest.fixture(name="main_mod") |
| 22 |
def get_main_mod(mod_path: str) -> ModuleType: |
| 23 |
main_mod = importlib.import_module(f"{mod_path}.main") |
| 24 |
return main_mod |
| 25 |
|
| 26 |
|
| 27 |
def test_settings(main_mod: ModuleType, monkeypatch: MonkeyPatch): |
| 28 |
monkeypatch.setenv("ADMIN_EMAIL", "[email protected]") |
| 29 |
settings = main_mod.get_settings() |
| 30 |
assert settings.app_name == "Awesome API" |
| 31 |
assert settings.admin_email == "[email protected]" |
| 32 |
assert settings.items_per_user == 50 |
| 33 |
|
| 34 |
|
| 35 |
def test_endpoint(main_mod: ModuleType, monkeypatch: MonkeyPatch): |
| 36 |
monkeypatch.setenv("ADMIN_EMAIL", "[email protected]") |
| 37 |
client = TestClient(main_mod.app) |
| 38 |
response = client.get("/info") |
| 39 |
assert response.status_code == 200 |
| 40 |
assert response.json() == { |
| 41 |
"app_name": "Awesome API", |
| 42 |
"admin_email": "[email protected]", |
| 43 |
"items_per_user": 50, |
| 44 |
} |