URL: http://github.com/fastapi/sqlmodel/pull/1723.diff
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 0000000000..23404f9d7c --- /dev/null +++ b/tests/test_tutorial/test_str_fields_and_column_length/test_tutorial003.py @@ -0,0 +1,61 @@ +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 + + +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=[ + "tutorial003_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 0000000000..ec024dfa66 --- /dev/null +++ b/tests/test_tutorial/test_str_fields_and_column_length/test_tutorial004.py @@ -0,0 +1,60 @@ +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 + + +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=[ + "tutorial004_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__")Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: