功能:隔离五类测试业务链路

This commit is contained in:
焦龙言
2026-09-23 08:27:14 +08:00
parent 43fe17cc70
commit f5df2150d8
18 changed files with 1206 additions and 831 deletions
@@ -1,6 +1,7 @@
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
from module_business.domain.test_batch_scope import TestBatchScope, batch_scope_condition
from module_business.entity.do.master_do import BizPallet
from module_business.entity.do.operation_do import BizPalletEvent, BizPalletInventory
@@ -13,10 +14,14 @@ class InventoryDao:
cls,
db: AsyncSession,
pallet_id: int,
scope: TestBatchScope,
) -> BizPalletInventory | None:
result = await db.execute(
select(BizPalletInventory)
.where(BizPalletInventory.pallet_id == pallet_id)
.where(
BizPalletInventory.pallet_id == pallet_id,
batch_scope_condition(BizPalletInventory.test_batch_id, scope),
)
.with_for_update()
.execution_options(populate_existing=True)
)
@@ -27,6 +32,7 @@ class InventoryDao:
cls,
db: AsyncSession,
pallet_ids: list[int],
scope: TestBatchScope,
) -> list[BizPalletInventory]:
ordered_ids = sorted(set(pallet_ids))
if not ordered_ids:
@@ -34,7 +40,10 @@ class InventoryDao:
result = await db.execute(
select(BizPalletInventory)
.where(BizPalletInventory.pallet_id.in_(ordered_ids))
.where(
BizPalletInventory.pallet_id.in_(ordered_ids),
batch_scope_condition(BizPalletInventory.test_batch_id, scope),
)
.order_by(BizPalletInventory.pallet_id)
.with_for_update()
.execution_options(populate_existing=True)
@@ -46,8 +55,14 @@ class InventoryDao:
cls,
db: AsyncSession,
qr_token: str,
scope: TestBatchScope,
) -> BizPallet | None:
result = await db.execute(select(BizPallet).where(BizPallet.qr_token == qr_token))
result = await db.execute(
select(BizPallet).where(
BizPallet.qr_token == qr_token,
batch_scope_condition(BizPallet.test_batch_id, scope),
)
)
return result.scalar_one_or_none()
@classmethod
@@ -55,6 +70,7 @@ class InventoryDao:
cls,
db: AsyncSession,
pallet_ids: list[int],
scope: TestBatchScope,
) -> list[BizPallet]:
"""在调用方已锁库存后,按托盘 ID 升序锁定资产行。"""
ordered_ids = sorted(set(pallet_ids))
@@ -63,7 +79,10 @@ class InventoryDao:
result = await db.execute(
select(BizPallet)
.where(BizPallet.pallet_id.in_(ordered_ids))
.where(
BizPallet.pallet_id.in_(ordered_ids),
batch_scope_condition(BizPallet.test_batch_id, scope),
)
.order_by(BizPallet.pallet_id)
.with_for_update()
.execution_options(populate_existing=True)
@@ -76,11 +95,13 @@ class InventoryDao:
db: AsyncSession,
pallet_id: int,
idempotency_key: str,
scope: TestBatchScope,
) -> BizPalletEvent | None:
result = await db.execute(
select(BizPalletEvent).where(
BizPalletEvent.pallet_id == pallet_id,
BizPalletEvent.idempotency_key == idempotency_key,
batch_scope_condition(BizPalletEvent.test_batch_id, scope),
)
)
return result.scalar_one_or_none()