mirror of
https://github.com/Xe138/AI-Trader.git
synced 2026-04-01 17:17:24 -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:
@@ -52,3 +52,32 @@ def test_calculate_period_metrics_negative_return():
|
||||
assert metrics["calendar_days"] == 8
|
||||
# Negative annualized return
|
||||
assert metrics["annualized_return_pct"] < 0
|
||||
|
||||
|
||||
def test_calculate_period_metrics_zero_starting_value():
|
||||
"""Test period metrics when starting value is zero (edge case)."""
|
||||
metrics = calculate_period_metrics(
|
||||
starting_value=0.0,
|
||||
ending_value=1000.0,
|
||||
start_date="2025-01-16",
|
||||
end_date="2025-01-20",
|
||||
trading_days=3
|
||||
)
|
||||
|
||||
# Should handle division by zero gracefully
|
||||
assert metrics["period_return_pct"] == 0.0
|
||||
assert metrics["annualized_return_pct"] == 0.0
|
||||
|
||||
|
||||
def test_calculate_period_metrics_negative_ending_value():
|
||||
"""Test period metrics when ending value is negative (edge case)."""
|
||||
metrics = calculate_period_metrics(
|
||||
starting_value=10000.0,
|
||||
ending_value=-100.0,
|
||||
start_date="2025-01-16",
|
||||
end_date="2025-01-20",
|
||||
trading_days=3
|
||||
)
|
||||
|
||||
# Should handle negative ending value gracefully
|
||||
assert metrics["annualized_return_pct"] == 0.0
|
||||
|
||||
@@ -46,11 +46,17 @@ def test_validate_both_dates():
|
||||
|
||||
|
||||
def test_validate_invalid_date_format():
|
||||
"""Test error on invalid date format."""
|
||||
"""Test error on invalid start_date format."""
|
||||
with pytest.raises(ValueError, match="Invalid date format"):
|
||||
validate_and_resolve_dates("2025-1-16", "2025-01-20")
|
||||
|
||||
|
||||
def test_validate_invalid_end_date_format():
|
||||
"""Test error on invalid end_date format."""
|
||||
with pytest.raises(ValueError, match="Invalid date format"):
|
||||
validate_and_resolve_dates("2025-01-16", "2025-1-20")
|
||||
|
||||
|
||||
def test_validate_start_after_end():
|
||||
"""Test error when start_date > end_date."""
|
||||
with pytest.raises(ValueError, match="start_date must be <= end_date"):
|
||||
@@ -220,3 +226,46 @@ def test_get_results_empty_404(test_db):
|
||||
|
||||
assert response.status_code == 404
|
||||
assert "No trading data found" in response.json()["detail"]
|
||||
|
||||
|
||||
def test_deprecated_date_parameter(test_db):
|
||||
"""Test that deprecated 'date' parameter returns 422 error."""
|
||||
app = create_app(db_path=test_db.db_path)
|
||||
app.state.test_mode = True
|
||||
|
||||
# Override the database dependency to use our test database
|
||||
from api.routes.results_v2 import get_database
|
||||
|
||||
def override_get_database():
|
||||
return test_db
|
||||
|
||||
app.dependency_overrides[get_database] = override_get_database
|
||||
|
||||
client = TestClient(app)
|
||||
|
||||
response = client.get("/results?date=2024-01-16")
|
||||
|
||||
assert response.status_code == 422
|
||||
assert "removed" in response.json()["detail"]
|
||||
assert "start_date" in response.json()["detail"]
|
||||
|
||||
|
||||
def test_invalid_date_returns_400(test_db):
|
||||
"""Test that invalid date format returns 400 error via API."""
|
||||
app = create_app(db_path=test_db.db_path)
|
||||
app.state.test_mode = True
|
||||
|
||||
# Override the database dependency to use our test database
|
||||
from api.routes.results_v2 import get_database
|
||||
|
||||
def override_get_database():
|
||||
return test_db
|
||||
|
||||
app.dependency_overrides[get_database] = override_get_database
|
||||
|
||||
client = TestClient(app)
|
||||
|
||||
response = client.get("/results?start_date=2024-1-16&end_date=2024-01-20")
|
||||
|
||||
assert response.status_code == 400
|
||||
assert "Invalid date format" in response.json()["detail"]
|
||||
|
||||
Reference in New Issue
Block a user