"""账号价值投影刷新与灰度回退测试。""" from __future__ import annotations from datetime import UTC, datetime from web.backend.models import Account from web.backend.services.account_value import ( refresh_account_value_tiers, value_tier_expr, ) def _account(name: str, **overrides) -> Account: values = { "username": name, "password": "p", "email": f"{name}@example.com", "email_password": "ep", } values.update(overrides) return Account(**values) def test_value_projection_refreshes_only_changed_accounts(db): now = datetime.now(UTC) accounts = [ _account( "tier-one", high_value_bairimeng_status="available", high_value_mengtu_status="available", high_value_checked_at=now, ), _account("tier-two-points", points=400), _account("tier-three-gold", gold_balance=1), _account("tier-four-queried", overview_queried_at=now), _account("tier-five-new"), ] db.add_all(accounts) db.commit() changed = refresh_account_value_tiers(db, now=now, batch_size=2) assert changed == 5 assert [ db.get(Account, account.id).value_tier for account in accounts ] == [1, 2, 3, 4, 5] assert refresh_account_value_tiers(db, now=now, batch_size=2) == 0 def test_value_tier_expression_falls_back_for_unprojected_account(db): now = datetime.now(UTC) account = _account( "fallback", points=400, high_value_checked_at=now, ) db.add(account) db.commit() tier = ( db.query(value_tier_expr(now=now)) .filter(Account.id == account.id) .scalar() ) assert account.value_tier is None assert tier == 2