test(shipping): 更新订单复核库存与通知流程测试
This commit is contained in:
1 parent
8ec028e6a3
commit
265a6e179a
168 files changed
+9420
-4613
No files matched your search
@@ -13,7 +13,7 @@ from types import SimpleNamespace
|
||||
from app import db
|
||||
from app.models import (
|
||||
ShippingOrderExtraction, ShippingOrderFieldDecision, ShippingOrderFieldValue, ShippingOrderLine,
|
||||
ShippingOrderReviewEvent, ShippingNotice, ShippingTaskFieldConflict,
|
||||
ShippingOrderReviewEvent, ShippingOrderTask, ShippingTaskFieldConflict,
|
||||
)
|
||||
from app.services.shipping_notice.order_schema import service as order_schema_service
|
||||
from app.services.shipping_notice.task_candidate_reconciliation import (
|
||||
@@ -37,20 +37,26 @@ def _review_payload() -> dict[str, object]:
|
||||
}
|
||||
|
||||
|
||||
def _task(**kwargs) -> ShippingNotice:
|
||||
task = ShippingNotice(
|
||||
task_no=kwargs.pop('task_no', 'SN-RECON-001'),
|
||||
source_type='manual',
|
||||
def _task(**kwargs) -> ShippingOrderTask:
|
||||
"""建单前的订单任务(OT):字段对账的容器必须是 ShippingOrderTask。
|
||||
|
||||
对账流水全部挂在 shipping_order_tasks 维度(Extraction / FieldConflict / ReviewEvent),
|
||||
po_number 与行集不再作为 Notice 列,改由当前 accepted Extraction 的 FieldValue 承载。
|
||||
"""
|
||||
lines = kwargs.pop('lines_json', [{'part_number': 'A001', 'qty': 10}])
|
||||
po_number = kwargs.pop('po_number', 'PO-100')
|
||||
task = ShippingOrderTask(
|
||||
task_no=kwargs.pop('task_no', 'OT-RECON-001'),
|
||||
source_type='eml',
|
||||
user_id=1,
|
||||
status='processing',
|
||||
pending_human_reason=kwargs.pop('pending_human_reason', 'rsv_req'),
|
||||
po_number=kwargs.pop('po_number', 'PO-100'),
|
||||
lines_json=kwargs.pop('lines_json', [{'part_number': 'A001', 'qty': 10}]),
|
||||
order_data_version=1,
|
||||
order_lines_migration_status='pending',
|
||||
**kwargs,
|
||||
)
|
||||
db.session.add(task)
|
||||
db.session.flush()
|
||||
_attach_dynamic_extraction(task, list(task.lines_json or []))
|
||||
_attach_dynamic_extraction(task, rows=list(lines or []), po_number=po_number)
|
||||
return task
|
||||
|
||||
|
||||
@@ -62,8 +68,15 @@ def _candidate(summary_lines: list[dict], *, po_number: str = 'PO-100') -> Simpl
|
||||
)
|
||||
|
||||
|
||||
def _attach_dynamic_extraction(task: ShippingNotice, rows: list[dict]) -> dict[str, str]:
|
||||
"""建立候选对账所需的动态 FieldValue,测试不得依赖固定订单行投影。"""
|
||||
def _attach_dynamic_extraction(
|
||||
task: ShippingOrderTask, rows: list[dict], po_number: str = 'PO-100',
|
||||
) -> dict[str, str]:
|
||||
"""建立候选对账所需的动态 FieldValue,测试不得依赖固定订单行投影。
|
||||
|
||||
OT 容器没有 po_number / lines_json 列:PO 与行集全部写入当前 accepted Extraction 的
|
||||
FieldValue。inventory_source_hint 显式配置为可编辑,供人工改单路径(update_notice_line)
|
||||
通过 Schema task_edit_policy 门禁写入并触发 order_data_version 递增。
|
||||
"""
|
||||
order_schema_service.initialize_schema({'display_name': '对账字段'}, actor_id=None)
|
||||
existing_fields = {
|
||||
item['field_code']: item
|
||||
@@ -76,11 +89,17 @@ def _attach_dynamic_extraction(task: ShippingNotice, rows: list[dict]) -> dict[s
|
||||
)
|
||||
for code, scope, value_type in (
|
||||
('po_number', 'header', 'string'),
|
||||
('part_number', 'line', 'string'), ('qty', 'line', 'decimal'), ('reservation_id', 'line', 'string'),
|
||||
('part_number', 'line', 'string'),
|
||||
('qty', 'line', 'decimal'),
|
||||
('reservation_id', 'line', 'string'),
|
||||
('inventory_source_hint', 'line', 'string'),
|
||||
)
|
||||
}
|
||||
draft = order_schema_service.create_draft({}, actor_id=None)
|
||||
purposes = {'part_number': 'part_number', 'qty': 'quantity', 'reservation_id': 'reservation_id'}
|
||||
purposes = {
|
||||
'part_number': 'part_number', 'qty': 'quantity',
|
||||
'reservation_id': 'reservation_id', 'inventory_source_hint': 'inventory_source_hint',
|
||||
}
|
||||
saved = order_schema_service.save_draft(draft['id'], {'fields': [{
|
||||
'field_id': field['id'], 'display_name': code, 'business_purpose': purposes.get(code, 'general'),
|
||||
'required': code != 'reservation_id',
|
||||
@@ -89,6 +108,8 @@ def _attach_dynamic_extraction(task: ShippingNotice, rows: list[dict]) -> dict[s
|
||||
'enum_options': [],
|
||||
'evidence_policy': {'required': True, 'source_precedence': ['attachment_table'], 'conflict_policy': 'block', 'allow_single_source': True},
|
||||
'relation_role': None, 'comparator_key': 'decimal' if code == 'qty' else 'normalized_text',
|
||||
# 人工改单路径通过 Schema 策略门禁放行库存源线索编辑,从而可测 409 版本冲突。
|
||||
**({'task_edit_policy': {'editable': True, 'change_effect': 'none'}} if code == 'inventory_source_hint' else {}),
|
||||
} for code, field in fields.items()], 'expected_lock_version': draft['lock_version']}, actor_id=None)
|
||||
published = order_schema_service.publish(draft['id'], actor_id=None, expected_lock_version=saved['lock_version'])
|
||||
extraction = ShippingOrderExtraction(
|
||||
@@ -97,21 +118,22 @@ def _attach_dynamic_extraction(task: ShippingNotice, rows: list[dict]) -> dict[s
|
||||
db.session.add(extraction)
|
||||
db.session.flush()
|
||||
header_field = fields['po_number']
|
||||
header_value = task.po_number
|
||||
header_decision = ShippingOrderFieldDecision(
|
||||
extraction_id=extraction.id, line_id=None, field_id=header_field['id'],
|
||||
decision_status='accepted' if header_value else 'missing',
|
||||
supporting_observation_ids_json=[], resolved_value_json=header_value,
|
||||
decision_status='accepted' if po_number else 'missing',
|
||||
supporting_observation_ids_json=[], resolved_value_json=po_number or None,
|
||||
)
|
||||
db.session.add(header_decision)
|
||||
db.session.flush()
|
||||
if header_value:
|
||||
if po_number:
|
||||
db.session.add(ShippingOrderFieldValue(
|
||||
extraction_id=extraction.id, line_id=None, field_id=header_field['id'], decision_id=header_decision.id,
|
||||
value_text=str(header_value), value_json=header_value,
|
||||
value_text=str(po_number), value_json=po_number,
|
||||
))
|
||||
for position, row in enumerate(rows):
|
||||
line = ShippingOrderLine(extraction_id=extraction.id, position=position)
|
||||
line = ShippingOrderLine(
|
||||
extraction_id=extraction.id, position=position, line_no=position + 1,
|
||||
)
|
||||
db.session.add(line)
|
||||
db.session.flush()
|
||||
for code, field in fields.items():
|
||||
@@ -212,7 +234,7 @@ def test_resolve_conflict_writes_current_field_value_and_is_idempotent(app):
|
||||
idempotency_key='resolve-1',
|
||||
)
|
||||
assert first['idempotent_replay'] is False
|
||||
task = db.session.get(ShippingNotice, task.id)
|
||||
task = db.session.get(ShippingOrderTask, task.id)
|
||||
value = ShippingOrderFieldValue.query.filter_by(
|
||||
extraction_id=task.current_order_extraction_id, field_id=field_id,
|
||||
).one()
|
||||
@@ -222,15 +244,22 @@ def test_resolve_conflict_writes_current_field_value_and_is_idempotent(app):
|
||||
assert '"evidence_type":"manual_input"' in event.evidence_ref
|
||||
assert event.reason == '原始订单附件已复核'
|
||||
|
||||
replay = resolve_field_conflict(
|
||||
task=task,
|
||||
conflict_id=conflict.id,
|
||||
resolution='take_incoming',
|
||||
evidence={'evidence_type': 'manual_input', 'reason': '客户电话确认'},
|
||||
reason='原始订单附件已复核',
|
||||
idempotency_key='resolve-1',
|
||||
)
|
||||
assert replay['idempotent_replay'] is True
|
||||
# OT 容器已把 last_conflict_resolve_idempotency_key 从 step_payload 退役(见迁移
|
||||
# iw1a2b3c4d5e):已解决冲突再次提交不再回放同键结果,而是以 ALREADY_RESOLVED 拒绝,
|
||||
# 从而保证重复解决不会二次覆盖/新建重复业务值(幂等安全)。
|
||||
try:
|
||||
resolve_field_conflict(
|
||||
task=task,
|
||||
conflict_id=conflict.id,
|
||||
resolution='take_incoming',
|
||||
evidence={'evidence_type': 'manual_input', 'reason': '客户电话确认'},
|
||||
reason='原始订单附件已复核',
|
||||
idempotency_key='resolve-1',
|
||||
)
|
||||
except Exception as exc:
|
||||
assert getattr(exc, 'code', None) == 'SHIPPING.FIELD_CONFLICT_ALREADY_RESOLVED'
|
||||
else:
|
||||
raise AssertionError('resolved conflict replay must be rejected as already-resolved')
|
||||
|
||||
|
||||
def test_update_notice_line_version_mismatch(app):
|
||||
@@ -241,6 +270,7 @@ def test_update_notice_line_version_mismatch(app):
|
||||
lines_json=[{'part_number': 'A001', 'qty': 10, 'reservation_id': '3000000001'}],
|
||||
)
|
||||
db.session.commit()
|
||||
# 先提交一次可编辑字段,成功写入并 bump order_data_version。
|
||||
resp = update_notice_line({
|
||||
**_review_payload(),
|
||||
'task_id': task.task_no,
|
||||
@@ -248,9 +278,10 @@ def test_update_notice_line_version_mismatch(app):
|
||||
'patch': {'inventory_source_hint': 'H9'},
|
||||
'expected_order_data_version': 1,
|
||||
})
|
||||
# replace 已 bump version,再带 1 应失败;先读当前版本再测过期。
|
||||
task = db.session.get(ShippingNotice, task.id)
|
||||
assert resp['status'] == 'ok'
|
||||
task = db.session.get(ShippingOrderTask, task.id)
|
||||
stale = int(task.order_data_version) - 1
|
||||
# 再带过期版本号应失败,返回版本冲突。
|
||||
resp = update_notice_line({
|
||||
**_review_payload(),
|
||||
'task_id': task.task_no,
|
||||
@@ -281,7 +312,7 @@ def test_update_notice_line_rejects_forged_structured_evidence(app):
|
||||
})
|
||||
assert response['status'] == 'failed'
|
||||
assert response['data']['error_code'] == 'SHIPPING.ORDER_REVIEW_EVIDENCE_INVALID'
|
||||
assert int(db.session.get(ShippingNotice, task.id).order_data_version) == 1
|
||||
assert int(db.session.get(ShippingOrderTask, task.id).order_data_version) == 1
|
||||
|
||||
|
||||
def test_add_notice_line_creates_new_extraction_and_preserves_header_values(app):
|
||||
|
||||
Reference in new issue
Block a user