优化(账号分配): 重构智能候选池与定量发放
按生命周期、租约、预售台账、积分、宝典和皮肤能力筛选账号池;支持指定客服定量发放、手工分配撤回,并为后续自助领取复用同一发放链路。
This commit is contained in:
@@ -18,14 +18,22 @@ import pytest
|
||||
from fastapi import HTTPException
|
||||
|
||||
from tests.support.db import get_test_engine, make_test_session
|
||||
from web.backend.models import Account, User
|
||||
from web.backend.models import (
|
||||
Account,
|
||||
AccountCommodityState,
|
||||
AccountLease,
|
||||
DouyuExternalOrder,
|
||||
User,
|
||||
)
|
||||
from web.backend.routers.accounts import (
|
||||
assign_account,
|
||||
assignment_options,
|
||||
assignments_summary,
|
||||
batch_assign_accounts,
|
||||
dispatch_assignments,
|
||||
list_accounts,
|
||||
)
|
||||
from web.backend.schemas import AccountAssign, BatchAssign
|
||||
from web.backend.schemas import AccountAssign, AssignmentDispatch, BatchAssign
|
||||
|
||||
|
||||
class _Base:
|
||||
@@ -197,6 +205,139 @@ class TestAssignablePool(_Base):
|
||||
assert "有效 CK 检测" in exc_info.value.detail
|
||||
|
||||
|
||||
class TestAssignmentPools(_Base):
|
||||
def test_occupancy_filter_excludes_lease_and_collab(self):
|
||||
free = self.make_account("pool-free")
|
||||
leased = self.make_account("pool-leased")
|
||||
collab = self.make_account("pool-collab")
|
||||
self.session.add(
|
||||
AccountLease(
|
||||
account_id=leased.id,
|
||||
holder_user_id=self.admin.id,
|
||||
purpose="platform_fulfillment",
|
||||
status="active",
|
||||
)
|
||||
)
|
||||
self.session.add(
|
||||
DouyuExternalOrder(
|
||||
order_no="ASSIGN-POOL-COLLAB",
|
||||
account_id=collab.id,
|
||||
account_username=collab.username,
|
||||
status="incomplete",
|
||||
)
|
||||
)
|
||||
self.session.commit()
|
||||
|
||||
assert self.names(
|
||||
assign_status="unassigned",
|
||||
assignable_only=True,
|
||||
occupancy="free",
|
||||
) == [free.username]
|
||||
assert set(
|
||||
self.names(
|
||||
assign_status="unassigned",
|
||||
assignable_only=True,
|
||||
occupancy="no_collab",
|
||||
)
|
||||
) == {free.username, leased.username}
|
||||
|
||||
def test_commodity_filter_uses_fresh_eligible_capability(self):
|
||||
match = self.make_account("pool-skin-match")
|
||||
stale = self.make_account("pool-skin-stale")
|
||||
self.session.add_all(
|
||||
[
|
||||
AccountCommodityState(
|
||||
account_id=match.id,
|
||||
platform="douyu",
|
||||
scope="elite",
|
||||
commodity_id="skin-001",
|
||||
commodity_name="测试皮肤",
|
||||
availability="available",
|
||||
account_eligibility="eligible",
|
||||
offer_state="active",
|
||||
valid_until=self.now + timedelta(hours=1),
|
||||
),
|
||||
AccountCommodityState(
|
||||
account_id=stale.id,
|
||||
platform="douyu",
|
||||
scope="elite",
|
||||
commodity_id="skin-001",
|
||||
commodity_name="测试皮肤",
|
||||
availability="available",
|
||||
account_eligibility="eligible",
|
||||
offer_state="active",
|
||||
valid_until=self.now - timedelta(hours=1),
|
||||
),
|
||||
]
|
||||
)
|
||||
self.session.commit()
|
||||
|
||||
assert self.names(
|
||||
assign_status="unassigned",
|
||||
assignable_only=True,
|
||||
commodity_id="skin-001",
|
||||
) == [match.username]
|
||||
|
||||
def test_dispatch_selects_requested_quantity_from_free_pool(self):
|
||||
free = [self.make_account(f"dispatch-free-{index}") for index in range(3)]
|
||||
leased = self.make_account("dispatch-leased")
|
||||
self.session.add(
|
||||
AccountLease(
|
||||
account_id=leased.id,
|
||||
holder_user_id=self.admin.id,
|
||||
purpose="platform_fulfillment",
|
||||
status="active",
|
||||
)
|
||||
)
|
||||
self.session.commit()
|
||||
|
||||
result = dispatch_assignments(
|
||||
AssignmentDispatch(
|
||||
assigned_to=self.support.id,
|
||||
quantity=2,
|
||||
occupancy="free",
|
||||
),
|
||||
db=self.session,
|
||||
current=self.admin,
|
||||
)
|
||||
|
||||
assert result["count"] == 2
|
||||
assigned = [
|
||||
account
|
||||
for account in free
|
||||
if self.session.get(Account, account.id).assigned_to == self.support.id
|
||||
]
|
||||
self.session.refresh(leased)
|
||||
assert len(assigned) == 2
|
||||
assert leased.assigned_to is None
|
||||
|
||||
def test_assignment_options_exposes_skin_counts(self):
|
||||
account = self.make_account("options-skin")
|
||||
self.session.add(
|
||||
AccountCommodityState(
|
||||
account_id=account.id,
|
||||
platform="douyu",
|
||||
scope="elite",
|
||||
commodity_id="skin-options",
|
||||
commodity_name="选项皮肤",
|
||||
availability="available",
|
||||
account_eligibility="eligible",
|
||||
offer_state="active",
|
||||
valid_until=self.now + timedelta(hours=1),
|
||||
)
|
||||
)
|
||||
self.session.commit()
|
||||
|
||||
result = assignment_options(db=self.session, current=self.admin)
|
||||
|
||||
assert result["occupancy"]["free"] == 1
|
||||
skin = next(
|
||||
item for item in result["skins"] if item["commodity_id"] == "skin-options"
|
||||
)
|
||||
assert skin["commodity_name"] == "选项皮肤"
|
||||
assert skin["account_count"] == 1
|
||||
|
||||
|
||||
class TestAllMatchingAssign(_Base):
|
||||
def test_assigns_every_matching_unassigned(self):
|
||||
free = [self.make_account(f"mf-{i}") for i in range(3)]
|
||||
|
||||
Reference in New Issue
Block a user