Coverage for tests / test_tutorial / test_settings / test_app01.py: 100%
32 statements
« prev ^ index » next coverage.py v7.13.3, created at 2026-02-12 18:15 +0000
« prev ^ index » next coverage.py v7.13.3, created at 2026-02-12 18:15 +0000
1import importlib 1abdc
2import sys 1abdc
4import pytest 1abdc
5from dirty_equals import IsAnyStr 1abdc
6from fastapi.testclient import TestClient 1abdc
7from inline_snapshot import snapshot 1abdc
8from pydantic import ValidationError 1abdc
9from pytest import MonkeyPatch 1abdc
12@pytest.fixture( 1abdc
13 name="mod_name",
14 params=[
15 pytest.param("app01_py310"),
16 ],
17)
18def get_mod_name(request: pytest.FixtureRequest): 1abdc
19 return f"docs_src.settings.{request.param}.main" 1abc
22@pytest.fixture(name="client") 1abdc
23def get_test_client(mod_name: str, monkeypatch: MonkeyPatch) -> TestClient: 1abdc
24 if mod_name in sys.modules: 1abc
25 del sys.modules[mod_name] 1abc
26 monkeypatch.setenv("ADMIN_EMAIL", "admin@example.com") 1abc
27 main_mod = importlib.import_module(mod_name) 1abc
28 return TestClient(main_mod.app) 1abc
31def test_settings_validation_error(mod_name: str, monkeypatch: MonkeyPatch): 1abdc
32 monkeypatch.delenv("ADMIN_EMAIL", raising=False) 1efg
33 if mod_name in sys.modules: 1efg
34 del sys.modules[mod_name] # pragma: no cover
36 with pytest.raises(ValidationError) as exc_info: 1efg
37 importlib.import_module(mod_name) 1efg
38 assert exc_info.value.errors() == [ 1efg
39 {
40 "loc": ("admin_email",),
41 "msg": "Field required",
42 "type": "missing",
43 "input": {},
44 "url": IsAnyStr,
45 }
46 ]
49def test_app(client: TestClient): 1abdc
50 response = client.get("/info") 1hij
51 data = response.json() 1hij
52 assert data == { 1hij
53 "app_name": "Awesome API",
54 "admin_email": "admin@example.com",
55 "items_per_user": 50,
56 }
59def test_openapi_schema(client: TestClient): 1abdc
60 response = client.get("/openapi.json") 1klm
61 assert response.status_code == 200, response.text 1klm
62 assert response.json() == snapshot( 1klm
63 {
64 "openapi": "3.1.0",
65 "info": {"title": "FastAPI", "version": "0.1.0"},
66 "paths": {
67 "/info": {
68 "get": {
69 "operationId": "info_info_get",
70 "responses": {
71 "200": {
72 "description": "Successful Response",
73 "content": {"application/json": {"schema": {}}},
74 }
75 },
76 "summary": "Info",
77 }
78 }
79 },
80 }
81 )