Coverage for tests/test_tutorial/test_one/test_tutorial004.py: 100%

25 statements  

« prev     ^ index     » next       coverage.py v7.17.2, created at 2026-11-16 00:02 +0000

1import importlib 1abcdefghijklm

2from types import ModuleType 1abcdefghijklm

3 

4import pytest 1abcdefghijklm

5from sqlalchemy import Engine 1abcdefghijklm

6from sqlalchemy.exc import MultipleResultsFound 1abcdefghijklm

7from sqlmodel import Session, delete 1abcdefghijklm

8 

9from ...conftest import PrintMock 1abcdefghijklm

10 

11 

12@pytest.fixture( 1abcdefghijklm

13 name="mod", 

14 params=[ 

15 pytest.param("tutorial004"), 

16 ], 

17) 

18def get_module(request: pytest.FixtureRequest, database_engine: Engine) -> ModuleType: 1abcdefghijklm

19 mod = importlib.import_module(f"docs_src.tutorial.one.{request.param}") 1abcdefghijklm

20 mod.engine = database_engine 1abcdefghijklm

21 return mod 1abcdefghijklm

22 

23 

24def test_tutorial(print_mock: PrintMock, mod: ModuleType): 1abcdefghijklm

25 with pytest.raises(MultipleResultsFound): 1nopqrstuvwxyz

26 mod.main() 1nopqrstuvwxyz

27 with Session(mod.engine) as session: 1nopqrstuvwxyz

28 # TODO: create delete() function 

29 # TODO: add overloads for .exec() with delete object 

30 session.exec(delete(mod.Hero)) 1nopqrstuvwxyz

31 hero = mod.Hero(name="Test Hero", secret_name="Secret Test Hero", age=24) 1nopqrstuvwxyz

32 session.add(hero) 1nopqrstuvwxyz

33 session.commit() 1nopqrstuvwxyz

34 session.refresh(hero) 1nopqrstuvwxyz

35 hero_id = hero.id 1nopqrstuvwxyz

36 assert hero_id is not None 1nopqrstuvwxyz

37 

38 mod.select_heroes() 1nopqrstuvwxyz

39 assert print_mock.calls == [ 1nopqrstuvwxyz

40 [ 

41 "Hero:", 

42 { 

43 "id": hero_id, 

44 "name": "Test Hero", 

45 "secret_name": "Secret Test Hero", 

46 "age": 24, 

47 }, 

48 ] 

49 ]