mirror of
https://github.com/fastapi/sqlmodel.git
synced 2026-02-04 11:44:01 +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,57 +0,0 @@
|
||||
from typing import Any, Dict, List, Union
|
||||
from unittest.mock import patch
|
||||
|
||||
from sqlmodel import create_engine
|
||||
|
||||
from ...conftest import get_testing_print_function, needs_py310
|
||||
|
||||
|
||||
def check_calls(calls: List[List[Union[str, Dict[str, Any]]]]):
|
||||
assert calls[0][0] == {
|
||||
"name": "Deadpond",
|
||||
"secret_name": "Dive Wilson",
|
||||
"age": None,
|
||||
"id": 1,
|
||||
}
|
||||
assert calls[1][0] == {
|
||||
"name": "Spider-Boy",
|
||||
"secret_name": "Pedro Parqueador",
|
||||
"age": None,
|
||||
"id": 2,
|
||||
}
|
||||
assert calls[2][0] == {
|
||||
"name": "Rusty-Man",
|
||||
"secret_name": "Tommy Sharp",
|
||||
"age": 48,
|
||||
"id": 3,
|
||||
}
|
||||
|
||||
|
||||
@needs_py310
|
||||
def test_tutorial_001(clear_sqlmodel):
|
||||
from docs_src.tutorial.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()
|
||||
check_calls(calls)
|
||||
|
||||
|
||||
@needs_py310
|
||||
def test_tutorial_002(clear_sqlmodel):
|
||||
from docs_src.tutorial.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()
|
||||
check_calls(calls)
|
||||
@@ -1,9 +1,11 @@
|
||||
import importlib
|
||||
from types import ModuleType
|
||||
from typing import Any, Dict, List, Union
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
from sqlmodel import create_engine
|
||||
|
||||
from ...conftest import get_testing_print_function
|
||||
from ...conftest import PrintMock, needs_py310
|
||||
|
||||
|
||||
def check_calls(calls: List[List[Union[str, Dict[str, Any]]]]):
|
||||
@@ -27,29 +29,35 @@ def check_calls(calls: List[List[Union[str, Dict[str, Any]]]]):
|
||||
}
|
||||
|
||||
|
||||
def test_tutorial_001(clear_sqlmodel):
|
||||
from docs_src.tutorial.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()
|
||||
check_calls(calls)
|
||||
@pytest.fixture(name="module")
|
||||
def get_module(request: pytest.FixtureRequest) -> ModuleType:
|
||||
module = importlib.import_module(f"docs_src.tutorial.select.{request.param}")
|
||||
module.sqlite_url = "sqlite://"
|
||||
module.engine = create_engine(module.sqlite_url)
|
||||
return module
|
||||
|
||||
|
||||
def test_tutorial_002(clear_sqlmodel):
|
||||
from docs_src.tutorial.select import tutorial002 as mod
|
||||
@pytest.mark.parametrize(
|
||||
"module",
|
||||
[
|
||||
"tutorial001",
|
||||
pytest.param("tutorial001_py310", marks=needs_py310),
|
||||
],
|
||||
indirect=True,
|
||||
)
|
||||
def test_tutorial_001(print_mock: PrintMock, module: ModuleType):
|
||||
module.main()
|
||||
check_calls(print_mock.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()
|
||||
check_calls(calls)
|
||||
@pytest.mark.parametrize(
|
||||
"module",
|
||||
[
|
||||
"tutorial002",
|
||||
pytest.param("tutorial002_py310", marks=needs_py310),
|
||||
],
|
||||
indirect=True,
|
||||
)
|
||||
def test_tutorial_002(print_mock: PrintMock, module: ModuleType):
|
||||
module.main()
|
||||
check_calls(print_mock.calls)
|
||||
|
||||
@@ -1,59 +0,0 @@
|
||||
from typing import Any, Dict, List, Union
|
||||
from unittest.mock import patch
|
||||
|
||||
from sqlmodel import create_engine
|
||||
|
||||
from ...conftest import get_testing_print_function, needs_py310
|
||||
|
||||
|
||||
def check_calls(calls: List[List[Union[str, Dict[str, Any]]]]):
|
||||
assert calls[0][0] == [
|
||||
{
|
||||
"name": "Deadpond",
|
||||
"secret_name": "Dive Wilson",
|
||||
"age": None,
|
||||
"id": 1,
|
||||
},
|
||||
{
|
||||
"name": "Spider-Boy",
|
||||
"secret_name": "Pedro Parqueador",
|
||||
"age": None,
|
||||
"id": 2,
|
||||
},
|
||||
{
|
||||
"name": "Rusty-Man",
|
||||
"secret_name": "Tommy Sharp",
|
||||
"age": 48,
|
||||
"id": 3,
|
||||
},
|
||||
]
|
||||
|
||||
|
||||
@needs_py310
|
||||
def test_tutorial_003(clear_sqlmodel):
|
||||
from docs_src.tutorial.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()
|
||||
check_calls(calls)
|
||||
|
||||
|
||||
@needs_py310
|
||||
def test_tutorial_002(clear_sqlmodel):
|
||||
from docs_src.tutorial.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()
|
||||
check_calls(calls)
|
||||
@@ -1,9 +1,11 @@
|
||||
import importlib
|
||||
from types import ModuleType
|
||||
from typing import Any, Dict, List, Union
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
from sqlmodel import create_engine
|
||||
|
||||
from ...conftest import get_testing_print_function
|
||||
from ...conftest import PrintMock, needs_py310
|
||||
|
||||
|
||||
def check_calls(calls: List[List[Union[str, Dict[str, Any]]]]):
|
||||
@@ -29,29 +31,35 @@ def check_calls(calls: List[List[Union[str, Dict[str, Any]]]]):
|
||||
]
|
||||
|
||||
|
||||
def test_tutorial_003(clear_sqlmodel):
|
||||
from docs_src.tutorial.select 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()
|
||||
check_calls(calls)
|
||||
@pytest.fixture(name="module")
|
||||
def get_module(request: pytest.FixtureRequest) -> ModuleType:
|
||||
module = importlib.import_module(f"docs_src.tutorial.select.{request.param}")
|
||||
module.sqlite_url = "sqlite://"
|
||||
module.engine = create_engine(module.sqlite_url)
|
||||
return module
|
||||
|
||||
|
||||
def test_tutorial_002(clear_sqlmodel):
|
||||
from docs_src.tutorial.select import tutorial004 as mod
|
||||
@pytest.mark.parametrize(
|
||||
"module",
|
||||
[
|
||||
"tutorial003",
|
||||
pytest.param("tutorial003_py310", marks=needs_py310),
|
||||
],
|
||||
indirect=True,
|
||||
)
|
||||
def test_tutorial_003(print_mock: PrintMock, module: ModuleType):
|
||||
module.main()
|
||||
check_calls(print_mock.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()
|
||||
check_calls(calls)
|
||||
@pytest.mark.parametrize(
|
||||
"module",
|
||||
[
|
||||
"tutorial004",
|
||||
pytest.param("tutorial004_py310", marks=needs_py310),
|
||||
],
|
||||
indirect=True,
|
||||
)
|
||||
def test_tutorial_004(print_mock: PrintMock, module: ModuleType):
|
||||
module.main()
|
||||
check_calls(print_mock.calls)
|
||||
|
||||
Reference in New Issue
Block a user