From 6e4a11634b7024bf302901c2444f597d7db7270a Mon Sep 17 00:00:00 2001 From: Russell Ballestrini Date: Mon, 10 Nov 2025 16:39:33 -0500 Subject: [PATCH] Fix SQLAlchemy RuntimeError in integration tests - Remove db.init_app() call causing 'already registered' error - Use db.engine.dispose() to clear existing engine - Use db.session.remove() to clean up sessions - Forces new connection with in-memory database config - Fixes 10 failing tests in test_app_activity_functions.py --- tests/integration/test_app_activity_functions.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/tests/integration/test_app_activity_functions.py b/tests/integration/test_app_activity_functions.py index de080a3..cb4920f 100644 --- a/tests/integration/test_app_activity_functions.py +++ b/tests/integration/test_app_activity_functions.py @@ -35,6 +35,9 @@ class TestFlaskAppActivityFunctions(unittest.TestCase): # Store original database URI self.original_db_uri = app.app.config.get("SQLALCHEMY_DATABASE_URI") + # Store original db engine + self.original_db_engine = db.engine if hasattr(db, 'engine') else None + # Configure test app BEFORE pushing context app.app.config["TESTING"] = True app.app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///:memory:" @@ -47,8 +50,11 @@ class TestFlaskAppActivityFunctions(unittest.TestCase): self.app_context = app.app.app_context() self.app_context.push() - # Reinitialize db with the test app to pick up new config - db.init_app(app.app) + # Force db to use the new in-memory database by clearing the engine + # This allows the in-memory database to be created + if hasattr(db, 'engine'): + db.engine.dispose() + db.session.remove() # Re-initialize db with test config to use in-memory database try: