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:
Motov Yurii
2025-12-24 16:47:05 +01:00
committed by GitHub
parent 4c4d5b44ff
commit 4ea6a32f6f
186 changed files with 1769 additions and 14628 deletions

View File

@@ -1,25 +0,0 @@
import subprocess
from pathlib import Path
from ....conftest import needs_py310
@needs_py310
def test_run_tests(clear_sqlmodel):
from docs_src.tutorial.fastapi.app_testing.tutorial001_py310 import test_main as mod
test_path = Path(mod.__file__).resolve().parent
top_level_path = Path(__file__).resolve().parent.parent.parent.parent.parent
result = subprocess.run(
[
"coverage",
"run",
"--parallel-mode",
"-m",
"pytest",
test_path,
],
cwd=top_level_path,
capture_output=True,
)
assert result.returncode == 0, result.stdout.decode("utf-8")

View File

@@ -1,25 +0,0 @@
import subprocess
from pathlib import Path
from ....conftest import needs_py39
@needs_py39
def test_run_tests(clear_sqlmodel):
from docs_src.tutorial.fastapi.app_testing.tutorial001_py39 import test_main as mod
test_path = Path(mod.__file__).resolve().parent
top_level_path = Path(__file__).resolve().parent.parent.parent.parent.parent
result = subprocess.run(
[
"coverage",
"run",
"--parallel-mode",
"-m",
"pytest",
test_path,
],
cwd=top_level_path,
capture_output=True,
)
assert result.returncode == 0, result.stdout.decode("utf-8")

View File

@@ -1,18 +1,44 @@
import importlib
import sys
from dataclasses import dataclass
from types import ModuleType
import pytest
from docs_src.tutorial.fastapi.app_testing.tutorial001 import main as app_mod
from docs_src.tutorial.fastapi.app_testing.tutorial001 import test_main_001 as test_mod
from tests.conftest import needs_py39, needs_py310
@pytest.fixture(name="prepare", autouse=True)
def prepare_fixture(clear_sqlmodel):
@dataclass
class Modules:
app: ModuleType
test: ModuleType
@pytest.fixture(
name="modules_path",
params=[
"tutorial001",
pytest.param("tutorial001_py39", marks=needs_py39),
pytest.param("tutorial001_py310", marks=needs_py310),
],
)
def get_modules_path(request: pytest.FixtureRequest) -> str:
return f"docs_src.tutorial.fastapi.app_testing.{request.param}"
@pytest.fixture(name="modules")
def load_modules(clear_sqlmodel, modules_path: str) -> Modules:
# Trigger side effects of registering table models in SQLModel
# This has to be called after clear_sqlmodel
importlib.reload(app_mod)
importlib.reload(test_mod)
app_mod_path = f"{modules_path}.main"
if app_mod_path in sys.modules:
app_mod = sys.modules[app_mod_path]
importlib.reload(app_mod)
else:
app_mod = importlib.import_module(app_mod_path)
test_mod = importlib.import_module(f"{modules_path}.test_main_001")
return Modules(app=app_mod, test=test_mod)
def test_tutorial():
test_mod.test_create_hero()
def test_tutorial(modules: Modules):
modules.test.test_create_hero()

View File

@@ -1,18 +1,44 @@
import importlib
import sys
from dataclasses import dataclass
from types import ModuleType
import pytest
from docs_src.tutorial.fastapi.app_testing.tutorial001 import main as app_mod
from docs_src.tutorial.fastapi.app_testing.tutorial001 import test_main_002 as test_mod
from tests.conftest import needs_py39, needs_py310
@pytest.fixture(name="prepare", autouse=True)
def prepare_fixture(clear_sqlmodel):
@dataclass
class Modules:
app: ModuleType
test: ModuleType
@pytest.fixture(
name="modules_path",
params=[
"tutorial001",
pytest.param("tutorial001_py39", marks=needs_py39),
pytest.param("tutorial001_py310", marks=needs_py310),
],
)
def get_modules_path(request: pytest.FixtureRequest) -> str:
return f"docs_src.tutorial.fastapi.app_testing.{request.param}"
@pytest.fixture(name="modules")
def load_modules(clear_sqlmodel, modules_path: str) -> Modules:
# Trigger side effects of registering table models in SQLModel
# This has to be called after clear_sqlmodel
importlib.reload(app_mod)
importlib.reload(test_mod)
app_mod_path = f"{modules_path}.main"
if app_mod_path in sys.modules:
app_mod = sys.modules[app_mod_path]
importlib.reload(app_mod)
else:
app_mod = importlib.import_module(app_mod_path)
test_mod = importlib.import_module(f"{modules_path}.test_main_002")
return Modules(app=app_mod, test=test_mod)
def test_tutorial():
test_mod.test_create_hero()
def test_tutorial(modules: Modules):
modules.test.test_create_hero()

