Coverage for tests / test_tutorial / test_settings / test_app01.py: 100%
31 statements
« prev ^ index » next coverage.py v7.13.3, created at 2026-04-06 01:24 +0000
« prev ^ index » next coverage.py v7.13.3, created at 2026-04-06 01:24 +0000
1import importlib 1abcd
2import sys 1abcd
4import pytest 1abcd
5from dirty_equals import IsAnyStr 1abcd
6from fastapi.testclient import TestClient 1abcd
7from inline_snapshot import snapshot 1abcd
8from pydantic import ValidationError 1abcd
9from pytest import MonkeyPatch 1abcd
12@pytest.fixture( 1abcd
13 name="mod_name",
14 params=[
15 pytest.param("app01_py310"),
16 ],
17)
18def get_mod_name(request: pytest.FixtureRequest): 1abcd
19 return f"docs_src.settings.{request.param}.main" 1efygzhiAjkB
22@pytest.fixture(name="client") 1abcd
23def get_test_client(mod_name: str, monkeypatch: MonkeyPatch) -> TestClient: 1abcd
24 if mod_name in sys.modules: 1efpghijk
25 del sys.modules[mod_name] # pragma: no cover
26 monkeypatch.setenv("ADMIN_EMAIL", "[email protected]") 1efpghijk
27 main_mod = importlib.import_module(mod_name) 1efpghijk
28 return TestClient(main_mod.app) 1efpghijk
31def test_settings_validation_error(mod_name: str, monkeypatch: MonkeyPatch): 1abcd
32 monkeypatch.delenv("ADMIN_EMAIL", raising=False) 1lmno
33 if mod_name in sys.modules: 1lmno
34 del sys.modules[mod_name] # pragma: no cover
36 with pytest.raises(ValidationError) as exc_info: 1lmno
37 importlib.import_module(mod_name) 1lmno
38 assert exc_info.value.errors() == [ 1lmno
39 {
40 "loc": ("admin_email",),
41 "msg": "Field required",
42 "type": "missing",
43 "input": {},
44 "url": IsAnyStr,
45 }
46 ]
49def test_app(client: TestClient): 1abcd
50 response = client.get("/info") 1qrst
51 data = response.json() 1qrst
52 assert data == { 1qrst
53 "app_name": "Awesome API",
54 "admin_email": "[email protected]",
55 "items_per_user": 50,
56 }
59def test_openapi_schema(client: TestClient): 1abcd
60 response = client.get("/openapi.json") 1uvwx
61 assert response.status_code == 200, response.text 1uvwx
62 assert response.json() == snapshot( 1uvwx
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 )