Files
worldquant-alpha-system/backend/tests/test_glb_pnl.py
T

56 lines
2.4 KiB
Python
Raw Normal View History

"""GLB display data uses raw caches without changing the correlation baseline."""
import pytest
from app.alphas import glb_pnl_series, pnl_points, upsert_alpha
from app.models import Pnl
from tests.conftest import alpha
COLUMNS = ["date", "pnl", "investability-constrained-pnl", "amer-pnl", "apac-pnl", "emea-pnl"]
RAW = {
"schema": {"properties": [{"name": name} for name in COLUMNS]},
"records": [["2025-01-02", 100, 80, 50, None, -20], ["2025-01-01", 0, 0, 0, 0, 0]],
}
@pytest.mark.parametrize("region", ["GLB", "USA"])
async def test_existing_raw_cache_exposes_glb_series_without_refresh(app, logged_in, region):
points = pnl_points(RAW)
async with app.state.runner.sessions.begin() as db:
await upsert_alpha(db, alpha("glb", settings={"region": region}))
db.add(Pnl(alpha_id="glb", raw=RAW, points=points))
response = await logged_in.get("/api/v1/alphas/glb/pnl")
assert response.status_code == 200
result = response.json()
assert result["cached"] and result["points"] == points
if region == "GLB":
assert [s["id"] for s in result["series"]] == COLUMNS[1:]
assert [s["points"][-1]["value"] for s in result["series"]] == [100, 80, 50, None, -20]
else:
assert result["series"] == []
def test_reordered_columns_and_incomplete_rows_preserve_gaps():
raw = {
"schema": {"properties": {"AMER-PNL": {}, "date": {}, "pnl": {}, "emea-pnl": {}}},
"records": [[12, "2025-01-01", 30, "NaN"], [20, "2025-01-02", 50]],
}
series = glb_pnl_series(raw, pnl_points(raw))
assert [s["id"] for s in series] == ["pnl", "amer-pnl", "emea-pnl"]
assert [p["value"] for p in series[1]["points"]] == [12, 20]
assert [p["value"] for p in series[2]["points"]] == [None, None]
def test_dictionary_records_and_legacy_cache():
raw = {"records": [{"timestamp": 1735689600000, "value": 5, "apac-pnl": -2}]}
series = glb_pnl_series(raw, pnl_points(raw))
assert series[1]["points"] == [{"date": "2025-01-01T00:00:00+00:00", "value": -2}]
assert glb_pnl_series({}, pnl_points(raw)) == series[:1]
async def test_uncached_glb_is_empty(app, logged_in):
async with app.state.runner.sessions.begin() as db:
await upsert_alpha(db, alpha("empty", settings={"region": "GLB"}))
result = (await logged_in.get("/api/v1/alphas/empty/pnl")).json()
assert result == {"cached": False, "points": [], "series": [], "fetched_at": None}