mirror of
https://github.com/Xe138/AI-Trader.git
synced 2026-04-11 05:07: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:
@@ -13,14 +13,13 @@ def test_db(tmp_path):
|
||||
initialize_database(db_path)
|
||||
|
||||
# Create a job record to satisfy foreign key constraint
|
||||
conn = get_db_connection(db_path)
|
||||
cursor = conn.cursor()
|
||||
cursor.execute("""
|
||||
INSERT INTO jobs (job_id, config_path, status, date_range, models, created_at)
|
||||
VALUES ('test-job', 'configs/default_config.json', 'running', '["2025-01-01"]', '["test-model"]', '2025-01-01T00:00:00Z')
|
||||
""")
|
||||
conn.commit()
|
||||
conn.close()
|
||||
with db_connection(db_path) as conn:
|
||||
cursor = conn.cursor()
|
||||
cursor.execute("""
|
||||
INSERT INTO jobs (job_id, config_path, status, date_range, models, created_at)
|
||||
VALUES ('test-job', 'configs/default_config.json', 'running', '["2025-01-01"]', '["test-model"]', '2025-01-01T00:00:00Z')
|
||||
""")
|
||||
conn.commit()
|
||||
|
||||
return db_path
|
||||
|
||||
@@ -36,23 +35,22 @@ def test_create_trading_session(test_db):
|
||||
db_path=test_db
|
||||
)
|
||||
|
||||
conn = get_db_connection(test_db)
|
||||
cursor = conn.cursor()
|
||||
with db_connection(test_db) as conn:
|
||||
cursor = conn.cursor()
|
||||
|
||||
session_id = executor._create_trading_session(cursor)
|
||||
conn.commit()
|
||||
session_id = executor._create_trading_session(cursor)
|
||||
conn.commit()
|
||||
|
||||
# Verify session created
|
||||
cursor.execute("SELECT * FROM trading_sessions WHERE id = ?", (session_id,))
|
||||
session = cursor.fetchone()
|
||||
# Verify session created
|
||||
cursor.execute("SELECT * FROM trading_sessions WHERE id = ?", (session_id,))
|
||||
session = cursor.fetchone()
|
||||
|
||||
assert session is not None
|
||||
assert session['job_id'] == "test-job"
|
||||
assert session['date'] == "2025-01-01"
|
||||
assert session['model'] == "test-model"
|
||||
assert session['started_at'] is not None
|
||||
assert session is not None
|
||||
assert session['job_id'] == "test-job"
|
||||
assert session['date'] == "2025-01-01"
|
||||
assert session['model'] == "test-model"
|
||||
assert session['started_at'] is not None
|
||||
|
||||
conn.close()
|
||||
|
||||
|
||||
@pytest.mark.skip(reason="Methods removed in schema migration Task 2. Will be deleted in Task 6.")
|
||||
@@ -85,27 +83,26 @@ async def test_store_reasoning_logs(test_db):
|
||||
{"role": "assistant", "content": "Bought AAPL 10 shares based on strong earnings", "timestamp": "2025-01-01T10:05:00Z"}
|
||||
]
|
||||
|
||||
conn = get_db_connection(test_db)
|
||||
cursor = conn.cursor()
|
||||
session_id = executor._create_trading_session(cursor)
|
||||
with db_connection(test_db) as conn:
|
||||
cursor = conn.cursor()
|
||||
session_id = executor._create_trading_session(cursor)
|
||||
|
||||
await executor._store_reasoning_logs(cursor, session_id, conversation, agent)
|
||||
conn.commit()
|
||||
await executor._store_reasoning_logs(cursor, session_id, conversation, agent)
|
||||
conn.commit()
|
||||
|
||||
# Verify logs stored
|
||||
cursor.execute("SELECT * FROM reasoning_logs WHERE session_id = ? ORDER BY message_index", (session_id,))
|
||||
logs = cursor.fetchall()
|
||||
# Verify logs stored
|
||||
cursor.execute("SELECT * FROM reasoning_logs WHERE session_id = ? ORDER BY message_index", (session_id,))
|
||||
logs = cursor.fetchall()
|
||||
|
||||
assert len(logs) == 2
|
||||
assert logs[0]['role'] == 'user'
|
||||
assert logs[0]['content'] == 'Analyze market'
|
||||
assert logs[0]['summary'] is None # No summary for user messages
|
||||
assert len(logs) == 2
|
||||
assert logs[0]['role'] == 'user'
|
||||
assert logs[0]['content'] == 'Analyze market'
|
||||
assert logs[0]['summary'] is None # No summary for user messages
|
||||
|
||||
assert logs[1]['role'] == 'assistant'
|
||||
assert logs[1]['content'] == 'Bought AAPL 10 shares based on strong earnings'
|
||||
assert logs[1]['summary'] is not None # Summary generated for assistant
|
||||
assert logs[1]['role'] == 'assistant'
|
||||
assert logs[1]['content'] == 'Bought AAPL 10 shares based on strong earnings'
|
||||
assert logs[1]['summary'] is not None # Summary generated for assistant
|
||||
|
||||
conn.close()
|
||||
|
||||
|
||||
@pytest.mark.skip(reason="Methods removed in schema migration Task 2. Will be deleted in Task 6.")
|
||||
@@ -139,23 +136,22 @@ async def test_update_session_summary(test_db):
|
||||
{"role": "assistant", "content": "Sold MSFT 5 shares", "timestamp": "2025-01-01T10:10:00Z"}
|
||||
]
|
||||
|
||||
conn = get_db_connection(test_db)
|
||||
cursor = conn.cursor()
|
||||
session_id = executor._create_trading_session(cursor)
|
||||
with db_connection(test_db) as conn:
|
||||
cursor = conn.cursor()
|
||||
session_id = executor._create_trading_session(cursor)
|
||||
|
||||
await executor._update_session_summary(cursor, session_id, conversation, agent)
|
||||
conn.commit()
|
||||
await executor._update_session_summary(cursor, session_id, conversation, agent)
|
||||
conn.commit()
|
||||
|
||||
# Verify session updated
|
||||
cursor.execute("SELECT * FROM trading_sessions WHERE id = ?", (session_id,))
|
||||
session = cursor.fetchone()
|
||||
# Verify session updated
|
||||
cursor.execute("SELECT * FROM trading_sessions WHERE id = ?", (session_id,))
|
||||
session = cursor.fetchone()
|
||||
|
||||
assert session['session_summary'] is not None
|
||||
assert len(session['session_summary']) > 0
|
||||
assert session['completed_at'] is not None
|
||||
assert session['total_messages'] == 3
|
||||
assert session['session_summary'] is not None
|
||||
assert len(session['session_summary']) > 0
|
||||
assert session['completed_at'] is not None
|
||||
assert session['total_messages'] == 3
|
||||
|
||||
conn.close()
|
||||
|
||||
|
||||
@pytest.mark.skip(reason="Methods removed in schema migration Task 2. Will be deleted in Task 6.")
|
||||
@@ -195,24 +191,23 @@ async def test_store_reasoning_logs_with_tool_messages(test_db):
|
||||
{"role": "assistant", "content": "AAPL is $150", "timestamp": "2025-01-01T10:02:00Z"}
|
||||
]
|
||||
|
||||
conn = get_db_connection(test_db)
|
||||
cursor = conn.cursor()
|
||||
session_id = executor._create_trading_session(cursor)
|
||||
with db_connection(test_db) as conn:
|
||||
cursor = conn.cursor()
|
||||
session_id = executor._create_trading_session(cursor)
|
||||
|
||||
await executor._store_reasoning_logs(cursor, session_id, conversation, agent)
|
||||
conn.commit()
|
||||
await executor._store_reasoning_logs(cursor, session_id, conversation, agent)
|
||||
conn.commit()
|
||||
|
||||
# Verify tool message stored correctly
|
||||
cursor.execute("SELECT * FROM reasoning_logs WHERE session_id = ? AND role = 'tool'", (session_id,))
|
||||
tool_log = cursor.fetchone()
|
||||
# Verify tool message stored correctly
|
||||
cursor.execute("SELECT * FROM reasoning_logs WHERE session_id = ? AND role = 'tool'", (session_id,))
|
||||
tool_log = cursor.fetchone()
|
||||
|
||||
assert tool_log is not None
|
||||
assert tool_log['tool_name'] == 'get_price'
|
||||
assert tool_log['tool_input'] == '{"symbol": "AAPL"}'
|
||||
assert tool_log['content'] == 'AAPL: $150.00'
|
||||
assert tool_log['summary'] is None # No summary for tool messages
|
||||
assert tool_log is not None
|
||||
assert tool_log['tool_name'] == 'get_price'
|
||||
assert tool_log['tool_input'] == '{"symbol": "AAPL"}'
|
||||
assert tool_log['content'] == 'AAPL: $150.00'
|
||||
assert tool_log['summary'] is None # No summary for tool messages
|
||||
|
||||
conn.close()
|
||||
|
||||
|
||||
@pytest.mark.skip(reason="Method _write_results_to_db() removed - positions written by trade tools")
|
||||
|
||||
Reference in New Issue
Block a user