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

22 statements  

« prev     ^ index     » next       coverage.py v7.9.2, created at 2025-07-15 09:31 +0000

1import importlib 1abcdef

2from dataclasses import dataclass 1abcdef

3from types import ModuleType 1abcdef

4 

5import pytest 1abcdef

6from sqlmodel import create_engine 1abcdef

7 

8from tests.conftest import PrintMock, needs_py39, needs_py310 1abcdef

9 

10expected_calls = [ 1abcdef

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 1abcdef

29class Modules: 1abcdef

30 app: ModuleType 1abcdef

31 database: ModuleType 1abcdef

32 

33 

34@pytest.fixture( 1abcdef

35 name="modules", 

36 params=[ 

37 "tutorial001", 

38 pytest.param("tutorial001_py39", marks=needs_py39), 

39 pytest.param("tutorial001_py310", marks=needs_py310), 

40 ], 

41) 

42def get_modules(request: pytest.FixtureRequest) -> Modules: 1abcdef

43 app_module = importlib.import_module( 1abcdef

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

45 ) 

46 database_module = importlib.import_module( 1abcdef

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

48 ) 

49 database_module.sqlite_url = "sqlite://" 1abcdef

50 database_module.engine = create_engine(database_module.sqlite_url) 1abcdef

51 app_module.engine = database_module.engine 1abcdef

52 

53 return Modules(app=app_module, database=database_module) 1abcdef

54 

55 

56def test_tutorial(print_mock: PrintMock, modules: Modules): 1abcdef

57 modules.app.main() 1ghijkl

58 assert print_mock.calls == expected_calls 1ghijkl