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,10 +1,19 @@
|
||||
from pathlib import Path
|
||||
|
||||
from ...conftest import coverage_run
|
||||
import pytest
|
||||
|
||||
from ...conftest import coverage_run, needs_py310
|
||||
|
||||
|
||||
def test_create_db_and_table(cov_tmp_path: Path):
|
||||
module = "docs_src.tutorial.create_db_and_table.tutorial001"
|
||||
@pytest.mark.parametrize(
|
||||
"module_name",
|
||||
[
|
||||
"tutorial001",
|
||||
pytest.param("tutorial001_py310", marks=needs_py310),
|
||||
],
|
||||
)
|
||||
def test_create_db_and_table(cov_tmp_path: Path, module_name: str):
|
||||
module = f"docs_src.tutorial.create_db_and_table.{module_name}"
|
||||
result = coverage_run(module=module, cwd=cov_tmp_path)
|
||||
assert "BEGIN" in result.stdout
|
||||
assert 'PRAGMA main.table_info("hero")' in result.stdout
|
||||
|
||||
@@ -1,19 +0,0 @@
|
||||
from pathlib import Path
|
||||
|
||||
from ...conftest import coverage_run, needs_py310
|
||||
|
||||
|
||||
@needs_py310
|
||||
def test_create_db_and_table(cov_tmp_path: Path):
|
||||
module = "docs_src.tutorial.create_db_and_table.tutorial001_py310"
|
||||
result = coverage_run(module=module, cwd=cov_tmp_path)
|
||||
assert "BEGIN" in result.stdout
|
||||
assert 'PRAGMA main.table_info("hero")' in result.stdout
|
||||
assert "CREATE TABLE hero (" in result.stdout
|
||||
assert "id INTEGER NOT NULL," in result.stdout
|
||||
assert "name VARCHAR NOT NULL," in result.stdout
|
||||
assert "secret_name VARCHAR NOT NULL," in result.stdout
|
||||
assert "age INTEGER," in result.stdout
|
||||
assert "PRIMARY KEY (id)" in result.stdout
|
||||
assert ")" in result.stdout
|
||||
assert "COMMIT" in result.stdout
|
||||
@@ -1,13 +1,31 @@
|
||||
import importlib
|
||||
from types import ModuleType
|
||||
|
||||
import pytest
|
||||
from sqlalchemy import inspect
|
||||
from sqlalchemy.engine.reflection import Inspector
|
||||
from sqlmodel import create_engine
|
||||
|
||||
from ...conftest import needs_py310
|
||||
|
||||
def test_create_db_and_table(clear_sqlmodel):
|
||||
from docs_src.tutorial.create_db_and_table import tutorial002 as mod
|
||||
|
||||
mod.sqlite_url = "sqlite://"
|
||||
mod.engine = create_engine(mod.sqlite_url)
|
||||
mod.create_db_and_tables()
|
||||
insp: Inspector = inspect(mod.engine)
|
||||
assert insp.has_table(str(mod.Hero.__tablename__))
|
||||
@pytest.fixture(
|
||||
name="module",
|
||||
params=[
|
||||
"tutorial002",
|
||||
pytest.param("tutorial002_py310", marks=needs_py310),
|
||||
],
|
||||
)
|
||||
def get_module(request: pytest.FixtureRequest) -> ModuleType:
|
||||
module = importlib.import_module(
|
||||
f"docs_src.tutorial.create_db_and_table.{request.param}"
|
||||
)
|
||||
module.sqlite_url = "sqlite://"
|
||||
module.engine = create_engine(module.sqlite_url)
|
||||
return module
|
||||
|
||||
|
||||
def test_create_db_and_table(module: ModuleType):
|
||||
module.create_db_and_tables()
|
||||
insp: Inspector = inspect(module.engine)
|
||||
assert insp.has_table(str(module.Hero.__tablename__))
|
||||
|
||||
@@ -1,16 +0,0 @@
|
||||
from sqlalchemy import inspect
|
||||
from sqlalchemy.engine.reflection import Inspector
|
||||
from sqlmodel import create_engine
|
||||
|
||||
from ...conftest import needs_py310
|
||||
|
||||
|
||||
@needs_py310
|
||||
def test_create_db_and_table(clear_sqlmodel):
|
||||
from docs_src.tutorial.create_db_and_table import tutorial002_py310 as mod
|
||||
|
||||
mod.sqlite_url = "sqlite://"
|
||||
mod.engine = create_engine(mod.sqlite_url)
|
||||
mod.create_db_and_tables()
|
||||
insp: Inspector = inspect(mod.engine)
|
||||
assert insp.has_table(str(mod.Hero.__tablename__))
|
||||
@@ -1,13 +1,31 @@
|
||||
import importlib
|
||||
from types import ModuleType
|
||||
|
||||
import pytest
|
||||
from sqlalchemy import inspect
|
||||
from sqlalchemy.engine.reflection import Inspector
|
||||
from sqlmodel import create_engine
|
||||
|
||||
from ...conftest import needs_py310
|
||||
|
||||
def test_create_db_and_table(clear_sqlmodel):
|
||||
from docs_src.tutorial.create_db_and_table import tutorial003 as mod
|
||||
|
||||
mod.sqlite_url = "sqlite://"
|
||||
mod.engine = create_engine(mod.sqlite_url)
|
||||
mod.create_db_and_tables()
|
||||
insp: Inspector = inspect(mod.engine)
|
||||
assert insp.has_table(str(mod.Hero.__tablename__))
|
||||
@pytest.fixture(
|
||||
name="module",
|
||||
params=[
|
||||
"tutorial003",
|
||||
pytest.param("tutorial003_py310", marks=needs_py310),
|
||||
],
|
||||
)
|
||||
def get_module(request: pytest.FixtureRequest) -> ModuleType:
|
||||
module = importlib.import_module(
|
||||
f"docs_src.tutorial.create_db_and_table.{request.param}"
|
||||
)
|
||||
module.sqlite_url = "sqlite://"
|
||||
module.engine = create_engine(module.sqlite_url)
|
||||
return module
|
||||
|
||||
|
||||
def test_create_db_and_table(module: ModuleType):
|
||||
module.create_db_and_tables()
|
||||
insp: Inspector = inspect(module.engine)
|
||||
assert insp.has_table(str(module.Hero.__tablename__))
|
||||
|
||||
@@ -1,16 +0,0 @@
|
||||
from sqlalchemy import inspect
|
||||
from sqlalchemy.engine.reflection import Inspector
|
||||
from sqlmodel import create_engine
|
||||
|
||||
from ...conftest import needs_py310
|
||||
|
||||
|
||||
@needs_py310
|
||||
def test_create_db_and_table(clear_sqlmodel):
|
||||
from docs_src.tutorial.create_db_and_table import tutorial003_py310 as mod
|
||||
|
||||
mod.sqlite_url = "sqlite://"
|
||||
mod.engine = create_engine(mod.sqlite_url)
|
||||
mod.create_db_and_tables()
|
||||
insp: Inspector = inspect(mod.engine)
|
||||
assert insp.has_table(str(mod.Hero.__tablename__))
|
||||
Reference in New Issue
Block a user