Skip to content

Commit a89485b

Browse files
committed
Parameterize more tests for commit
1 parent d8c31ca commit a89485b

File tree

3 files changed

+35
-32
lines changed

3 files changed

+35
-32
lines changed

tests/conftest.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from flask.ctx import AppContext
1212

1313
from flask_sqlalchemy import SQLAlchemy
14+
from flask_sqlalchemy.model import Model
1415

1516

1617
@pytest.fixture
@@ -34,7 +35,7 @@ def app_ctx(app: Flask) -> t.Generator[AppContext, None, None]:
3435
# We defer creation of those classes until the fixture,
3536
# so that each test gets a fresh class with its own metadata.
3637
test_classes = [
37-
None,
38+
Model,
3839
(
3940
"BaseDeclarativeBase",
4041
(sa_orm.DeclarativeBase,),
@@ -63,18 +64,18 @@ def app_ctx(app: Flask) -> t.Generator[AppContext, None, None]:
6364

6465
@pytest.fixture(params=test_classes)
6566
def db(app: Flask, request: pytest.FixtureRequest) -> SQLAlchemy:
66-
if request.param is not None:
67+
if request.param is not Model:
6768
return SQLAlchemy(app, model_class=types.new_class(*request.param))
6869
else:
6970
return SQLAlchemy(app)
7071

7172

7273
@pytest.fixture(params=test_classes)
7374
def model_class(request: pytest.FixtureRequest) -> t.Any:
74-
if request.param is not None:
75+
if request.param is not Model:
7576
return types.new_class(*request.param)
7677
else:
77-
return None
78+
return request.param
7879

7980

8081
@pytest.fixture

tests/test_engine.py

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
import os.path
4+
import typing as t
45
import unittest.mock
56

67
import pytest
@@ -20,34 +21,34 @@ def test_default_engine(app: Flask, db: SQLAlchemy) -> None:
2021

2122

2223
@pytest.mark.usefixtures("app_ctx")
23-
def test_engine_per_bind(app: Flask) -> None:
24+
def test_engine_per_bind(app: Flask, model_class: t.Any) -> None:
2425
app.config["SQLALCHEMY_BINDS"] = {"a": "sqlite://"}
25-
db = SQLAlchemy(app)
26+
db = SQLAlchemy(app, model_class=model_class)
2627
assert db.engines["a"] is not db.engine
2728

2829

2930
@pytest.mark.usefixtures("app_ctx")
30-
def test_config_engine_options(app: Flask) -> None:
31+
def test_config_engine_options(app: Flask, model_class: t.Any) -> None:
3132
app.config["SQLALCHEMY_ENGINE_OPTIONS"] = {"echo": True}
32-
db = SQLAlchemy(app)
33+
db = SQLAlchemy(app, model_class=model_class)
3334
assert db.engine.echo
3435

3536

3637
@pytest.mark.usefixtures("app_ctx")
37-
def test_init_engine_options(app: Flask) -> None:
38+
def test_init_engine_options(app: Flask, model_class: t.Any) -> None:
3839
app.config["SQLALCHEMY_ENGINE_OPTIONS"] = {"echo": False}
3940
app.config["SQLALCHEMY_BINDS"] = {"a": "sqlite://"}
40-
db = SQLAlchemy(app, engine_options={"echo": True})
41+
db = SQLAlchemy(app, engine_options={"echo": True}, model_class=model_class)
4142
# init is default
4243
assert db.engines["a"].echo
4344
# config overrides init
4445
assert not db.engine.echo
4546

4647

4748
@pytest.mark.usefixtures("app_ctx")
48-
def test_config_echo(app: Flask) -> None:
49+
def test_config_echo(app: Flask, model_class: t.Any) -> None:
4950
app.config["SQLALCHEMY_ECHO"] = True
50-
db = SQLAlchemy(app)
51+
db = SQLAlchemy(app, model_class=model_class)
5152
assert db.engine.echo
5253
assert db.engine.pool.echo
5354

@@ -62,35 +63,35 @@ def test_config_echo(app: Flask) -> None:
6263
{"url": sa.engine.URL.create("sqlite")},
6364
],
6465
)
65-
def test_url_type(app: Flask, value: str | sa.engine.URL) -> None:
66+
def test_url_type(app: Flask, model_class: t.Any, value: str | sa.engine.URL) -> None:
6667
app.config["SQLALCHEMY_BINDS"] = {"a": value}
67-
db = SQLAlchemy(app)
68+
db = SQLAlchemy(app, model_class=model_class)
6869
assert str(db.engines["a"].url) == "sqlite://"
6970

7071

71-
def test_no_binds_error(app: Flask) -> None:
72+
def test_no_binds_error(app: Flask, model_class: t.Any) -> None:
7273
del app.config["SQLALCHEMY_DATABASE_URI"]
7374

