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,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 = [
|
||||
[
|
||||
@@ -38,15 +40,22 @@ expected_calls = [
|
||||
]
|
||||
|
||||
|
||||
def test_tutorial001():
|
||||
from docs_src.tutorial.connect.insert import tutorial001 as mod
|
||||
@pytest.fixture(
|
||||
name="module",
|
||||
params=[
|
||||
"tutorial001",
|
||||
pytest.param("tutorial001_py310", marks=needs_py310),
|
||||
],
|
||||
)
|
||||
def get_module(request: pytest.FixtureRequest) -> ModuleType:
|
||||
module = importlib.import_module(
|
||||
f"docs_src.tutorial.connect.insert.{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_tutorial001(print_mock: PrintMock, module: ModuleType):
|
||||
module.main()
|
||||
assert print_mock.calls == expected_calls
|
||||
|
||||
@@ -1,53 +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",
|
||||
},
|
||||
],
|
||||
]
|
||||
|
||||
|
||||
@needs_py310
|
||||
def test_tutorial001():
|
||||
from docs_src.tutorial.connect.insert 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
|
||||
Reference in New Issue
Block a user