feat(savings): 省钱战绩卡比价口径 + 关停 savings demo 兜底
- 新增 GET /api/v1/compare/stats(comparison_record: status=success 计数 + saved_amount_cents 求和),供「我的」页省钱战绩卡比价口径(完成比价 + 累计发现可省),不要求下单 - savings.py 停用 demo seeder(ensure_seeded 等移除),只计真实 source='compare';简化 beat_percent 为 compare-only - 测试: compare/stats 用例 + savings/welfare 改纯真实语义 - 文档: 新增 compare-stats.md;更新 savings-battle.md / savings_record.md / comparison_record.md / API README Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -193,11 +193,38 @@ def test_detail_cross_user_404(client) -> None:
|
||||
assert r.status_code == 404
|
||||
|
||||
|
||||
def test_stats_compare_count_and_saved(client) -> None:
|
||||
"""比价口径战绩:完成比价数 + 累计发现可省(各成功比价 saved 之和)。"""
|
||||
token = _login(client, "13800002011")
|
||||
# 新用户:无记录 → 全 0(配合「我的」页未比价锁定态)
|
||||
s0 = client.get("/api/v1/compare/stats", headers=_auth(token)).json()
|
||||
assert s0["compare_count"] == 0
|
||||
assert s0["discovered_saved_cents"] == 0
|
||||
|
||||
# 报 2 条成功比价(各省 128.50−123.50=5.00 元=500 分)
|
||||
client.post("/api/v1/compare/record", json=_food_payload("stat-1"), headers=_auth(token))
|
||||
client.post("/api/v1/compare/record", json=_food_payload("stat-2"), headers=_auth(token))
|
||||
s = client.get("/api/v1/compare/stats", headers=_auth(token)).json()
|
||||
assert s["compare_count"] == 2
|
||||
assert s["discovered_saved_cents"] == 1000
|
||||
|
||||
# 失败帧(只有源平台)不计入成功比价数
|
||||
fail = _food_payload("stat-fail")
|
||||
fail["comparison_results"] = [
|
||||
{"platform_id": "taobao_flash", "platform_name": "淘宝闪购",
|
||||
"package": "com.taobao.taobao", "price": 128.50, "is_source": True, "rank": 1},
|
||||
]
|
||||
client.post("/api/v1/compare/record", json=fail, headers=_auth(token))
|
||||
s2 = client.get("/api/v1/compare/stats", headers=_auth(token)).json()
|
||||
assert s2["compare_count"] == 2 # 仍 2(failed 不计)
|
||||
|
||||
|
||||
def test_requires_auth(client) -> None:
|
||||
"""不带 token 统一 401。"""
|
||||
assert client.post("/api/v1/compare/record", json={"trace_id": "t"}).status_code == 401
|
||||
assert client.get("/api/v1/compare/records").status_code == 401
|
||||
assert client.get("/api/v1/compare/records/1").status_code == 401
|
||||
assert client.get("/api/v1/compare/stats").status_code == 401
|
||||
|
||||
|
||||
def test_trace_id_required(client) -> None:
|
||||
|
||||
Reference in New Issue
Block a user