View File

@@ -1,18 +1,44 @@
import importlib
import sys
from dataclasses import dataclass
from types import ModuleType
import pytest
from docs_src.tutorial.fastapi.app_testing.tutorial001 import main as app_mod
from docs_src.tutorial.fastapi.app_testing.tutorial001 import test_main_003 as test_mod
from tests.conftest import needs_py39, needs_py310
@pytest.fixture(name="prepare", autouse=True)
def prepare_fixture(clear_sqlmodel):
@dataclass
class Modules:
app: ModuleType
test: ModuleType
@pytest.fixture(
name="modules_path",
params=[
"tutorial001",
pytest.param("tutorial001_py39", marks=needs_py39),
pytest.param("tutorial001_py310", marks=needs_py310),
],
)
def get_modules_path(request: pytest.FixtureRequest) -> str:
return f"docs_src.tutorial.fastapi.app_testing.{request.param}"
@pytest.fixture(name="modules")
def load_modules(clear_sqlmodel, modules_path: str) -> Modules:
# Trigger side effects of registering table models in SQLModel
# This has to be called after clear_sqlmodel
importlib.reload(app_mod)
importlib.reload(test_mod)
app_mod_path = f"{modules_path}.main"
if app_mod_path in sys.modules:
app_mod = sys.modules[app_mod_path]
importlib.reload(app_mod)
else:
app_mod = importlib.import_module(app_mod_path)
test_mod = importlib.import_module(f"{modules_path}.test_main_003")
return Modules(app=app_mod, test=test_mod)
def test_tutorial():
test_mod.test_create_hero()
def test_tutorial(modules: Modules):
modules.test.test_create_hero()

View File

@@ -1,18 +1,44 @@
import importlib
import sys
from dataclasses import dataclass
from types import ModuleType
import pytest
from docs_src.tutorial.fastapi.app_testing.tutorial001 import main as app_mod
from docs_src.tutorial.fastapi.app_testing.tutorial001 import test_main_004 as test_mod
from tests.conftest import needs_py39, needs_py310
@pytest.fixture(name="prepare", autouse=True)
def prepare_fixture(clear_sqlmodel):
@dataclass
class Modules:
app: ModuleType
test: ModuleType
@pytest.fixture(
name="modules_path",
params=[
"tutorial001",
pytest.param("tutorial001_py39", marks=needs_py39),
pytest.param("tutorial001_py310", marks=needs_py310),
],
)
def get_modules_path(request: pytest.FixtureRequest) -> str:
return f"docs_src.tutorial.fastapi.app_testing.{request.param}"
@pytest.fixture(name="modules")
def load_modules(clear_sqlmodel, modules_path: str) -> Modules:
# Trigger side effects of registering table models in SQLModel
# This has to be called after clear_sqlmodel
importlib.reload(app_mod)
importlib.reload(test_mod)
app_mod_path = f"{modules_path}.main"
if app_mod_path in sys.modules:
app_mod = sys.modules[app_mod_path]
importlib.reload(app_mod)
else:
app_mod = importlib.import_module(app_mod_path)
test_mod = importlib.import_module(f"{modules_path}.test_main_004")
return Modules(app=app_mod, test=test_mod)
def test_tutorial():
test_mod.test_create_hero()
def test_tutorial(modules: Modules):
modules.test.test_create_hero()

View File

@@ -1,11 +1,30 @@
import importlib
import subprocess
from pathlib import Path
from types import ModuleType
import pytest
from ....conftest import needs_py39, needs_py310
def test_run_tests(clear_sqlmodel):
from docs_src.tutorial.fastapi.app_testing.tutorial001 import test_main as mod
@pytest.fixture(
name="module",
params=[
"tutorial001",
pytest.param("tutorial001_py39", marks=needs_py39),
pytest.param("tutorial001_py310", marks=needs_py310),
],
)
def get_module(request: pytest.FixtureRequest) -> ModuleType:
module = importlib.import_module(
f"docs_src.tutorial.fastapi.app_testing.{request.param}.test_main"
)
return module
test_path = Path(mod.__file__).resolve().parent
def test_run_tests(module: ModuleType):
test_path = Path(module.__file__).resolve().parent
top_level_path = Path(__file__).resolve().parent.parent.parent.parent.parent
result = subprocess.run(
[