fix(douyu): 补全账号能力目录并回收失效租约
This commit is contained in:
@@ -8,6 +8,7 @@ from web.backend.models import (
|
||||
Account,
|
||||
AccountCommodityState,
|
||||
AccountLease,
|
||||
AccountSyncState,
|
||||
AccountWorkItem,
|
||||
AutomationJob,
|
||||
PlatformFulfillmentSetting,
|
||||
@@ -61,7 +62,7 @@ def _state(
|
||||
return row
|
||||
|
||||
|
||||
def test_selects_only_stale_demand_accounts(db):
|
||||
def test_selects_stale_and_never_scanned_demand_accounts(db):
|
||||
user = User(
|
||||
username="cap-refresh-admin",
|
||||
password_hash="h",
|
||||
@@ -111,7 +112,7 @@ def test_selects_only_stale_demand_accounts(db):
|
||||
|
||||
selected = job.build_select_query(db, automation_job, NOW).all()
|
||||
|
||||
assert [row.id for row in selected] == [stale.id]
|
||||
assert [row.id for row in selected] == [stale.id, other.id, leased.id]
|
||||
|
||||
|
||||
def test_no_pending_task_means_no_refresh(db):
|
||||
@@ -177,6 +178,16 @@ def test_refresh_ignores_tasks_before_match_cutoff(db):
|
||||
commodity_id="NEW",
|
||||
valid_until=NOW - timedelta(minutes=1),
|
||||
)
|
||||
db.add(
|
||||
AccountSyncState(
|
||||
account_id=old_account.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,
|
||||
@@ -225,7 +236,91 @@ def test_run_enqueues_stale_accounts_without_external_query(db):
|
||||
assert result["scanned"] == 1
|
||||
assert result["success"] == 1
|
||||
assert work.status == "pending"
|
||||
assert work.priority == 40
|
||||
assert work.priority == 10
|
||||
|
||||
|
||||
def test_refresh_skips_accounts_with_active_sync_work(db):
|
||||
db.add(
|
||||
PlatformFulfillmentTask(
|
||||
source_system="order_site",
|
||||
source_order_id="3",
|
||||
source_order_item_id="3",
|
||||
internal_sku_key="skin",
|
||||
commodity_id="C",
|
||||
execution_status="ready",
|
||||
binding_status="not_started",
|
||||
exchange_status="not_started",
|
||||
paid_at=NOW,
|
||||
)
|
||||
)
|
||||
active = _account(db, "cap-active-work")
|
||||
next_account = _account(db, "cap-next-work")
|
||||
_state(db, active, valid_until=NOW - timedelta(minutes=1))
|
||||
_state(db, next_account, valid_until=NOW - timedelta(minutes=1))
|
||||
db.add(
|
||||
AccountWorkItem(
|
||||
account_id=active.id,
|
||||
work_type="storehome_sync",
|
||||
priority=10,
|
||||
status="pending",
|
||||
due_at=NOW,
|
||||
dedupe_key=f"storehome_sync:{active.id}",
|
||||
)
|
||||
)
|
||||
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] == [next_account.id]
|
||||
|
||||
|
||||
def test_refresh_stops_at_queue_target(db, monkeypatch):
|
||||
monkeypatch.setenv("ACCOUNT_SYNC_QUEUE_TARGET_PENDING", "1")
|
||||
db.add(
|
||||
PlatformFulfillmentTask(
|
||||
source_system="order_site",
|
||||
source_order_id="4",
|
||||
source_order_item_id="4",
|
||||
internal_sku_key="skin",
|
||||
commodity_id="C",
|
||||
execution_status="ready",
|
||||
binding_status="not_started",
|
||||
exchange_status="not_started",
|
||||
paid_at=NOW,
|
||||
)
|
||||
)
|
||||
account = _account(db, "cap-queue-full")
|
||||
_state(db, account, valid_until=NOW - timedelta(minutes=1))
|
||||
db.add(
|
||||
AccountWorkItem(
|
||||
account_id=account.id,
|
||||
work_type="storehome_sync",
|
||||
priority=10,
|
||||
status="pending",
|
||||
due_at=NOW,
|
||||
dedupe_key=f"storehome_sync:{account.id}",
|
||||
)
|
||||
)
|
||||
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)
|
||||
|
||||
assert result["scanned"] == 0
|
||||
assert db.query(AccountWorkItem).count() == 1
|
||||
|
||||
|
||||
def test_default_enabled_job_is_created_enabled(db, monkeypatch):
|
||||
|
||||
Reference in New Issue
Block a user