From 7249a7d3dde08fbbfed1a44d1cd806736c3aa92f Mon Sep 17 00:00:00 2001 From: krzysdz Date: Mon, 9 Mar 2026 19:11:56 +0100 Subject: [PATCH] tests: dispose engines in app fixture cleanup test_metadata.py::test_reflect requires manual engine disposal, because it overwrites `app.extensions["sqlalchemy"]` --- tests/conftest.py | 14 ++++++++++++-- tests/test_metadata.py | 4 ++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index d4ab92f4..705c6368 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -15,11 +15,21 @@ @pytest.fixture -def app(request: pytest.FixtureRequest, tmp_path: Path) -> Flask: +def app(request: pytest.FixtureRequest, tmp_path: Path) -> t.Generator[Flask]: app = Flask(request.module.__name__, instance_path=str(tmp_path / "instance")) app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite://" app.config["SQLALCHEMY_RECORD_QUERIES"] = False - return app + yield app + + if app.extensions: + db = app.extensions["sqlalchemy"] + # Duplicate SQLAlchemy.engines logic to avoid errors when the app + # is not initialized properly. + if app in db._app_engines: + engines = db._app_engines[app] + if engines: + for engine in engines.values(): + engine.dispose() @pytest.fixture diff --git a/tests/test_metadata.py b/tests/test_metadata.py index 8b54e5bc..523e59af 100644 --- a/tests/test_metadata.py +++ b/tests/test_metadata.py @@ -156,6 +156,10 @@ def test_reflect(app: Flask) -> None: db.Table("post", sa.Column("id", sa.Integer, primary_key=True), bind_key="post") db.create_all() + # Dispose engines to close connections and avoid warning + for engine in db.engines.values(): + engine.dispose() + del app.extensions["sqlalchemy"] db = SQLAlchemy(app) assert not db.metadata.tables