Skip to content

Commit 9c5c51b

Browse files
committed
Add tests
1 parent 9ba902b commit 9c5c51b

File tree

5 files changed

+253
-0
lines changed

5 files changed

+253
-0
lines changed

tests/test_tutorial/test_str_fields_and_column_length/__init__.py

Whitespace-only changes.
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import importlib
2+
import runpy
3+
import sys
4+
from collections.abc import Generator
5+
from types import ModuleType
6+
from unittest.mock import patch
7+
8+
import pytest
9+
from sqlalchemy import create_mock_engine
10+
from sqlalchemy.sql.type_api import TypeEngine
11+
from sqlmodel import create_engine
12+
13+
from ...conftest import needs_py310
14+
15+
16+
def mysql_dump(sql: TypeEngine, *args, **kwargs):
17+
dialect = sql.compile(dialect=mysql_engine.dialect)
18+
sql_str = str(dialect).rstrip()
19+
if sql_str:
20+
print(sql_str + ";")
21+
22+
23+
mysql_engine = create_mock_engine("mysql://", mysql_dump)
24+
25+
26+
@pytest.fixture(
27+
name="mod",
28+
params=[
29+
pytest.param("tutorial001_py39"),
30+
pytest.param("tutorial001_py310", marks=needs_py310),
31+
],
32+
)
33+
def get_module(request: pytest.FixtureRequest) -> Generator[ModuleType, None, None]:
34+
with patch("sqlmodel.create_engine"): # To avoid "No module named 'MySQLdb'" error
35+
mod = importlib.import_module(
36+
f"docs_src.tutorial.str_fields_and_column_length.{request.param}"
37+
)
38+
yield mod
39+
40+
41+
def test_sqlite_ddl_sql(mod: ModuleType, caplog: pytest.LogCaptureFixture):
42+
mod.sqlite_url = "sqlite://"
43+
mod.engine = create_engine(mod.sqlite_url, echo=True)
44+
mod.create_db_and_tables()
45+
assert "CREATE TABLE hero (" in caplog.text
46+
assert "name VARCHAR NOT NULL" in caplog.text
47+
48+
49+
def test_mysql_ddl_sql(mod: ModuleType, capsys: pytest.CaptureFixture[str]):
50+
importlib.reload(mod)
51+
52+
mod.SQLModel.metadata.create_all(bind=mysql_engine, checkfirst=False)
53+
captured = capsys.readouterr()
54+
assert "CREATE TABLE hero (" in captured.out
55+
assert "name VARCHAR(255) NOT NULL" in captured.out
56+
57+
58+
# For coverage
59+
def test_run_main(mod: ModuleType):
60+
# Remove module to avoid double-import warning
61+
sys.modules.pop(mod.__name__, None)
62+
63+
runpy.run_module(mod.__name__, run_name="__main__")
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import importlib
2+
import runpy
3+
import sys
4+
from collections.abc import Generator
5+
from types import ModuleType
6+
from unittest.mock import patch
7+
8+
import pytest
9+
from sqlalchemy import create_mock_engine
10+
from sqlalchemy.sql.type_api import TypeEngine
11+
from sqlmodel import create_engine
12+
13+
from ...conftest import needs_py310
14+
15+
16+
def mysql_dump(sql: TypeEngine, *args, **kwargs):
17+
dialect = sql.compile(dialect=mysql_engine.dialect)
18+
sql_str = str(dialect).rstrip()
19+
if sql_str:
20+
print(sql_str + ";")
21+
22+
23+
mysql_engine = create_mock_engine("mysql://", mysql_dump)
24+
25+
26+
@pytest.fixture(
27+
name="mod",
28+
params=[
29+
pytest.param("tutorial002_py39"),
30+
pytest.param("tutorial002_py310", marks=needs_py310),
31+
],
32+
)
33+
def get_module(request: pytest.FixtureRequest) -> Generator[ModuleType, None, None]:
34+
with patch("sqlmodel.create_engine"): # To avoid "No module named 'MySQLdb'" error
35+
mod = importlib.import_module(
36+
f"docs_src.tutorial.str_fields_and_column_length.{request.param}"
37+
)
38+
yield mod
39+
40+
41+
def test_sqlite_ddl_sql(mod: ModuleType, caplog: pytest.LogCaptureFixture):
42+
mod.sqlite_url = "sqlite://"
43+
mod.engine = create_engine(mod.sqlite_url, echo=True)
44+
mod.create_db_and_tables()
45+
assert "CREATE TABLE hero (" in caplog.text
46+
assert "name VARCHAR(100) NOT NULL" in caplog.text
47+
48+
49+
def test_mysql_ddl_sql(mod: ModuleType, capsys: pytest.CaptureFixture[str]):
50+
importlib.reload(mod)
51+
52+
mod.SQLModel.metadata.create_all(bind=mysql_engine, checkfirst=False)
53+
captured = capsys.readouterr()
54+
assert "CREATE TABLE hero (" in captured.out
55+
assert "name VARCHAR(100) NOT NULL" in captured.out
56+
57+
58+
# For coverage
59+
def test_run_main(mod: ModuleType):
60+
# Remove module to avoid double-import warning
61+
sys.modules.pop(mod.__name__, None)
62+
63+
runpy.run_module(mod.__name__, run_name="__main__")
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import importlib
2+
import runpy
3+
import sys
4+
from collections.abc import Generator
5+
from types import ModuleType
6+
from unittest.mock import patch
7+
8+
import pytest
9+
from sqlalchemy import create_mock_engine
10+
from sqlalchemy.exc import CompileError
11+
from sqlalchemy.sql.type_api import TypeEngine
12+
from sqlmodel import create_engine
13+
14+
from ...conftest import needs_py310
15+
16+
17+
def mysql_dump(sql: TypeEngine, *args, **kwargs):
18+
dialect = sql.compile(dialect=mysql_engine.dialect)
19+
sql_str = str(dialect).rstrip()
20+
if sql_str:
21+
print(sql_str + ";")
22+
23+
24+
mysql_engine = create_mock_engine("mysql://", mysql_dump)
25+
26+
27+
@pytest.fixture(
28+
name="mod",
29+
params=[
30+
pytest.param("tutorial003_py39"),
31+
pytest.param("tutorial003_py310", marks=needs_py310),
32+
],
33+
)
34+
def get_module(request: pytest.FixtureRequest) -> Generator[ModuleType, None, None]:
35+
with patch("sqlmodel.create_engine"): # To avoid "No module named 'MySQLdb'" error
36+
mod = importlib.import_module(
37+
f"docs_src.tutorial.str_fields_and_column_length.{request.param}"
38+
)
39+
yield mod
40+
41+
42+
def test_sqlite_ddl_sql(mod: ModuleType, caplog: pytest.LogCaptureFixture):
43+
mod.sqlite_url = "sqlite://"
44+
mod.engine = create_engine(mod.sqlite_url, echo=True)
45+
mod.create_db_and_tables()
46+
assert "CREATE TABLE hero (" in caplog.text
47+
assert "name VARCHAR NOT NULL" in caplog.text
48+
49+
50+
def test_mysql_ddl_sql(mod: ModuleType):
51+
importlib.reload(mod)
52+
53+
with pytest.raises(CompileError) as exc_info:
54+
mod.SQLModel.metadata.create_all(bind=mysql_engine, checkfirst=False)
55+
56+
assert "VARCHAR requires a length on dialect mysql" in str(exc_info.value)
57+
58+
59+
# For coverage
60+
def test_run_main(mod: ModuleType):
61+
# Remove module to avoid double-import warning
62+
sys.modules.pop(mod.__name__, None)
63+
64+
runpy.run_module(mod.__name__, run_name="__main__")
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import importlib
2+
import runpy
3+
import sys
4+
from collections.abc import Generator
5+
from types import ModuleType
6+
from unittest.mock import patch
7+
8+
import pytest
9+
from sqlalchemy import create_mock_engine
10+
from sqlalchemy.sql.type_api import TypeEngine
11+
from sqlmodel import create_engine
12+
13+
from ...conftest import needs_py310
14+
15+
16+
def mysql_dump(sql: TypeEngine, *args, **kwargs):
17+
dialect = sql.compile(dialect=mysql_engine.dialect)
18+
sql_str = str(dialect).rstrip()
19+
if sql_str:
20+
print(sql_str + ";")
21+
22+
23+
mysql_engine = create_mock_engine("mysql://", mysql_dump)
24+
25+
26+
@pytest.fixture(
27+
name="mod",
28+
params=[
29+
pytest.param("tutorial004_py39"),
30+
pytest.param("tutorial004_py310", marks=needs_py310),
31+
],
32+
)
33+
def get_module(request: pytest.FixtureRequest) -> Generator[ModuleType, None, None]:
34+
with patch("sqlmodel.create_engine"): # To avoid "No module named 'MySQLdb'" error
35+
mod = importlib.import_module(
36+
f"docs_src.tutorial.str_fields_and_column_length.{request.param}"
37+
)
38+
yield mod
39+
40+
41+
def test_sqlite_ddl_sql(mod: ModuleType, caplog: pytest.LogCaptureFixture):
42+
mod.sqlite_url = "sqlite://"
43+
mod.engine = create_engine(mod.sqlite_url, echo=True)
44+
mod.create_db_and_tables()
45+
assert "CREATE TABLE hero (" in caplog.text
46+
assert "name VARCHAR(255) NOT NULL" in caplog.text
47+
48+
49+
def test_mysql_ddl_sql(mod: ModuleType, capsys: pytest.CaptureFixture[str]):
50+
importlib.reload(mod)
51+
52+
mod.SQLModel.metadata.create_all(bind=mysql_engine, checkfirst=False)
53+
captured = capsys.readouterr()
54+
assert "CREATE TABLE hero (" in captured.out
55+
assert "name VARCHAR(255) NOT NULL" in captured.out
56+
57+
58+
# For coverage
59+
def test_run_main(mod: ModuleType):
60+
# Remove module to avoid double-import warning
61+
sys.modules.pop(mod.__name__, None)
62+
63+
runpy.run_module(mod.__name__, run_name="__main__")

0 commit comments

Comments
 (0)