From: Yurii Motov Date: Tue, 27 Jan 2026 15:49:52 +0000 (+0100) Subject: Add tests X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=9c5c51b230acde7cee73cf0635bf190ab57a6ee3;p=thirdparty%2Ffastapi%2Fsqlmodel.git Add tests --- diff --git a/tests/test_tutorial/test_str_fields_and_column_length/__init__.py b/tests/test_tutorial/test_str_fields_and_column_length/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/tests/test_tutorial/test_str_fields_and_column_length/test_tutorial001.py b/tests/test_tutorial/test_str_fields_and_column_length/test_tutorial001.py new file mode 100644 index 00000000..755457eb --- /dev/null +++ b/tests/test_tutorial/test_str_fields_and_column_length/test_tutorial001.py @@ -0,0 +1,63 @@ +import importlib +import runpy +import sys +from collections.abc import Generator +from types import ModuleType +from unittest.mock import patch + +import pytest +from sqlalchemy import create_mock_engine +from sqlalchemy.sql.type_api import TypeEngine +from sqlmodel import create_engine + +from ...conftest import needs_py310 + + +def mysql_dump(sql: TypeEngine, *args, **kwargs): + dialect = sql.compile(dialect=mysql_engine.dialect) + sql_str = str(dialect).rstrip() + if sql_str: + print(sql_str + ";") + + +mysql_engine = create_mock_engine("mysql://", mysql_dump) + + +@pytest.fixture( + name="mod", + params=[ + pytest.param("tutorial001_py39"), + pytest.param("tutorial001_py310", marks=needs_py310), + ], +) +def get_module(request: pytest.FixtureRequest) -> Generator[ModuleType, None, None]: + with patch("sqlmodel.create_engine"): # To avoid "No module named 'MySQLdb'" error + mod = importlib.import_module( + f"docs_src.tutorial.str_fields_and_column_length.{request.param}" + ) + yield mod + + +def test_sqlite_ddl_sql(mod: ModuleType, caplog: pytest.LogCaptureFixture): + mod.sqlite_url = "sqlite://" + mod.engine = create_engine(mod.sqlite_url, echo=True) + mod.create_db_and_tables() + assert "CREATE TABLE hero (" in caplog.text + assert "name VARCHAR NOT NULL" in caplog.text + + +def test_mysql_ddl_sql(mod: ModuleType, capsys: pytest.CaptureFixture[str]): + importlib.reload(mod) + + mod.SQLModel.metadata.create_all(bind=mysql_engine, checkfirst=False) + captured = capsys.readouterr() + assert "CREATE TABLE hero (" in captured.out + assert "name VARCHAR(255) NOT NULL" in captured.out + + +# For coverage +def test_run_main(mod: ModuleType): + # Remove module to avoid double-import warning + sys.modules.pop(mod.__name__, None) + + runpy.run_module(mod.__name__, run_name="__main__") diff --git a/tests/test_tutorial/test_str_fields_and_column_length/test_tutorial002.py b/tests/test_tutorial/test_str_fields_and_column_length/test_tutorial002.py new file mode 100644 index 00000000..11be8afb --- /dev/null +++ b/tests/test_tutorial/test_str_fields_and_column_length/test_tutorial002.py @@ -0,0 +1,63 @@ +import importlib +import runpy +import sys +from collections.abc import Generator +from types import ModuleType +from unittest.mock import patch + +import pytest +from sqlalchemy import create_mock_engine +from sqlalchemy.sql.type_api import TypeEngine +from sqlmodel import create_engine + +from ...conftest import needs_py310 + + +def mysql_dump(sql: TypeEngine, *args, **kwargs): + dialect = sql.compile(dialect=mysql_engine.dialect) + sql_str = str(dialect).rstrip() + if sql_str: + print(sql_str + ";") + + +mysql_engine = create_mock_engine("mysql://", mysql_dump) + + +@pytest.fixture( + name="mod", + params=[ + pytest.param("tutorial002_py39"), + pytest.param("tutorial002_py310", marks=needs_py310), + ], +) +def get_module(request: pytest.FixtureRequest) -> Generator[ModuleType, None, None]: + with patch("sqlmodel.create_engine"): # To avoid "No module named 'MySQLdb'" error + mod = importlib.import_module( + f"docs_src.tutorial.str_fields_and_column_length.{request.param}" + ) + yield mod + + +def test_sqlite_ddl_sql(mod: ModuleType, caplog: pytest.LogCaptureFixture): + mod.sqlite_url = "sqlite://" + mod.engine = create_engine(mod.sqlite_url, echo=True) + mod.create_db_and_tables() + assert "CREATE TABLE hero (" in caplog.text + assert "name VARCHAR(100) NOT NULL" in caplog.text + + +def test_mysql_ddl_sql(mod: ModuleType, capsys: pytest.CaptureFixture[str]): + importlib.reload(mod) + + mod.SQLModel.metadata.create_all(bind=mysql_engine, checkfirst=False) + captured = capsys.readouterr() + assert "CREATE TABLE hero (" in captured.out + assert "name VARCHAR(100) NOT NULL" in captured.out + + +# For coverage +def test_run_main(mod: ModuleType): + # Remove module to avoid double-import warning + sys.modules.pop(mod.__name__, None) + + runpy.run_module(mod.__name__, run_name="__main__") diff --git a/tests/test_tutorial/test_str_fields_and_column_length/test_tutorial003.py b/tests/test_tutorial/test_str_fields_and_column_length/test_tutorial003.py new file mode 100644 index 00000000..63027719 --- /dev/null +++ b/tests/test_tutorial/test_str_fields_and_column_length/test_tutorial003.py @@ -0,0 +1,64 @@ +import importlib +import runpy +import sys +from collections.abc import Generator +from types import ModuleType +from unittest.mock import patch + +import pytest +from sqlalchemy import create_mock_engine +from sqlalchemy.exc import CompileError +from sqlalchemy.sql.type_api import TypeEngine +from sqlmodel import create_engine + +from ...conftest import needs_py310 + + +def mysql_dump(sql: TypeEngine, *args, **kwargs): + dialect = sql.compile(dialect=mysql_engine.dialect) + sql_str = str(dialect).rstrip() + if sql_str: + print(sql_str + ";") + + +mysql_engine = create_mock_engine("mysql://", mysql_dump) + + +@pytest.fixture( + name="mod", + params=[ + pytest.param("tutorial003_py39"), + pytest.param("tutorial003_py310", marks=needs_py310), + ], +) +def get_module(request: pytest.FixtureRequest) -> Generator[ModuleType, None, None]: + with patch("sqlmodel.create_engine"): # To avoid "No module named 'MySQLdb'" error + mod = importlib.import_module( + f"docs_src.tutorial.str_fields_and_column_length.{request.param}" + ) + yield mod + + +def test_sqlite_ddl_sql(mod: ModuleType, caplog: pytest.LogCaptureFixture): + mod.sqlite_url = "sqlite://" + mod.engine = create_engine(mod.sqlite_url, echo=True) + mod.create_db_and_tables() + assert "CREATE TABLE hero (" in caplog.text + assert "name VARCHAR NOT NULL" in caplog.text + + +def test_mysql_ddl_sql(mod: ModuleType): + importlib.reload(mod) + + with pytest.raises(CompileError) as exc_info: + mod.SQLModel.metadata.create_all(bind=mysql_engine, checkfirst=False) + + assert "VARCHAR requires a length on dialect mysql" in str(exc_info.value) + + +# For coverage +def test_run_main(mod: ModuleType): + # Remove module to avoid double-import warning + sys.modules.pop(mod.__name__, None) + + runpy.run_module(mod.__name__, run_name="__main__") diff --git a/tests/test_tutorial/test_str_fields_and_column_length/test_tutorial004.py b/tests/test_tutorial/test_str_fields_and_column_length/test_tutorial004.py new file mode 100644 index 00000000..f6367499 --- /dev/null +++ b/tests/test_tutorial/test_str_fields_and_column_length/test_tutorial004.py @@ -0,0 +1,63 @@ +import importlib +import runpy +import sys +from collections.abc import Generator +from types import ModuleType +from unittest.mock import patch + +import pytest +from sqlalchemy import create_mock_engine +from sqlalchemy.sql.type_api import TypeEngine +from sqlmodel import create_engine + +from ...conftest import needs_py310 + + +def mysql_dump(sql: TypeEngine, *args, **kwargs): + dialect = sql.compile(dialect=mysql_engine.dialect) + sql_str = str(dialect).rstrip() + if sql_str: + print(sql_str + ";") + + +mysql_engine = create_mock_engine("mysql://", mysql_dump) + + +@pytest.fixture( + name="mod", + params=[ + pytest.param("tutorial004_py39"), + pytest.param("tutorial004_py310", marks=needs_py310), + ], +) +def get_module(request: pytest.FixtureRequest) -> Generator[ModuleType, None, None]: + with patch("sqlmodel.create_engine"): # To avoid "No module named 'MySQLdb'" error + mod = importlib.import_module( + f"docs_src.tutorial.str_fields_and_column_length.{request.param}" + ) + yield mod + + +def test_sqlite_ddl_sql(mod: ModuleType, caplog: pytest.LogCaptureFixture): + mod.sqlite_url = "sqlite://" + mod.engine = create_engine(mod.sqlite_url, echo=True) + mod.create_db_and_tables() + assert "CREATE TABLE hero (" in caplog.text + assert "name VARCHAR(255) NOT NULL" in caplog.text + + +def test_mysql_ddl_sql(mod: ModuleType, capsys: pytest.CaptureFixture[str]): + importlib.reload(mod) + + mod.SQLModel.metadata.create_all(bind=mysql_engine, checkfirst=False) + captured = capsys.readouterr() + assert "CREATE TABLE hero (" in captured.out + assert "name VARCHAR(255) NOT NULL" in captured.out + + +# For coverage +def test_run_main(mod: ModuleType): + # Remove module to avoid double-import warning + sys.modules.pop(mod.__name__, None) + + runpy.run_module(mod.__name__, run_name="__main__")