124 lines
3.3 KiB
Python
124 lines
3.3 KiB
Python
"""履约需求驱动的账号商品能力刷新作业。"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from datetime import UTC, datetime, timedelta
|
|
|
|
from web.backend.models import (
|
|
Account,
|
|
AccountCommodityState,
|
|
AccountLease,
|
|
AutomationJob,
|
|
PlatformFulfillmentTask,
|
|
User,
|
|
)
|
|
from web.backend.services.automation.jobs import account_capability_refresh as job
|
|
|
|
NOW = datetime.now(UTC).replace(microsecond=0)
|
|
|
|
|
|
def _account(db, name: str) -> Account:
|
|
row = Account(
|
|
username=name,
|
|
password="p",
|
|
email=f"{name}@example.com",
|
|
email_password="ep",
|
|
cookie="acf_uid=1; acf_ccn=c",
|
|
ck_check_status="valid",
|
|
status="active",
|
|
auto_managed=True,
|
|
overview_queried_at=NOW - timedelta(days=1),
|
|
)
|
|
db.add(row)
|
|
db.flush()
|
|
return row
|
|
|
|
|
|
def _state(
|
|
db,
|
|
account: Account,
|
|
*,
|
|
commodity_id: str = "C",
|
|
valid_until: datetime | None = None,
|
|
) -> AccountCommodityState:
|
|
row = AccountCommodityState(
|
|
account_id=account.id,
|
|
platform="douyu",
|
|
scope="elite",
|
|
commodity_id=commodity_id,
|
|
availability="available",
|
|
observed_at=NOW - timedelta(hours=2),
|
|
valid_until=valid_until,
|
|
next_check_at=valid_until,
|
|
)
|
|
db.add(row)
|
|
return row
|
|
|
|
|
|
def test_selects_only_stale_demand_accounts(db):
|
|
user = User(
|
|
username="cap-refresh-admin",
|
|
password_hash="h",
|
|
role="super_admin",
|
|
is_active=True,
|
|
)
|
|
db.add(user)
|
|
db.flush()
|
|
db.add(
|
|
PlatformFulfillmentTask(
|
|
source_system="order_site",
|
|
source_order_id="1",
|
|
source_order_item_id="1",
|
|
internal_sku_key="skin",
|
|
commodity_id="C",
|
|
execution_status="ready",
|
|
binding_status="not_started",
|
|
exchange_status="not_started",
|
|
paid_at=NOW,
|
|
)
|
|
)
|
|
stale = _account(db, "cap-stale")
|
|
fresh = _account(db, "cap-fresh")
|
|
other = _account(db, "cap-other")
|
|
leased = _account(db, "cap-leased")
|
|
_state(db, stale, valid_until=NOW - timedelta(minutes=1))
|
|
_state(db, fresh, valid_until=NOW + timedelta(hours=1))
|
|
_state(db, other, commodity_id="OTHER", valid_until=NOW - timedelta(minutes=1))
|
|
_state(db, leased, valid_until=NOW - timedelta(minutes=1))
|
|
db.add(
|
|
AccountLease(
|
|
account_id=leased.id,
|
|
holder_user_id=user.id,
|
|
scope="elite",
|
|
purpose="platform_fulfillment",
|
|
status="active",
|
|
)
|
|
)
|
|
db.commit()
|
|
automation_job = AutomationJob(
|
|
key=job.JOB_KEY,
|
|
title="履约商品能力补充刷新",
|
|
interval_seconds=3600,
|
|
batch_size=10,
|
|
concurrency=3,
|
|
)
|
|
|
|
selected = job.build_select_query(db, automation_job, NOW).all()
|
|
|
|
assert [row.id for row in selected] == [stale.id]
|
|
|
|
|
|
def test_no_pending_task_means_no_refresh(db):
|
|
stale = _account(db, "cap-no-demand")
|
|
_state(db, stale, valid_until=NOW - timedelta(minutes=1))
|
|
db.commit()
|
|
automation_job = AutomationJob(
|
|
key=job.JOB_KEY,
|
|
title="履约商品能力补充刷新",
|
|
interval_seconds=3600,
|
|
batch_size=10,
|
|
concurrency=3,
|
|
)
|
|
|
|
assert job.build_select_query(db, automation_job, NOW).all() == []
|