Skip to content

Commit

Permalink
skip invalid default bind
Browse files Browse the repository at this point in the history
  • Loading branch information
davidism committed Oct 11, 2022
1 parent 44eaa5e commit 64bdde6
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ Unreleased

- Export typing information instead of using external typeshed definitions.
:issue:`1112`
- If default engine options are set, but ``SQLALCHEMY_DATABASE_URI`` is not set, an
invalid default bind will not be configured. :issue:`1117`


Version 3.0.0
Expand Down
2 changes: 1 addition & 1 deletion src/flask_sqlalchemy/extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ def init_app(self, app: Flask) -> None:
if basic_uri is not None:
basic_engine_options["url"] = basic_uri

if basic_engine_options:
if "url" in basic_engine_options:
engine_options.setdefault(None, {}).update(basic_engine_options)

if not engine_options:
Expand Down
11 changes: 10 additions & 1 deletion tests/test_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def test_url_type(app: Flask, value: str | sa.engine.URL) -> None:
assert str(db.engines["a"].url) == "sqlite://"


def test_no_default_url(app: Flask) -> None:
def test_no_binds_error(app: Flask) -> None:
del app.config["SQLALCHEMY_DATABASE_URI"]

with pytest.raises(RuntimeError) as info:
Expand All @@ -78,6 +78,15 @@ def test_no_default_url(app: Flask) -> None:
assert str(info.value) == e


@pytest.mark.usefixtures("app_ctx")
def test_no_default_url(app: Flask) -> None:
del app.config["SQLALCHEMY_DATABASE_URI"]
app.config["SQLALCHEMY_BINDS"] = {"a": "sqlite://"}
db = SQLAlchemy(app, engine_options={"echo": True})
assert None not in db.engines
assert "a" in db.engines


@pytest.mark.usefixtures("app_ctx")
def test_sqlite_relative_path(app: Flask) -> None:
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///test.db"
Expand Down

0 comments on commit 64bdde6

Please sign in to comment.