|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from flask import Flask |
| 4 | + |
| 5 | +from flask_sqlalchemy import SQLAlchemy |
| 6 | + |
| 7 | + |
| 8 | +def test_repr_no_context() -> None: |
| 9 | + db = SQLAlchemy() |
| 10 | + app = Flask(__name__) |
| 11 | + app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite://" |
| 12 | + |
| 13 | + db.init_app(app) |
| 14 | + assert repr(db) == "<SQLAlchemy>" |
| 15 | + |
| 16 | + |
| 17 | +def test_repr_default() -> None: |
| 18 | + db = SQLAlchemy() |
| 19 | + app = Flask(__name__) |
| 20 | + app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite://" |
| 21 | + |
| 22 | + db.init_app(app) |
| 23 | + with app.app_context(): |
| 24 | + assert repr(db) == "<SQLAlchemy sqlite://>" |
| 25 | + |
| 26 | + |
| 27 | +def test_repr_default_plustwo() -> None: |
| 28 | + db = SQLAlchemy() |
| 29 | + app = Flask(__name__) |
| 30 | + app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite://" |
| 31 | + app.config["SQLALCHEMY_BINDS"] = { |
| 32 | + "a": "sqlite:///:memory:", |
| 33 | + "b": "sqlite:///test.db", |
| 34 | + } |
| 35 | + |
| 36 | + db.init_app(app) |
| 37 | + with app.app_context(): |
| 38 | + assert repr(db) == "<SQLAlchemy sqlite:// +2 engines>" |
| 39 | + |
| 40 | + |
| 41 | +def test_repr_nodefault() -> None: |
| 42 | + db = SQLAlchemy() |
| 43 | + app = Flask(__name__) |
| 44 | + app.config["SQLALCHEMY_BINDS"] = {"x": "sqlite:///:memory:"} |
| 45 | + |
| 46 | + db.init_app(app) |
| 47 | + with app.app_context(): |
| 48 | + assert repr(db) == "<SQLAlchemy (No default engine) +1 engines>" |
| 49 | + |
| 50 | + |
| 51 | +def test_repr_nodefault_plustwo() -> None: |
| 52 | + db = SQLAlchemy() |
| 53 | + app = Flask(__name__) |
| 54 | + app.config["SQLALCHEMY_BINDS"] = { |
| 55 | + "a": "sqlite:///:memory:", |
| 56 | + "b": "sqlite:///test.db", |
| 57 | + } |
| 58 | + |
| 59 | + db.init_app(app) |
| 60 | + with app.app_context(): |
| 61 | + assert repr(db) == "<SQLAlchemy (No default engine) +2 engines>" |
0 commit comments