mirror of
https://github.com/Xe138/AI-Trader.git
synced 2026-04-01 17:17:24 -04:00
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>
101 lines
3.1 KiB
Python
101 lines
3.1 KiB
Python
"""Verify /results endpoint replaces /reasoning endpoint."""
|
|
|
|
import pytest
|
|
from fastapi.testclient import TestClient
|
|
from api.main import create_app
|
|
from api.database import Database
|
|
|
|
|
|
def test_results_with_full_reasoning_replaces_old_endpoint(tmp_path):
|
|
"""Test /results?reasoning=full provides same data as old /reasoning."""
|
|
|
|
# Create test database with file path (not in-memory, to avoid sharing issues)
|
|
import json
|
|
db_path = str(tmp_path / "test.db")
|
|
db = Database(db_path)
|
|
|
|
# Create job first
|
|
db.connection.execute("""
|
|
INSERT INTO jobs (job_id, config_path, status, date_range, models, created_at)
|
|
VALUES (?, ?, ?, ?, ?, ?)
|
|
""", ('test-job-123', 'test_config.json', 'completed',
|
|
json.dumps({'init_date': '2025-01-15', 'end_date': '2025-01-15'}),
|
|
json.dumps(['test-model']), '2025-01-15T10:00:00Z'))
|
|
db.connection.commit()
|
|
|
|
trading_day_id = db.create_trading_day(
|
|
job_id='test-job-123',
|
|
model='test-model',
|
|
date='2025-01-15',
|
|
starting_cash=10000.0,
|
|
starting_portfolio_value=10000.0,
|
|
ending_cash=8500.0,
|
|
ending_portfolio_value=10000.0,
|
|
daily_profit=0.0,
|
|
daily_return_pct=0.0,
|
|
days_since_last_trading=0
|
|
)
|
|
|
|
# Add actions
|
|
db.create_action(trading_day_id, 'buy', 'AAPL', 10, 150.0)
|
|
|
|
# Add holdings
|
|
db.create_holding(trading_day_id, 'AAPL', 10)
|
|
|
|
# Update with reasoning
|
|
db.connection.execute("""
|
|
UPDATE trading_days
|
|
SET reasoning_summary = 'Bought AAPL based on earnings',
|
|
reasoning_full = ?,
|
|
total_actions = 1
|
|
WHERE id = ?
|
|
""", (json.dumps([
|
|
{"role": "user", "content": "System prompt"},
|
|
{"role": "assistant", "content": "I will buy AAPL"}
|
|
]), trading_day_id))
|
|
|
|
db.connection.commit()
|
|
db.connection.close()
|
|
|
|
# Create test app with the test database
|
|
app = create_app(db_path=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 Database(db_path)
|
|
|
|
app.dependency_overrides[get_database] = override_get_database
|
|
|
|
client = TestClient(app)
|
|
|
|
# Query new endpoint with explicit date to avoid default lookback filter
|
|
response = client.get("/results?job_id=test-job-123&start_date=2025-01-15&end_date=2025-01-15&reasoning=full")
|
|
|
|
assert response.status_code == 200
|
|
data = response.json()
|
|
|
|
# Verify structure matches old endpoint needs
|
|
assert data['count'] == 1
|
|
result = data['results'][0]
|
|
|
|
assert result['date'] == '2025-01-15'
|
|
assert result['model'] == 'test-model'
|
|
assert result['trades'][0]['action_type'] == 'buy'
|
|
assert result['trades'][0]['symbol'] == 'AAPL'
|
|
assert isinstance(result['reasoning'], list)
|
|
assert len(result['reasoning']) == 2
|
|
|
|
|
|
def test_reasoning_endpoint_returns_404():
|
|
"""Verify /reasoning endpoint is removed."""
|
|
|
|
app = create_app(db_path=":memory:")
|
|
client = TestClient(app)
|
|
|
|
response = client.get("/reasoning?job_id=test-job-123")
|
|
|
|
assert response.status_code == 404
|