TradingAgents/tests/unit
Ahmet Guzererler eafdce3121 fix: harden dataflows layer against silent failures and data corruption
## Problem (Incident Post-mortem)

The pipeline was emitting hundreds of errors:
  'Invalid number of return arguments after parsing column name: Date'

Root cause: after _clean_dataframe() lowercases all columns, stockstats.wrap()
promotes 'date' to the DataFrame index. Subsequent df['Date'] access caused
stockstats to try parsing 'Date' as a technical indicator name.

## Fixes

### 1. Fix df['Date'] stockstats bug (already shipped in prior commit)
- stockstats_utils.py + y_finance.py: use df.index.strftime() instead of
  df['Date'] after wrap()

### 2. Extract _load_or_fetch_ohlcv() — single OHLCV authority
- Eliminates duplicated 30-line download+cache boilerplate in two places
- Cache filename is always derived from today's date — hardcoded stale date
  '2015-01-01-2025-03-25' in local mode is gone
- Corruption/truncation detection: files <50 rows or unparseable are deleted
  and re-fetched rather than silently returning bad data
- Drops on_bad_lines='skip' — malformed CSVs now raise instead of silently
  dropping rows that would distort indicator calculations

### 3. YFinanceError typed exception
- Defined in stockstats_utils.py; raised instead of print()+return ''
- get_stockstats_indicator now raises YFinanceError on failure so errors
  surface to callers rather than delivering empty strings to LLM agents
- interface.py route_to_vendor now catches YFinanceError alongside
  AlphaVantageError and FinnhubError — failures appear in observability
  telemetry and can trigger vendor fallback

### 4. Explicit date column discovery in alpha_vantage_common
- _filter_csv_by_date_range: replaced df.columns[0] positional assumption
  with explicit search for 'time'/'timestamp'/'date' column
- ValueError re-raised (not swallowed) so bad API response shape is visible

### 5. Structured logging
- Replaced all print() calls in changed files with logging.getLogger()
- Added logging import + logger to alpha_vantage_common

## Tests
- tests/unit/test_incident_fixes.py: 12 new unit tests covering all fixes
  (dynamic cache filename, corruption re-fetch, YFinanceError propagation,
  explicit column lookup, empty download raises)
- tests/integration/test_stockstats_live.py: 11 live tests against real
  yfinance API (all major indicators, weekend N/A, regression guard)
- All 70 tests pass (59 unit + 11 live integration)
2026-03-22 00:07:32 +01:00
..
agents/utils test: add unit tests for _tokenize in FinancialSituationMemory 2026-03-21 01:54:23 +00:00
__init__.py Add integration and unit tests for scanner routing, TTM analysis, vendor fail-fast, and yfinance data layer 2026-03-19 13:51:57 +01:00
conftest.py Add integration and unit tests for scanner routing, TTM analysis, vendor fail-fast, and yfinance data layer 2026-03-19 13:51:57 +01:00
test_alpha_vantage_exceptions.py fix(tests): complete PR #26 — enforce socket isolation in unit tier and add test suite reference doc (#30) 2026-03-19 17:41:25 +01:00
test_alpha_vantage_integration.py Add integration and unit tests for scanner routing, TTM analysis, vendor fail-fast, and yfinance data layer 2026-03-19 13:51:57 +01:00
test_alpha_vantage_scanner.py Add integration and unit tests for scanner routing, TTM analysis, vendor fail-fast, and yfinance data layer 2026-03-19 13:51:57 +01:00
test_api_usage.py Add API consumption estimation module and CLI command 2026-03-21 17:25:26 +00:00
test_cli_main_tools.py 🧪 Implement robust tool call parsing and unit tests 2026-03-21 14:56:12 +00:00
test_config_wiring.py 🧹 fix: remove unused imports from agent_utils.py 2026-03-21 08:30:43 +00:00
test_debate_rounds.py Add integration and unit tests for scanner routing, TTM analysis, vendor fail-fast, and yfinance data layer 2026-03-19 13:51:57 +01:00
test_e2e_api_integration.py Add integration and unit tests for scanner routing, TTM analysis, vendor fail-fast, and yfinance data layer 2026-03-19 13:51:57 +01:00
test_env_override.py fix(tests): complete PR #26 — enforce socket isolation in unit tier and add test suite reference doc (#30) 2026-03-19 17:41:25 +01:00
test_finnhub_integration.py Fix Finnhub API error handling and add coverage 2026-03-21 14:51:29 +00:00
test_incident_fixes.py fix: harden dataflows layer against silent failures and data corruption 2026-03-22 00:07:32 +01:00
test_industry_deep_dive.py test: add tests and parsing logic for text formats in _extract_top_sectors 2026-03-21 08:28:35 +00:00
test_json_utils.py Add integration and unit tests for scanner routing, TTM analysis, vendor fail-fast, and yfinance data layer 2026-03-19 13:51:57 +01:00
test_macro_bridge.py Add integration and unit tests for scanner routing, TTM analysis, vendor fail-fast, and yfinance data layer 2026-03-19 13:51:57 +01:00
test_macro_regime.py Merge pull request #54 from aguzererler/test-macro-regime-fmt-pct-18208719821293052000 2026-03-21 17:36:20 +01:00
test_notebook_sync.py feat: NotebookLM sync with date-specific sources and consolidation (#28) 2026-03-19 15:39:25 +01:00
test_peer_comparison.py Add integration and unit tests for scanner routing, TTM analysis, vendor fail-fast, and yfinance data layer 2026-03-19 13:51:57 +01:00
test_portfolio_tools.py feat: add portfolio risk metrics module and LangChain agent tools 2026-03-20 14:42:43 +00:00
test_scanner_fallback.py refactor(tests): consolidate live-API tests into integration/, move mocked tests to unit/ 2026-03-19 14:24:52 +01:00
test_scanner_graph.py Add integration and unit tests for scanner routing, TTM analysis, vendor fail-fast, and yfinance data layer 2026-03-19 13:51:57 +01:00
test_scanner_mocked.py Add integration and unit tests for scanner routing, TTM analysis, vendor fail-fast, and yfinance data layer 2026-03-19 13:51:57 +01:00
test_stockstats_utils.py 🧪 Resolve PRs #56, #58, and #60 2026-03-21 16:51:49 +00:00
test_ttm_analysis.py Merge pull request #69 from aguzererler/copilot/review-financial-tools-implementation 2026-03-21 22:41:55 +01:00
test_vendor_failfast.py Add integration and unit tests for scanner routing, TTM analysis, vendor fail-fast, and yfinance data layer 2026-03-19 13:51:57 +01:00
test_yfinance_integration.py 🧪 Add tests for _extract_article_data in yfinance_news.py 2026-03-21 08:23:12 +00:00