mirror of
https://github.com/Xe138/AI-Trader.git
synced 2026-04-05 18:27:25 -04:00
test: improve test coverage from 61% to 84.81%
Major improvements: - Fixed all 42 broken tests (database connection leaks) - Added db_connection() context manager for proper cleanup - Created comprehensive test suites for undertested modules New test coverage: - tools/general_tools.py: 26 tests (97% coverage) - tools/price_tools.py: 11 tests (validates NASDAQ symbols, date handling) - api/price_data_manager.py: 12 tests (85% coverage) - api/routes/results_v2.py: 3 tests (98% coverage) - agent/reasoning_summarizer.py: 2 tests (87% coverage) - api/routes/period_metrics.py: 2 edge case tests (100% coverage) - agent/mock_provider: 1 test (100% coverage) Database fixes: - Added db_connection() context manager to prevent leaks - Updated 16+ test files to use context managers - Fixed drop_all_tables() to match new schema - Added CHECK constraint for action_type - Added ON DELETE CASCADE to trading_days foreign key Test improvements: - Updated SQL INSERT statements with all required fields - Fixed date parameter handling in API integration tests - Added edge case tests for validation functions - Fixed import errors across test suite Results: - Total coverage: 84.81% (was 61%) - Tests passing: 406 (was 364 with 42 failures) - Total lines covered: 6364 of 7504 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import os
|
||||
import pytest
|
||||
from pathlib import Path
|
||||
from api.database import initialize_dev_database, cleanup_dev_database
|
||||
from api.database import initialize_dev_database, cleanup_dev_database, db_connection
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
@@ -30,18 +30,16 @@ def test_initialize_dev_database_creates_fresh_db(tmp_path, clean_env):
|
||||
# Create initial database with some data
|
||||
from api.database import get_db_connection, initialize_database
|
||||
initialize_database(db_path)
|
||||
conn = get_db_connection(db_path)
|
||||
conn.execute("INSERT INTO jobs (job_id, config_path, status, date_range, models, created_at) VALUES (?, ?, ?, ?, ?, ?)",
|
||||
("test-job", "config.json", "completed", "2025-01-01:2025-01-31", '["model1"]', "2025-01-01T00:00:00"))
|
||||
conn.commit()
|
||||
conn.close()
|
||||
with db_connection(db_path) as conn:
|
||||
conn.execute("INSERT INTO jobs (job_id, config_path, status, date_range, models, created_at) VALUES (?, ?, ?, ?, ?, ?)",
|
||||
("test-job", "config.json", "completed", "2025-01-01:2025-01-31", '["model1"]', "2025-01-01T00:00:00"))
|
||||
conn.commit()
|
||||
|
||||
# Verify data exists
|
||||
conn = get_db_connection(db_path)
|
||||
cursor = conn.cursor()
|
||||
cursor.execute("SELECT COUNT(*) FROM jobs")
|
||||
assert cursor.fetchone()[0] == 1
|
||||
conn.close()
|
||||
with db_connection(db_path) as conn:
|
||||
cursor = conn.cursor()
|
||||
cursor.execute("SELECT COUNT(*) FROM jobs")
|
||||
assert cursor.fetchone()[0] == 1
|
||||
|
||||
# Close all connections before reinitializing
|
||||
conn.close()
|
||||
@@ -59,11 +57,10 @@ def test_initialize_dev_database_creates_fresh_db(tmp_path, clean_env):
|
||||
initialize_dev_database(db_path)
|
||||
|
||||
# Verify data is cleared
|
||||
conn = get_db_connection(db_path)
|
||||
cursor = conn.cursor()
|
||||
cursor.execute("SELECT COUNT(*) FROM jobs")
|
||||
count = cursor.fetchone()[0]
|
||||
conn.close()
|
||||
with db_connection(db_path) as conn:
|
||||
cursor = conn.cursor()
|
||||
cursor.execute("SELECT COUNT(*) FROM jobs")
|
||||
count = cursor.fetchone()[0]
|
||||
assert count == 0, f"Expected 0 jobs after reinitialization, found {count}"
|
||||
|
||||
|
||||
@@ -97,21 +94,19 @@ def test_initialize_dev_respects_preserve_flag(tmp_path, clean_env):
|
||||
# Create database with data
|
||||
from api.database import get_db_connection, initialize_database
|
||||
initialize_database(db_path)
|
||||
conn = get_db_connection(db_path)
|
||||
conn.execute("INSERT INTO jobs (job_id, config_path, status, date_range, models, created_at) VALUES (?, ?, ?, ?, ?, ?)",
|
||||
("test-job", "config.json", "completed", "2025-01-01:2025-01-31", '["model1"]', "2025-01-01T00:00:00"))
|
||||
conn.commit()
|
||||
conn.close()
|
||||
with db_connection(db_path) as conn:
|
||||
conn.execute("INSERT INTO jobs (job_id, config_path, status, date_range, models, created_at) VALUES (?, ?, ?, ?, ?, ?)",
|
||||
("test-job", "config.json", "completed", "2025-01-01:2025-01-31", '["model1"]', "2025-01-01T00:00:00"))
|
||||
conn.commit()
|
||||
|
||||
# Initialize with preserve flag
|
||||
initialize_dev_database(db_path)
|
||||
|
||||
# Verify data is preserved
|
||||
conn = get_db_connection(db_path)
|
||||
cursor = conn.cursor()
|
||||
cursor.execute("SELECT COUNT(*) FROM jobs")
|
||||
assert cursor.fetchone()[0] == 1
|
||||
conn.close()
|
||||
with db_connection(db_path) as conn:
|
||||
cursor = conn.cursor()
|
||||
cursor.execute("SELECT COUNT(*) FROM jobs")
|
||||
assert cursor.fetchone()[0] == 1
|
||||
|
||||
|
||||
def test_get_db_connection_resolves_dev_path():
|
||||
|
||||
Reference in New Issue
Block a user