Coverage for tests/test_tutorial/test_relationship_attributes/test_delete_records_relationship/test_tutorial004.py: 100%

26 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 IntegrityError 1abcdefghijklm

7from sqlmodel import Session, select 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( 1abcdefghijklm

20 f"docs_src.tutorial.relationship_attributes.cascade_delete_relationships.{request.param}" 

21 ) 

22 mod.engine = database_engine 1abcdefghijklm

23 return mod 1abcdefghijklm

24 

25 

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

27 mod.create_db_and_tables() 1nopqrstuvwxyz

28 mod.create_heroes() 1nopqrstuvwxyz

29 mod.select_deleted_heroes() 1nopqrstuvwxyz

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

31 team = session.exec(select(mod.Team).where(mod.Team.name == "Wakaland")).one() 1nopqrstuvwxyz

32 team.heroes.clear() 1nopqrstuvwxyz

33 session.add(team) 1nopqrstuvwxyz

34 session.commit() 1nopqrstuvwxyz

35 mod.delete_team() 1nopqrstuvwxyz

36 assert print_mock.calls == [ 1nopqrstuvwxyz

37 [ 

38 "Created hero:", 

39 { 

40 "age": None, 

41 "id": 1, 

42 "name": "Deadpond", 

43 "secret_name": "Dive Wilson", 

44 "team_id": 1, 

45 }, 

46 ], 

47 [ 

48 "Created hero:", 

49 { 

50 "age": 48, 

51 "id": 2, 

52 "name": "Rusty-Man", 

53 "secret_name": "Tommy Sharp", 

54 "team_id": 2, 

55 }, 

56 ], 

57 [ 

58 "Created hero:", 

59 { 

60 "age": None, 

61 "id": 3, 

62 "name": "Spider-Boy", 

63 "secret_name": "Pedro Parqueador", 

64 "team_id": None, 

65 }, 

66 ], 

67 [ 

68 "Updated hero:", 

69 { 

70 "age": None, 

71 "id": 3, 

72 "name": "Spider-Boy", 

73 "secret_name": "Pedro Parqueador", 

74 "team_id": 2, 

75 }, 

76 ], 

77 [ 

78 "Team Wakaland:", 

79 {"headquarters": "Wakaland Capital City", "id": 3, "name": "Wakaland"}, 

80 ], 

81 [ 

82 "Black Lion has no team:", 

83 { 

84 "age": 35, 

85 "id": 4, 

86 "name": "Black Lion", 

87 "secret_name": "Trevor Challa", 

88 "team_id": 3, 

89 }, 

90 ], 

91 [ 

92 "Princess Sure-E has no team:", 

93 { 

94 "age": None, 

95 "id": 5, 

96 "name": "Princess Sure-E", 

97 "secret_name": "Sure-E", 

98 "team_id": 3, 

99 }, 

100 ], 

101 [ 

102 "Deleted team:", 

103 {"headquarters": "Wakaland Capital City", "id": 3, "name": "Wakaland"}, 

104 ], 

105 ] 

106 

107 with pytest.raises(IntegrityError) as exc: 1nopqrstuvwxyz

108 mod.main() 1nopqrstuvwxyz

109 assert "foreign key" in str(exc.value).lower() 1nopqrstuvwxyz