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
This commit is contained in:
Russell Ballestrini 2025-11-10 16:39:33 -05:00
parent 352c9879c9
commit 6e4a11634b

View file

@ -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: