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:
2025-11-07 21:02:38 -05:00
parent 61baf3f90f
commit 14cf88f642
30 changed files with 1840 additions and 1013 deletions

View File

@@ -102,7 +102,48 @@ async def test_context_injector_tracks_position_after_successful_trade(injector)
assert injector._current_position is not None
assert injector._current_position["CASH"] == 1100.0
assert injector._current_position["AAPL"] == 7
assert injector._current_position["MSFT"] == 5
@pytest.mark.asyncio
async def test_context_injector_injects_session_id():
"""Test that session_id is injected when provided."""
injector = ContextInjector(
signature="test-sig",
today_date="2025-01-15",
session_id="test-session-123"
)
request = MockRequest("buy", {"symbol": "AAPL", "amount": 5})
async def capturing_handler(req):
# Verify session_id was injected
assert "session_id" in req.args
assert req.args["session_id"] == "test-session-123"
return create_mcp_result({"CASH": 100.0})
await injector(request, capturing_handler)
@pytest.mark.asyncio
async def test_context_injector_handles_dict_result():
"""Test handling when handler returns a plain dict instead of CallToolResult."""
injector = ContextInjector(
signature="test-sig",
today_date="2025-01-15"
)
request = MockRequest("buy", {"symbol": "AAPL", "amount": 5})
async def dict_handler(req):
# Return plain dict instead of CallToolResult
return {"CASH": 500.0, "AAPL": 10}
result = await injector(request, dict_handler)
# Verify position was still updated
assert injector._current_position is not None
assert injector._current_position["CASH"] == 500.0
assert injector._current_position["AAPL"] == 10
@pytest.mark.asyncio