Files
syops/backend/tests/test_shipping_processing_ownership.py
hugoandCursor 39fe3c05f2 feat(freight-rpa): 库存/收料改名在手/入站,报表归属RPA,类名统一FreightRpa
把 shipping 侧 inventory/receipt 词根迁到 onhand/inbound,29 张表与
枚举/权限/前端/i18n 同步改名;在手与入站报表配置及文件改由 freight_rpa
持有,发货与调拨仅按 source_code 消费;ORM 类加 FreightRpa 前缀,
上传动作改为 freight_rpa_upload。含类名与上传动作改名规格文档。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-10-19 10:46:06 +08:00

111 lines
4.4 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""
tests/test_shipping_processing_ownership.py
用途:验收投影不一致 fail-closed 与任务归属 ConflictError。
依赖:processing_ownership、processing_projection_invariants、pytest、unittest.mock。
逻辑:不要求完整履约 E2E;ownership 用 mock 链路;不一致用 reconcile 断言动作剥离。
"""
from __future__ import annotations
from types import SimpleNamespace
from unittest.mock import MagicMock, patch
import pytest
from app.core.errors import ConflictError
from app.services.shipping_notice.processing_ownership import (
assert_notice_task_ownership,
record_projection_inconsistency_audit,
)
from app.services.shipping_notice.processing_projection_invariants import (
PROJECTION_INCONSISTENT_ERROR_CODE,
TASK_OWNERSHIP_MISMATCH_ERROR_CODE,
detect_notice_pre_create_inconsistency,
reconcile_processing_projection_facts,
)
def test_detect_notice_pre_create_inconsistency():
"""有正式号 + 建单前 stage → True;履约 stage → False。"""
assert detect_notice_pre_create_inconsistency(
stage='waiting_review',
shipping_notice_no='SN-1',
) is True
assert detect_notice_pre_create_inconsistency(
stage='onhand_matching',
shipping_notice_no='SN-1',
) is False
assert detect_notice_pre_create_inconsistency(
stage='waiting_review',
shipping_notice_no=None,
) is False
def test_reconcile_marks_inconsistency_and_strips_fulfillment():
"""服务级拆链:reconcile 后断言 stage/actions/inconsistency 码。"""
payload = reconcile_processing_projection_facts({
'status': 'blocked',
'stage': 'customer_resolving',
'source_type': 'eml',
'work_item_no': 'OT-9',
'shipping_notice_no': 'SN-LEFT',
'progress_version': 4,
'current_gate': {
'gate_code': 'customer',
'status': 'blocked',
'next_action_code': 'confirm_customer',
'blocking_reason_code': 'customer_unresolved',
'attempt_no': 1,
},
'available_actions': ['confirm_customer', 'match_inventory', 'download_excel'],
}, excel_ready=True)
assert payload['stage'] == 'customer_resolving'
assert payload['projection_inconsistency_code'] == PROJECTION_INCONSISTENT_ERROR_CODE
assert 'confirm_customer' in payload['available_actions']
assert 'open_onhand_match_review' not in payload['available_actions']
assert 'download_excel' not in payload['available_actions']
assert len(payload['stage_nodes']) == 15
def test_projection_inconsistency_audit_uses_allowed_failure_result(app):
"""不一致审计必须以 audit_service 接受的 failure 落库,不能在 GET 路径反复报参数非法。"""
with app.app_context():
with patch(
'app.services.shipping_notice.processing_ownership.append_shipping_audit_event',
) as append_mock, patch(
'app.services.shipping_notice.processing_ownership.db.session.flush',
):
record_projection_inconsistency_audit({
'projection_inconsistency_code': PROJECTION_INCONSISTENT_ERROR_CODE,
'work_item_no': 'OT-9',
'shipping_notice_no': 'SN-9',
'stage': 'waiting_review',
})
assert append_mock.call_args.kwargs['result'] == 'failure'
def test_assert_ownership_noop_without_expected_context():
"""未传期望 OT/Case/Run 时不拦截(兼容旧客户端)。"""
task = SimpleNamespace(id=1, task_no='SN-1', order_task_id=None)
assert_notice_task_ownership(task)
def test_assert_ownership_mismatch_raises_conflict(app):
"""期望 OT 与 Notice 挂接 OT 不一致时抛出固定错误码。"""
task = SimpleNamespace(id=7, task_no='SN-7', order_task_id=99)
linked_ot = SimpleNamespace(task_no='OT-REAL', extraction_run_id=None, candidate_id=None)
with app.app_context():
with patch('app.services.shipping_notice.processing_ownership.db') as mock_db, \
patch('app.services.shipping_notice.processing_ownership.append_shipping_audit_event'):
mock_db.session.get.return_value = linked_ot
mock_db.session.flush = MagicMock()
with pytest.raises(ConflictError) as exc:
assert_notice_task_ownership(
task,
expected_order_task_no='OT-OTHER',
actor_id=1,
)
assert exc.value.code == TASK_OWNERSHIP_MISMATCH_ERROR_CODE