Coverage for tests/test_tutorial/test_relationship_attributes/test_delete_records_relationship/test_tutorial004.py: 100%
25 statements
« prev ^ index » next coverage.py v7.6.1, created at 2024-09-09 00:02 +0000
« prev ^ index » next coverage.py v7.6.1, created at 2024-09-09 00:02 +0000
1from unittest.mock import patch 1ghijkl
3import pytest 1ghijkl
4from sqlalchemy.exc import IntegrityError 1ghijkl
5from sqlmodel import Session, create_engine, select 1ghijkl
7from tests.conftest import get_testing_print_function 1ghijkl
10def test_tutorial(clear_sqlmodel): 1ghijkl
11 from docs_src.tutorial.relationship_attributes.cascade_delete_relationships import ( 1abcdef
12 tutorial004 as mod,
13 )
15 mod.sqlite_url = "sqlite://" 1abcdef
16 mod.engine = create_engine(mod.sqlite_url) 1abcdef
17 calls = [] 1abcdef
19 new_print = get_testing_print_function(calls) 1abcdef
21 with patch("builtins.print", new=new_print): 1abcdef
22 mod.create_db_and_tables() 1abcdef
23 mod.create_heroes() 1abcdef
24 mod.select_deleted_heroes() 1abcdef
25 with Session(mod.engine) as session: 1abcdef
26 team = session.exec( 1abcdef
27 select(mod.Team).where(mod.Team.name == "Wakaland")
28 ).one()
29 team.heroes.clear() 1abcdef
30 session.add(team) 1abcdef
31 session.commit() 1abcdef
32 mod.delete_team() 1abcdef
33 assert calls == [ 1abcdef
34 [
35 "Created hero:",
36 {
37 "age": None,
38 "id": 1,
39 "name": "Deadpond",
40 "secret_name": "Dive Wilson",
41 "team_id": 1,
42 },
43 ],
44 [
45 "Created hero:",
46 {
47 "age": 48,
48 "id": 2,
49 "name": "Rusty-Man",
50 "secret_name": "Tommy Sharp",
51 "team_id": 2,
52 },
53 ],
54 [
55 "Created hero:",
56 {
57 "age": None,
58 "id": 3,
59 "name": "Spider-Boy",
60 "secret_name": "Pedro Parqueador",
61 "team_id": None,
62 },
63 ],
64 [
65 "Updated hero:",
66 {
67 "age": None,
68 "id": 3,
69 "name": "Spider-Boy",
70 "secret_name": "Pedro Parqueador",
71 "team_id": 2,
72 },
73 ],
74 [
75 "Team Wakaland:",
76 {"headquarters": "Wakaland Capital City", "id": 3, "name": "Wakaland"},
77 ],
78 [
79 "Black Lion has no team:",
80 {
81 "age": 35,
82 "id": 4,
83 "name": "Black Lion",
84 "secret_name": "Trevor Challa",
85 "team_id": 3,
86 },
87 ],
88 [
89 "Princess Sure-E has no team:",
90 {
91 "age": None,
92 "id": 5,
93 "name": "Princess Sure-E",
94 "secret_name": "Sure-E",
95 "team_id": 3,
96 },
97 ],
98 [
99 "Deleted team:",
100 {"headquarters": "Wakaland Capital City", "id": 3, "name": "Wakaland"},
101 ],
102 ]
104 with pytest.raises(IntegrityError) as exc: 1abcdef
105 mod.main() 1abcdef
106 assert "FOREIGN KEY constraint failed" in str(exc.value) 1abcdef