fix(fulfillment): 主动补全有效 CK 商品能力缓存
This commit is contained in:
@@ -92,6 +92,16 @@ def test_selects_stale_and_never_scanned_demand_accounts(db):
|
||||
_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(
|
||||
AccountSyncState(
|
||||
account_id=fresh.id,
|
||||
platform="douyu",
|
||||
domain="storehome",
|
||||
status="success",
|
||||
last_success_at=NOW,
|
||||
valid_until=NOW + timedelta(hours=1),
|
||||
)
|
||||
)
|
||||
db.add(
|
||||
AccountLease(
|
||||
account_id=leased.id,
|
||||
@@ -115,9 +125,24 @@ def test_selects_stale_and_never_scanned_demand_accounts(db):
|
||||
assert [row.id for row in selected] == [stale.id, other.id, leased.id]
|
||||
|
||||
|
||||
def test_no_pending_task_means_no_refresh(db):
|
||||
stale = _account(db, "cap-no-demand")
|
||||
def test_no_pending_task_still_refreshes_stale_ck_accounts(db):
|
||||
stale = _account(db, "cap-no-demand-stale")
|
||||
fresh = _account(db, "cap-no-demand-fresh")
|
||||
invalid = _account(db, "cap-no-demand-invalid")
|
||||
_state(db, stale, valid_until=NOW - timedelta(minutes=1))
|
||||
_state(db, fresh, valid_until=NOW - timedelta(minutes=1))
|
||||
_state(db, invalid, valid_until=NOW - timedelta(minutes=1))
|
||||
invalid.ck_check_status = "invalid"
|
||||
db.add(
|
||||
AccountSyncState(
|
||||
account_id=fresh.id,
|
||||
platform="douyu",
|
||||
domain="storehome",
|
||||
status="success",
|
||||
last_success_at=NOW,
|
||||
valid_until=NOW + timedelta(hours=1),
|
||||
)
|
||||
)
|
||||
db.commit()
|
||||
automation_job = AutomationJob(
|
||||
key=job.JOB_KEY,
|
||||
@@ -127,7 +152,46 @@ def test_no_pending_task_means_no_refresh(db):
|
||||
concurrency=3,
|
||||
)
|
||||
|
||||
assert job.build_select_query(db, automation_job, NOW).all() == []
|
||||
selected = job.build_select_query(db, automation_job, NOW).all()
|
||||
|
||||
assert [row.id for row in selected] == [stale.id]
|
||||
|
||||
|
||||
def test_demand_refresh_runs_before_global_backfill(db):
|
||||
db.add(
|
||||
PlatformFulfillmentTask(
|
||||
source_system="order_site",
|
||||
source_order_id="demand-first",
|
||||
source_order_item_id="demand-first",
|
||||
internal_sku_key="skin",
|
||||
commodity_id="C",
|
||||
execution_status="ready",
|
||||
binding_status="not_started",
|
||||
exchange_status="not_started",
|
||||
paid_at=NOW,
|
||||
)
|
||||
)
|
||||
demand = _account(db, "cap-demand-first")
|
||||
backfill = _account(db, "cap-backfill-second")
|
||||
_state(db, demand, commodity_id="C", valid_until=NOW - timedelta(minutes=1))
|
||||
_state(
|
||||
db,
|
||||
backfill,
|
||||
commodity_id="OTHER",
|
||||
valid_until=NOW - timedelta(minutes=1),
|
||||
)
|
||||
db.commit()
|
||||
automation_job = AutomationJob(
|
||||
key=job.JOB_KEY,
|
||||
title="履约商品能力补充刷新",
|
||||
interval_seconds=3600,
|
||||
batch_size=2,
|
||||
concurrency=3,
|
||||
)
|
||||
|
||||
selected = job.build_select_query(db, automation_job, NOW).all()
|
||||
|
||||
assert [row.id for row in selected] == [demand.id, backfill.id]
|
||||
|
||||
|
||||
def test_refresh_ignores_tasks_before_match_cutoff(db):
|
||||
@@ -239,6 +303,34 @@ def test_run_enqueues_stale_accounts_without_external_query(db):
|
||||
assert work.priority == 10
|
||||
|
||||
|
||||
def test_run_without_pending_tasks_enqueues_global_backfill(db):
|
||||
stale = _account(db, "cap-run-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,
|
||||
)
|
||||
|
||||
result = job.run_account_capability_refresh(db, automation_job)
|
||||
|
||||
work = (
|
||||
db.query(AccountWorkItem)
|
||||
.filter_by(account_id=stale.id, work_type="storehome_sync")
|
||||
.one()
|
||||
)
|
||||
assert result["scanned"] == 1
|
||||
assert result["success"] == 1
|
||||
assert result["detail"]["demand_refresh"] == 0
|
||||
assert result["detail"]["backfill_refresh"] == 1
|
||||
assert work.status == "pending"
|
||||
assert work.priority == 30
|
||||
assert work.reason == "账号商品能力补全"
|
||||
|
||||
|
||||
def test_refresh_skips_accounts_with_active_sync_work(db):
|
||||
db.add(
|
||||
PlatformFulfillmentTask(
|
||||
|
||||
@@ -27,9 +27,11 @@ def test_list_jobs_registers_default_jobs_when_api_process_registry_is_empty(db)
|
||||
|
||||
result = automation_router.list_jobs(db=db, current=admin)
|
||||
|
||||
keys = {job["key"] for job in result["jobs"]}
|
||||
jobs = {job["key"]: job for job in result["jobs"]}
|
||||
keys = set(jobs)
|
||||
assert "account_sync_queue" in keys
|
||||
assert "account_supply_replenishment" in keys
|
||||
assert jobs["account_sync_queue"]["batch_size"] == 40
|
||||
assert result["scheduler"]["enabled"] is not None
|
||||
|
||||
|
||||
|
||||
@@ -45,7 +45,7 @@ TESTS_DIR = Path(__file__).resolve().parent
|
||||
PROJECT_ROOT = TESTS_DIR.parent
|
||||
VERSIONS_DIR = PROJECT_ROOT / "web" / "backend" / "migrations" / "versions"
|
||||
|
||||
HEAD_REVISION = "20261128_0137"
|
||||
HEAD_REVISION = "20261128_0138"
|
||||
|
||||
# 迁移新增、但不声明在模型里的复合查询索引。
|
||||
EXTRA_INDEXES = (
|
||||
|
||||
Reference in New Issue
Block a user