From 38bef98ab1964bc9365d17c4a5ca17cdbd32673b Mon Sep 17 00:00:00 2001 From: Tyler Hoang Date: Mon, 18 May 2026 02:34:46 -0700 Subject: fix: correct historical ratios share and debt inputs --- backend/tests/test_api.py | 96 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) (limited to 'backend/tests/test_api.py') diff --git a/backend/tests/test_api.py b/backend/tests/test_api.py index 94c9c20..d0aafa4 100644 --- a/backend/tests/test_api.py +++ b/backend/tests/test_api.py @@ -993,6 +993,102 @@ def test_compute_historical_ratios_margins(monkeypatch) -> None: assert result["price_to_sales"] == [None, None, None, None] +def test_compute_historical_ratios_uses_period_share_counts_oldest_first(monkeypatch) -> None: + clear_service_caches() + monkeypatch.setattr( + data_service, + "get_income_statement", + lambda symbol, quarterly=False: annual_frame( + { + "Total Revenue": [40.0, 40.0, 40.0, 40.0], + "Gross Profit": [20.0, 20.0, 20.0, 20.0], + "Operating Income": [15.0, 15.0, 15.0, 15.0], + "Net Income": [10.0, 10.0, 10.0, 10.0], + "EBITDA": [2_000_000.0, 2_000_000.0, 2_000_000.0, 2_000_000.0], + } + ), + ) + monkeypatch.setattr( + data_service, + "get_balance_sheet", + lambda symbol, quarterly=False: annual_frame( + { + "Stockholders Equity": [20.0, 20.0, 20.0, 20.0], + "Total Assets": [50.0, 50.0, 50.0, 50.0], + "Total Debt": [100_000.0, 100_000.0, 100_000.0, 100_000.0], + "Current Assets": [10.0, 10.0, 10.0, 10.0], + "Current Liabilities": [5.0, 5.0, 5.0, 5.0], + "Cash And Cash Equivalents": [0.0, 0.0, 0.0, 0.0], + "Ordinary Shares Number": [10.0, 20.0, 40.0, 80.0], + } + ), + ) + monkeypatch.setattr(data_service, "get_cash_flow", lambda symbol, quarterly=False: pd.DataFrame()) + monkeypatch.setattr(data_service, "get_shares_outstanding", lambda symbol: 999.0) + monkeypatch.setattr( + data_service, + "get_price_history", + lambda symbol, period="5y": [ + {"date": "2024-09-30", "close": 10.0}, + {"date": "2023-09-30", "close": 10.0}, + {"date": "2022-09-30", "close": 10.0}, + {"date": "2021-09-30", "close": 10.0}, + ], + ) + + result = data_service.compute_historical_ratios("AAPL") + + assert result["trailing_pe"] == [80.0, 40.0, 20.0, 10.0] + assert result["price_to_book"] == [40.0, 20.0, 10.0, 5.0] + assert result["price_to_sales"] == [20.0, 10.0, 5.0, 2.5] + + +def test_compute_historical_ratios_uses_long_term_debt_fallback(monkeypatch) -> None: + clear_service_caches() + monkeypatch.setattr( + data_service, + "get_income_statement", + lambda symbol, quarterly=False: annual_frame( + { + "Total Revenue": [5_000_000.0, 5_000_000.0, 5_000_000.0, 5_000_000.0], + "Gross Profit": [2_500_000.0, 2_500_000.0, 2_500_000.0, 2_500_000.0], + "Operating Income": [2_100_000.0, 2_100_000.0, 2_100_000.0, 2_100_000.0], + "Net Income": [1_000_000.0, 1_000_000.0, 1_000_000.0, 1_000_000.0], + "EBITDA": [2_000_000.0, 2_000_000.0, 2_000_000.0, 2_000_000.0], + } + ), + ) + monkeypatch.setattr( + data_service, + "get_balance_sheet", + lambda symbol, quarterly=False: annual_frame( + { + "Stockholders Equity": [4_000_000.0, 4_000_000.0, 4_000_000.0, 4_000_000.0], + "Total Assets": [8_000_000.0, 8_000_000.0, 8_000_000.0, 8_000_000.0], + "Long Term Debt And Capital Lease Obligation": [2_000_000.0, 2_000_000.0, 2_000_000.0, 2_000_000.0], + "Cash And Cash Equivalents": [1_000_000.0, 1_000_000.0, 1_000_000.0, 1_000_000.0], + "Ordinary Shares Number": [1_000_000.0, 1_000_000.0, 1_000_000.0, 1_000_000.0], + } + ), + ) + monkeypatch.setattr(data_service, "get_cash_flow", lambda symbol, quarterly=False: pd.DataFrame()) + monkeypatch.setattr(data_service, "get_shares_outstanding", lambda symbol: None) + monkeypatch.setattr( + data_service, + "get_price_history", + lambda symbol, period="5y": [ + {"date": "2024-09-30", "close": 10.0}, + {"date": "2023-09-30", "close": 10.0}, + {"date": "2022-09-30", "close": 10.0}, + {"date": "2021-09-30", "close": 10.0}, + ], + ) + + result = data_service.compute_historical_ratios("AAPL") + + assert result["ev_to_ebitda"] == [5.5, 5.5, 5.5, 5.5] + + def test_compute_historical_ratios_empty_income(monkeypatch) -> None: clear_service_caches() monkeypatch.setattr(data_service, "get_income_statement", lambda symbol, quarterly=False: pd.DataFrame()) -- cgit v1.3-2-g0d8e