✨ 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:
@@ -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