Files
live-hub-py/tests/test_account_supply_profile.py
T
yml f15ae99012 feat(accounts): 增加账号供给画像投影
- 新增 account_supply_profiles 表与迁移
- 按租约/生命周期/价值/能力计算角色、质量分、冗余
- 新增 account_supply_profile_projection 作业,默认开启
- 账号列表展示供给画像列
- 补充画像角色/评分/游标/作业测试
2026-11-24 16:16:18 +08:00

162 lines
4.8 KiB
Python

"""账号供给画像投影:角色/质量分/冗余/游标刷新测试。"""
from __future__ import annotations
from datetime import UTC, datetime, timedelta
from web.backend.models import (
Account,
AccountCommodityState,
AccountLease,
AccountSupplyProfile,
AutomationJob,
User,
)
from web.backend.services.account_supply_profile_service import (
refresh_account_supply_profiles,
)
from web.backend.services.automation.jobs import (
account_supply_profile_projection as job,
)
NOW = datetime.now(UTC).replace(microsecond=0)
def _account(name: str, **overrides) -> Account:
values = {
"username": name,
"password": "p",
"email": f"{name}@example.com",
"email_password": "ep",
"status": "active",
"auto_managed": True,
"cookie": "acf_uid=1",
"ck_check_status": "valid",
"ck_checked_at": NOW,
"cookie_updated_at": NOW,
"value_tier": 3,
}
values.update(overrides)
return Account(**values)
def _state(account: Account, commodity_id: str = "C") -> AccountCommodityState:
return AccountCommodityState(
account_id=account.id,
platform="douyu",
scope="elite",
commodity_id=commodity_id,
account_eligibility="eligible",
offer_state="active",
stock_state="in_stock",
availability="available",
observed_at=NOW,
valid_until=NOW + timedelta(hours=1),
available_from=NOW - timedelta(hours=1),
available_until=NOW + timedelta(hours=1),
)
def _user(db) -> User:
user = User(
username="profile-admin",
password_hash="h",
role="super_admin",
is_active=True,
)
db.add(user)
db.commit()
return user
def test_profile_assigns_hard_roles_and_scores(db):
user = _user(db)
protected = _account(
"profile-protected",
value_tier=1,
high_value_bairimeng_status="available",
high_value_mengtu_status="available",
high_value_checked_at=NOW,
overview_queried_at=NOW,
)
onboarding = _account("profile-onboarding", cookie="", ck_check_status="")
repair = _account("profile-repair", ck_check_status="invalid")
ordinary = _account("profile-ordinary", value_tier=3)
db.add_all([protected, onboarding, repair, ordinary])
db.flush()
db.add_all(
[
AccountLease(
account_id=protected.id,
holder_user_id=user.id,
scope="elite",
purpose="platform_fulfillment",
status="active",
),
_state(protected, "C1"),
_state(ordinary, "C1"),
]
)
db.commit()
result = refresh_account_supply_profiles(db, limit=100, now=NOW)
db.commit()
profiles = {
int(row.account_id): row for row in db.query(AccountSupplyProfile).all()
}
assert result["processed"] == 4
assert profiles[protected.id].supply_role == "fulfillment_protected"
assert profiles[protected.id].redundancy_class == "protected"
assert profiles[protected.id].quality_score > 0
assert profiles[protected.id].ck_remaining_hours == 168
assert profiles[onboarding.id].supply_role == "onboarding"
assert profiles[onboarding.id].lifecycle_state == "no_cookie"
assert profiles[onboarding.id].recoverable is True
assert profiles[repair.id].supply_role == "repair"
assert profiles[repair.id].recoverable is True
assert profiles[ordinary.id].supply_role == "supply_ordinary"
assert profiles[ordinary.id].capability_count == 1
assert profiles[ordinary.id].onboarding_status == "partial"
def test_profile_cursor_moves_forward_and_wraps(db):
accounts = [_account(f"profile-cursor-{index}") for index in range(5)]
db.add_all(accounts)
db.commit()
first = refresh_account_supply_profiles(db, limit=2, now=NOW)
db.commit()
second = refresh_account_supply_profiles(
db, limit=2, after_account_id=first["nextId"], now=NOW
)
db.commit()
assert first["processed"] == 2
assert first["hasMore"] is True
assert second["nextId"] > first["nextId"]
def test_projection_job_persists_cursor(db):
_user(db)
accounts = [_account(f"profile-job-{index}") for index in range(3)]
db.add_all(accounts)
db.commit()
automation_job = AutomationJob(
key=job.JOB_KEY,
title="账号供给画像刷新",
enabled=True,
batch_size=2,
concurrency=1,
config={},
)
db.add(automation_job)
db.commit()
result = job.run_account_supply_profile_projection(db, automation_job)
assert result["scanned"] == 2
assert result["detail"]["hasMore"] is True
assert automation_job.config["last_account_id"] == result["detail"]["nextId"]
assert db.query(AccountSupplyProfile).count() == 2