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,8 +1,25 @@
|
||||
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
|
||||
|
||||
|
||||
@pytest.fixture(
|
||||
name="mod",
|
||||
params=[
|
||||
"tutorial001",
|
||||
pytest.param("tutorial001_py310", marks=needs_py310),
|
||||
],
|
||||
)
|
||||
def get_module(request: pytest.FixtureRequest) -> ModuleType:
|
||||
mod = importlib.import_module(f"docs_src.tutorial.offset_and_limit.{request.param}")
|
||||
mod.sqlite_url = "sqlite://"
|
||||
mod.engine = create_engine(mod.sqlite_url)
|
||||
return mod
|
||||
|
||||
|
||||
expected_calls = [
|
||||
[
|
||||
@@ -20,15 +37,6 @@ expected_calls = [
|
||||
]
|
||||
|
||||
|
||||
def test_tutorial(clear_sqlmodel):
|
||||
from docs_src.tutorial.offset_and_limit 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
|
||||
def test_tutorial(print_mock: PrintMock, mod: ModuleType):
|
||||
mod.main()
|
||||
assert print_mock.calls == expected_calls
|
||||
|
||||
@@ -1,35 +0,0 @@
|
||||
from unittest.mock import patch
|
||||
|
||||
from sqlmodel import create_engine
|
||||
|
||||
from ...conftest import get_testing_print_function, needs_py310
|
||||
|
||||
expected_calls = [
|
||||
[
|
||||
[
|
||||
{"id": 1, "name": "Deadpond", "secret_name": "Dive Wilson", "age": None},
|
||||
{
|
||||
"id": 2,
|
||||
"name": "Spider-Boy",
|
||||
"secret_name": "Pedro Parqueador",
|
||||
"age": None,
|
||||
},
|
||||
{"id": 3, "name": "Rusty-Man", "secret_name": "Tommy Sharp", "age": 48},
|
||||
]
|
||||
]
|
||||
]
|
||||
|
||||
|
||||
@needs_py310
|
||||
def test_tutorial(clear_sqlmodel):
|
||||
from docs_src.tutorial.offset_and_limit 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
|
||||
@@ -1,8 +1,25 @@
|
||||
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
|
||||
|
||||
|
||||
@pytest.fixture(
|
||||
name="mod",
|
||||
params=[
|
||||
"tutorial002",
|
||||
pytest.param("tutorial002_py310", marks=needs_py310),
|
||||
],
|
||||
)
|
||||
def get_module(request: pytest.FixtureRequest) -> ModuleType:
|
||||
mod = importlib.import_module(f"docs_src.tutorial.offset_and_limit.{request.param}")
|
||||
mod.sqlite_url = "sqlite://"
|
||||
mod.engine = create_engine(mod.sqlite_url)
|
||||
return mod
|
||||
|
||||
|
||||
expected_calls = [
|
||||
[
|
||||
@@ -20,15 +37,6 @@ expected_calls = [
|
||||
]
|
||||
|
||||
|
||||
def test_tutorial(clear_sqlmodel):
|
||||
from docs_src.tutorial.offset_and_limit 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 == expected_calls
|
||||
def test_tutorial(print_mock: PrintMock, mod: ModuleType):
|
||||
mod.main()
|
||||
assert print_mock.calls == expected_calls
|
||||
|
||||
@@ -1,35 +0,0 @@
|
||||
from unittest.mock import patch
|
||||
|
||||
from sqlmodel import create_engine
|
||||
|
||||
from ...conftest import get_testing_print_function, needs_py310
|
||||
|
||||
expected_calls = [
|
||||
[
|
||||
[
|
||||
{
|
||||
"id": 4,
|
||||
"name": "Tarantula",
|
||||
"secret_name": "Natalia Roman-on",
|
||||
"age": 32,
|
||||
},
|
||||
{"id": 5, "name": "Black Lion", "secret_name": "Trevor Challa", "age": 35},
|
||||
{"id": 6, "name": "Dr. Weird", "secret_name": "Steve Weird", "age": 36},
|
||||
]
|
||||
]
|
||||
]
|
||||
|
||||
|
||||
@needs_py310
|
||||
def test_tutorial(clear_sqlmodel):
|
||||
from docs_src.tutorial.offset_and_limit 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,25 @@
|
||||
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
|
||||
|
||||
|
||||
@pytest.fixture(
|
||||
name="mod",
|
||||
params=[
|
||||
"tutorial003",
|
||||
pytest.param("tutorial003_py310", marks=needs_py310),
|
||||
],
|
||||
)
|
||||
def get_module(request: pytest.FixtureRequest) -> ModuleType:
|
||||
mod = importlib.import_module(f"docs_src.tutorial.offset_and_limit.{request.param}")
|
||||
mod.sqlite_url = "sqlite://"
|
||||
mod.engine = create_engine(mod.sqlite_url)
|
||||
return mod
|
||||
|
||||
|
||||
expected_calls = [
|
||||
[
|
||||
@@ -18,15 +35,6 @@ expected_calls = [
|
||||
]
|
||||
|
||||
|
||||
def test_tutorial(clear_sqlmodel):
|
||||
from docs_src.tutorial.offset_and_limit 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 == expected_calls
|
||||
def test_tutorial(print_mock: PrintMock, mod: ModuleType):
|
||||
mod.main()
|
||||
assert print_mock.calls == expected_calls
|
||||
|
||||
@@ -1,33 +0,0 @@
|
||||
from unittest.mock import patch
|
||||
|
||||
from sqlmodel import create_engine
|
||||
|
||||
from ...conftest import get_testing_print_function, needs_py310
|
||||
|
||||
expected_calls = [
|
||||
[
|
||||
[
|
||||
{
|
||||
"id": 7,
|
||||
"name": "Captain North America",
|
||||
"secret_name": "Esteban Rogelios",
|
||||
"age": 93,
|
||||
}
|
||||
]
|
||||
]
|
||||
]
|
||||
|
||||
|
||||
@needs_py310
|
||||
def test_tutorial(clear_sqlmodel):
|
||||
from docs_src.tutorial.offset_and_limit 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,22 +1,29 @@
|
||||
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
|
||||
|
||||
|
||||
def test_tutorial(clear_sqlmodel):
|
||||
from docs_src.tutorial.offset_and_limit import tutorial004 as mod
|
||||
|
||||
@pytest.fixture(
|
||||
name="mod",
|
||||
params=[
|
||||
"tutorial004",
|
||||
pytest.param("tutorial004_py310", marks=needs_py310),
|
||||
],
|
||||
)
|
||||
def get_module(request: pytest.FixtureRequest) -> ModuleType:
|
||||
mod = importlib.import_module(f"docs_src.tutorial.offset_and_limit.{request.param}")
|
||||
mod.sqlite_url = "sqlite://"
|
||||
mod.engine = create_engine(mod.sqlite_url)
|
||||
calls = []
|
||||
return mod
|
||||
|
||||
new_print = get_testing_print_function(calls)
|
||||
|
||||
with patch("builtins.print", new=new_print):
|
||||
mod.main()
|
||||
assert calls == [
|
||||
def test_tutorial(print_mock: PrintMock, mod: ModuleType):
|
||||
mod.main()
|
||||
assert print_mock.calls == [
|
||||
[
|
||||
[
|
||||
{"name": "Dr. Weird", "secret_name": "Steve Weird", "age": 36, "id": 6},
|
||||
|
||||
@@ -1,27 +0,0 @@
|
||||
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.offset_and_limit 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 == [
|
||||
[
|
||||
[
|
||||
{"name": "Dr. Weird", "secret_name": "Steve Weird", "age": 36, "id": 6},
|
||||
{"name": "Rusty-Man", "secret_name": "Tommy Sharp", "age": 48, "id": 3},
|
||||
]
|
||||
]
|
||||
]
|
||||
Reference in New Issue
Block a user