From 52635efd7d435b091b4f13897511ca8e2c48f0b9 Mon Sep 17 00:00:00 2001 From: Tyler Hoang Date: Mon, 18 May 2026 22:43:16 -0700 Subject: fix: add no-key fallback for sector ratio benchmarks --- backend/tests/test_api.py | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) (limited to 'backend/tests/test_api.py') diff --git a/backend/tests/test_api.py b/backend/tests/test_api.py index 300069c..345c0a3 100644 --- a/backend/tests/test_api.py +++ b/backend/tests/test_api.py @@ -1212,6 +1212,49 @@ def test_get_ratios_sector_benchmark_fields(monkeypatch) -> None: assert result["dividend_yield"]["vs_sector"] == pytest.approx(0.012) +def test_compute_sector_ratio_benchmarks_without_fmp_key(monkeypatch) -> None: + clear_service_caches() + monkeypatch.delenv("FMP_API_KEY", raising=False) + monkeypatch.setattr( + data_service, + "get_company_info", + lambda symbol: ( + {"sector": "Technology"} + if symbol == "AAPL" + else {"sector": "Technology"} + if symbol == "MSFT" + else {"sector": "Technology"} + if symbol == "NVDA" + else {"sector": "Healthcare"} + ), + ) + monkeypatch.setattr( + data_service, + "search_tickers", + lambda query: [ + {"symbol": "MSFT", "name": "Microsoft", "exchange": "NASDAQ"}, + {"symbol": "NVDA", "name": "NVIDIA", "exchange": "NASDAQ"}, + {"symbol": "UNH", "name": "UnitedHealth", "exchange": "NYSE"}, + ], + ) + monkeypatch.setattr( + data_service, + "compute_ttm_ratios", + lambda symbol: ( + {"trailing_pe": 30.0, "current_ratio": 2.0} + if symbol == "MSFT" + else {"trailing_pe": 20.0, "current_ratio": 1.5} + if symbol == "NVDA" + else {"trailing_pe": 10.0, "current_ratio": 1.0} + ), + ) + + result = data_service.compute_sector_ratio_benchmarks("AAPL") + + assert result["trailing_pe"] == pytest.approx(25.0) + assert result["current_ratio"] == pytest.approx(1.75) + + def test_ticker_ratios_route(monkeypatch) -> None: """GET /api/tickers/{symbol}/ratios returns a valid RatiosResponse shape.""" clear_service_caches() -- cgit v1.3-2-g0d8e