Coverage for tests / test_tutorial / test_code_structure / test_tutorial002.py: 100%

22 statements  

« prev     ^ index     » next       coverage.py v7.13.0, created at 2026-06-10 09:40 +0000

1import importlib 1abcdefgh

2from dataclasses import dataclass 1abcdefgh

3from types import ModuleType 1abcdefgh

4 

5import pytest 1abcdefgh

6from sqlmodel import create_engine 1abcdefgh

7 

8from ...conftest import PrintMock 1abcdefgh

9 

10expected_calls = [ 1abcdefgh

11 [ 

12 "Created hero:", 

13 { 

14 "id": 1, 

15 "name": "Deadpond", 

16 "age": None, 

17 "secret_name": "Dive Wilson", 

18 "team_id": 1, 

19 }, 

20 ], 

21 [ 

22 "Hero's team:", 

23 {"name": "Z-Force", "headquarters": "Sister Margaret's Bar", "id": 1}, 

24 ], 

25] 

26 

27 

28@dataclass 1abcdefgh

29class Modules: 1abcdefgh

30 app: ModuleType 1abcdefgh

31 database: ModuleType 1abcdefgh

32 

33 

34@pytest.fixture( 1abcdefgh

35 name="modules", 

36 params=[ 

37 pytest.param("tutorial002_py310"), 

38 ], 

39) 

40def get_modules(request: pytest.FixtureRequest) -> Modules: 1abcdefgh

41 app_module = importlib.import_module( 1abcdefgh

42 f"docs_src.tutorial.code_structure.{request.param}.app" 

43 ) 

44 database_module = importlib.import_module( 1abcdefgh

45 f"docs_src.tutorial.code_structure.{request.param}.database" 

46 ) 

47 database_module.sqlite_url = "sqlite://" 1abcdefgh

48 database_module.engine = create_engine(database_module.sqlite_url) 1abcdefgh

49 app_module.engine = database_module.engine 1abcdefgh

50 

51 return Modules(app=app_module, database=database_module) 1abcdefgh

52 

53 

54def test_tutorial(print_mock: PrintMock, modules: Modules): 1abcdefgh

55 modules.app.main() 1ijklmnop

56 assert print_mock.calls == expected_calls 1ijklmnop