diff options
| author | Tyler Hoang <tyler@tylerhoang.xyz> | 2026-05-18 00:15:12 -0700 |
|---|---|---|
| committer | Tyler Hoang <tyler@tylerhoang.xyz> | 2026-05-18 00:15:12 -0700 |
| commit | 28f9aae05d8b46a3fe6ca3d5bbce34634604db05 (patch) | |
| tree | 171acf5d3cf5294f9b38d15686730dbb06486ec1 /backend | |
| parent | 1df80dc1f868b906c5540b66a211947c6886484d (diff) | |
feat: add /api/tickers/{symbol}/financials endpoint
Diffstat (limited to 'backend')
| -rw-r--r-- | backend/app/main.py | 7 | ||||
| -rw-r--r-- | backend/tests/test_api.py | 37 |
2 files changed, 43 insertions, 1 deletions
diff --git a/backend/app/main.py b/backend/app/main.py index fc98a5e..eb5fa40 100644 --- a/backend/app/main.py +++ b/backend/app/main.py @@ -9,7 +9,7 @@ from fastapi import FastAPI, HTTPException, Query, status from fastapi.middleware.cors import CORSMiddleware from app.db import watchlist -from app.schemas import HistoryPoint, MarketIndex, SearchResult, TickerOverview, WatchlistResponse +from app.schemas import FinancialsResponse, HistoryPoint, MarketIndex, SearchResult, TickerOverview, WatchlistResponse from app.services import data_service load_dotenv() @@ -65,6 +65,11 @@ def ticker_history(symbol: str, period: str = Query(default="1y", pattern="^(1m| return data_service.get_price_history(symbol, period=period) +@app.get("/api/tickers/{symbol}/financials", response_model=FinancialsResponse) +def ticker_financials(symbol: str, period: str = Query(default="annual", pattern="^(annual|quarterly)$")) -> dict: + return data_service.get_financials(symbol, period=period) + + @app.get("/api/watchlist", response_model=WatchlistResponse) def get_watchlist() -> dict: items = [] diff --git a/backend/tests/test_api.py b/backend/tests/test_api.py index f98b582..e5dcead 100644 --- a/backend/tests/test_api.py +++ b/backend/tests/test_api.py @@ -543,3 +543,40 @@ def test_get_financials_empty_statements(monkeypatch) -> None: assert result["income"]["rows"] == [] assert result["balance"]["columns"] == [] assert result["cash_flow"]["columns"] == [] + + +def test_financials_route_returns_structure(monkeypatch) -> None: + monkeypatch.setattr( + main.data_service, + "get_financials", + lambda symbol, period="annual": { + "period": "annual", + "income": {"columns": ["FY 2024", "TTM"], "rows": [ + {"label": "Total Revenue", "indent": 0, "is_total": True, + "is_section": False, "is_margin": False, "values": [391_000.0, 394_500.0]}, + ]}, + "balance": {"columns": [], "rows": []}, + "cash_flow": {"columns": [], "rows": []}, + }, + ) + result = main.ticker_financials("AAPL", period="annual") + assert result["period"] == "annual" + assert result["income"]["columns"][0] == "FY 2024" + assert result["income"]["rows"][0]["label"] == "Total Revenue" + + +def test_financials_route_period_param(monkeypatch) -> None: + captured: list[str] = [] + + def mock_get_financials(symbol, period="annual"): + captured.append(period) + return { + "period": period, + "income": {"columns": [], "rows": []}, + "balance": {"columns": [], "rows": []}, + "cash_flow": {"columns": [], "rows": []}, + } + + monkeypatch.setattr(main.data_service, "get_financials", mock_get_financials) + main.ticker_financials("AAPL", period="quarterly") + assert captured == ["quarterly"] |
