Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

fix(tests): dispose SQLAlchemy engines in miscellaneous test files (#3213)

LearningCircuit48941aaf4bf9
diff --git a/tests/database/test_initialize_functions.py b/tests/database/test_initialize_functions.py
index fc729570e1..7f328edecb 100644
--- a/tests/database/test_initialize_functions.py
+++ b/tests/database/test_initialize_functions.py
@@ -22,14 +22,16 @@ class TestCheckDatabaseSchema:
with tempfile.TemporaryDirectory() as temp_dir:
db_path = Path(temp_dir) / "test.db"
engine = create_engine(f"sqlite:///{db_path}")
+ try:
+ # Create tables
+ Base.metadata.create_all(engine)
- # Create tables
- Base.metadata.create_all(engine)
+ result = check_database_schema(engine)
- result = check_database_schema(engine)
-
- assert isinstance(result, dict)
- assert "tables" in result
+ assert isinstance(result, dict)
+ assert "tables" in result
+ finally:
+ engine.dispose()
def test_lists_existing_tables(self):
"""check_database_schema lists existing tables."""
@@ -40,14 +42,16 @@ class TestCheckDatabaseSchema:
with tempfile.TemporaryDirectory() as temp_dir:
db_path = Path(temp_dir) / "test.db"
engine = create_engine(f"sqlite:///{db_path}")
+ try:
+ # Create tables
+ Base.metadata.create_all(engine)
- # Create tables
- Base.metadata.create_all(engine)
-
- result = check_database_schema(engine)
+ result = check_database_schema(engine)
- # Should have tables dict
- assert isinstance(result["tables"], dict)
+ # Should have tables dict
+ assert isinstance(result["tables"], dict)
+ finally:
+ engine.dispose()
def test_lists_missing_tables(self):
"""check_database_schema identifies missing tables."""
@@ -58,12 +62,14 @@ class TestCheckDatabaseSchema:
with tempfile.TemporaryDirectory() as temp_dir:
db_path = Path(temp_dir) / "test.db"
engine = create_engine(f"sqlite:///{db_path}")
+ try:
+ # Don't create any tables
+ result = check_database_schema(engine)
- # Don't create any tables
- result = check_database_schema(engine)
-
- assert "missing_tables" in result
- assert isinstance(result["missing_tables"], list)
+ assert "missing_tables" in result
+ assert isinstance(result["missing_tables"], list)
+ finally:
+ engine.dispose()
def test_detects_news_tables(self):
"""check_database_schema detects news tables presence."""
@@ -74,14 +80,16 @@ class TestCheckDatabaseSchema:
with tempfile.TemporaryDirectory() as temp_dir:
db_path = Path(temp_dir) / "test.db"
engine = create_engine(f"sqlite:///{db_path}")
+ try:
+ # Create tables
+ Base.metadata.create_all(engine)
- # Create tables
- Base.metadata.create_all(engine)
-
- result = check_database_schema(engine)
+ result = check_database_schema(engine)
- assert "has_news_tables" in result
- assert isinstance(result["has_news_tables"], bool)
+ assert "has_news_tables" in result
+ assert isinstance(result["has_news_tables"], bool)
+ finally:
+ engine.dispose()
def test_returns_columns_for_each_table(self):
"""check_database_schema returns column names for existing tables."""
@@ -92,15 +100,17 @@ class TestCheckDatabaseSchema:
with tempfile.TemporaryDirectory() as temp_dir:
db_path = Path(temp_dir) / "test.db"
engine = create_engine(f"sqlite:///{db_path}")
+ try:
+ # Create tables
+ Base.metadata.create_all(engine)
- # Create tables
- Base.metadata.create_all(engine)
+ result = check_database_schema(engine)
- result = check_database_schema(engine)
-
- # Each table in tables dict should have a list of columns
- for table_name, columns in result["tables"].items():
- assert isinstance(columns, list)
+ # Each table in tables dict should have a list of columns
+ for table_name, columns in result["tables"].items():
+ assert isinstance(columns, list)
+ finally:
+ engine.dispose()
class TestInitializeDefaultSettings:
@@ -175,17 +185,19 @@ class TestInitializeDatabase:
with tempfile.TemporaryDirectory() as temp_dir:
db_path = Path(temp_dir) / "test.db"
engine = create_engine(f"sqlite:///{db_path}")
+ try:
+ initialize_database(engine)
- initialize_database(engine)
-
- # Verify tables were created
- from sqlalchemy import inspect
+ # Verify tables were created
+ from sqlalchemy import inspect
- inspector = inspect(engine)
- tables = inspector.get_table_names()
+ inspector = inspect(engine)
+ tables = inspector.get_table_names()
- # Should have at least some tables
- assert len(tables) > 0
+ # Should have at least some tables
+ assert len(tables) > 0
+ finally:
+ engine.dispose()
def test_calls_run_migrations(self):
"""initialize_database calls run_migrations."""
@@ -194,13 +206,15 @@ class TestInitializeDatabase:
with tempfile.TemporaryDirectory() as temp_dir:
db_path = Path(temp_dir) / "test.db"
engine = create_engine(f"sqlite:///{db_path}")
+ try:
+ with patch(
+ "local_deep_research.database.initialize.run_migrations"
+ ) as mock_migrations:
+ initialize_database(engine)
- with patch(
- "local_deep_research.database.initialize.run_migrations"
- ) as mock_migrations:
- initialize_database(engine)
-
- mock_migrations.assert_called_once_with(engine)
+ mock_migrations.assert_called_once_with(engine)
+ finally:
+ engine.dispose()
def test_initializes_settings_when_session_provided(self):
"""initialize_database initializes settings when session provided."""
@@ -209,14 +223,17 @@ class TestInitializeDatabase:
with tempfile.TemporaryDirectory() as temp_dir:
db_path = Path(temp_dir) / "test.db"
engine = create_engine(f"sqlite:///{db_path}")
- mock_session = Mock(spec=Session)
+ try:
+ mock_session = Mock(spec=Session)
- with patch(
- "local_deep_research.database.initialize._initialize_default_settings"
- ) as mock_init_settings:
- initialize_database(engine, db_session=mock_session)
+ with patch(
+ "local_deep_research.database.initialize._initialize_default_settings"
+ ) as mock_init_settings:
+ initialize_database(engine, db_session=mock_session)
- mock_init_settings.assert_called_once_with(mock_session)
+ mock_init_settings.assert_called_once_with(mock_session)
+ finally:
+ engine.dispose()
def test_skips_settings_when_no_session(self):
"""initialize_database skips settings init when no session provided."""
@@ -225,13 +242,15 @@ class TestInitializeDatabase:
with tempfile.TemporaryDirectory() as temp_dir:
db_path = Path(temp_dir) / "test.db"
engine = create_engine(f"sqlite:///{db_path}")
+ try:
+ with patch(
+ "local_deep_research.database.initialize._initialize_default_settings"
+ ) as mock_init_settings:
+ initialize_database(engine)
- with patch(
- "local_deep_research.database.initialize._initialize_default_settings"
- ) as mock_init_settings:
- initialize_database(engine)
-
- mock_init_settings.assert_not_called()
+ mock_init_settings.assert_not_called()
+ finally:
+ engine.dispose()
def test_handles_checkfirst_for_existing_tables(self):
"""initialize_database uses checkfirst=True for existing tables."""
@@ -240,16 +259,18 @@ class TestInitializeDatabase:
with tempfile.TemporaryDirectory() as temp_dir:
db_path = Path(temp_dir) / "test.db"
engine = create_engine(f"sqlite:///{db_path}")
+ try:
+ # Create tables first
+ Base.metadata.create_all(engine)
- # Create tables first
- Base.metadata.create_all(engine)
-
- # Run initialize again - should not fail
- initialize_database(engine)
+ # Run initialize again - should not fail
+ initialize_database(engine)
- # Verify tables still exist
- from sqlalchemy import inspect
+ # Verify tables still exist
+ from sqlalchemy import inspect
- inspector = inspect(engine)
- tables = inspector.get_table_names()
- assert len(tables) > 0
+ inspector = inspect(engine)
+ tables = inspector.get_table_names()
+ assert len(tables) > 0
+ finally:
+ engine.dispose()
diff --git a/tests/database/test_orm_conversions.py b/tests/database/test_orm_conversions.py
index b198661b93..5591345f57 100644
--- a/tests/database/test_orm_conversions.py
+++ b/tests/database/test_orm_conversions.py
@@ -31,6 +31,7 @@ def test_db():
session = Session()
yield session
session.close()
+ engine.dispose()
def test_research_history_orm_queries(test_db):
@@ -276,3 +277,4 @@ if __name__ == "__main__":
print("\n✅ All ORM conversion tests passed!")
finally:
session.close()
+ engine.dispose()
diff --git a/tests/security/test_sql_injection.py b/tests/security/test_sql_injection.py
index ed915767f1..f0673ccedc 100644
--- a/tests/security/test_sql_injection.py
+++ b/tests/security/test_sql_injection.py
@@ -32,6 +32,7 @@ class TestSQLInjectionPrevention:
session = Session()
yield session
session.close()
+ engine.dispose()
def test_research_query_with_malicious_id(self, test_db_session):
"""Test that SQLAlchemy ORM prevents SQL injection via filter_by."""
@@ -214,6 +215,7 @@ class TestDatabaseStorageSecurityIntegration:
session = Session()
yield session
session.close()
+ engine.dispose()
def test_end_to_end_sql_injection_prevention(self, test_db_session):
"""
diff --git a/tests/test_context_overflow_detection.py b/tests/test_context_overflow_detection.py
index c476d59534..bc8d493918 100644
--- a/tests/test_context_overflow_detection.py
+++ b/tests/test_context_overflow_detection.py
@@ -24,6 +24,7 @@ class TestContextOverflowDetection:
session = Session()
yield session
session.close()
+ engine.dispose()
@pytest.fixture
def token_callback(self):
diff --git a/tests/web/database/test_benchmark_schema_high_value.py b/tests/web/database/test_benchmark_schema_high_value.py
index b92d22dde4..26ee99ddf2 100644
--- a/tests/web/database/test_benchmark_schema_high_value.py
+++ b/tests/web/database/test_benchmark_schema_high_value.py
@@ -141,7 +141,8 @@ class TestCreateBenchmarkTablesSimple:
"""Create an in-memory SQLite engine shared across tests in this class."""
engine = create_engine("sqlite:///:memory:")
create_benchmark_tables_simple(engine)
- return engine
+ yield engine
+ engine.dispose()
def test_creates_all_four_tables(self, db_engine):
inspector = inspect(db_engine)