Skip to content

Commit

Permalink
Fix repr and add tests (pallets-eco#1296)
Browse files Browse the repository at this point in the history
  • Loading branch information
pamelafox authored Jan 15, 2024
1 parent 3ecd038 commit fec440f
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 4 deletions.
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>"

0 comments on commit fec440f

Please sign in to comment.