✨ Add support for cascade delete relationships: cascade_delete, ondelete, and passive_deletes (#983)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Sebastián Ramírez <tiangolo@gmail.com>
This commit is contained in:
@@ -108,3 +108,14 @@ def test_sa_column_no_index() -> None:
|
||||
index=True,
|
||||
sa_column=Column(Integer, primary_key=True),
|
||||
)
|
||||
|
||||
|
||||
def test_sa_column_no_ondelete() -> None:
|
||||
with pytest.raises(RuntimeError):
|
||||
|
||||
class Item(SQLModel, table=True):
|
||||
id: Optional[int] = Field(
|
||||
default=None,
|
||||
sa_column=Column(Integer, primary_key=True),
|
||||
ondelete="CASCADE",
|
||||
)
|
||||
|
||||
37
tests/test_ondelete_raises.py
Normal file
37
tests/test_ondelete_raises.py
Normal file
@@ -0,0 +1,37 @@
|
||||
from typing import Any, List, Union
|
||||
|
||||
import pytest
|
||||
from sqlmodel import Field, Relationship, SQLModel
|
||||
|
||||
|
||||
def test_ondelete_requires_nullable(clear_sqlmodel: Any) -> None:
|
||||
with pytest.raises(RuntimeError) as exc:
|
||||
|
||||
class Team(SQLModel, table=True):
|
||||
id: Union[int, None] = Field(default=None, primary_key=True)
|
||||
|
||||
heroes: List["Hero"] = Relationship(
|
||||
back_populates="team", passive_deletes="all"
|
||||
)
|
||||
|
||||
class Hero(SQLModel, table=True):
|
||||
id: Union[int, None] = Field(default=None, primary_key=True)
|
||||
name: str = Field(index=True)
|
||||
secret_name: str
|
||||
age: Union[int, None] = Field(default=None, index=True)
|
||||
|
||||
team_id: int = Field(foreign_key="team.id", ondelete="SET NULL")
|
||||
team: Team = Relationship(back_populates="heroes")
|
||||
|
||||
assert 'ondelete="SET NULL" requires nullable=True' in str(exc.value)
|
||||
|
||||
|
||||
def test_ondelete_requires_foreign_key(clear_sqlmodel: Any) -> None:
|
||||
with pytest.raises(RuntimeError) as exc:
|
||||
|
||||
class Team(SQLModel, table=True):
|
||||
id: Union[int, None] = Field(default=None, primary_key=True)
|
||||
|
||||
age: int = Field(ondelete="CASCADE")
|
||||
|
||||
assert "ondelete can only be used with foreign_key" in str(exc.value)
|
||||
@@ -0,0 +1,72 @@
|
||||
from unittest.mock import patch
|
||||
|
||||
from sqlmodel import create_engine
|
||||
|
||||
from ....conftest import get_testing_print_function
|
||||
|
||||
|
||||
def test_tutorial(clear_sqlmodel):
|
||||
from docs_src.tutorial.relationship_attributes.cascade_delete_relationships import (
|
||||
tutorial001 as mod,
|
||||
)
|
||||
|
||||
mod.sqlite_url = "sqlite://"
|
||||
mod.engine = create_engine(mod.sqlite_url)
|
||||
calls = []
|
||||
|
||||
new_print = get_testing_print_function(calls)
|
||||
|
||||
with patch("builtins.print", new=new_print):
|
||||
mod.main()
|
||||
assert calls == [
|
||||
[
|
||||
"Created hero:",
|
||||
{
|
||||
"name": "Deadpond",
|
||||
"secret_name": "Dive Wilson",
|
||||
"team_id": 1,
|
||||
"id": 1,
|
||||
"age": None,
|
||||
},
|
||||
],
|
||||
[
|
||||
"Created hero:",
|
||||
{
|
||||
"name": "Rusty-Man",
|
||||
"secret_name": "Tommy Sharp",
|
||||
"team_id": 2,
|
||||
"id": 2,
|
||||
"age": 48,
|
||||
},
|
||||
],
|
||||
[
|
||||
"Created hero:",
|
||||
{
|
||||
"name": "Spider-Boy",
|
||||
"secret_name": "Pedro Parqueador",
|
||||
"team_id": None,
|
||||
"id": 3,
|
||||
"age": None,
|
||||
},
|
||||
],
|
||||
[
|
||||
"Updated hero:",
|
||||
{
|
||||
"name": "Spider-Boy",
|
||||
"secret_name": "Pedro Parqueador",
|
||||
"team_id": 2,
|
||||
"id": 3,
|
||||
"age": None,
|
||||
},
|
||||
],
|
||||
[
|
||||
"Team Wakaland:",
|
||||
{"name": "Wakaland", "id": 3, "headquarters": "Wakaland Capital City"},
|
||||
],
|
||||
[
|
||||
"Deleted team:",
|
||||
{"name": "Wakaland", "id": 3, "headquarters": "Wakaland Capital City"},
|
||||
],
|
||||
["Black Lion not found:", None],
|
||||
["Princess Sure-E not found:", None],
|
||||
]
|
||||
@@ -0,0 +1,73 @@
|
||||
from unittest.mock import patch
|
||||
|
||||
from sqlmodel import create_engine
|
||||
|
||||
from ....conftest import get_testing_print_function, needs_py310
|
||||
|
||||
|
||||
@needs_py310
|
||||
def test_tutorial(clear_sqlmodel):
|
||||
from docs_src.tutorial.relationship_attributes.cascade_delete_relationships import (
|
||||
tutorial001_py310 as mod,
|
||||
)
|
||||
|
||||
mod.sqlite_url = "sqlite://"
|
||||
mod.engine = create_engine(mod.sqlite_url)
|
||||
calls = []
|
||||
|
||||
new_print = get_testing_print_function(calls)
|
||||
|
||||
with patch("builtins.print", new=new_print):
|
||||
mod.main()
|
||||
assert calls == [
|
||||
[
|
||||
"Created hero:",
|
||||
{
|
||||
"name": "Deadpond",
|
||||
"secret_name": "Dive Wilson",
|
||||
"team_id": 1,
|
||||
"id": 1,
|
||||
"age": None,
|
||||
},
|
||||
],
|
||||
[
|
||||
"Created hero:",
|
||||
{
|
||||
"name": "Rusty-Man",
|
||||
"secret_name": "Tommy Sharp",
|
||||
"team_id": 2,
|
||||
"id": 2,
|
||||
"age": 48,
|
||||
},
|
||||
],
|
||||
[
|
||||
"Created hero:",
|
||||
{
|
||||
"name": "Spider-Boy",
|
||||
"secret_name": "Pedro Parqueador",
|
||||
"team_id": None,
|
||||
"id": 3,
|
||||
"age": None,
|
||||
},
|
||||
],
|
||||
[
|
||||
"Updated hero:",
|
||||
{
|
||||
"name": "Spider-Boy",
|
||||
"secret_name": "Pedro Parqueador",
|
||||
"team_id": 2,
|
||||
"id": 3,
|
||||
"age": None,
|
||||
},
|
||||
],
|
||||
[
|
||||
"Team Wakaland:",
|
||||
{"name": "Wakaland", "id": 3, "headquarters": "Wakaland Capital City"},
|
||||
],
|
||||
[
|
||||
"Deleted team:",
|
||||
{"name": "Wakaland", "id": 3, "headquarters": "Wakaland Capital City"},
|
||||
],
|
||||
["Black Lion not found:", None],
|
||||
["Princess Sure-E not found:", None],
|
||||
]
|
||||
@@ -0,0 +1,73 @@
|
||||
from unittest.mock import patch
|
||||
|
||||
from sqlmodel import create_engine
|
||||
|
||||
from ....conftest import get_testing_print_function, needs_py39
|
||||
|
||||
|
||||
@needs_py39
|
||||
def test_tutorial(clear_sqlmodel):
|
||||
from docs_src.tutorial.relationship_attributes.cascade_delete_relationships import (
|
||||
tutorial001_py39 as mod,
|
||||
)
|
||||
|
||||
mod.sqlite_url = "sqlite://"
|
||||
mod.engine = create_engine(mod.sqlite_url)
|
||||
calls = []
|
||||
|
||||
new_print = get_testing_print_function(calls)
|
||||
|
||||
with patch("builtins.print", new=new_print):
|
||||
mod.main()
|
||||
assert calls == [
|
||||
[
|
||||
"Created hero:",
|
||||
{
|
||||
"name": "Deadpond",
|
||||
"secret_name": "Dive Wilson",
|
||||
"team_id": 1,
|
||||
"id": 1,
|
||||
"age": None,
|
||||
},
|
||||
],
|
||||
[
|
||||
"Created hero:",
|
||||
{
|
||||
"name": "Rusty-Man",
|
||||
"secret_name": "Tommy Sharp",
|
||||
"team_id": 2,
|
||||
"id": 2,
|
||||
"age": 48,
|
||||
},
|
||||
],
|
||||
[
|
||||
"Created hero:",
|
||||
{
|
||||
"name": "Spider-Boy",
|
||||
"secret_name": "Pedro Parqueador",
|
||||
"team_id": None,
|
||||
"id": 3,
|
||||
"age": None,
|
||||
},
|
||||
],
|
||||
[
|
||||
"Updated hero:",
|
||||
{
|
||||
"name": "Spider-Boy",
|
||||
"secret_name": "Pedro Parqueador",
|
||||
"team_id": 2,
|
||||
"id": 3,
|
||||
"age": None,
|
||||
},
|
||||
],
|
||||
[
|
||||
"Team Wakaland:",
|
||||
{"name": "Wakaland", "id": 3, "headquarters": "Wakaland Capital City"},
|
||||
],
|
||||
[
|
||||
"Deleted team:",
|
||||
{"name": "Wakaland", "id": 3, "headquarters": "Wakaland Capital City"},
|
||||
],
|
||||
["Black Lion not found:", None],
|
||||
["Princess Sure-E not found:", None],
|
||||
]
|
||||
@@ -0,0 +1,90 @@
|
||||
from unittest.mock import patch
|
||||
|
||||
from sqlmodel import create_engine
|
||||
|
||||
from ....conftest import get_testing_print_function
|
||||
|
||||
|
||||
def test_tutorial(clear_sqlmodel):
|
||||
from docs_src.tutorial.relationship_attributes.cascade_delete_relationships import (
|
||||
tutorial002 as mod,
|
||||
)
|
||||
|
||||
mod.sqlite_url = "sqlite://"
|
||||
mod.engine = create_engine(mod.sqlite_url)
|
||||
calls = []
|
||||
|
||||
new_print = get_testing_print_function(calls)
|
||||
|
||||
with patch("builtins.print", new=new_print):
|
||||
mod.main()
|
||||
assert calls == [
|
||||
[
|
||||
"Created hero:",
|
||||
{
|
||||
"age": None,
|
||||
"id": 1,
|
||||
"name": "Deadpond",
|
||||
"secret_name": "Dive Wilson",
|
||||
"team_id": 1,
|
||||
},
|
||||
],
|
||||
[
|
||||
"Created hero:",
|
||||
{
|
||||
"age": 48,
|
||||
"id": 2,
|
||||
"name": "Rusty-Man",
|
||||
"secret_name": "Tommy Sharp",
|
||||
"team_id": 2,
|
||||
},
|
||||
],
|
||||
[
|
||||
"Created hero:",
|
||||
{
|
||||
"age": None,
|
||||
"id": 3,
|
||||
"name": "Spider-Boy",
|
||||
"secret_name": "Pedro Parqueador",
|
||||
"team_id": None,
|
||||
},
|
||||
],
|
||||
[
|
||||
"Updated hero:",
|
||||
{
|
||||
"age": None,
|
||||
"id": 3,
|
||||
"name": "Spider-Boy",
|
||||
"secret_name": "Pedro Parqueador",
|
||||
"team_id": 2,
|
||||
},
|
||||
],
|
||||
[
|
||||
"Team Wakaland:",
|
||||
{"headquarters": "Wakaland Capital City", "id": 3, "name": "Wakaland"},
|
||||
],
|
||||
[
|
||||
"Deleted team:",
|
||||
{"headquarters": "Wakaland Capital City", "id": 3, "name": "Wakaland"},
|
||||
],
|
||||
[
|
||||
"Black Lion has no team:",
|
||||
{
|
||||
"age": 35,
|
||||
"id": 4,
|
||||
"name": "Black Lion",
|
||||
"secret_name": "Trevor Challa",
|
||||
"team_id": None,
|
||||
},
|
||||
],
|
||||
[
|
||||
"Princess Sure-E has no team:",
|
||||
{
|
||||
"age": None,
|
||||
"id": 5,
|
||||
"name": "Princess Sure-E",
|
||||
"secret_name": "Sure-E",
|
||||
"team_id": None,
|
||||
},
|
||||
],
|
||||
]
|
||||
@@ -0,0 +1,91 @@
|
||||
from unittest.mock import patch
|
||||
|
||||
from sqlmodel import create_engine
|
||||
|
||||
from ....conftest import get_testing_print_function, needs_py310
|
||||
|
||||
|
||||
@needs_py310
|
||||
def test_tutorial(clear_sqlmodel):
|
||||
from docs_src.tutorial.relationship_attributes.cascade_delete_relationships import (
|
||||
tutorial002_py310 as mod,
|
||||
)
|
||||
|
||||
mod.sqlite_url = "sqlite://"
|
||||
mod.engine = create_engine(mod.sqlite_url)
|
||||
calls = []
|
||||
|
||||
new_print = get_testing_print_function(calls)
|
||||
|
||||
with patch("builtins.print", new=new_print):
|
||||
mod.main()
|
||||
assert calls == [
|
||||
[
|
||||
"Created hero:",
|
||||
{
|
||||
"age": None,
|
||||
"id": 1,
|
||||
"name": "Deadpond",
|
||||
"secret_name": "Dive Wilson",
|
||||
"team_id": 1,
|
||||
},
|
||||
],
|
||||
[
|
||||
"Created hero:",
|
||||
{
|
||||
"age": 48,
|
||||
"id": 2,
|
||||
"name": "Rusty-Man",
|
||||
"secret_name": "Tommy Sharp",
|
||||
"team_id": 2,
|
||||
},
|
||||
],
|
||||
[
|
||||
"Created hero:",
|
||||
{
|
||||
"age": None,
|
||||
"id": 3,
|
||||
"name": "Spider-Boy",
|
||||
"secret_name": "Pedro Parqueador",
|
||||
"team_id": None,
|
||||
},
|
||||
],
|
||||
[
|
||||
"Updated hero:",
|
||||
{
|
||||
"age": None,
|
||||
"id": 3,
|
||||
"name": "Spider-Boy",
|
||||
"secret_name": "Pedro Parqueador",
|
||||
"team_id": 2,
|
||||
},
|
||||
],
|
||||
[
|
||||
"Team Wakaland:",
|
||||
{"headquarters": "Wakaland Capital City", "id": 3, "name": "Wakaland"},
|
||||
],
|
||||
[
|
||||
"Deleted team:",
|
||||
{"headquarters": "Wakaland Capital City", "id": 3, "name": "Wakaland"},
|
||||
],
|
||||
[
|
||||
"Black Lion has no team:",
|
||||
{
|
||||
"age": 35,
|
||||
"id": 4,
|
||||
"name": "Black Lion",
|
||||
"secret_name": "Trevor Challa",
|
||||
"team_id": None,
|
||||
},
|
||||
],
|
||||
[
|
||||
"Princess Sure-E has no team:",
|
||||
{
|
||||
"age": None,
|
||||
"id": 5,
|
||||
"name": "Princess Sure-E",
|
||||
"secret_name": "Sure-E",
|
||||
"team_id": None,
|
||||
},
|
||||
],
|
||||
]
|
||||
@@ -0,0 +1,91 @@
|
||||
from unittest.mock import patch
|
||||
|
||||
from sqlmodel import create_engine
|
||||
|
||||
from ....conftest import get_testing_print_function, needs_py39
|
||||
|
||||
|
||||
@needs_py39
|
||||
def test_tutorial(clear_sqlmodel):
|
||||
from docs_src.tutorial.relationship_attributes.cascade_delete_relationships import (
|
||||
tutorial002_py39 as mod,
|
||||
)
|
||||
|
||||
mod.sqlite_url = "sqlite://"
|
||||
mod.engine = create_engine(mod.sqlite_url)
|
||||
calls = []
|
||||
|
||||
new_print = get_testing_print_function(calls)
|
||||
|
||||
with patch("builtins.print", new=new_print):
|
||||
mod.main()
|
||||
assert calls == [
|
||||
[
|
||||
"Created hero:",
|
||||
{
|
||||
"age": None,
|
||||
"id": 1,
|
||||
"name": "Deadpond",
|
||||
"secret_name": "Dive Wilson",
|
||||
"team_id": 1,
|
||||
},
|
||||
],
|
||||
[
|
||||
"Created hero:",
|
||||
{
|
||||
"age": 48,
|
||||
"id": 2,
|
||||
"name": "Rusty-Man",
|
||||
"secret_name": "Tommy Sharp",
|
||||
"team_id": 2,
|
||||
},
|
||||
],
|
||||
[
|
||||
"Created hero:",
|
||||
{
|
||||
"age": None,
|
||||
"id": 3,
|
||||
"name": "Spider-Boy",
|
||||
"secret_name": "Pedro Parqueador",
|
||||
"team_id": None,
|
||||
},
|
||||
],
|
||||
[
|
||||
"Updated hero:",
|
||||
{
|
||||
"age": None,
|
||||
"id": 3,
|
||||
"name": "Spider-Boy",
|
||||
"secret_name": "Pedro Parqueador",
|
||||
"team_id": 2,
|
||||
},
|
||||
],
|
||||
[
|
||||
"Team Wakaland:",
|
||||
{"headquarters": "Wakaland Capital City", "id": 3, "name": "Wakaland"},
|
||||
],
|
||||
[
|
||||
"Deleted team:",
|
||||
{"headquarters": "Wakaland Capital City", "id": 3, "name": "Wakaland"},
|
||||
],
|
||||
[
|
||||
"Black Lion has no team:",
|
||||
{
|
||||
"age": 35,
|
||||
"id": 4,
|
||||
"name": "Black Lion",
|
||||
"secret_name": "Trevor Challa",
|
||||
"team_id": None,
|
||||
},
|
||||
],
|
||||
[
|
||||
"Princess Sure-E has no team:",
|
||||
{
|
||||
"age": None,
|
||||
"id": 5,
|
||||
"name": "Princess Sure-E",
|
||||
"secret_name": "Sure-E",
|
||||
"team_id": None,
|
||||
},
|
||||
],
|
||||
]
|
||||
@@ -0,0 +1,90 @@
|
||||
from unittest.mock import patch
|
||||
|
||||
from sqlmodel import create_engine
|
||||
|
||||
from tests.conftest import get_testing_print_function
|
||||
|
||||
|
||||
def test_tutorial(clear_sqlmodel):
|
||||
from docs_src.tutorial.relationship_attributes.cascade_delete_relationships import (
|
||||
tutorial003 as mod,
|
||||
)
|
||||
|
||||
mod.sqlite_url = "sqlite://"
|
||||
mod.engine = create_engine(mod.sqlite_url)
|
||||
calls = []
|
||||
|
||||
new_print = get_testing_print_function(calls)
|
||||
|
||||
with patch("builtins.print", new=new_print):
|
||||
mod.main()
|
||||
assert calls == [
|
||||
[
|
||||
"Created hero:",
|
||||
{
|
||||
"age": None,
|
||||
"id": 1,
|
||||
"name": "Deadpond",
|
||||
"secret_name": "Dive Wilson",
|
||||
"team_id": 1,
|
||||
},
|
||||
],
|
||||
[
|
||||
"Created hero:",
|
||||
{
|
||||
"age": 48,
|
||||
"id": 2,
|
||||
"name": "Rusty-Man",
|
||||
"secret_name": "Tommy Sharp",
|
||||
"team_id": 2,
|
||||
},
|
||||
],
|
||||
[
|
||||
"Created hero:",
|
||||
{
|
||||
"age": None,
|
||||
"id": 3,
|
||||
"name": "Spider-Boy",
|
||||
"secret_name": "Pedro Parqueador",
|
||||
"team_id": None,
|
||||
},
|
||||
],
|
||||
[
|
||||
"Updated hero:",
|
||||
{
|
||||
"age": None,
|
||||
"id": 3,
|
||||
"name": "Spider-Boy",
|
||||
"secret_name": "Pedro Parqueador",
|
||||
"team_id": 2,
|
||||
},
|
||||
],
|
||||
[
|
||||
"Team Wakaland:",
|
||||
{"id": 3, "headquarters": "Wakaland Capital City", "name": "Wakaland"},
|
||||
],
|
||||
[
|
||||
"Deleted team:",
|
||||
{"id": 3, "headquarters": "Wakaland Capital City", "name": "Wakaland"},
|
||||
],
|
||||
[
|
||||
"Black Lion has no team:",
|
||||
{
|
||||
"age": 35,
|
||||
"id": 4,
|
||||
"name": "Black Lion",
|
||||
"secret_name": "Trevor Challa",
|
||||
"team_id": None,
|
||||
},
|
||||
],
|
||||
[
|
||||
"Princess Sure-E has no team:",
|
||||
{
|
||||
"age": None,
|
||||
"id": 5,
|
||||
"name": "Princess Sure-E",
|
||||
"secret_name": "Sure-E",
|
||||
"team_id": None,
|
||||
},
|
||||
],
|
||||
]
|
||||
@@ -0,0 +1,91 @@
|
||||
from unittest.mock import patch
|
||||
|
||||
from sqlmodel import create_engine
|
||||
|
||||
from ....conftest import get_testing_print_function, needs_py310
|
||||
|
||||
|
||||
@needs_py310
|
||||
def test_tutorial(clear_sqlmodel):
|
||||
from docs_src.tutorial.relationship_attributes.cascade_delete_relationships import (
|
||||
tutorial003_py310 as mod,
|
||||
)
|
||||
|
||||
mod.sqlite_url = "sqlite://"
|
||||
mod.engine = create_engine(mod.sqlite_url)
|
||||
calls = []
|
||||
|
||||
new_print = get_testing_print_function(calls)
|
||||
|
||||
with patch("builtins.print", new=new_print):
|
||||
mod.main()
|
||||
assert calls == [
|
||||
[
|
||||
"Created hero:",
|
||||
{
|
||||
"age": None,
|
||||
"id": 1,
|
||||
"name": "Deadpond",
|
||||
"secret_name": "Dive Wilson",
|
||||
"team_id": 1,
|
||||
},
|
||||
],
|
||||
[
|
||||
"Created hero:",
|
||||
{
|
||||
"age": 48,
|
||||
"id": 2,
|
||||
"name": "Rusty-Man",
|
||||
"secret_name": "Tommy Sharp",
|
||||
"team_id": 2,
|
||||
},
|
||||
],
|
||||
[
|
||||
"Created hero:",
|
||||
{
|
||||
"age": None,
|
||||
"id": 3,
|
||||
"name": "Spider-Boy",
|
||||
"secret_name": "Pedro Parqueador",
|
||||
"team_id": None,
|
||||
},
|
||||
],
|
||||
[
|
||||
"Updated hero:",
|
||||
{
|
||||
"age": None,
|
||||
"id": 3,
|
||||
"name": "Spider-Boy",
|
||||
"secret_name": "Pedro Parqueador",
|
||||
"team_id": 2,
|
||||
},
|
||||
],
|
||||
[
|
||||
"Team Wakaland:",
|
||||
{"id": 3, "headquarters": "Wakaland Capital City", "name": "Wakaland"},
|
||||
],
|
||||
[
|
||||
"Deleted team:",
|
||||
{"id": 3, "headquarters": "Wakaland Capital City", "name": "Wakaland"},
|
||||
],
|
||||
[
|
||||
"Black Lion has no team:",
|
||||
{
|
||||
"age": 35,
|
||||
"id": 4,
|
||||
"name": "Black Lion",
|
||||
"secret_name": "Trevor Challa",
|
||||
"team_id": None,
|
||||
},
|
||||
],
|
||||
[
|
||||
"Princess Sure-E has no team:",
|
||||
{
|
||||
"age": None,
|
||||
"id": 5,
|
||||
"name": "Princess Sure-E",
|
||||
"secret_name": "Sure-E",
|
||||
"team_id": None,
|
||||
},
|
||||
],
|
||||
]
|
||||
@@ -0,0 +1,91 @@
|
||||
from unittest.mock import patch
|
||||
|
||||
from sqlmodel import create_engine
|
||||
|
||||
from ....conftest import get_testing_print_function, needs_py39
|
||||
|
||||
|
||||
@needs_py39
|
||||
def test_tutorial(clear_sqlmodel):
|
||||
from docs_src.tutorial.relationship_attributes.cascade_delete_relationships import (
|
||||
tutorial003_py39 as mod,
|
||||
)
|
||||
|
||||
mod.sqlite_url = "sqlite://"
|
||||
mod.engine = create_engine(mod.sqlite_url)
|
||||
calls = []
|
||||
|
||||
new_print = get_testing_print_function(calls)
|
||||
|
||||
with patch("builtins.print", new=new_print):
|
||||
mod.main()
|
||||
assert calls == [
|
||||
[
|
||||
"Created hero:",
|
||||
{
|
||||
"age": None,
|
||||
"id": 1,
|
||||
"name": "Deadpond",
|
||||
"secret_name": "Dive Wilson",
|
||||
"team_id": 1,
|
||||
},
|
||||
],
|
||||
[
|
||||
"Created hero:",
|
||||
{
|
||||
"age": 48,
|
||||
"id": 2,
|
||||
"name": "Rusty-Man",
|
||||
"secret_name": "Tommy Sharp",
|
||||
"team_id": 2,
|
||||
},
|
||||
],
|
||||
[
|
||||
"Created hero:",
|
||||
{
|
||||
"age": None,
|
||||
"id": 3,
|
||||
"name": "Spider-Boy",
|
||||
"secret_name": "Pedro Parqueador",
|
||||
"team_id": None,
|
||||
},
|
||||
],
|
||||
[
|
||||
"Updated hero:",
|
||||
{
|
||||
"age": None,
|
||||
"id": 3,
|
||||
"name": "Spider-Boy",
|
||||
"secret_name": "Pedro Parqueador",
|
||||
"team_id": 2,
|
||||
},
|
||||
],
|
||||
[
|
||||
"Team Wakaland:",
|
||||
{"id": 3, "headquarters": "Wakaland Capital City", "name": "Wakaland"},
|
||||
],
|
||||
[
|
||||
"Deleted team:",
|
||||
{"id": 3, "headquarters": "Wakaland Capital City", "name": "Wakaland"},
|
||||
],
|
||||
[
|
||||
"Black Lion has no team:",
|
||||
{
|
||||
"age": 35,
|
||||
"id": 4,
|
||||
"name": "Black Lion",
|
||||
"secret_name": "Trevor Challa",
|
||||
"team_id": None,
|
||||
},
|
||||
],
|
||||
[
|
||||
"Princess Sure-E has no team:",
|
||||
{
|
||||
"age": None,
|
||||
"id": 5,
|
||||
"name": "Princess Sure-E",
|
||||
"secret_name": "Sure-E",
|
||||
"team_id": None,
|
||||
},
|
||||
],
|
||||
]
|
||||
@@ -0,0 +1,106 @@
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
from sqlalchemy.exc import IntegrityError
|
||||
from sqlmodel import Session, create_engine, select
|
||||
|
||||
from tests.conftest import get_testing_print_function
|
||||
|
||||
|
||||
def test_tutorial(clear_sqlmodel):
|
||||
from docs_src.tutorial.relationship_attributes.cascade_delete_relationships import (
|
||||
tutorial004 as mod,
|
||||
)
|
||||
|
||||
mod.sqlite_url = "sqlite://"
|
||||
mod.engine = create_engine(mod.sqlite_url)
|
||||
calls = []
|
||||
|
||||
new_print = get_testing_print_function(calls)
|
||||
|
||||
with patch("builtins.print", new=new_print):
|
||||
mod.create_db_and_tables()
|
||||
mod.create_heroes()
|
||||
mod.select_deleted_heroes()
|
||||
with Session(mod.engine) as session:
|
||||
team = session.exec(
|
||||
select(mod.Team).where(mod.Team.name == "Wakaland")
|
||||
).one()
|
||||
team.heroes.clear()
|
||||
session.add(team)
|
||||
session.commit()
|
||||
mod.delete_team()
|
||||
assert calls == [
|
||||
[
|
||||
"Created hero:",
|
||||
{
|
||||
"age": None,
|
||||
"id": 1,
|
||||
"name": "Deadpond",
|
||||
"secret_name": "Dive Wilson",
|
||||
"team_id": 1,
|
||||
},
|
||||
],
|
||||
[
|
||||
"Created hero:",
|
||||
{
|
||||
"age": 48,
|
||||
"id": 2,
|
||||
"name": "Rusty-Man",
|
||||
"secret_name": "Tommy Sharp",
|
||||
"team_id": 2,
|
||||
},
|
||||
],
|
||||
[
|
||||
"Created hero:",
|
||||
{
|
||||
"age": None,
|
||||
"id": 3,
|
||||
"name": "Spider-Boy",
|
||||
"secret_name": "Pedro Parqueador",
|
||||
"team_id": None,
|
||||
},
|
||||
],
|
||||
[
|
||||
"Updated hero:",
|
||||
{
|
||||
"age": None,
|
||||
"id": 3,
|
||||
"name": "Spider-Boy",
|
||||
"secret_name": "Pedro Parqueador",
|
||||
"team_id": 2,
|
||||
},
|
||||
],
|
||||
[
|
||||
"Team Wakaland:",
|
||||
{"headquarters": "Wakaland Capital City", "id": 3, "name": "Wakaland"},
|
||||
],
|
||||
[
|
||||
"Black Lion has no team:",
|
||||
{
|
||||
"age": 35,
|
||||
"id": 4,
|
||||
"name": "Black Lion",
|
||||
"secret_name": "Trevor Challa",
|
||||
"team_id": 3,
|
||||
},
|
||||
],
|
||||
[
|
||||
"Princess Sure-E has no team:",
|
||||
{
|
||||
"age": None,
|
||||
"id": 5,
|
||||
"name": "Princess Sure-E",
|
||||
"secret_name": "Sure-E",
|
||||
"team_id": 3,
|
||||
},
|
||||
],
|
||||
[
|
||||
"Deleted team:",
|
||||
{"headquarters": "Wakaland Capital City", "id": 3, "name": "Wakaland"},
|
||||
],
|
||||
]
|
||||
|
||||
with pytest.raises(IntegrityError) as exc:
|
||||
mod.main()
|
||||
assert "FOREIGN KEY constraint failed" in str(exc.value)
|
||||
@@ -0,0 +1,107 @@
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
from sqlalchemy.exc import IntegrityError
|
||||
from sqlmodel import Session, create_engine, select
|
||||
|
||||
from tests.conftest import get_testing_print_function, needs_py310
|
||||
|
||||
|
||||
@needs_py310
|
||||
def test_tutorial(clear_sqlmodel):
|
||||
from docs_src.tutorial.relationship_attributes.cascade_delete_relationships import (
|
||||
tutorial004_py310 as mod,
|
||||
)
|
||||
|
||||
mod.sqlite_url = "sqlite://"
|
||||
mod.engine = create_engine(mod.sqlite_url)
|
||||
calls = []
|
||||
|
||||
new_print = get_testing_print_function(calls)
|
||||
|
||||
with patch("builtins.print", new=new_print):
|
||||
mod.create_db_and_tables()
|
||||
mod.create_heroes()
|
||||
mod.select_deleted_heroes()
|
||||
with Session(mod.engine) as session:
|
||||
team = session.exec(
|
||||
select(mod.Team).where(mod.Team.name == "Wakaland")
|
||||
).one()
|
||||
team.heroes.clear()
|
||||
session.add(team)
|
||||
session.commit()
|
||||
mod.delete_team()
|
||||
assert calls == [
|
||||
[
|
||||
"Created hero:",
|
||||
{
|
||||
"age": None,
|
||||
"id": 1,
|
||||
"name": "Deadpond",
|
||||
"secret_name": "Dive Wilson",
|
||||
"team_id": 1,
|
||||
},
|
||||
],
|
||||
[
|
||||
"Created hero:",
|
||||
{
|
||||
"age": 48,
|
||||
"id": 2,
|
||||
"name": "Rusty-Man",
|
||||
"secret_name": "Tommy Sharp",
|
||||
"team_id": 2,
|
||||
},
|
||||
],
|
||||
[
|
||||
"Created hero:",
|
||||
{
|
||||
"age": None,
|
||||
"id": 3,
|
||||
"name": "Spider-Boy",
|
||||
"secret_name": "Pedro Parqueador",
|
||||
"team_id": None,
|
||||
},
|
||||
],
|
||||
[
|
||||
"Updated hero:",
|
||||
{
|
||||
"age": None,
|
||||
"id": 3,
|
||||
"name": "Spider-Boy",
|
||||
"secret_name": "Pedro Parqueador",
|
||||
"team_id": 2,
|
||||
},
|
||||
],
|
||||
[
|
||||
"Team Wakaland:",
|
||||
{"headquarters": "Wakaland Capital City", "id": 3, "name": "Wakaland"},
|
||||
],
|
||||
[
|
||||
"Black Lion has no team:",
|
||||
{
|
||||
"age": 35,
|
||||
"id": 4,
|
||||
"name": "Black Lion",
|
||||
"secret_name": "Trevor Challa",
|
||||
"team_id": 3,
|
||||
},
|
||||
],
|
||||
[
|
||||
"Princess Sure-E has no team:",
|
||||
{
|
||||
"age": None,
|
||||
"id": 5,
|
||||
"name": "Princess Sure-E",
|
||||
"secret_name": "Sure-E",
|
||||
"team_id": 3,
|
||||
},
|
||||
],
|
||||
[
|
||||
"Deleted team:",
|
||||
{"headquarters": "Wakaland Capital City", "id": 3, "name": "Wakaland"},
|
||||
],
|
||||
]
|
||||
|
||||
with pytest.raises(IntegrityError) as exc:
|
||||
mod.main()
|
||||
assert "FOREIGN KEY constraint failed" in str(exc.value)
|
||||
@@ -0,0 +1,107 @@
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
from sqlalchemy.exc import IntegrityError
|
||||
from sqlmodel import Session, create_engine, select
|
||||
|
||||
from tests.conftest import get_testing_print_function, needs_py39
|
||||
|
||||
|
||||
@needs_py39
|
||||
def test_tutorial(clear_sqlmodel):
|
||||
from docs_src.tutorial.relationship_attributes.cascade_delete_relationships import (
|
||||
tutorial004_py39 as mod,
|
||||
)
|
||||
|
||||
mod.sqlite_url = "sqlite://"
|
||||
mod.engine = create_engine(mod.sqlite_url)
|
||||
calls = []
|
||||
|
||||
new_print = get_testing_print_function(calls)
|
||||
|
||||
with patch("builtins.print", new=new_print):
|
||||
mod.create_db_and_tables()
|
||||
mod.create_heroes()
|
||||
mod.select_deleted_heroes()
|
||||
with Session(mod.engine) as session:
|
||||
team = session.exec(
|
||||
select(mod.Team).where(mod.Team.name == "Wakaland")
|
||||
).one()
|
||||
team.heroes.clear()
|
||||
session.add(team)
|
||||
session.commit()
|
||||
mod.delete_team()
|
||||
assert calls == [
|
||||
[
|
||||
"Created hero:",
|
||||
{
|
||||
"age": None,
|
||||
"id": 1,
|
||||
"name": "Deadpond",
|
||||
"secret_name": "Dive Wilson",
|
||||
"team_id": 1,
|
||||
},
|
||||
],
|
||||
[
|
||||
"Created hero:",
|
||||
{
|
||||
"age": 48,
|
||||
"id": 2,
|
||||
"name": "Rusty-Man",
|
||||
"secret_name": "Tommy Sharp",
|
||||
"team_id": 2,
|
||||
},
|
||||
],
|
||||
[
|
||||
"Created hero:",
|
||||
{
|
||||
"age": None,
|
||||
"id": 3,
|
||||
"name": "Spider-Boy",
|
||||
"secret_name": "Pedro Parqueador",
|
||||
"team_id": None,
|
||||
},
|
||||
],
|
||||
[
|
||||
"Updated hero:",
|
||||
{
|
||||
"age": None,
|
||||
"id": 3,
|
||||
"name": "Spider-Boy",
|
||||
"secret_name": "Pedro Parqueador",
|
||||
"team_id": 2,
|
||||
},
|
||||
],
|
||||
[
|
||||
"Team Wakaland:",
|
||||
{"headquarters": "Wakaland Capital City", "id": 3, "name": "Wakaland"},
|
||||
],
|
||||
[
|
||||
"Black Lion has no team:",
|
||||
{
|
||||
"age": 35,
|
||||
"id": 4,
|
||||
"name": "Black Lion",
|
||||
"secret_name": "Trevor Challa",
|
||||
"team_id": 3,
|
||||
},
|
||||
],
|
||||
[
|
||||
"Princess Sure-E has no team:",
|
||||
{
|
||||
"age": None,
|
||||
"id": 5,
|
||||
"name": "Princess Sure-E",
|
||||
"secret_name": "Sure-E",
|
||||
"team_id": 3,
|
||||
},
|
||||
],
|
||||
[
|
||||
"Deleted team:",
|
||||
{"headquarters": "Wakaland Capital City", "id": 3, "name": "Wakaland"},
|
||||
],
|
||||
]
|
||||
|
||||
with pytest.raises(IntegrityError) as exc:
|
||||
mod.main()
|
||||
assert "FOREIGN KEY constraint failed" in str(exc.value)
|
||||
@@ -0,0 +1,94 @@
|
||||
from unittest.mock import patch
|
||||
|
||||
from sqlmodel import create_engine
|
||||
|
||||
from tests.conftest import get_testing_print_function
|
||||
|
||||
|
||||
def test_tutorial(clear_sqlmodel):
|
||||
from docs_src.tutorial.relationship_attributes.cascade_delete_relationships import (
|
||||
tutorial005 as mod,
|
||||
)
|
||||
|
||||
mod.sqlite_url = "sqlite://"
|
||||
mod.engine = create_engine(mod.sqlite_url)
|
||||
calls = []
|
||||
|
||||
new_print = get_testing_print_function(calls)
|
||||
|
||||
with patch("builtins.print", new=new_print):
|
||||
mod.main()
|
||||
assert calls == [
|
||||
[
|
||||
"Created hero:",
|
||||
{
|
||||
"name": "Deadpond",
|
||||
"secret_name": "Dive Wilson",
|
||||
"team_id": 1,
|
||||
"id": 1,
|
||||
"age": None,
|
||||
},
|
||||
],
|
||||
[
|
||||
"Created hero:",
|
||||
{
|
||||
"name": "Rusty-Man",
|
||||
"secret_name": "Tommy Sharp",
|
||||
"team_id": 2,
|
||||
"id": 2,
|
||||
"age": 48,
|
||||
},
|
||||
],
|
||||
[
|
||||
"Created hero:",
|
||||
{
|
||||
"name": "Spider-Boy",
|
||||
"secret_name": "Pedro Parqueador",
|
||||
"team_id": None,
|
||||
"id": 3,
|
||||
"age": None,
|
||||
},
|
||||
],
|
||||
[
|
||||
"Updated hero:",
|
||||
{
|
||||
"name": "Spider-Boy",
|
||||
"secret_name": "Pedro Parqueador",
|
||||
"team_id": 2,
|
||||
"id": 3,
|
||||
"age": None,
|
||||
},
|
||||
],
|
||||
[
|
||||
"Team Wakaland:",
|
||||
{"id": 3, "headquarters": "Wakaland Capital City", "name": "Wakaland"},
|
||||
],
|
||||
[
|
||||
"Team with removed heroes:",
|
||||
{"id": 3, "headquarters": "Wakaland Capital City", "name": "Wakaland"},
|
||||
],
|
||||
[
|
||||
"Deleted team:",
|
||||
{"id": 3, "headquarters": "Wakaland Capital City", "name": "Wakaland"},
|
||||
],
|
||||
[
|
||||
"Black Lion has no team:",
|
||||
{
|
||||
"name": "Black Lion",
|
||||
"secret_name": "Trevor Challa",
|
||||
"team_id": None,
|
||||
"id": 4,
|
||||
"age": 35,
|
||||
},
|
||||
],
|
||||
[
|
||||
"Princess Sure-E has no team:",
|
||||
{
|
||||
"name": "Princess Sure-E",
|
||||
"secret_name": "Sure-E",
|
||||
"team_id": None,
|
||||
"id": 5,
|
||||
"age": None,
|
||||
},
|
||||
],
|
||||
]
|
||||
@@ -0,0 +1,95 @@
|
||||
from unittest.mock import patch
|
||||
|
||||
from sqlmodel import create_engine
|
||||
|
||||
from tests.conftest import get_testing_print_function, needs_py310
|
||||
|
||||
|
||||
@needs_py310
|
||||
def test_tutorial(clear_sqlmodel):
|
||||
from docs_src.tutorial.relationship_attributes.cascade_delete_relationships import (
|
||||
tutorial005_py310 as mod,
|
||||
)
|
||||
|
||||
mod.sqlite_url = "sqlite://"
|
||||
mod.engine = create_engine(mod.sqlite_url)
|
||||
calls = []
|
||||
|
||||
new_print = get_testing_print_function(calls)
|
||||
|
||||
with patch("builtins.print", new=new_print):
|
||||
mod.main()
|
||||
assert calls == [
|
||||
[
|
||||
"Created hero:",
|
||||
{
|
||||
"name": "Deadpond",
|
||||
"secret_name": "Dive Wilson",
|
||||
"team_id": 1,
|
||||
"id": 1,
|
||||
"age": None,
|
||||
},
|
||||
],
|
||||
[
|
||||
"Created hero:",
|
||||
{
|
||||
"name": "Rusty-Man",
|
||||
"secret_name": "Tommy Sharp",
|
||||
"team_id": 2,
|
||||
"id": 2,
|
||||
"age": 48,
|
||||
},
|
||||
],
|
||||
[
|
||||
"Created hero:",
|
||||
{
|
||||
"name": "Spider-Boy",
|
||||
"secret_name": "Pedro Parqueador",
|
||||
"team_id": None,
|
||||
"id": 3,
|
||||
"age": None,
|
||||
},
|
||||
],
|
||||
[
|
||||
"Updated hero:",
|
||||
{
|
||||
"name": "Spider-Boy",
|
||||
"secret_name": "Pedro Parqueador",
|
||||
"team_id": 2,
|
||||
"id": 3,
|
||||
"age": None,
|
||||
},
|
||||
],
|
||||
[
|
||||
"Team Wakaland:",
|
||||
{"id": 3, "headquarters": "Wakaland Capital City", "name": "Wakaland"},
|
||||
],
|
||||
[
|
||||
"Team with removed heroes:",
|
||||
{"id": 3, "headquarters": "Wakaland Capital City", "name": "Wakaland"},
|
||||
],
|
||||
[
|
||||
"Deleted team:",
|
||||
{"id": 3, "headquarters": "Wakaland Capital City", "name": "Wakaland"},
|
||||
],
|
||||
[
|
||||
"Black Lion has no team:",
|
||||
{
|
||||
"name": "Black Lion",
|
||||
"secret_name": "Trevor Challa",
|
||||
"team_id": None,
|
||||
"id": 4,
|
||||
"age": 35,
|
||||
},
|
||||
],
|
||||
[
|
||||
"Princess Sure-E has no team:",
|
||||
{
|
||||
"name": "Princess Sure-E",
|
||||
"secret_name": "Sure-E",
|
||||
"team_id": None,
|
||||
"id": 5,
|
||||
"age": None,
|
||||
},
|
||||
],
|
||||
]
|
||||
@@ -0,0 +1,95 @@
|
||||
from unittest.mock import patch
|
||||
|
||||
from sqlmodel import create_engine
|
||||
|
||||
from tests.conftest import get_testing_print_function, needs_py39
|
||||
|
||||
|
||||
@needs_py39
|
||||
def test_tutorial(clear_sqlmodel):
|
||||
from docs_src.tutorial.relationship_attributes.cascade_delete_relationships import (
|
||||
tutorial005_py39 as mod,
|
||||
)
|
||||
|
||||
mod.sqlite_url = "sqlite://"
|
||||
mod.engine = create_engine(mod.sqlite_url)
|
||||
calls = []
|
||||
|
||||
new_print = get_testing_print_function(calls)
|
||||
|
||||
with patch("builtins.print", new=new_print):
|
||||
mod.main()
|
||||
assert calls == [
|
||||
[
|
||||
"Created hero:",
|
||||
{
|
||||
"name": "Deadpond",
|
||||
"secret_name": "Dive Wilson",
|
||||
"team_id": 1,
|
||||
"id": 1,
|
||||
"age": None,
|
||||
},
|
||||
],
|
||||
[
|
||||
"Created hero:",
|
||||
{
|
||||
"name": "Rusty-Man",
|
||||
"secret_name": "Tommy Sharp",
|
||||
"team_id": 2,
|
||||
"id": 2,
|
||||
"age": 48,
|
||||
},
|
||||
],
|
||||
[
|
||||
"Created hero:",
|
||||
{
|
||||
"name": "Spider-Boy",
|
||||
"secret_name": "Pedro Parqueador",
|
||||
"team_id": None,
|
||||
"id": 3,
|
||||
"age": None,
|
||||
},
|
||||
],
|
||||
[
|
||||
"Updated hero:",
|
||||
{
|
||||
"name": "Spider-Boy",
|
||||
"secret_name": "Pedro Parqueador",
|
||||
"team_id": 2,
|
||||
"id": 3,
|
||||
"age": None,
|
||||
},
|
||||
],
|
||||
[
|
||||
"Team Wakaland:",
|
||||
{"id": 3, "headquarters": "Wakaland Capital City", "name": "Wakaland"},
|
||||
],
|
||||
[
|
||||
"Team with removed heroes:",
|
||||
{"id": 3, "headquarters": "Wakaland Capital City", "name": "Wakaland"},
|
||||
],
|
||||
[
|
||||
"Deleted team:",
|
||||
{"id": 3, "headquarters": "Wakaland Capital City", "name": "Wakaland"},
|
||||
],
|
||||
[
|
||||
"Black Lion has no team:",
|
||||
{
|
||||
"name": "Black Lion",
|
||||
"secret_name": "Trevor Challa",
|
||||
"team_id": None,
|
||||
"id": 4,
|
||||
"age": 35,
|
||||
},
|
||||
],
|
||||
[
|
||||
"Princess Sure-E has no team:",
|
||||
{
|
||||
"name": "Princess Sure-E",
|
||||
"secret_name": "Sure-E",
|
||||
"team_id": None,
|
||||
"id": 5,
|
||||
"age": None,
|
||||
},
|
||||
],
|
||||
]
|
||||
Reference in New Issue
Block a user