test(shipping): 更新订单复核库存与通知流程测试

This commit is contained in:
hugo committed 2026-09-04 20:53:06 +08:00
1 parent 8ec028e6a3
commit 265a6e179a
168 files changed
+9420 -4613

No files matched your search

@@ -0,0 +1,84 @@
"""
用途:验证正式客户识别结果是客户确认与流程 Gate 的唯一真相源。
依赖:内存数据库、客户识别结果服务、ShippingOrderTask/Candidate。
逻辑:覆盖非 bill_to 证据可确认,以及仅有旧 Bill-to Match 时不得放行两条边界。
"""
from __future__ import annotations
from app import db
from app.models import (
ShippingCustomer,
ShippingCustomerShadowResolution,
ShippingOrderCandidate,
ShippingOrderIntake,
ShippingOrderTask,
User,
)
from app.services.shipping_notice.customer_resolution_service import (
confirm_customer_resolution,
customer_resolution_facts,
)
def _task_with_resolution(*, status: str = 'unique_candidate') -> tuple[ShippingOrderTask, ShippingCustomerShadowResolution, ShippingCustomer]:
"""构造 OT、Candidate 和规则证据快照;不构造 bill_to 字段或 CustomerMatch。"""
user = User(username='resolution-user', email='resolution@example.com', role='user')
customer = ShippingCustomer(standard_name='规则识别客户', short_code='RID', status='active')
db.session.add_all([user, customer])
db.session.flush()
intake = ShippingOrderIntake(
intake_no='INT-RESOLUTION', asset_id=1, user_id=user.id, parser_version='test',
)
db.session.add(intake)
db.session.flush()
candidate = ShippingOrderCandidate(
candidate_no='CAND-RESOLUTION', intake_id=intake.id, primary_document_id=1,
candidate_key='resolution-test', candidate_status='accepted',
)
db.session.add(candidate)
db.session.flush()
task = ShippingOrderTask(task_no='OT-RESOLUTION', user_id=user.id, candidate_id=candidate.id)
resolution = ShippingCustomerShadowResolution(
candidate_id=candidate.id,
intake_id=intake.id,
resolution_version='shadow-v2',
status=status,
evidence_json=[{'definition_id': 'customer-code', 'normalized_value': 'C-100'}],
candidate_matches_json=[{'customer_id': customer.id, 'score': 100}],
resolution_context_json={'rule_set': {'checksum': 'a' * 64}},
identity_rule_set_checksum='a' * 64,
top_customer_id=customer.id,
top_score=100,
)
db.session.add_all([task, resolution])
db.session.commit()
return task, resolution, customer
def test_non_bill_to_evidence_can_confirm_customer(app):
"""关键断言:客户代码证据可确认客户,流程不要求 bill_to 字段或旧匹配行。"""
with app.app_context():
task, resolution, customer = _task_with_resolution()
confirm_customer_resolution(task, customer_id=customer.id, actor_id=None, method='manual')
db.session.commit()
facts = customer_resolution_facts(task)
assert facts['status'] == 'confirmed'
assert facts['customer_id'] == customer.id
assert resolution.confirmed_customer_id == customer.id
# 关键断言:确认不篡改规则证据及其 checksum。
assert resolution.evidence_json == [{'definition_id': 'customer-code', 'normalized_value': 'C-100'}]
assert resolution.identity_rule_set_checksum == 'a' * 64
def test_unconfirmed_resolution_fails_closed_without_old_bill_to_fallback(app):
"""关键断言:未确认的统一识别结果不得从旧字段或旧匹配路径获得客户身份。"""
with app.app_context():
task, _resolution, _customer = _task_with_resolution(status='unique_candidate')
facts = customer_resolution_facts(task)
assert facts['status'] == 'unique_candidate'
assert facts['customer_id'] is None
@@ -199,7 +199,8 @@ def test_case_bundle_prefers_persisted_formal_gate_over_case_draft(monkeypatch):
"""正式 field_completeness 阻断必须覆盖 Case draft 的 idle/order_review_build 推导。"""
from app.services.shipping_notice import reconciliation_projector
case = _fake_case(status='draft', candidate_id=None)
# 正式门禁事实只在 Case 已确认(confirmed)时覆盖;未确认 draft 保持 order_review_build(见结算设计)。
case = _fake_case(status='confirmed', candidate_id=None)
monkeypatch.setattr(
reconciliation_projector,
'_project_case_formal_gate_summary',