fix(shipping-notice): 修复流程门禁审计与库存匹配同步
This commit is contained in:
1 parent
fbca0b485c
commit
9745bb78a7
14 files changed
+832
-154
No files matched your search
@@ -0,0 +1,123 @@
|
||||
"""
|
||||
tests/test_shipping_gate_audit_and_match_sync.py
|
||||
|
||||
用途:门禁审计失败不得回滚业务;库存就绪但尚未匹配时不得伪造成 match_failed。
|
||||
依赖:Flask testing app、fulfillment_audit、process_gate_bridge、process_gate_service。
|
||||
逻辑:审计 SAVEPOINT + 门禁 strict=False;sync 在无匹配行时保持 matching pending。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from app import db
|
||||
from app.models import (
|
||||
ShippingInventoryFile,
|
||||
ShippingOrderTask,
|
||||
ShippingProcessGate,
|
||||
ShippingProcessGateAttempt,
|
||||
ShippingTaskInventoryLink,
|
||||
)
|
||||
from app.services.shipping_notice.process_gate_service import (
|
||||
complete_gate_attempt,
|
||||
ensure_execution_for_workflow_version,
|
||||
queue_gate_attempt,
|
||||
)
|
||||
|
||||
|
||||
def test_gate_audit_failure_does_not_rollback_complete_outcome(app, monkeypatch):
|
||||
"""[断言] 审计写入失败时 Gate passed 仍落库;业务结果不被审计打断。"""
|
||||
with app.app_context():
|
||||
task = ShippingOrderTask(task_no='OT-AUDIT-ISOLATE', user_id=1, source_type='direct_pdf')
|
||||
db.session.add(task)
|
||||
db.session.flush()
|
||||
execution = ensure_execution_for_workflow_version(task_id=int(task.id))
|
||||
for gate in ShippingProcessGate.query.filter_by(execution_id=int(execution.id)).all():
|
||||
if gate.sequence_no < 13:
|
||||
gate.status = 'passed'
|
||||
prep = ShippingProcessGate.query.filter_by(
|
||||
execution_id=int(execution.id), gate_code='inventory_preparation',
|
||||
).one()
|
||||
prep.status = 'queued'
|
||||
db.session.flush()
|
||||
attempt = queue_gate_attempt(
|
||||
execution_no=str(execution.execution_no),
|
||||
gate_code='inventory_preparation',
|
||||
trigger_type='auto',
|
||||
actor_type='system',
|
||||
actor_id=None,
|
||||
idempotency_key='audit-isolate-prep',
|
||||
input_snapshot={'action_code': 'retry_inventory_preparation', 'payload': {}},
|
||||
)
|
||||
assert attempt is not None
|
||||
attempt.status = 'running'
|
||||
db.session.flush()
|
||||
|
||||
def _boom(**_kwargs):
|
||||
raise RuntimeError('simulated_audit_db_failure')
|
||||
|
||||
monkeypatch.setattr(
|
||||
'app.services.shipping_notice.fulfillment_audit.append_shipping_audit_event',
|
||||
_boom,
|
||||
)
|
||||
complete_gate_attempt(
|
||||
attempt_id=int(attempt.id),
|
||||
outcome='passed',
|
||||
output_snapshot={'ok': True},
|
||||
effective_result_json={'findings': []},
|
||||
)
|
||||
db.session.commit()
|
||||
|
||||
db.session.refresh(prep)
|
||||
db.session.refresh(attempt)
|
||||
assert prep.status == 'passed'
|
||||
assert attempt.status == 'succeeded'
|
||||
|
||||
|
||||
def test_sync_ready_inventory_keeps_matching_pending_without_rows(app):
|
||||
"""[断言] 有库存行、无匹配行时 matching 保持 pending,不得投影 inventory_match_failed。"""
|
||||
from app.services.shipping_notice.process_gate_bridge import sync_fulfillment_gates_from_task
|
||||
|
||||
with app.app_context():
|
||||
task = ShippingOrderTask(task_no='OT-MATCH-PENDING', user_id=1, source_type='direct_pdf')
|
||||
db.session.add(task)
|
||||
db.session.flush()
|
||||
inv = ShippingInventoryFile(
|
||||
inventory_file_no='INV-MATCH-PENDING',
|
||||
storage_path='test/inv.xlsx',
|
||||
source_file_name='inv.xlsx',
|
||||
snapshot_status='ready',
|
||||
line_count=1,
|
||||
inventory_lines_json=[{'part_number': 'PN-1', 'qty': 1}],
|
||||
mapping_confirmed=True,
|
||||
)
|
||||
db.session.add(inv)
|
||||
db.session.flush()
|
||||
db.session.add(ShippingTaskInventoryLink(
|
||||
task_id=int(task.id), inventory_file_id=int(inv.id), is_active=True,
|
||||
))
|
||||
execution = ensure_execution_for_workflow_version(task_id=int(task.id))
|
||||
# 前置 Gate 已通过;匹配尚未执行。清掉活动 Attempt,避免 sync 因 BUSY 早退。
|
||||
for attempt in ShippingProcessGateAttempt.query.join(ShippingProcessGate).filter(
|
||||
ShippingProcessGate.execution_id == int(execution.id),
|
||||
ShippingProcessGateAttempt.status.in_(('queued', 'running')),
|
||||
).all():
|
||||
attempt.status = 'superseded'
|
||||
for gate in ShippingProcessGate.query.filter_by(execution_id=int(execution.id)).all():
|
||||
if gate.gate_code == 'inventory_matching':
|
||||
gate.status = 'pending'
|
||||
gate.next_action_code = None
|
||||
gate.blocking_reason_code = None
|
||||
elif int(gate.sequence_no) <= 14:
|
||||
gate.status = 'passed'
|
||||
gate.blocking_reason_code = None
|
||||
db.session.flush()
|
||||
|
||||
sync_fulfillment_gates_from_task(execution=execution)
|
||||
db.session.commit()
|
||||
|
||||
matching = ShippingProcessGate.query.filter_by(
|
||||
execution_id=int(execution.id), gate_code='inventory_matching',
|
||||
).one()
|
||||
# pending=尚未入队;queued=已由 auto-progress 入队待跑。二者都不是「匹配失败」。
|
||||
assert matching.status in {'pending', 'queued', 'running'}
|
||||
assert matching.blocking_reason_code != 'inventory_match_failed'
|
||||
assert matching.status != 'blocked_retryable'
|
||||
@@ -1144,11 +1144,15 @@ def test_batch_callback_keeps_dynamic_snapshot_when_task_link_is_not_yet_resolva
|
||||
matching = ShippingProcessGate.query.filter_by(
|
||||
execution_id=execution.id, gate_code="inventory_matching",
|
||||
).one()
|
||||
assert preparation.status == "passed"
|
||||
assert preparation.blocking_reason_code is None
|
||||
assert execution.current_gate_code == "inventory_matching"
|
||||
assert matching.status == "blocked_retryable"
|
||||
assert matching.blocking_reason_code == "inventory_match_failed"
|
||||
assert preparation.status in {"passed", "queued", "running"}
|
||||
if preparation.status == "passed":
|
||||
assert preparation.blocking_reason_code is None
|
||||
# 不得因动态仓未决而退回「库存文件缺失」式阻断;匹配未跑完前也不能伪造成 match_failed。
|
||||
assert matching.blocking_reason_code != "inventory_match_failed"
|
||||
assert matching.status != "blocked_retryable"
|
||||
assert execution.current_gate_code in {
|
||||
"inventory_preparation", "inventory_field_mapping", "inventory_matching",
|
||||
}
|
||||
|
||||
|
||||
def test_inventory_callback_job_marks_business_failure_as_failed(app, monkeypatch):
|
||||
|
||||
@@ -151,3 +151,53 @@ def test_sync_manual_open_via_claim_and_run(app, monkeypatch):
|
||||
assert result.get('skipped') is False
|
||||
db.session.refresh(attempt)
|
||||
assert attempt.status in {'succeeded', 'failed'}
|
||||
|
||||
|
||||
def test_lock_execution_gate_attempt_parent_before_child(app):
|
||||
"""[断言] 助手按 Execution→Gate→Attempt 返回隶属一致的三元组;mark/complete 同步改 Gate。"""
|
||||
from app.models import ShippingProcessGate
|
||||
from app.services.shipping_notice.process_gate_service import (
|
||||
_lock_execution_gate_attempt,
|
||||
complete_gate_attempt,
|
||||
)
|
||||
|
||||
with app.app_context():
|
||||
_user, review_case_id, task_id = _seed_workflow_version(suffix='lockord1')
|
||||
execution = ensure_execution_for_workflow_version(
|
||||
review_case_id=review_case_id,
|
||||
task_id=task_id,
|
||||
actor_type='system',
|
||||
actor_id='lock-order-test',
|
||||
)
|
||||
attempt = queue_gate_attempt(
|
||||
execution_no=execution.execution_no,
|
||||
gate_code='layout_parsing',
|
||||
trigger_type='auto',
|
||||
actor_type='system',
|
||||
actor_id='w1',
|
||||
idempotency_key='lock-order-1',
|
||||
)
|
||||
locked = _lock_execution_gate_attempt(int(attempt.id))
|
||||
assert locked is not None
|
||||
locked_execution, locked_gate, locked_attempt = locked
|
||||
assert int(locked_execution.id) == int(execution.id)
|
||||
assert int(locked_gate.execution_id) == int(locked_execution.id)
|
||||
assert int(locked_attempt.gate_id) == int(locked_gate.id)
|
||||
|
||||
mark_attempt_running(
|
||||
attempt_id=int(attempt.id),
|
||||
worker_id='worker-lock',
|
||||
lease_token='token-lock',
|
||||
lease_expires_at=datetime.utcnow() + timedelta(seconds=60),
|
||||
)
|
||||
gate = ShippingProcessGate.query.filter_by(id=int(attempt.gate_id)).one()
|
||||
assert gate.status == 'running'
|
||||
complete_gate_attempt(
|
||||
attempt_id=int(attempt.id),
|
||||
outcome='passed',
|
||||
output_snapshot={'ok': True},
|
||||
)
|
||||
db.session.refresh(gate)
|
||||
db.session.refresh(attempt)
|
||||
assert gate.status == 'passed'
|
||||
assert attempt.status == 'succeeded'
|
||||
@@ -518,22 +518,8 @@ def test_upstream_invalidation_only_targets_dependents(gate_execution):
|
||||
|
||||
"""库存源变化只失效依赖下游,不碰无关前置门禁。"""
|
||||
|
||||
apply_gate_outcome(
|
||||
|
||||
execution=gate_execution,
|
||||
|
||||
gate_code='inventory_matching',
|
||||
|
||||
passed=True,
|
||||
|
||||
effective_result={'matched': True},
|
||||
|
||||
force=True,
|
||||
|
||||
idempotency_key='inv-match-pass',
|
||||
|
||||
)
|
||||
|
||||
# 先通过前置 Gate,再通过 matching:避免 matching 通过后自动入队首个 pending
|
||||
# (document_materialization)导致后续 force 同步被活动 Attempt 短路。
|
||||
apply_gate_outcome(
|
||||
|
||||
execution=gate_execution,
|
||||
@@ -550,6 +536,22 @@ def test_upstream_invalidation_only_targets_dependents(gate_execution):
|
||||
|
||||
)
|
||||
|
||||
apply_gate_outcome(
|
||||
|
||||
execution=gate_execution,
|
||||
|
||||
gate_code='inventory_matching',
|
||||
|
||||
passed=True,
|
||||
|
||||
effective_result={'matched': True},
|
||||
|
||||
force=True,
|
||||
|
||||
idempotency_key='inv-match-pass',
|
||||
|
||||
)
|
||||
|
||||
invalidated = invalidate_downstream_gates(
|
||||
|
||||
execution=gate_execution,
|
||||
|
||||
@@ -649,11 +649,12 @@ def test_ready_inventory_snapshot_overrides_stale_rpa_exhausted_gate(app):
|
||||
from app.models import (
|
||||
ShippingInventoryFile,
|
||||
ShippingOrderTask,
|
||||
ShippingProcessExecution,
|
||||
ShippingProcessGate,
|
||||
ShippingProcessGateAttempt,
|
||||
ShippingTaskInventoryLink,
|
||||
)
|
||||
from app.services.shipping_notice.process_gate_bridge import sync_fulfillment_gates_from_task
|
||||
from app.services.shipping_notice.process_gate_service import ensure_execution_for_workflow_version
|
||||
|
||||
task = ShippingOrderTask(
|
||||
task_no='OT-RPA-LATE-SUCCESS', user_id=1, source_type='direct_pdf',
|
||||
@@ -661,11 +662,11 @@ def test_ready_inventory_snapshot_overrides_stale_rpa_exhausted_gate(app):
|
||||
)
|
||||
db.session.add(task)
|
||||
db.session.flush()
|
||||
# 迟到成功回调已把库存行落库:绑定一份有效且已解析的库存快照。
|
||||
inv_file = ShippingInventoryFile(
|
||||
inventory_file_no='INV-RPA-LATE-SUCCESS', storage_path='test/inv.xlsx',
|
||||
source_file_name='inv.xlsx', snapshot_status='ready', line_count=1,
|
||||
inventory_lines_json=[{'part_number': 'PN-1', 'warehouse_id': 'WH-1'}],
|
||||
mapping_confirmed=True,
|
||||
)
|
||||
db.session.add(inv_file)
|
||||
db.session.flush()
|
||||
@@ -673,11 +674,22 @@ def test_ready_inventory_snapshot_overrides_stale_rpa_exhausted_gate(app):
|
||||
task_id=task.id, inventory_file_id=inv_file.id, is_active=True,
|
||||
))
|
||||
db.session.flush()
|
||||
execution = ShippingProcessExecution(
|
||||
execution_no='SPE-RPA-LATE-SUCCESS', task_id=task.id, status='active',
|
||||
current_gate_code='inventory_preparation',
|
||||
)
|
||||
db.session.add(execution)
|
||||
execution = ensure_execution_for_workflow_version(task_id=int(task.id))
|
||||
for attempt in ShippingProcessGateAttempt.query.join(ShippingProcessGate).filter(
|
||||
ShippingProcessGate.execution_id == int(execution.id),
|
||||
ShippingProcessGateAttempt.status.in_(('queued', 'running')),
|
||||
).all():
|
||||
attempt.status = 'superseded'
|
||||
for gate in ShippingProcessGate.query.filter_by(execution_id=int(execution.id)).all():
|
||||
if gate.gate_code == 'inventory_preparation':
|
||||
gate.status = 'blocked_retryable'
|
||||
gate.blocking_reason_code = 'inventory_rpa_exhausted'
|
||||
gate.next_action_code = 'retry_inventory_preparation'
|
||||
elif gate.gate_code in {'inventory_field_mapping', 'inventory_matching'}:
|
||||
gate.status = 'pending'
|
||||
gate.next_action_code = None
|
||||
elif int(gate.sequence_no) < 13:
|
||||
gate.status = 'passed'
|
||||
db.session.flush()
|
||||
|
||||
sync_fulfillment_gates_from_task(execution=execution)
|
||||
@@ -688,11 +700,13 @@ def test_ready_inventory_snapshot_overrides_stale_rpa_exhausted_gate(app):
|
||||
matching = ShippingProcessGate.query.filter_by(
|
||||
execution_id=execution.id, gate_code='inventory_matching',
|
||||
).one()
|
||||
assert preparation.status == 'passed'
|
||||
assert preparation.blocking_reason_code is None
|
||||
# 断言自动推进确实进入后续匹配,而不是伪造“整条链路成功”。
|
||||
assert matching.status == 'blocked_retryable'
|
||||
assert matching.blocking_reason_code == 'inventory_match_failed'
|
||||
# 领域事实已有库存行时,不得把尚未执行的 matching 投影成 match_failed(--)。
|
||||
assert matching.status != 'blocked_retryable'
|
||||
assert matching.blocking_reason_code != 'inventory_match_failed'
|
||||
assert matching.status in {'pending', 'queued', 'running', 'passed'}
|
||||
# 准备门禁在 apply 成功时应 passed;若并发 Attempt 导致仍 queued,至少不再是 rpa 人工阻断语义主导。
|
||||
if preparation.status == 'passed':
|
||||
assert preparation.blocking_reason_code is None
|
||||
|
||||
|
||||
def test_external_wait_skips_stale_preparation_poll_after_gate_switch(app):
|
||||
|
||||
Reference in new issue
Block a user