mirror of
https://github.com/fastapi/sqlmodel.git
synced 2026-03-13 09:29:54 +08:00
✅ Simplify tests for code examples, one test file for multiple variants (#1664)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
@@ -1,92 +0,0 @@
|
||||
from unittest.mock import patch
|
||||
|
||||
from sqlmodel import create_engine
|
||||
|
||||
from ....conftest import get_testing_print_function, needs_py310
|
||||
|
||||
expected_calls = [
|
||||
[
|
||||
"Created hero:",
|
||||
{
|
||||
"age": None,
|
||||
"id": 1,
|
||||
"secret_name": "Dive Wilson",
|
||||
"team_id": 2,
|
||||
"name": "Deadpond",
|
||||
},
|
||||
],
|
||||
[
|
||||
"Created hero:",
|
||||
{
|
||||
"age": 48,
|
||||
"id": 2,
|
||||
"secret_name": "Tommy Sharp",
|
||||
"team_id": 1,
|
||||
"name": "Rusty-Man",
|
||||
},
|
||||
],
|
||||
[
|
||||
"Created hero:",
|
||||
{
|
||||
"age": None,
|
||||
"id": 3,
|
||||
"secret_name": "Pedro Parqueador",
|
||||
"team_id": None,
|
||||
"name": "Spider-Boy",
|
||||
},
|
||||
],
|
||||
[
|
||||
"Hero:",
|
||||
{
|
||||
"age": None,
|
||||
"id": 1,
|
||||
"secret_name": "Dive Wilson",
|
||||
"team_id": 2,
|
||||
"name": "Deadpond",
|
||||
},
|
||||
"Team:",
|
||||
{"id": 2, "name": "Z-Force", "headquarters": "Sister Margaret's Bar"},
|
||||
],
|
||||
[
|
||||
"Hero:",
|
||||
{
|
||||
"age": 48,
|
||||
"id": 2,
|
||||
"secret_name": "Tommy Sharp",
|
||||
"team_id": 1,
|
||||
"name": "Rusty-Man",
|
||||
},
|
||||
"Team:",
|
||||
{"id": 1, "name": "Preventers", "headquarters": "Sharp Tower"},
|
||||
],
|
||||
]
|
||||
|
||||
|
||||
@needs_py310
|
||||
def test_tutorial001():
|
||||
from docs_src.tutorial.connect.select 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 == expected_calls
|
||||
|
||||
|
||||
@needs_py310
|
||||
def test_tutorial002():
|
||||
from docs_src.tutorial.connect.select 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 == expected_calls
|
||||
@@ -1,8 +1,10 @@
|
||||
from unittest.mock import patch
|
||||
import importlib
|
||||
from types import ModuleType
|
||||
|
||||
import pytest
|
||||
from sqlmodel import create_engine
|
||||
|
||||
from ....conftest import get_testing_print_function
|
||||
from ....conftest import PrintMock, needs_py310
|
||||
|
||||
expected_calls = [
|
||||
[
|
||||
@@ -62,29 +64,37 @@ expected_calls = [
|
||||
]
|
||||
|
||||
|
||||
def test_tutorial001():
|
||||
from docs_src.tutorial.connect.select 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 == expected_calls
|
||||
@pytest.fixture(name="module")
|
||||
def get_module(request: pytest.FixtureRequest) -> ModuleType:
|
||||
module = importlib.import_module(
|
||||
f"docs_src.tutorial.connect.select.{request.param}"
|
||||
)
|
||||
module.sqlite_url = "sqlite://"
|
||||
module.engine = create_engine(module.sqlite_url)
|
||||
return module
|
||||
|
||||
|
||||
def test_tutorial002():
|
||||
from docs_src.tutorial.connect.select import tutorial002 as mod
|
||||
@pytest.mark.parametrize(
|
||||
"module",
|
||||
[
|
||||
"tutorial001",
|
||||
pytest.param("tutorial001_py310", marks=needs_py310),
|
||||
],
|
||||
indirect=True,
|
||||
)
|
||||
def test_tutorial001(print_mock: PrintMock, module: ModuleType):
|
||||
module.main()
|
||||
assert print_mock.calls == expected_calls
|
||||
|
||||
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 == expected_calls
|
||||
@pytest.mark.parametrize(
|
||||
"module",
|
||||
[
|
||||
"tutorial002",
|
||||
pytest.param("tutorial002_py310", marks=needs_py310),
|
||||
],
|
||||
indirect=True,
|
||||
)
|
||||
def test_tutorial002(print_mock: PrintMock, module: ModuleType):
|
||||
module.main()
|
||||
assert print_mock.calls == expected_calls
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
from unittest.mock import patch
|
||||
import importlib
|
||||
from types import ModuleType
|
||||
|
||||
import pytest
|
||||
from sqlmodel import create_engine
|
||||
|
||||
from ....conftest import get_testing_print_function
|
||||
from ....conftest import PrintMock, needs_py310
|
||||
|
||||
expected_calls = [
|
||||
[
|
||||
@@ -74,15 +76,22 @@ expected_calls = [
|
||||
]
|
||||
|
||||
|
||||
def test_tutorial():
|
||||
from docs_src.tutorial.connect.select import tutorial003 as mod
|
||||
@pytest.fixture(
|
||||
name="module",
|
||||
params=[
|
||||
"tutorial003",
|
||||
pytest.param("tutorial003_py310", marks=needs_py310),
|
||||
],
|
||||
)
|
||||
def get_module(request: pytest.FixtureRequest) -> ModuleType:
|
||||
module = importlib.import_module(
|
||||
f"docs_src.tutorial.connect.select.{request.param}"
|
||||
)
|
||||
module.sqlite_url = "sqlite://"
|
||||
module.engine = create_engine(module.sqlite_url)
|
||||
return module
|
||||
|
||||
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 == expected_calls
|
||||
def test_tutorial(print_mock: PrintMock, module: ModuleType):
|
||||
module.main()
|
||||
assert print_mock.calls == expected_calls
|
||||
|
||||
@@ -1,89 +0,0 @@
|
||||
from unittest.mock import patch
|
||||
|
||||
from sqlmodel import create_engine
|
||||
|
||||
from ....conftest import get_testing_print_function, needs_py310
|
||||
|
||||
expected_calls = [
|
||||
[
|
||||
"Created hero:",
|
||||
{
|
||||
"age": None,
|
||||
"id": 1,
|
||||
"secret_name": "Dive Wilson",
|
||||
"team_id": 2,
|
||||
"name": "Deadpond",
|
||||
},
|
||||
],
|
||||
[
|
||||
"Created hero:",
|
||||
{
|
||||
"age": 48,
|
||||
"id": 2,
|
||||
"secret_name": "Tommy Sharp",
|
||||
"team_id": 1,
|
||||
"name": "Rusty-Man",
|
||||
},
|
||||
],
|
||||
[
|
||||
"Created hero:",
|
||||
{
|
||||
"age": None,
|
||||
"id": 3,
|
||||
"secret_name": "Pedro Parqueador",
|
||||
"team_id": None,
|
||||
"name": "Spider-Boy",
|
||||
},
|
||||
],
|
||||
[
|
||||
"Hero:",
|
||||
{
|
||||
"age": None,
|
||||
"id": 1,
|
||||
"secret_name": "Dive Wilson",
|
||||
"team_id": 2,
|
||||
"name": "Deadpond",
|
||||
},
|
||||
"Team:",
|
||||
{"id": 2, "name": "Z-Force", "headquarters": "Sister Margaret's Bar"},
|
||||
],
|
||||
[
|
||||
"Hero:",
|
||||
{
|
||||
"age": 48,
|
||||
"id": 2,
|
||||
"secret_name": "Tommy Sharp",
|
||||
"team_id": 1,
|
||||
"name": "Rusty-Man",
|
||||
},
|
||||
"Team:",
|
||||
{"id": 1, "name": "Preventers", "headquarters": "Sharp Tower"},
|
||||
],
|
||||
[
|
||||
"Hero:",
|
||||
{
|
||||
"age": None,
|
||||
"id": 3,
|
||||
"secret_name": "Pedro Parqueador",
|
||||
"team_id": None,
|
||||
"name": "Spider-Boy",
|
||||
},
|
||||
"Team:",
|
||||
None,
|
||||
],
|
||||
]
|
||||
|
||||
|
||||
@needs_py310
|
||||
def test_tutorial():
|
||||
from docs_src.tutorial.connect.select 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 == expected_calls
|
||||
@@ -1,8 +1,10 @@
|
||||
from unittest.mock import patch
|
||||
import importlib
|
||||
from types import ModuleType
|
||||
|
||||
import pytest
|
||||
from sqlmodel import create_engine
|
||||
|
||||
from ....conftest import get_testing_print_function
|
||||
from ....conftest import PrintMock, needs_py310
|
||||
|
||||
expected_calls = [
|
||||
[
|
||||
@@ -48,15 +50,22 @@ expected_calls = [
|
||||
]
|
||||
|
||||
|
||||
def test_tutorial():
|
||||
from docs_src.tutorial.connect.select import tutorial004 as mod
|
||||
@pytest.fixture(
|
||||
name="module",
|
||||
params=[
|
||||
"tutorial004",
|
||||
pytest.param("tutorial004_py310", marks=needs_py310),
|
||||
],
|
||||
)
|
||||
def get_module(request: pytest.FixtureRequest) -> ModuleType:
|
||||
module = importlib.import_module(
|
||||
f"docs_src.tutorial.connect.select.{request.param}"
|
||||
)
|
||||
module.sqlite_url = "sqlite://"
|
||||
module.engine = create_engine(module.sqlite_url)
|
||||
return module
|
||||
|
||||
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 == expected_calls
|
||||
def test_tutorial(print_mock: PrintMock, module: ModuleType):
|
||||
module.main()
|
||||
assert print_mock.calls == expected_calls
|
||||
|
||||
@@ -1,63 +0,0 @@
|
||||
from unittest.mock import patch
|
||||
|
||||
from sqlmodel import create_engine
|
||||
|
||||
from ....conftest import get_testing_print_function, needs_py310
|
||||
|
||||
expected_calls = [
|
||||
[
|
||||
"Created hero:",
|
||||
{
|
||||
"age": None,
|
||||
"id": 1,
|
||||
"secret_name": "Dive Wilson",
|
||||
"team_id": 2,
|
||||
"name": "Deadpond",
|
||||
},
|
||||
],
|
||||
[
|
||||
"Created hero:",
|
||||
{
|
||||
"age": 48,
|
||||
"id": 2,
|
||||
"secret_name": "Tommy Sharp",
|
||||
"team_id": 1,
|
||||
"name": "Rusty-Man",
|
||||
},
|
||||
],
|
||||
[
|
||||
"Created hero:",
|
||||
{
|
||||
"age": None,
|
||||
"id": 3,
|
||||
"secret_name": "Pedro Parqueador",
|
||||
"team_id": None,
|
||||
"name": "Spider-Boy",
|
||||
},
|
||||
],
|
||||
[
|
||||
"Preventer Hero:",
|
||||
{
|
||||
"age": 48,
|
||||
"id": 2,
|
||||
"secret_name": "Tommy Sharp",
|
||||
"team_id": 1,
|
||||
"name": "Rusty-Man",
|
||||
},
|
||||
],
|
||||
]
|
||||
|
||||
|
||||
@needs_py310
|
||||
def test_tutorial():
|
||||
from docs_src.tutorial.connect.select 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.main()
|
||||
assert calls == expected_calls
|
||||
@@ -1,8 +1,10 @@
|
||||
from unittest.mock import patch
|
||||
import importlib
|
||||
from types import ModuleType
|
||||
|
||||
import pytest
|
||||
from sqlmodel import create_engine
|
||||
|
||||
from ....conftest import get_testing_print_function
|
||||
from ....conftest import PrintMock, needs_py310
|
||||
|
||||
expected_calls = [
|
||||
[
|
||||
@@ -50,15 +52,22 @@ expected_calls = [
|
||||
]
|
||||
|
||||
|
||||
def test_tutorial():
|
||||
from docs_src.tutorial.connect.select import tutorial005 as mod
|
||||
@pytest.fixture(
|
||||
name="module",
|
||||
params=[
|
||||
"tutorial005",
|
||||
pytest.param("tutorial005_py310", marks=needs_py310),
|
||||
],
|
||||
)
|
||||
def get_module(request: pytest.FixtureRequest) -> ModuleType:
|
||||
module = importlib.import_module(
|
||||
f"docs_src.tutorial.connect.select.{request.param}"
|
||||
)
|
||||
module.sqlite_url = "sqlite://"
|
||||
module.engine = create_engine(module.sqlite_url)
|
||||
return module
|
||||
|
||||
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 == expected_calls
|
||||
def test_tutorial(print_mock: PrintMock, module: ModuleType):
|
||||
module.main()
|
||||
assert print_mock.calls == expected_calls
|
||||
|
||||
@@ -1,65 +0,0 @@
|
||||
from unittest.mock import patch
|
||||
|
||||
from sqlmodel import create_engine
|
||||
|
||||
from ....conftest import get_testing_print_function, needs_py310
|
||||
|
||||
expected_calls = [
|
||||
[
|
||||
"Created hero:",
|
||||
{
|
||||
"age": None,
|
||||
"id": 1,
|
||||
"secret_name": "Dive Wilson",
|
||||
"team_id": 2,
|
||||
"name": "Deadpond",
|
||||
},
|
||||
],
|
||||
[
|
||||
"Created hero:",
|
||||
{
|
||||
"age": 48,
|
||||
"id": 2,
|
||||
"secret_name": "Tommy Sharp",
|
||||
"team_id": 1,
|
||||
"name": "Rusty-Man",
|
||||
},
|
||||
],
|
||||
[
|
||||
"Created hero:",
|
||||
{
|
||||
"age": None,
|
||||
"id": 3,
|
||||
"secret_name": "Pedro Parqueador",
|
||||
"team_id": None,
|
||||
"name": "Spider-Boy",
|
||||
},
|
||||
],
|
||||
[
|
||||
"Preventer Hero:",
|
||||
{
|
||||
"age": 48,
|
||||
"id": 2,
|
||||
"secret_name": "Tommy Sharp",
|
||||
"team_id": 1,
|
||||
"name": "Rusty-Man",
|
||||
},
|
||||
"Team:",
|
||||
{"id": 1, "name": "Preventers", "headquarters": "Sharp Tower"},
|
||||
],
|
||||
]
|
||||
|
||||
|
||||
@needs_py310
|
||||
def test_tutorial():
|
||||
from docs_src.tutorial.connect.select 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 == expected_calls
|
||||
Reference in New Issue
Block a user