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 test_database_init.py (#3242)

LearningCircuit0e5b26cdb74f
diff --git a/tests/database/test_database_init.py b/tests/database/test_database_init.py
index 32a6143b6c..cb624d2c4b 100644
--- a/tests/database/test_database_init.py
+++ b/tests/database/test_database_init.py
@@ -28,12 +28,27 @@ class TestDatabaseInitialization:
yield temp_dir
shutil.rmtree(temp_dir)
+ @pytest.fixture
+ def db_engine(self, temp_dir):
+ """Create and dispose a SQLite engine with all tables."""
+ db_path = str(Path(temp_dir) / "test.db")
+ engine = create_engine(f"sqlite:///{db_path}")
+ Base.metadata.create_all(engine)
+ yield engine
+ engine.dispose()
+
+ @pytest.fixture
+ def db_session(self, db_engine):
+ """Create a session bound to db_engine, closed on teardown."""
+ Session = sessionmaker(bind=db_engine)
+ session = Session()
+ yield session
+ session.close()
+
def test_basic_database_creation(self, temp_dir):
"""Test creating a basic SQLite database."""
db_path = str(Path(temp_dir) / "test.db")
engine = create_engine(f"sqlite:///{db_path}")
-
- # Create all tables
Base.metadata.create_all(engine)
# Verify database file exists
@@ -51,6 +66,8 @@ class TestDatabaseInitialization:
assert "token_usage" in tables
assert "search_cache" in tables
+ engine.dispose()
+
def test_database_creation_with_function(self, temp_dir):
"""Test database creation through standard SQLAlchemy."""
db_path = str(Path(temp_dir) / "test_user.db")
@@ -72,12 +89,16 @@ class TestDatabaseInitialization:
result = conn.execute(text("SELECT 1"))
assert result.fetchone()[0] == 1
+ engine.dispose()
+
def test_encrypted_database_creation(self, temp_dir):
"""Test creating an encrypted database with SQLCipher."""
db_path = str(Path(temp_dir) / "encrypted.db")
password = "test_password_123"
# Try to create encrypted database (will fail if sqlcipher not available)
+ engine = None
+ bad_engine = None
try:
# Check if pysqlcipher3 is available
import importlib.util
@@ -110,8 +131,8 @@ class TestDatabaseInitialization:
session.close()
# Verify database is encrypted by trying to open without password
+ bad_engine = create_engine(f"sqlite:///{db_path}")
with pytest.raises((OperationalError, Exception)):
- bad_engine = create_engine(f"sqlite:///{db_path}")
with bad_engine.connect() as conn:
from sqlalchemy import text
@@ -120,14 +141,15 @@ class TestDatabaseInitialization:
except (ImportError, TypeError) as e:
# Skip if SQLCipher not available or compatibility issues
pytest.skip(f"SQLCipher test skipped: {e}")
+ finally:
+ if bad_engine is not None:
+ bad_engine.dispose()
+ if engine is not None:
+ engine.dispose()
- def test_database_schema_completeness(self, temp_dir):
+ def test_database_schema_completeness(self, db_engine):
"""Test that all expected tables and columns are created."""
- db_path = str(Path(temp_dir) / "schema_test.db")
- engine = create_engine(f"sqlite:///{db_path}")
- Base.metadata.create_all(engine)
-
- inspector = inspect(engine)
+ inspector = inspect(db_engine)
# Test ResearchHistory table schema
research_columns = {
@@ -187,13 +209,9 @@ class TestDatabaseInitialization:
}
assert expected_settings_columns.issubset(settings_columns)
- def test_database_indexes(self, temp_dir):
+ def test_database_indexes(self, db_engine):
"""Test that proper indexes are created."""
- db_path = str(Path(temp_dir) / "index_test.db")
- engine = create_engine(f"sqlite:///{db_path}")
- Base.metadata.create_all(engine)
-
- inspector = inspect(engine)
+ inspector = inspect(db_engine)
# Check indexes on research_history
research_indexes = inspector.get_indexes("research_history")
@@ -209,15 +227,8 @@ class TestDatabaseInitialization:
# Check unique constraints
# Username and email should have unique constraints in users table
- def test_database_foreign_keys(self, temp_dir):
+ def test_database_foreign_keys(self, db_engine, db_session):
"""Test that foreign key relationships work correctly."""
- db_path = str(Path(temp_dir) / "fk_test.db")
- engine = create_engine(f"sqlite:///{db_path}")
- Base.metadata.create_all(engine)
-
- Session = sessionmaker(bind=engine)
- session = Session()
-
# Create a research record
research = ResearchHistory(
id=str(uuid.uuid4()),
@@ -226,8 +237,8 @@ class TestDatabaseInitialization:
status="completed",
created_at="2024-01-01T00:00:00",
)
- session.add(research)
- session.commit()
+ db_session.add(research)
+ db_session.commit()
# Create related records
from local_deep_research.database.models import (
@@ -242,7 +253,7 @@ class TestDatabaseInitialization:
url="https://example.com",
created_at="2024-01-01T00:01:00",
)
- session.add(resource)
+ db_session.add(resource)
# Add token usage
usage = TokenUsage(
@@ -253,25 +264,16 @@ class TestDatabaseInitialization:
completion_tokens=20,
total_tokens=100,
)
- session.add(usage)
+ db_session.add(usage)
- session.commit()
+ db_session.commit()
# Verify relationships
assert resource.research_id == research.id
assert usage.research_id == str(research.id)
- session.close()
-
- def test_database_cascade_deletes(self, temp_dir):
+ def test_database_cascade_deletes(self, db_engine, db_session):
"""Test cascade delete behavior."""
- db_path = str(Path(temp_dir) / "cascade_test.db")
- engine = create_engine(f"sqlite:///{db_path}")
- Base.metadata.create_all(engine)
-
- Session = sessionmaker(bind=engine)
- session = Session()
-
# Create a benchmark run with results
from local_deep_research.database.models import (
BenchmarkResult,
@@ -286,8 +288,8 @@ class TestDatabaseInitialization:
evaluation_config={},
datasets_config={},
)
- session.add(run)
- session.commit()
+ db_session.add(run)
+ db_session.commit()
# Add results
for i in range(3):
@@ -299,51 +301,42 @@ class TestDatabaseInitialization:
question=f"Question {i}",
correct_answer=f"Answer {i}",
)
- session.add(result)
+ db_session.add(result)
- session.commit()
+ db_session.commit()
# Verify results exist
result_count = (
- session.query(BenchmarkResult)
+ db_session.query(BenchmarkResult)
.filter_by(benchmark_run_id=run.id)
.count()
)
assert result_count == 3
# Delete the run
- session.delete(run)
- session.commit()
+ db_session.delete(run)
+ db_session.commit()
# Verify cascade delete worked
result_count = (
- session.query(BenchmarkResult)
+ db_session.query(BenchmarkResult)
.filter_by(benchmark_run_id=run.id)
.count()
)
assert result_count == 0
- session.close()
-
- def test_database_transactions(self, temp_dir):
+ def test_database_transactions(self, db_engine, db_session):
"""Test transaction rollback behavior."""
- db_path = str(Path(temp_dir) / "transaction_test.db")
- engine = create_engine(f"sqlite:///{db_path}")
- Base.metadata.create_all(engine)
-
- Session = sessionmaker(bind=engine)
- session = Session()
-
# Add a user
user = User(username="testuser")
- session.add(user)
- session.commit()
+ db_session.add(user)
+ db_session.commit()
# Start a transaction that will fail
try:
# Add another user with duplicate username (should fail)
user2 = User(username="testuser")
- session.add(user2)
+ db_session.add(user2)
# Add a valid setting
setting = Setting(
@@ -352,35 +345,28 @@ class TestDatabaseInitialization:
type="string",
category="test",
)
- session.add(setting)
+ db_session.add(setting)
# This should fail due to unique constraint
- session.commit()
+ db_session.commit()
except Exception:
- session.rollback()
+ db_session.rollback()
# Verify rollback worked - setting should not exist
setting_count = (
- session.query(Setting).filter_by(key="test.setting").count()
+ db_session.query(Setting).filter_by(key="test.setting").count()
)
assert setting_count == 0
# Original user should still exist
- user_count = session.query(User).count()
+ user_count = db_session.query(User).count()
assert user_count == 1
- session.close()
-
- def test_database_performance_with_large_dataset(self, temp_dir):
+ def test_database_performance_with_large_dataset(
+ self, db_engine, db_session
+ ):
"""Test database performance with larger datasets."""
- db_path = str(Path(temp_dir) / "performance_test.db")
- engine = create_engine(f"sqlite:///{db_path}")
- Base.metadata.create_all(engine)
-
- Session = sessionmaker(bind=engine)
- session = Session()
-
# Add many research records
research_count = 1000
for i in range(research_count):
@@ -393,13 +379,13 @@ class TestDatabaseInitialization:
duration_seconds=100 + i % 500,
progress=100 if i % 2 == 0 else 50,
)
- session.add(research)
+ db_session.add(research)
# Commit in batches
if i % 100 == 0:
- session.commit()
+ db_session.commit()
- session.commit()
+ db_session.commit()
# Test query performance
import time
@@ -407,7 +393,9 @@ class TestDatabaseInitialization:
# Query completed research
start = time.time()
completed = (
- session.query(ResearchHistory).filter_by(status="completed").count()
+ db_session.query(ResearchHistory)
+ .filter_by(status="completed")
+ .count()
)
query_time = time.time() - start
@@ -417,7 +405,7 @@ class TestDatabaseInitialization:
# Test ordering
start = time.time()
recent = (
- session.query(ResearchHistory)
+ db_session.query(ResearchHistory)
.order_by(ResearchHistory.created_at.desc())
.limit(10)
.all()
@@ -427,8 +415,6 @@ class TestDatabaseInitialization:
assert len(recent) == 10
assert order_time < 0.1
- session.close()
-
def test_user_specific_database_path(self, temp_dir):
"""Test user-specific database paths for multi-user support."""
# Test database path generation for different users
@@ -491,3 +477,5 @@ class TestDatabaseInitialization:
session1.close()
session2.close()
+ engine1.dispose()
+ engine2.dispose()