Files
market_intel/tests/test_ops_summary_logic.py
dekun 270e114658 feat: show ops-map summaries as detailed period tables
Replace paragraph text with day-hit stats (days/days_hit/pct) so leverage and move charts list each hour with counts.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-08-05 17:12:23 +08:00

55 lines
1.8 KiB
Python

"""对齐前端 opsSummary 表规则的轻量逻辑测试。"""
def leverage_rows(buckets, min_leverage=100):
active = [b for b in buckets if b["n"] > 0]
def hit_rate(b):
if b.get("pct_days_hit") is not None:
return b["pct_days_hit"]
return b.get("pct_ge_min")
def days(b):
return b["days"] if "days" in b else (1 if b["n"] > 0 else 0)
def days_hit(b):
if "days_hit" in b:
return b["days_hit"]
return 1 if (b.get("mean") or 0) >= min_leverage and b["n"] > 0 else 0
rows = [
{
"label": b["label"],
"days": days(b),
"days_hit": days_hit(b),
"pct": hit_rate(b),
"mean": b.get("mean"),
"n": b["n"],
}
for b in active
]
rows.sort(
key=lambda r: (r["pct"] if r["pct"] is not None else -1, r["days_hit"], r["mean"] or 0),
reverse=True,
)
return rows
def test_week_table_uses_days_hit():
buckets = [
{"label": "09:00", "n": 10, "mean": 110, "pct_ge_min": 0.9, "days": 5, "days_hit": 2, "pct_days_hit": 0.4},
{"label": "14:00", "n": 10, "mean": 105, "pct_ge_min": 0.5, "days": 5, "days_hit": 4, "pct_days_hit": 0.8},
{"label": "21:00", "n": 2, "mean": 200, "pct_ge_min": 1.0, "days": 1, "days_hit": 1, "pct_days_hit": 1.0},
]
rows = leverage_rows(buckets)
assert [r["label"] for r in rows] == ["21:00", "14:00", "09:00"]
assert rows[1]["days"] == 5 and rows[1]["days_hit"] == 4
def test_day_keeps_small_n():
rows = leverage_rows(
[{"label": "09:00", "n": 1, "mean": 118, "pct_ge_min": 1.0, "days": 1, "days_hit": 1, "pct_days_hit": 1.0}]
)
assert len(rows) == 1
assert rows[0]["days"] == 1 and rows[0]["days_hit"] == 1