!11 feat(shipping-notice): 实现滚动履约功能,优化发货通知流程与代码清理

Merge pull request !11 from 曹惠鹏/hugo-shipping-notice
This commit is contained in:
sophie authored and Gitee committed 2026-08-26 00:25:10 +00:00
commit 2ff9fdafe4
402 files changed
+23045 -20484

No files matched your search

@@ -16,7 +16,7 @@ from app import db
from app.core.errors import ConflictError, NotFoundError, ValidationError
from app.models import (
ShippingBusinessSequence,
ShippingCase,
ShippingNotice,
ShippingCustomer,
ShippingCustomerExternalRef,
ShippingCustomerIdentifier,
@@ -74,14 +74,14 @@ def test_unused_customer_can_delete_and_identifiers_are_cascade_only(app):
@pytest.mark.parametrize('reference_kind', [
'case', 'sequence', 'external_ref', 'import_target', 'import_result', 'shadow_top', 'shadow_manual', 'shadow_match',
'notice', 'sequence', 'external_ref', 'import_target', 'import_result', 'shadow_top', 'shadow_manual', 'shadow_match',
])
def test_business_and_audit_references_block_delete(app, reference_kind: str):
"""每类业务或识别审计引用都必须在统一影响结果中阻断永久删除。"""
with app.app_context():
customer = _customer()
if reference_kind == 'case':
db.session.add(ShippingCase(case_no='CASE-LIFECYCLE', customer_id=customer.id))
if reference_kind == 'notice':
db.session.add(ShippingNotice(task_no='SN-LIFECYCLE', customer_id=customer.id))
elif reference_kind == 'sequence':
db.session.add(ShippingBusinessSequence(customer_id=customer.id, business_date=date(2026, 7, 16)))
elif reference_kind == 'external_ref':
@@ -194,14 +194,14 @@ def test_delete_unused_customer_cascades_configurable_identity_evidence(app):
@pytest.mark.parametrize('reference_kind', [
'case', 'sequence', 'external_ref', 'import_target', 'import_result', 'shadow_top', 'shadow_manual', 'shadow_match',
'notice', 'sequence', 'external_ref', 'import_target', 'import_result', 'shadow_top', 'shadow_manual', 'shadow_match',
])
def test_delete_rechecks_every_lifecycle_blocker(app, reference_kind: str):
"""永久删除必须复用完整生命周期统计,任一业务或审计引用均返回冲突。"""
with app.app_context():
customer = _customer('BLK')
if reference_kind == 'case':
db.session.add(ShippingCase(case_no='CASE-DELETE-BLOCK', customer_id=customer.id))
if reference_kind == 'notice':
db.session.add(ShippingNotice(task_no='SN-DELETE-BLOCK', customer_id=customer.id))
elif reference_kind == 'sequence':
db.session.add(ShippingBusinessSequence(customer_id=customer.id, business_date=date(2026, 7, 16)))
elif reference_kind == 'external_ref':
@@ -267,13 +267,13 @@ def test_delete_blocks_reference_created_after_preview(app):
preview = get_customer_lifecycle_impact(customer.id)
assert preview['can_delete'] is True
db.session.add(ShippingCase(case_no='CASE-DELETE-RACE', customer_id=customer.id))
db.session.add(ShippingNotice(task_no='SN-DELETE-RACE', customer_id=customer.id))
db.session.commit()
with pytest.raises(ConflictError) as exc_info:
delete_customer(customer.id, confirmation_short_code='RCE', reason='误建', actor_id=None)
assert exc_info.value.code == 'SHIPPING.CUSTOMER_DELETE_BLOCKED'
assert exc_info.value.details['blockers']['business_cases'] == 1
assert exc_info.value.details['blockers']['shipping_notices'] == 1
assert db.session.get(ShippingCustomer, customer.id) is not None
@@ -16,7 +16,7 @@ from app import db
from app.core.errors import ConflictError, ValidationError
from app.models import (
ShippingBusinessSequence,
ShippingCase,
ShippingNotice,
ShippingCustomer,
ShippingCustomerExternalRef,
ShippingCustomerIdentifier,
@@ -71,8 +71,8 @@ def test_merge_moves_future_configuration_and_preserves_history(app):
normalized_value='same@example.com', status='approved', source='manual',
),
ShippingCustomerExternalRef(customer_id=source.id, source_system='ERP', legacy_customer_key='LEGACY-SRC'),
ShippingCase(
case_no='CASE-MERGE-HISTORY', customer_id=source.id, business_no='SRC26071601',
ShippingNotice(
task_no='SN-MERGE-HISTORY', customer_id=source.id, business_no='SRC26071601',
customer_name_snapshot=source.standard_name, customer_short_code_snapshot=source.short_code,
),
ShippingBusinessSequence(customer_id=source.id, business_date=date(2026, 7, 16), current_value=1),
@@ -82,7 +82,7 @@ def test_merge_moves_future_configuration_and_preserves_history(app):
target_id = target.id
preview = get_customer_merge_impact(source_id, target_id)
assert preview['historical_references']['business_cases'] == 1
assert preview['historical_references']['shipping_notices'] == 1
# 合并预检同时展示旧识别证据与阶段一可配置证据的迁移统计。
assert preview['migrate'] == {
'identifiers': 1, 'duplicate_identifiers': 1, 'identity_evidences': 0,
@@ -105,10 +105,10 @@ def test_merge_moves_future_configuration_and_preserves_history(app):
assert db.session.query(ShippingCustomerIdentifier).filter_by(customer_id=source_id).count() == 0
assert db.session.query(ShippingCustomerIdentifier).filter_by(customer_id=target_id).count() == 2
assert db.session.query(ShippingCustomerExternalRef).filter_by(customer_id=target_id).count() == 1
historical_case = db.session.query(ShippingCase).filter_by(case_no='CASE-MERGE-HISTORY').one()
assert historical_case.customer_id == source_id
assert historical_case.business_no == 'SRC26071601'
assert historical_case.customer_short_code_snapshot == 'SRC'
historical_notice = db.session.query(ShippingNotice).filter_by(task_no='SN-MERGE-HISTORY').one()
assert historical_notice.customer_id == source_id
assert historical_notice.business_no == 'SRC26071601'
assert historical_notice.customer_short_code_snapshot == 'SRC'
assert db.session.query(ShippingBusinessSequence).filter_by(customer_id=source_id).count() == 1
merge = db.session.get(ShippingCustomerMerge, result['merge_id'])
assert merge is not None and merge.source_customer_id_snapshot == source_id
@@ -13,7 +13,7 @@ from sqlalchemy.exc import IntegrityError
from app import db
from app.core.errors import ConflictError, ValidationError
from app.models import ShippingCase, ShippingCustomer, ShippingCustomerIdentifier, ShippingCustomerLifecycleEvent
from app.models import ShippingCustomer, ShippingCustomerIdentifier, ShippingCustomerLifecycleEvent, ShippingNotice
from app.services.admin.shipping_customer_service import (
create_customer,
create_identifier,
@@ -219,7 +219,7 @@ def test_customer_list_returns_preaggregated_reference_counts(app):
"""列表返回业务单和识别证据数量,避免管理端逐客户补查。"""
with app.app_context():
customer = _create_customer()
db.session.add(ShippingCase(case_no='CASE-COUNT-1', customer_id=customer['id']))
db.session.add(ShippingNotice(task_no='SN-COUNT-1', customer_id=customer['id']))
db.session.add(ShippingCustomerIdentifier(
customer_id=customer['id'],
identifier_type='name_alias',
@@ -231,5 +231,5 @@ def test_customer_list_returns_preaggregated_reference_counts(app):
db.session.commit()
result = list_customers({'page': 1, 'page_size': 20})
assert result['items'][0]['case_count'] == 1
assert result['items'][0]['notice_count'] == 1
assert result['items'][0]['identifier_count'] == 1
@@ -1,8 +1,8 @@
"""
tests/services/shipping_notice/test_reconciliation_phase34_contract.py
用途:Phase 3 统一 Projection 四事实 + Phase 4 管理端 replay 权限 +
BRANCH F ownership_anomaly rollback 的合同测试。
用途:Phase 3 统一 Projection 四事实 + Phase 4 BRANCH F
ownership_anomaly rollback 的合同测试。
依赖:recovery_contract、reconciliation_projector、
reconciliation_service.reconcile_and_progress(mock 掉 DB 层仅验证纯函数矩阵)。
逻辑:
@@ -14,9 +14,6 @@ tests/services/shipping_notice/test_reconciliation_phase34_contract.py
retryable/failed/running/idle)。
[P3 T4] project_case_primary_action 1a~1i CTA 分派矩阵。
[P3 T5] project_review_case_projection_bundle 六字段非空(Case + automation + actions)。
[P4 T6] reconcile_and_progress PRE-CHECK:
a) replay_mode=True + is_admin=False → SHIPPING.RECONCILE_FORCE_CREATE_UNAUTHORIZED 422
b) legacy_job_id=999 + is_admin=False → 422
[P4 T7] ownership_anomaly BRANCH F:
断言触发路径最终 outcome=ownership_anomaly_rejected 且不会脏写
(这里只测 ValidationError 降级情形,Review Case 不含旧流程字段)。
@@ -287,7 +284,7 @@ def test_pending_order_freeze_readiness_waits_for_the_user_to_submit_validation(
# ============================================================
# [P4 T6] 管理员权限拦截(纯函数 PRE-CHECK,通过 monkeypatch 替换锁 DB 查询)
# [P4] BRANCH F 归属异常(通过 monkeypatch 替换锁 DB 查询)
# ============================================================
def _mock_case_for_p4(case_no='CASE-2608-0042'):
@@ -372,72 +369,6 @@ def _install_minimal_mocks(monkeypatch, case=None, app=None):
return case
@pytest.mark.pure_unit
def test_p4_t6a_replay_mode_without_admin_raises(monkeypatch):
"""[P4 T6a] replay_mode=True + is_admin=False → 422 SHIPPING.RECONCILE_FORCE_CREATE_UNAUTHORIZED。"""
from app.core.errors import ValidationError
from app.services.shipping_notice.reconciliation_service import reconcile_and_progress
_install_minimal_mocks(monkeypatch)
with pytest.raises(ValidationError) as exc:
reconcile_and_progress(
user_id=1,
case_no='CASE-2608-0042',
operation_id=str(uuid.uuid4()),
user_action='auto',
is_admin=False,
replay_mode=True,
)
assert exc.value.code == 'SHIPPING.RECONCILE_FORCE_CREATE_UNAUTHORIZED'
@pytest.mark.pure_unit
def test_p4_t6b_legacy_job_id_without_admin_raises(monkeypatch):
"""[P4 T6b] legacy_job_id=928 + is_admin=False → 422 权限拒绝。"""
from app.core.errors import ValidationError
from app.services.shipping_notice.reconciliation_service import reconcile_and_progress
_install_minimal_mocks(monkeypatch)
with pytest.raises(ValidationError) as exc:
reconcile_and_progress(
user_id=1,
case_no='CASE-2608-0042',
operation_id=str(uuid.uuid4()),
user_action='auto',
is_admin=False,
legacy_job_id=928,
)
assert exc.value.code == 'SHIPPING.RECONCILE_FORCE_CREATE_UNAUTHORIZED'
@pytest.mark.pure_unit
def test_p4_t6c_legacy_job_requires_failed_or_dead(monkeypatch):
"""[P4 T6c] legacy_job 必须是 failed/dead,否则 422。"""
from app.core.errors import ValidationError
from app.services.shipping_notice.reconciliation_service import reconcile_and_progress
_install_minimal_mocks(monkeypatch)
bad_job = _fake_job(job_id=991, status='running') # 不是失败态
from app.services.shipping_notice import reconciliation_service as mod
def _get(coll, jid): # noqa: ARG001
if str(getattr(coll, '__name__', '')) == 'Job' and jid == 991:
return bad_job
return None
monkeypatch.setattr(mod.db.session, 'get', _get)
with pytest.raises(ValidationError):
reconcile_and_progress(
user_id=1,
case_no='CASE-2608-0042',
operation_id=str(uuid.uuid4()),
user_action='auto',
is_admin=True,
legacy_job_id=991,
)
# ============================================================
# [P4 T7] BRANCH F ownership_anomaly 422 降级(Review Case 不含旧流程字段)
# ============================================================