57 lines
1.9 KiB
Python
57 lines
1.9 KiB
Python
"""直播账号履约自动选号 worker。"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from datetime import UTC, datetime, timedelta
|
|
from unittest.mock import patch
|
|
|
|
from web.backend.models import PlatformFulfillmentTask, User
|
|
from web.backend.services.platform_fulfillment import auto_allocator
|
|
|
|
|
|
def test_auto_allocator_only_scans_ready_unassigned_tasks(db):
|
|
actor = User(username="auto-alloc-actor", password_hash="h", role="super_admin")
|
|
ready = PlatformFulfillmentTask(
|
|
source_system="order_site",
|
|
source_order_id="1",
|
|
source_order_item_id="1",
|
|
platform_order_id="AUTO-1",
|
|
internal_sku_key="skin",
|
|
commodity_id="commodity-1",
|
|
execution_status="ready",
|
|
binding_status="not_started",
|
|
exchange_status="not_started",
|
|
updated_at=datetime.now(UTC) - timedelta(minutes=2),
|
|
)
|
|
mapped_pending = PlatformFulfillmentTask(
|
|
source_system="order_site",
|
|
source_order_id="2",
|
|
source_order_item_id="2",
|
|
platform_order_id="AUTO-2",
|
|
internal_sku_key="skin",
|
|
commodity_id="commodity-1",
|
|
execution_status="pending_mapping",
|
|
binding_status="not_started",
|
|
exchange_status="not_started",
|
|
updated_at=datetime.now(UTC) - timedelta(minutes=2),
|
|
)
|
|
db.add_all([actor, ready, mapped_pending])
|
|
db.commit()
|
|
|
|
calls: list[int] = []
|
|
with (
|
|
patch.object(auto_allocator.service, "_integration_actor", return_value=actor),
|
|
patch.object(
|
|
auto_allocator.service,
|
|
"allocate_task",
|
|
side_effect=lambda _db, *, task_id, current: (
|
|
calls.append(task_id)
|
|
or {"task": {"account": {"id": 9}}}
|
|
),
|
|
),
|
|
):
|
|
result = auto_allocator.run_auto_allocation_once(db, limit=10)
|
|
|
|
assert calls == [ready.id]
|
|
assert result == {"checked": 1, "allocated": 1, "failed": 0}
|