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

20 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

6 

7from ...conftest import PrintMock 1abcdefghijklm

8 

9 

10@pytest.fixture( 1abcdefghijklm

11 name="mod", 

12 params=[ 

13 pytest.param("tutorial004"), 

14 ], 

15) 

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

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

18 mod.engine = database_engine 1abcdefghijklm

19 return mod 1abcdefghijklm

20 

21 

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

23 mod.main() 1nopqrstuvwxyz

24 assert len(print_mock.calls) == 1 1nopqrstuvwxyz

25 assert len(print_mock.calls[0]) == 1 1nopqrstuvwxyz

26 heroes = print_mock.calls[0][0] 1nopqrstuvwxyz

27 assert len(heroes) == 2 1nopqrstuvwxyz

28 assert len({hero["id"] for hero in heroes}) == 2 1nopqrstuvwxyz

29 # Without ORDER BY, the database determines which matching heroes are returned. 

30 expected_heroes = [ 1nopqrstuvwxyz

31 {"name": "Rusty-Man", "secret_name": "Tommy Sharp", "age": 48, "id": 3}, 

32 {"name": "Black Lion", "secret_name": "Trevor Challa", "age": 35, "id": 5}, 

33 {"name": "Dr. Weird", "secret_name": "Steve Weird", "age": 36, "id": 6}, 

34 { 

35 "name": "Captain North America", 

36 "secret_name": "Esteban Rogelios", 

37 "age": 93, 

38 "id": 7, 

39 }, 

40 ] 

41 for hero in heroes: 1nopqrstuvwxyz

42 assert hero in expected_heroes 1nopqrstuvwxyz