fix(P0-02): 积分榜改为追加快照(append-only) + available_at cutoff
去掉 uq_standings_league_season_team,改为 (league, season, team, available_at) 唯一; 每次采集 INSERT 新行(available_at=now),ON CONFLICT DO NOTHING,不覆盖旧行。 standings_slice(before):DISTINCT ON (team_id) WHERE available_at<=cutoff ORDER available_at DESC;before=None → cutoff=now()。 公开 list_standings 取每队最新可用快照(子查询 max available_at)。 迁移 0023 + 切片/路由/docs 同步;测试 test_p0_standings_cutoff(5/5)。 284 测试全绿。
This commit is contained in:
@@ -194,8 +194,8 @@ async def test_r2_standings_actually_upserts(monkeypatch):
|
||||
assert first.zone == "Champions League" # 优先取 label
|
||||
|
||||
|
||||
async def test_r2_standings_upsert_updates_existing(monkeypatch):
|
||||
"""行为测试: 已存在同 (league, season, team) 时应就地更新而非新增。"""
|
||||
async def test_r2_standings_append_new_row(monkeypatch):
|
||||
"""P0-02 行为测试: 每次采集 INSERT 新行(带 available_at),不更新旧行。"""
|
||||
import src.data.bzzoiro as bz
|
||||
from src.db.models import League, Standing
|
||||
|
||||
@@ -217,16 +217,20 @@ async def test_r2_standings_upsert_updates_existing(monkeypatch):
|
||||
existing = Standing(league_id=42, season="2025-2026", team_id=7, position=9)
|
||||
existing.points = 1
|
||||
|
||||
# 查询顺序: League → Team 预载(命中) → Standing 查询(命中已有行)
|
||||
db = _FakeDb(results=[_FakeResult([league]), _FakeResult([team]), _FakeResult([existing])])
|
||||
# 查询顺序: League(命中) → Team 预载(命中) → (P0-02 不再查询 Standing)
|
||||
db = _FakeDb(results=[_FakeResult([league]), _FakeResult([team])])
|
||||
|
||||
result = await bz.ingest_bzzoiro_standings(db, leagues=["EPL"])
|
||||
|
||||
assert result["total_upserted"] == 1
|
||||
assert existing.points == 30, "已有行应被就地更新"
|
||||
# P0-02: 追加快照——新增 Standing 行,旧行不被修改
|
||||
new_rows = [o for o in db.added if isinstance(o, Standing)]
|
||||
assert len(new_rows) == 1, "P0-02 应新增一条 Standing 行"
|
||||
assert new_rows[0].points == 30, "新行应承载新采集数据"
|
||||
assert new_rows[0].available_at is not None, "新行必须含 available_at"
|
||||
# 旧行未被修改(仍保持原值)
|
||||
assert existing.points == 1, "P0-02 旧行不应被覆盖"
|
||||
assert result["leagues"]["EPL"]["teams_created"] == 0
|
||||
# 不应新增 Standing(只有 league/team 层面的 add)
|
||||
assert not [o for o in db.added if isinstance(o, Standing)]
|
||||
|
||||
|
||||
def test_r2_source_contains_real_upsert_loop():
|
||||
@@ -237,7 +241,9 @@ def test_r2_source_contains_real_upsert_loop():
|
||||
assert "total_upserted" in src
|
||||
assert 'result["total_upserted"] +=' in src, "total_upserted 必须真的被累加"
|
||||
assert "Standing(" in src, "必须真的构造 Standing"
|
||||
assert "select(Standing)" in src, "必须查询已有快照以决定 insert/update"
|
||||
# P0-02: 追加快照——每次 INSERT 新行(带 available_at),不查询旧行做 upsert
|
||||
assert "available_at" in src, "P0-02 采集必须设置 available_at"
|
||||
assert "scalar_one_or_none" not in src, "P0-02 不应再按 (league, season, team) 做 upsert 查询"
|
||||
|
||||
|
||||
# ============================================================
|
||||
|
||||
Reference in New Issue
Block a user