fire-planner/tests/test_api_networth.py
Viktor Barzin 4a0ef1faf6
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
fire-planner: filterable date range on the home-page history chart
User asked for a manual date range on the Dashboard chart instead of
the hard-coded 12-month window.

Backend (/networth/history):
- Read LIVE from wf_sync's daily_account_valuation JOIN accounts so the
  chart spans the broker's full daily series. The account_snapshot
  cache is only the latest snapshot — never had >2 daily points for
  charting. Falls back to the cache when wf_sync isn't wired (tests).
- Accept `from=YYYY-MM-DD` and `to=YYYY-MM-DD` query params. When `from`
  is set, the window is [from, to or today] (inclusive). Otherwise the
  legacy `days` look-back still applies. 422 when from > to.

Frontend:
- New HistoryRangePicker component: preset buttons (1m / 3m / 6m / 1y /
  3y / All) plus two date inputs for an explicit custom range.
- Dashboard wires the picker to the chart via react-query keyed on the
  selected range, so the chart re-fetches on change.

Tests:
- Renamed `respects_days_filter` → `respects_from_to_filter` and added
  inverted-window rejection. Old test asserted "days=1 returns 1 point"
  which only worked when 'today' was within the seed window — the new
  windowing is correct and explicit.

271 → 272 tests passing.
2026-05-28 09:04:58 +00:00

129 lines
4.6 KiB
Python

"""Tests for /networth and /networth/history."""
from __future__ import annotations
from collections.abc import AsyncIterator
from datetime import date
from decimal import Decimal
import pytest_asyncio
from httpx import ASGITransport, AsyncClient
from sqlalchemy.ext.asyncio import AsyncEngine, AsyncSession, async_sessionmaker
from fire_planner.api.dependencies import get_session
from fire_planner.app import app
from fire_planner.db import AccountSnapshot
@pytest_asyncio.fixture
async def client(engine: AsyncEngine,
session: AsyncSession) -> AsyncIterator[AsyncClient]:
factory = async_sessionmaker(engine, expire_on_commit=False)
async def _override() -> AsyncIterator[AsyncSession]:
async with factory() as s:
yield s
app.dependency_overrides[get_session] = _override
transport = ASGITransport(app=app)
async with AsyncClient(transport=transport, base_url="http://test") as ac:
yield ac
app.dependency_overrides.clear()
async def _seed_snapshots(session: AsyncSession) -> None:
rows = [
AccountSnapshot(
external_id="wealthfolio:isa:2026-04-23",
snapshot_date=date(2026, 4, 23),
account_id="isa",
account_name="ISA",
account_type="ISA",
currency="GBP",
market_value=Decimal("280000"),
market_value_gbp=Decimal("280000"),
),
AccountSnapshot(
external_id="wealthfolio:schwab:2026-04-23",
snapshot_date=date(2026, 4, 23),
account_id="schwab",
account_name="Schwab",
account_type="BROKERAGE",
currency="USD",
market_value=Decimal("780000"),
market_value_gbp=Decimal("615000"),
),
AccountSnapshot(
external_id="wealthfolio:isa:2026-04-25",
snapshot_date=date(2026, 4, 25),
account_id="isa",
account_name="ISA",
account_type="ISA",
currency="GBP",
market_value=Decimal("300000"),
market_value_gbp=Decimal("300000"),
),
AccountSnapshot(
external_id="wealthfolio:schwab:2026-04-25",
snapshot_date=date(2026, 4, 25),
account_id="schwab",
account_name="Schwab",
account_type="BROKERAGE",
currency="USD",
market_value=Decimal("800000"),
market_value_gbp=Decimal("640000"),
),
]
for r in rows:
session.add(r)
await session.commit()
async def test_get_networth_returns_latest(client: AsyncClient, session: AsyncSession) -> None:
await _seed_snapshots(session)
resp = await client.get("/networth")
assert resp.status_code == 200
body = resp.json()
assert body["snapshot_date"] == "2026-04-25"
assert Decimal(body["total_gbp"]) == Decimal("940000")
by_id = {a["account_id"]: a for a in body["accounts"]}
assert Decimal(by_id["isa"]["market_value_gbp"]) == Decimal("300000")
assert Decimal(by_id["schwab"]["market_value_gbp"]) == Decimal("640000")
async def test_get_networth_empty_when_no_snapshots(client: AsyncClient) -> None:
resp = await client.get("/networth")
assert resp.status_code == 200
body = resp.json()
assert body["accounts"] == []
assert Decimal(body["total_gbp"]) == Decimal("0")
async def test_networth_history_returns_per_date(client: AsyncClient,
session: AsyncSession) -> None:
await _seed_snapshots(session)
resp = await client.get("/networth/history")
assert resp.status_code == 200
points = resp.json()["points"]
assert len(points) == 2
by_date = {p["snapshot_date"]: p for p in points}
assert Decimal(by_date["2026-04-23"]["total_gbp"]) == Decimal("895000")
assert Decimal(by_date["2026-04-25"]["total_gbp"]) == Decimal("940000")
assert Decimal(by_date["2026-04-25"]["by_account"]["ISA"]) == Decimal("300000")
async def test_networth_history_respects_from_to_filter(
client: AsyncClient,
session: AsyncSession,
) -> None:
"""The `from`/`to` params return only points in [from, to] inclusive."""
await _seed_snapshots(session)
resp = await client.get("/networth/history?from=2026-04-25&to=2026-04-25")
assert resp.status_code == 200
points = resp.json()["points"]
assert len(points) == 1
assert points[0]["snapshot_date"] == "2026-04-25"
async def test_networth_history_rejects_inverted_window(client: AsyncClient) -> None:
resp = await client.get("/networth/history?from=2026-05-01&to=2026-04-01")
assert resp.status_code == 422