]> git.ipfire.org Git - thirdparty/fastapi/sqlmodel.git/commitdiff
Add tests
authorYurii Motov <yurii.motov.monte@gmail.com>
Tue, 27 Jan 2026 15:49:52 +0000 (16:49 +0100)
committerYurii Motov <yurii.motov.monte@gmail.com>
Tue, 27 Jan 2026 16:27:52 +0000 (17:27 +0100)
tests/test_tutorial/test_str_fields_and_column_length/__init__.py [new file with mode: 0644]
tests/test_tutorial/test_str_fields_and_column_length/test_tutorial001.py [new file with mode: 0644]
tests/test_tutorial/test_str_fields_and_column_length/test_tutorial002.py [new file with mode: 0644]
tests/test_tutorial/test_str_fields_and_column_length/test_tutorial003.py [new file with mode: 0644]
tests/test_tutorial/test_str_fields_and_column_length/test_tutorial004.py [new file with mode: 0644]

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 (file)
index 0000000..e69de29
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 (file)
index 0000000..755457e
--- /dev/null
@@ -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 (file)
index 0000000..11be8af
--- /dev/null
@@ -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 (file)
index 0000000..6302771
--- /dev/null
@@ -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 (file)
index 0000000..f636749
--- /dev/null
@@ -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__")