Files
live-hub-py/tests/test_platform_fulfillment_auto_allocator.py
T

83 lines
2.7 KiB
Python

"""直播账号履约自动选号 worker。"""
from __future__ import annotations
from datetime import UTC, datetime, timedelta
from unittest.mock import patch
from web.backend.models import (
PlatformFulfillmentSetting,
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")
db.add(
PlatformFulfillmentSetting(
id=1,
lease_duration_minutes=None,
order_match_started_at=datetime.now(UTC) - timedelta(days=1),
)
)
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",
paid_at=datetime.now(UTC),
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",
paid_at=datetime.now(UTC),
updated_at=datetime.now(UTC) - timedelta(minutes=2),
)
old_ready = PlatformFulfillmentTask(
source_system="order_site",
source_order_id="3",
source_order_item_id="3",
platform_order_id="AUTO-OLD",
internal_sku_key="skin",
commodity_id="commodity-1",
execution_status="ready",
binding_status="not_started",
exchange_status="not_started",
paid_at=datetime.now(UTC) - timedelta(days=2),
created_at=datetime.now(UTC),
updated_at=datetime.now(UTC) - timedelta(days=2),
)
db.add_all([actor, ready, mapped_pending, old_ready])
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}