Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix repr and add tests #1296

Merged
merged 4 commits into from
Jan 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
Version 3.1.2
-------------

- Fix issue with calling ``repr()`` on ``SQLAlchemy`` instance with no default engine. :issue:`1295`


Version 3.1.1
-------------

Expand Down
10 changes: 6 additions & 4 deletions src/flask_sqlalchemy/extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,12 +281,14 @@ def __repr__(self) -> str:
if not has_app_context():
return f"<{type(self).__name__}>"

message = f"{type(self).__name__} {self.engine.url}"
num_default_engines = 1 if self.engines.get(None) else 0
engine_str = self.engine.url if num_default_engines else "(No default engine)"

if len(self.engines) > 1:
message = f"{message} +{len(self.engines) - 1}"
num_other_engines = len(self.engines) - num_default_engines
if num_other_engines >= 1:
engine_str = f"{engine_str} +{num_other_engines} engines"

return f"<{message}>"
return f"<{type(self).__name__} {engine_str}>"

def init_app(self, app: Flask) -> None:
"""Initialize a Flask application for use with this extension instance. This
Expand Down
61 changes: 61 additions & 0 deletions tests/test_extension_repr.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
from __future__ import annotations

from flask import Flask

from flask_sqlalchemy import SQLAlchemy


def test_repr_no_context() -> None:
db = SQLAlchemy()
app = Flask(__name__)
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite://"

db.init_app(app)
assert repr(db) == "<SQLAlchemy>"


def test_repr_default() -> None:
db = SQLAlchemy()
app = Flask(__name__)
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite://"

db.init_app(app)
with app.app_context():
assert repr(db) == "<SQLAlchemy sqlite://>"


def test_repr_default_plustwo() -> None:
db = SQLAlchemy()
app = Flask(__name__)
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite://"
app.config["SQLALCHEMY_BINDS"] = {
"a": "sqlite:///:memory:",
"b": "sqlite:///test.db",
}

db.init_app(app)
with app.app_context():
assert repr(db) == "<SQLAlchemy sqlite:// +2 engines>"


def test_repr_nodefault() -> None:
db = SQLAlchemy()
app = Flask(__name__)
app.config["SQLALCHEMY_BINDS"] = {"x": "sqlite:///:memory:"}

db.init_app(app)
with app.app_context():
assert repr(db) == "<SQLAlchemy (No default engine) +1 engines>"


def test_repr_nodefault_plustwo() -> None:
db = SQLAlchemy()
app = Flask(__name__)
app.config["SQLALCHEMY_BINDS"] = {
"a": "sqlite:///:memory:",
"b": "sqlite:///test.db",
}

db.init_app(app)
with app.app_context():
assert repr(db) == "<SQLAlchemy (No default engine) +2 engines>"