mirror of
https://github.com/fastapi/sqlmodel.git
synced 2026-02-04 03:33:28 +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,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.one import tutorial006 as mod
|
||||
|
||||
@pytest.fixture(
|
||||
name="mod",
|
||||
params=[
|
||||
"tutorial006",
|
||||
pytest.param("tutorial006_py310", marks=needs_py310),
|
||||
],
|
||||
)
|
||||
def get_module(request: pytest.FixtureRequest) -> ModuleType:
|
||||
mod = importlib.import_module(f"docs_src.tutorial.one.{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 == [
|
||||
[
|
||||
"Hero:",
|
||||
{"name": "Deadpond", "secret_name": "Dive Wilson", "age": None, "id": 1},
|
||||
|
||||
Reference in New Issue
Block a user