feat(shipping-notice): 添加订单文档 Profile 盲测集和样本管理功能,支持创建、执行和批准盲测,增强订单冻结和库存核验功能
This commit is contained in:
1 parent
c35223d49d
commit
abd8fc1e16
98 files changed
+4629
-528
No files matched your search
@@ -7,6 +7,7 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import date
|
||||
from decimal import Decimal
|
||||
from io import BytesIO
|
||||
from unittest.mock import patch
|
||||
|
||||
@@ -18,11 +19,13 @@ from app import db
|
||||
from app.models import (
|
||||
ShippingExcelTemplate, ShippingInventoryParsingProfile, ShippingInventorySource, ShippingOrderExtraction,
|
||||
ShippingOrderField, ShippingOrderFieldDecision, ShippingOrderFieldValue, ShippingOrderLine,
|
||||
ShippingCase, ShippingInventoryField, ShippingOrderSchema, ShippingOrderSchemaVersion, ShippingRpaProfile, ShippingTask,
|
||||
ShippingCase, ShippingInventoryField, ShippingOrderSchema, ShippingOrderSchemaVersion, ShippingOrderSnapshot,
|
||||
ShippingOrderSnapshotLine, ShippingRpaProfile, ShippingTask,
|
||||
)
|
||||
from app.services.shipping_notice import excel_template
|
||||
from app.services.shipping_notice.export import _filename, _system_fact, generate_shipment_excel
|
||||
from app.services.shipping_notice.fulfillment_workflow import _commit_generated_export
|
||||
from app.services.shipping_notice.reservation_service import record_inventory_verification
|
||||
from app.core.errors import ConflictError, ValidationError
|
||||
|
||||
|
||||
@@ -31,6 +34,36 @@ def _inventory_field_id(field_code: str) -> str:
|
||||
return str(ShippingInventoryField.query.filter_by(field_code=field_code).one().id)
|
||||
|
||||
|
||||
def _freeze_export_snapshot(
|
||||
task: ShippingTask, extraction: ShippingOrderExtraction, version: ShippingOrderSchemaVersion,
|
||||
*, header: dict[int, object] | None = None, lines: list[tuple[int, int, str, int, str]] | None = None,
|
||||
) -> None:
|
||||
"""为导出测试建立冻结订单和可用库存核验,避免测试绕过正式履约门禁。"""
|
||||
snapshot = ShippingOrderSnapshot(
|
||||
task_id=task.id, extraction_id=extraction.id, schema_version_id=version.id,
|
||||
order_data_version=1, rule_versions_json={},
|
||||
header_values_json={str(key): value for key, value in (header or {}).items()},
|
||||
)
|
||||
db.session.add(snapshot)
|
||||
db.session.flush()
|
||||
for position, field_id, part_number, qty, reservation_id in lines or []:
|
||||
db.session.add(ShippingOrderSnapshotLine(
|
||||
snapshot_id=snapshot.id, position=position, part_number=part_number,
|
||||
reservation_id=reservation_id, qty=Decimal(str(qty)),
|
||||
field_values_json={str(field_id): part_number},
|
||||
))
|
||||
db.session.flush()
|
||||
task.order_snapshot_id = snapshot.id
|
||||
record_inventory_verification(
|
||||
task, inventory_snapshot_id=task.inventory_snapshot_id, source_config_version='test-v1',
|
||||
line_results=[{
|
||||
'reservation_id': line.reservation_id, 'part_number': line.part_number,
|
||||
'required_qty': line.qty, 'available_qty': line.qty, 'line_status': 'ok',
|
||||
} for line in snapshot.lines],
|
||||
)
|
||||
db.session.flush()
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def _template_object_storage(monkeypatch):
|
||||
"""使用内存私有对象存储替身,验证模板不依赖本机文件系统。"""
|
||||
@@ -82,6 +115,37 @@ def test_filename_pattern_supports_business_number_and_split_business_variables(
|
||||
assert _filename(task, template) == 'WYC26060201_WYC26060201_DBS.xlsx'
|
||||
|
||||
|
||||
def test_filename_pattern_falls_back_to_controlled_environment_configuration(app):
|
||||
"""模板未提供规则时可使用受控环境变量,调用方仍不能提交任意文件名。"""
|
||||
with app.app_context():
|
||||
app.config['SHIPPING_NOTICE_EXPORT_FILENAME_PATTERN'] = 'default_{task_no}.xlsx'
|
||||
task = ShippingTask(task_no='TASK-FILENAME-DEFAULT')
|
||||
template = ShippingExcelTemplate(template_code='DEFAULT', display_name='Default', definition_json={})
|
||||
assert _filename(task, template) == 'default_TASK-FILENAME-DEFAULT.xlsx'
|
||||
|
||||
|
||||
def test_publish_allows_empty_template_filename_pattern_for_environment_fallback(app, tmp_path):
|
||||
"""空模板规则代表显式采用环境变量回退,发布时仍须完整校验 Excel 映射。"""
|
||||
app.config['SHIPPING_NOTICE_STORAGE_ROOT'] = str(tmp_path / 'shipping')
|
||||
with app.app_context():
|
||||
draft = excel_template.create_draft(_workbook_upload(), {
|
||||
'template_code': 'ENV_FALLBACK',
|
||||
'display_name': 'Environment fallback',
|
||||
'definition': {
|
||||
'filename_pattern': '',
|
||||
'mappings': [{
|
||||
'sheet': 'Header', 'row_mode': 'header', 'start_row': 1, 'target_column': 1,
|
||||
'source_kind': 'constant', 'source_key': 'fallback', 'formatter': 'text',
|
||||
}],
|
||||
},
|
||||
}, actor_id=None)
|
||||
|
||||
published = excel_template.publish(draft['id'], actor_id=None)
|
||||
|
||||
assert published['status'] == 'published'
|
||||
assert published['definition']['filename_pattern'] == ''
|
||||
|
||||
|
||||
def test_published_template_generates_dynamic_artifact(app, tmp_path):
|
||||
"""发布模板后按稳定 field_id 写入订单值,并锁定模板、Schema、Profile 与库存快照。"""
|
||||
app.config['SHIPPING_NOTICE_STORAGE_ROOT'] = str(tmp_path / 'shipping')
|
||||
@@ -158,6 +222,11 @@ def test_published_template_generates_dynamic_artifact(app, tmp_path):
|
||||
)
|
||||
db.session.add(task)
|
||||
db.session.flush()
|
||||
_freeze_export_snapshot(
|
||||
task, extraction, version,
|
||||
header={customer.id: 'acme', po_number.id: '16754796'},
|
||||
lines=[(1, part.id, 'PN-1', 1, 'RES-EXPORT-1')],
|
||||
)
|
||||
|
||||
path, filename, artifact = generate_shipment_excel(task)
|
||||
db.session.commit()
|
||||
@@ -337,6 +406,13 @@ def test_export_matches_inventory_by_profile_keys_not_row_position(app, tmp_path
|
||||
],
|
||||
)
|
||||
db.session.add(task); db.session.flush()
|
||||
_freeze_export_snapshot(
|
||||
task, extraction, version,
|
||||
lines=[
|
||||
(1, part.id, 'PN-A', 1, 'RES-KEYED-A'),
|
||||
(2, part.id, 'PN-B', 1, 'RES-KEYED-B'),
|
||||
],
|
||||
)
|
||||
|
||||
path, _filename, _artifact = generate_shipment_excel(task)
|
||||
workbook = load_workbook(path, read_only=True)
|
||||
@@ -501,6 +577,10 @@ def test_export_rejects_ambiguous_inventory_values_and_cleans_partial_file(app,
|
||||
],
|
||||
)
|
||||
db.session.add(task); db.session.flush()
|
||||
_freeze_export_snapshot(
|
||||
task, extraction, version,
|
||||
lines=[(1, part.id, 'PN-A', 1, 'RES-AMBIGUOUS-A')],
|
||||
)
|
||||
|
||||
with pytest.raises(ValueError, match='值不一致'):
|
||||
generate_shipment_excel(task)
|
||||
|
||||
Reference in new issue
Block a user