7475
with pytest.raises(RuntimeError) as info:
75-
SQLAlchemy(app)
76+
SQLAlchemy(app, model_class=model_class)
7677

7778
e = "Either 'SQLALCHEMY_DATABASE_URI' or 'SQLALCHEMY_BINDS' must be set."
7879
assert str(info.value) == e
7980

8081

8182
@pytest.mark.usefixtures("app_ctx")
82-
def test_no_default_url(app: Flask) -> None:
83+
def test_no_default_url(app: Flask, model_class: t.Any) -> None:
8384
del app.config["SQLALCHEMY_DATABASE_URI"]
8485
app.config["SQLALCHEMY_BINDS"] = {"a": "sqlite://"}
85-
db = SQLAlchemy(app, engine_options={"echo": True})
86+
db = SQLAlchemy(app, model_class=model_class, engine_options={"echo": True})
8687
assert None not in db.engines
8788
assert "a" in db.engines
8889

8990

9091
@pytest.mark.usefixtures("app_ctx")
91-
def test_sqlite_relative_path(app: Flask) -> None:
92+
def test_sqlite_relative_path(app: Flask, model_class: t.Any) -> None:
9293
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///test.db"
93-
db = SQLAlchemy(app)
94+
db = SQLAlchemy(app, model_class=model_class)
9495
db.create_all()
9596
assert not isinstance(db.engine.pool, sa.pool.StaticPool)
9697
db_path = db.engine.url.database
@@ -99,9 +100,9 @@ def test_sqlite_relative_path(app: Flask) -> None:
99100

100101

101102
@pytest.mark.usefixtures("app_ctx")
102-
def test_sqlite_driver_level_uri(app: Flask) -> None:
103+
def test_sqlite_driver_level_uri(app: Flask, model_class: t.Any) -> None:
103104
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///file:test.db?uri=true"
104-
db = SQLAlchemy(app)
105+
db = SQLAlchemy(app, model_class=model_class)
105106
db.create_all()
106107
db_path = db.engine.url.database
107108
assert db_path is not None
@@ -110,17 +111,21 @@ def test_sqlite_driver_level_uri(app: Flask) -> None:
110111

111112

112113
@unittest.mock.patch.object(SQLAlchemy, "_make_engine", autospec=True)
113-
def test_sqlite_memory_defaults(make_engine: unittest.mock.Mock, app: Flask) -> None:
114-
SQLAlchemy(app)
114+
def test_sqlite_memory_defaults(
115+
make_engine: unittest.mock.Mock, app: Flask, model_class: t.Any
116+
) -> None:
117+
SQLAlchemy(app, model_class=model_class)
115118
options = make_engine.call_args[0][2]
116119
assert options["poolclass"] is sa.pool.StaticPool
117120
assert options["connect_args"]["check_same_thread"] is False
118121

119122

120123
@unittest.mock.patch.object(SQLAlchemy, "_make_engine", autospec=True)
121-
def test_mysql_defaults(make_engine: unittest.mock.Mock, app: Flask) -> None:
124+
def test_mysql_defaults(
125+
make_engine: unittest.mock.Mock, app: Flask, model_class: t.Any
126+
) -> None:
122127
app.config["SQLALCHEMY_DATABASE_URI"] = "mysql:///test"
123-
SQLAlchemy(app)
128+
SQLAlchemy(app, model_class=model_class)
124129
options = make_engine.call_args[0][2]
125130
assert options["pool_recycle"] == 7200
126131
assert options["url"].query["charset"] == "utf8mb4"

tests/test_metadata.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class Base(sa_orm.DeclarativeBase):
5555

5656

5757
def test_metadata_from_custom_model(model_class: t.Any) -> None:
58-
if model_class is not None:
58+
if model_class is not Model:
5959
# In 2.x, SQLAlchemy creates the metadata attribute
6060
base = model_class
6161
else:
@@ -77,17 +77,14 @@ def test_custom_metadata_overrides_custom_model_legacy() -> None:
7777

7878
def test_metadata_per_bind(app: Flask, model_class: t.Any) -> None:
7979
app.config["SQLALCHEMY_BINDS"] = {"a": "sqlite://"}
80-
if model_class is not None:
81-
db = SQLAlchemy(app, model_class=model_class)
82-
else:
83-
db = SQLAlchemy(app)
80+
db = SQLAlchemy(app, model_class=model_class)
8481
assert db.metadatas["a"] is not db.metadata
8582
assert db.metadatas["a"].info["bind_key"] == "a"
8683

8784

8885
def test_copy_naming_convention(app: Flask, model_class: t.Any) -> None:
8986
app.config["SQLALCHEMY_BINDS"] = {"a": "sqlite://"}
90-
if model_class is not None:
87+
if model_class is not Model:
9188
model_class.metadata = sa.MetaData(
9289
naming_convention={"pk": "spk_%(table_name)s"}
9390
)

0 commit comments

Comments
 (0)