feat(fulfillment): 展示可分配账号库存
This commit is contained in:
@@ -188,6 +188,150 @@ class _Base:
|
||||
return row
|
||||
|
||||
|
||||
class TestAccountAvailability(_Base):
|
||||
COMMODITIES = (
|
||||
"bundle-commodity-a",
|
||||
"bundle-commodity-b",
|
||||
"bundle-commodity-c",
|
||||
)
|
||||
|
||||
def setup_method(self):
|
||||
super().setup_method()
|
||||
setting = self.session.get(PlatformFulfillmentSetting, 1)
|
||||
setting.allocation_policy_json = {
|
||||
"sameRoleReuseEnabled": True,
|
||||
"preserveBundlePool": True,
|
||||
"bundleRules": [
|
||||
{
|
||||
"id": "availability-bundle",
|
||||
"name": "三商品全资格",
|
||||
"enabled": True,
|
||||
"mode": "prefer_all",
|
||||
"commodityIds": list(self.COMMODITIES),
|
||||
}
|
||||
],
|
||||
}
|
||||
self.session.commit()
|
||||
|
||||
def commodity_account(
|
||||
self,
|
||||
name: str,
|
||||
commodity_ids: tuple[str, ...],
|
||||
*,
|
||||
role_available_at: datetime | None = None,
|
||||
) -> Account:
|
||||
account = Account(
|
||||
username=name,
|
||||
password="p",
|
||||
email=f"{name}@example.com",
|
||||
email_password="ep",
|
||||
status="active",
|
||||
ck_check_status="valid",
|
||||
cookie="PHPSESSID=x",
|
||||
elite_opened=True,
|
||||
points=3000,
|
||||
auto_managed=True,
|
||||
role_change_available_at=role_available_at,
|
||||
)
|
||||
self.session.add(account)
|
||||
self.session.flush()
|
||||
for commodity_id in commodity_ids:
|
||||
self.session.add(
|
||||
AccountCommodityState(
|
||||
account_id=account.id,
|
||||
platform="douyu",
|
||||
scope="elite",
|
||||
commodity_id=commodity_id,
|
||||
commodity_name=commodity_id,
|
||||
account_eligibility="eligible",
|
||||
offer_state="active",
|
||||
stock_state="in_stock",
|
||||
availability="available",
|
||||
observed_at=NOW,
|
||||
valid_until=NOW + timedelta(hours=6),
|
||||
source="test_fixture",
|
||||
)
|
||||
)
|
||||
self.session.commit()
|
||||
return account
|
||||
|
||||
def test_counts_allocatable_bundle_and_unique_accounts(self):
|
||||
self.commodity_account("availability-full", self.COMMODITIES)
|
||||
leased = self.commodity_account("availability-leased", self.COMMODITIES)
|
||||
self.commodity_account(
|
||||
"availability-role-cooldown",
|
||||
self.COMMODITIES,
|
||||
role_available_at=NOW + timedelta(days=1),
|
||||
)
|
||||
self.commodity_account(
|
||||
"availability-single",
|
||||
(self.COMMODITIES[0],),
|
||||
)
|
||||
exchanged = self.commodity_account(
|
||||
"availability-exchanged",
|
||||
(self.COMMODITIES[0],),
|
||||
)
|
||||
self.session.add(
|
||||
AccountLease(
|
||||
account_id=leased.id,
|
||||
holder_user_id=self.admin_id,
|
||||
purpose="platform_fulfillment",
|
||||
status="active",
|
||||
)
|
||||
)
|
||||
self.session.add(
|
||||
AccountEvent(
|
||||
account_id=exchanged.id,
|
||||
event_type="exchange",
|
||||
category="business",
|
||||
occurred_at=NOW - timedelta(days=10),
|
||||
subject_type="commodity",
|
||||
subject_key=self.COMMODITIES[0],
|
||||
source="test_fixture",
|
||||
)
|
||||
)
|
||||
self.session.commit()
|
||||
|
||||
result = service.get_account_availability(
|
||||
self.session,
|
||||
commodity_ids=list(self.COMMODITIES),
|
||||
now=NOW,
|
||||
)
|
||||
|
||||
assert result["uniqueAllocatable"] == 2
|
||||
for commodity_id in self.COMMODITIES:
|
||||
item = result["items"][commodity_id]
|
||||
assert item["bundleCommodityCount"] == 3
|
||||
assert item["fullBundleAllocatable"] == 1
|
||||
assert item["roleCooldown"] == 1
|
||||
assert item["leased"] == 1
|
||||
|
||||
assert result["items"][self.COMMODITIES[0]]["allocatable"] == 2
|
||||
assert result["items"][self.COMMODITIES[1]]["allocatable"] == 1
|
||||
assert result["items"][self.COMMODITIES[2]]["allocatable"] == 1
|
||||
|
||||
def test_availability_permission_excludes_support(self):
|
||||
checker = require_permission("platform_fulfillment:admin")
|
||||
support = User(
|
||||
username="availability-support",
|
||||
password_hash="h",
|
||||
role="support",
|
||||
is_active=True,
|
||||
)
|
||||
operator = User(
|
||||
username="availability-operator",
|
||||
password_hash="h",
|
||||
role="operation",
|
||||
is_active=True,
|
||||
)
|
||||
|
||||
with pytest.raises(HTTPException) as exc_info:
|
||||
checker(current_user=support)
|
||||
|
||||
assert exc_info.value.status_code == 403
|
||||
assert checker(current_user=operator) is operator
|
||||
|
||||
|
||||
class TestWorkbenchReadPath(_Base):
|
||||
def mapped_task(self, mapping: PlatformCommodityMapping, index: int):
|
||||
task = self.task(mapped=False)
|
||||
|
||||
Reference in New Issue
Block a user