Coverage for tests / test_tutorial / test_settings / test_app03.py: 100%

25 statements  

« prev     ^ index     » next       coverage.py v7.13.3, created at 2026-04-06 01:24 +0000

1import importlib 1abcd

2from types import ModuleType 1abcd

3 

4import pytest 1abcd

5from fastapi.testclient import TestClient 1abcd

6from pytest import MonkeyPatch 1abcd

7 

8 

9@pytest.fixture( 1abcd

10 name="mod_path", 

11 params=[ 

12 pytest.param("app03_py310"), 

13 pytest.param("app03_an_py310"), 

14 ], 

15) 

16def get_mod_path(request: pytest.FixtureRequest): 1abcd

17 mod_path = f"docs_src.settings.{request.param}" 1tuvwxyzABCDEFG

18 return mod_path 1tuvwxyzABCDEFG

19 

20 

21@pytest.fixture(name="main_mod") 1abcd

22def get_main_mod(mod_path: str) -> ModuleType: 1abcd

23 main_mod = importlib.import_module(f"{mod_path}.main") 1tuvwxyzABCDEFG

24 return main_mod 1tuvwxyzABCDEFG

25 

26 

27def test_settings(main_mod: ModuleType, monkeypatch: MonkeyPatch): 1abcd

28 monkeypatch.setenv("ADMIN_EMAIL", "[email protected]") 1efghijkl

29 settings = main_mod.get_settings() 1efghijkl

30 assert settings.app_name == "Awesome API" 1efghijkl

31 assert settings.admin_email == "[email protected]" 1efghijkl

32 assert settings.items_per_user == 50 1efghijkl

33 

34 

35def test_endpoint(main_mod: ModuleType, monkeypatch: MonkeyPatch): 1abcd

36 monkeypatch.setenv("ADMIN_EMAIL", "[email protected]") 1mnopqrs

37 client = TestClient(main_mod.app) 1mnopqrs

38 response = client.get("/info") 1mnopqrs

39 assert response.status_code == 200 1mnopqrs

40 assert response.json() == { 1mnopqrs

41 "app_name": "Awesome API", 

42 "admin_email": "[email protected]", 

43 "items_per_user": 50, 

44 }