summaryrefslogtreecommitdiff
path: root/backend/tests/test_api.py
diff options
context:
space:
mode:
authorTyler Hoang <tyler@tylerhoang.xyz>2026-05-18 02:34:46 -0700
committerTyler Hoang <tyler@tylerhoang.xyz>2026-05-18 02:34:46 -0700
commit38bef98ab1964bc9365d17c4a5ca17cdbd32673b (patch)
treeab42296c9bb6263502dc92cf65474e19b05be368 /backend/tests/test_api.py
parentb24b745dbb047674c1ac05f2531cab83f45c2291 (diff)
fix: correct historical ratios share and debt inputs
Diffstat (limited to 'backend/tests/test_api.py')
-rw-r--r--backend/tests/test_api.py96
1 files changed, 96 insertions, 0 deletions
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())