refactor(shipping-line): 统一订单与通知明细项次并移除旧键
This commit is contained in:
1 parent
938f3dd0e8
commit
b5d894a923
11 files changed
+331
-302
No files matched your search
@@ -20,7 +20,8 @@ from app.models import (
|
||||
ShippingExcelTemplate, ShippingInventoryParsingProfile, ShippingInventorySource, ShippingOrderExtraction,
|
||||
ShippingOrderField, ShippingOrderFieldDecision, ShippingOrderFieldValue, ShippingOrderLine,
|
||||
ShippingCustomer, ShippingInventoryField, ShippingInventoryFile, ShippingOrderSchema, ShippingOrderSchemaVersion,
|
||||
ShippingOrderSnapshot, ShippingOrderSnapshotLine, ShippingRpaProfile, ShippingNotice,
|
||||
ShippingOrderSnapshot, ShippingOrderSnapshotLine, ShippingRpaProfile, ShippingNotice, ShippingOrderTask,
|
||||
ShippingNoticeLineInventoryMatch, ShippingTaskInventoryLink,
|
||||
)
|
||||
from app.services.shipping_notice import excel_template
|
||||
from app.services.shipping_notice.export import (
|
||||
@@ -32,6 +33,7 @@ from app.services.shipping_notice.export import (
|
||||
generate_shipment_excel,
|
||||
)
|
||||
from app.services.shipping_notice.fulfillment_workflow import _commit_generated_export
|
||||
from app.services.shipping_notice.notice_line_service import replace_notice_lines_from_snapshot
|
||||
from app.services.shipping_notice.inventory_file_store import (
|
||||
active_inventory_lines_for_task,
|
||||
replace_task_inventory_link,
|
||||
@@ -77,10 +79,11 @@ def test_only_templates_consuming_system_warehouse_id_require_task_warehouse_fac
|
||||
|
||||
|
||||
def _freeze_export_snapshot(
|
||||
task: ShippingNotice, extraction: ShippingOrderExtraction, version: ShippingOrderSchemaVersion,
|
||||
task: ShippingOrderTask, extraction: ShippingOrderExtraction, version: ShippingOrderSchemaVersion,
|
||||
*, header: dict[int, object] | None = None, lines: list[tuple[int, int, str, int, str]] | None = None,
|
||||
) -> None:
|
||||
"""为导出测试建立冻结订单和可用库存核验,避免测试绕过正式履约门禁。"""
|
||||
customer_id: int | None = None, inventory_file_id: int | None = None,
|
||||
) -> ShippingOrderSnapshot:
|
||||
"""为导出测试在 OT 上建立冻结订单和可用库存核验,避免测试绕过正式履约门禁。"""
|
||||
field_codes = {
|
||||
str(item.get('field_id')): str(item.get('field_code') or '')
|
||||
for item in (version.definition_json or {}).get('fields') or []
|
||||
@@ -91,6 +94,7 @@ def _freeze_export_snapshot(
|
||||
)
|
||||
snapshot = ShippingOrderSnapshot(
|
||||
task_id=task.id, extraction_id=extraction.id, schema_version_id=version.id,
|
||||
customer_id=customer_id,
|
||||
order_data_version=1, rule_versions_json={},
|
||||
header_values_json={str(key): value for key, value in (header or {}).items()},
|
||||
customer_po_number=po_number,
|
||||
@@ -99,20 +103,59 @@ def _freeze_export_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)),
|
||||
snapshot_id=snapshot.id, position=position, line_no=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',
|
||||
task, inventory_snapshot_id=inventory_file_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()
|
||||
return snapshot
|
||||
|
||||
|
||||
def _materialize_export_notice(
|
||||
*, ot: ShippingOrderTask, snapshot: ShippingOrderSnapshot, inventory_file: ShippingInventoryFile,
|
||||
source_code: str, notice_no: str,
|
||||
) -> ShippingNotice:
|
||||
"""从 OT 的冻结快照构造正式 Notice、NoticeLine、匹配事实与活动库存链接。
|
||||
|
||||
导出链路只接受已物化的正式 Notice;OT 承载订单/库存事实,Notice 通过
|
||||
order_task_id 指向 OT。行级匹配事实按 OT id 落库,库存 Link 绑定 Notice 本身。
|
||||
"""
|
||||
notice = ShippingNotice(
|
||||
task_no=notice_no, source_type='manual', order_task_id=ot.id, user_id=ot.user_id,
|
||||
)
|
||||
db.session.add(notice)
|
||||
db.session.flush()
|
||||
replace_notice_lines_from_snapshot(
|
||||
notice_id=notice.id, snapshot=snapshot,
|
||||
inventory_facts_by_snapshot_line_id={
|
||||
int(line.id): {
|
||||
'inventory_source_code': source_code,
|
||||
'inventory_file_id': int(inventory_file.id),
|
||||
'inventory_warehouse_id': inventory_file.warehouse_id,
|
||||
}
|
||||
for line in snapshot.lines
|
||||
},
|
||||
)
|
||||
for line in snapshot.lines:
|
||||
db.session.add(ShippingNoticeLineInventoryMatch(
|
||||
task_id=ot.id, snapshot_line_id=line.id, line_no=line.line_no,
|
||||
match_result='matched', inventory_status='ready',
|
||||
inventory_snapshot_id=inventory_file.id, inventory_source_code=source_code,
|
||||
is_current=True, detail_json={},
|
||||
))
|
||||
db.session.flush()
|
||||
replace_task_inventory_link(notice, inventory_file, role='owner')
|
||||
db.session.flush()
|
||||
return notice
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
@@ -153,23 +196,21 @@ def _workbook_upload() -> FileStorage:
|
||||
def test_filename_pattern_supports_business_number_and_split_business_variables():
|
||||
"""业务单号与客户日期流水变量均从业务单快照渲染,不能依赖当前客户主数据。"""
|
||||
task = ShippingNotice(
|
||||
task_no='TASK-FILENAME', inventory_source_code='DBS', business_no='WYC26060201',
|
||||
customer_short_code_snapshot='WYC', business_date=date(2026, 6, 2), business_sequence_value=1,
|
||||
task_no='TASK-FILENAME', business_no='WYC26060201',
|
||||
business_date=date(2026, 6, 2), business_sequence_value=1,
|
||||
)
|
||||
template = ShippingExcelTemplate(
|
||||
template_code='FILENAME', display_name='Filename',
|
||||
definition_json={'filename_pattern': '{business_no}_{customer_short_code}{business_date}{sequence}_{source_code}.xlsx'},
|
||||
definition_json={'filename_pattern': '{business_no}_{business_date}_{sequence}.xlsx'},
|
||||
)
|
||||
|
||||
assert _filename(task, template) == 'WYC26060201_WYC26060201_DBS.xlsx'
|
||||
assert _filename(task, template) == 'WYC26060201_260602_01.xlsx'
|
||||
|
||||
|
||||
def test_inventory_execution_prefers_active_link_and_does_not_overwrite_task_cache(app, tmp_path):
|
||||
"""业务执行必须使用活动库存 Link,忽略请求库存和旧任务缓存。"""
|
||||
"""业务执行必须使用活动库存 Link,忽略请求库存。"""
|
||||
with app.app_context():
|
||||
task = ShippingNotice(
|
||||
task_no='TASK-INVENTORY-SOURCE', inventory_lines_json=[{'part_number': 'STALE'}],
|
||||
)
|
||||
task = ShippingOrderTask(task_no='OT-INVENTORY-SOURCE', user_id=1)
|
||||
inventory_file = ShippingInventoryFile(
|
||||
inventory_file_no='SIF-INVENTORY-SOURCE', storage_path=str(tmp_path / 'inventory.xlsx'),
|
||||
inventory_lines_json=[{'part_number': 'ACTIVE'}], line_count=1,
|
||||
@@ -185,22 +226,20 @@ def test_inventory_execution_prefers_active_link_and_does_not_overwrite_task_cac
|
||||
)
|
||||
|
||||
assert resolved == [{'part_number': 'ACTIVE'}]
|
||||
assert task.inventory_lines_json == [{'part_number': 'STALE'}]
|
||||
|
||||
|
||||
def test_inventory_execution_keeps_legacy_fallback_without_active_link(app):
|
||||
"""无活动 Link 的历史任务仍可读取请求库存或旧缓存,避免升级后无法恢复。"""
|
||||
"""无活动 Link 的历史任务仍可读取请求库存,避免升级后无法恢复。"""
|
||||
with app.app_context():
|
||||
task = ShippingNotice(
|
||||
task_no='TASK-INVENTORY-LEGACY', inventory_lines_json=[{'part_number': 'CACHE'}],
|
||||
)
|
||||
task = ShippingOrderTask(task_no='OT-INVENTORY-LEGACY', user_id=1)
|
||||
db.session.add(task)
|
||||
db.session.flush()
|
||||
|
||||
assert resolve_inventory_lines_for_execution(
|
||||
task, legacy_payload_lines=[{'part_number': 'PAYLOAD'}],
|
||||
) == [{'part_number': 'PAYLOAD'}]
|
||||
assert resolve_inventory_lines_for_execution(task) == [{'part_number': 'CACHE'}]
|
||||
# 既无活动 Link 也无旧缓存时返回空,不伪造或回退到其它任务库存。
|
||||
assert resolve_inventory_lines_for_execution(task) == []
|
||||
|
||||
|
||||
def test_active_inventory_json_is_not_silently_reparsed_from_stale_file(app, tmp_path, monkeypatch):
|
||||
@@ -208,7 +247,7 @@ def test_active_inventory_json_is_not_silently_reparsed_from_stale_file(app, tmp
|
||||
with app.app_context():
|
||||
storage_path = tmp_path / 'stale.xlsx'
|
||||
storage_path.write_bytes(b'stale-source')
|
||||
task = ShippingNotice(task_no='TASK-INVENTORY-NO-REPARSE')
|
||||
task = ShippingOrderTask(task_no='OT-INVENTORY-NO-REPARSE', user_id=1)
|
||||
inventory_file = ShippingInventoryFile(
|
||||
inventory_file_no='SIF-INVENTORY-NO-REPARSE', storage_path=str(storage_path),
|
||||
inventory_lines_json=[{'1': 'PN-A', '2': 'RSV-A', '6': 'ODM-FIXED'}],
|
||||
@@ -269,6 +308,8 @@ def test_published_template_generates_dynamic_artifact(app, tmp_path):
|
||||
"""发布模板后按稳定 field_id 写入订单值,并锁定模板、Schema、Profile 与库存快照。"""
|
||||
app.config['SHIPPING_NOTICE_STORAGE_ROOT'] = str(tmp_path / 'shipping')
|
||||
with app.app_context():
|
||||
from tests.conftest import confirm_test_customer_resolution
|
||||
|
||||
schema = ShippingOrderSchema(schema_code=DEFAULT_SCHEMA_FAMILY_CODE, display_name='Order')
|
||||
db.session.add(schema)
|
||||
db.session.flush()
|
||||
@@ -315,10 +356,15 @@ def test_published_template_generates_dynamic_artifact(app, tmp_path):
|
||||
parsing_profile_id=parsing.id, excel_template_id=published['id'], warehouse_resolution_mode='fixed',
|
||||
fixed_warehouse_id='DUB', report_path_json={}, query_parameters_json=[],
|
||||
)
|
||||
extraction = ShippingOrderExtraction(schema_version_id=version.id, input_snapshot_hash='i' * 64, status='accepted')
|
||||
ot = ShippingOrderTask(task_no='OT-EXPORT-1', user_id=1, status='ready', current_step='customer_review')
|
||||
db.session.add(ot)
|
||||
db.session.flush()
|
||||
extraction = ShippingOrderExtraction(
|
||||
task_id=ot.id, schema_version_id=version.id, input_snapshot_hash='i' * 64, status='accepted',
|
||||
)
|
||||
db.session.add_all([source, extraction])
|
||||
db.session.flush()
|
||||
order_line = ShippingOrderLine(extraction_id=extraction.id, position=1, line_key='1')
|
||||
order_line = ShippingOrderLine(extraction_id=extraction.id, position=1, line_no=2)
|
||||
db.session.add(order_line)
|
||||
db.session.flush()
|
||||
header_decision = ShippingOrderFieldDecision(extraction_id=extraction.id, field_id=customer.id, decision_status='accepted', resolved_value_json='acme')
|
||||
@@ -336,21 +382,26 @@ def test_published_template_generates_dynamic_artifact(app, tmp_path):
|
||||
)
|
||||
db.session.add(confirmed_customer)
|
||||
db.session.flush()
|
||||
task = ShippingNotice(
|
||||
task_no='TASK-EXPORT-1', current_order_extraction_id=extraction.id, inventory_source_code='DUB',
|
||||
customer_id=confirmed_customer.id,
|
||||
customer_resolution_status='confirmed',
|
||||
# 此模板只消费 ODM 等 inventory_field;任务级 Warehouse ID 缺失不应阻断导出。
|
||||
inventory_warehouse_id=None, inventory_lines_json=[{
|
||||
confirm_test_customer_resolution(ot, customer=confirmed_customer, extraction=extraction)
|
||||
inventory_file = ShippingInventoryFile(
|
||||
inventory_file_no='SIF-EXPORT-1', user_id=None,
|
||||
source_file_name='inventory.xlsx', storage_path=str(tmp_path / 'inventory.xlsx'),
|
||||
inventory_lines_json=[{
|
||||
_inventory_field_id('part_number'): 'PN-1', _inventory_field_id('odm_id'): 'ODM-9',
|
||||
}], inventory_snapshot_id=None,
|
||||
}],
|
||||
line_count=1, share_mode='task_only', warehouse_id='DUB', source_code='DUB', snapshot_status='ready',
|
||||
)
|
||||
db.session.add(task)
|
||||
db.session.add(inventory_file)
|
||||
db.session.flush()
|
||||
_freeze_export_snapshot(
|
||||
task, extraction, version,
|
||||
snapshot = _freeze_export_snapshot(
|
||||
ot, extraction, version,
|
||||
header={customer.id: 'acme', po_number.id: '16754796'},
|
||||
lines=[(1, part.id, 'PN-1', 1, '1000000001')],
|
||||
customer_id=confirmed_customer.id, inventory_file_id=inventory_file.id,
|
||||
)
|
||||
# 此模板只消费 ODM 等 inventory_field;任务级 Warehouse ID 缺失不应阻断导出。
|
||||
task = _materialize_export_notice(
|
||||
ot=ot, snapshot=snapshot, inventory_file=inventory_file, source_code='DUB', notice_no='TASK-EXPORT-1',
|
||||
)
|
||||
|
||||
assert task.business_no is None
|
||||
@@ -427,7 +478,7 @@ def test_export_mapping_uses_sequence_but_never_derives_formal_line_amount(app):
|
||||
|
||||
assert _mapping_value(
|
||||
{'source_kind': 'order_field', 'source_key': fields['line_no'].id, 'formatter': 'integer', 'row_mode': 'line'},
|
||||
task=task, order_values={}, inventory_values={}, export_sequence=1,
|
||||
task=task, order_values={}, inventory_values={}, notice_line_no=1,
|
||||
) == 1
|
||||
assert _mapping_value(
|
||||
{'source_kind': 'order_field', 'source_key': fields['line_amount_with_tax'].id, 'formatter': 'decimal', 'row_mode': 'line'},
|
||||
@@ -523,6 +574,8 @@ def test_export_matches_inventory_by_profile_keys_not_row_position(app, tmp_path
|
||||
"""库存行顺序与订单相反时仍须按稳定 match_keys 写入正确 ODM,禁止按数组下标配对。"""
|
||||
app.config['SHIPPING_NOTICE_STORAGE_ROOT'] = str(tmp_path / 'shipping')
|
||||
with app.app_context():
|
||||
from tests.conftest import confirm_test_customer_resolution
|
||||
|
||||
schema = ShippingOrderSchema(schema_code=DEFAULT_SCHEMA_FAMILY_CODE, display_name='Order')
|
||||
db.session.add(schema); db.session.flush()
|
||||
part = _lookup_standard_fields('part_number')['part_number']
|
||||
@@ -552,10 +605,16 @@ def test_export_matches_inventory_by_profile_keys_not_row_position(app, tmp_path
|
||||
parsing_profile_id=parsing.id, excel_template_id=published['id'], warehouse_resolution_mode='fixed',
|
||||
fixed_warehouse_id='DUB', report_path_json={}, query_parameters_json=[],
|
||||
))
|
||||
extraction = ShippingOrderExtraction(schema_version_id=version.id, input_snapshot_hash='i' * 64, status='accepted')
|
||||
customer = ShippingCustomer(standard_name='键控匹配客户', short_code='KEY', status='active')
|
||||
db.session.add(customer); db.session.flush()
|
||||
ot = ShippingOrderTask(task_no='OT-KEYED', user_id=1, status='ready', current_step='customer_review')
|
||||
db.session.add(ot); db.session.flush()
|
||||
extraction = ShippingOrderExtraction(
|
||||
task_id=ot.id, schema_version_id=version.id, input_snapshot_hash='i' * 64, status='accepted',
|
||||
)
|
||||
db.session.add(extraction); db.session.flush()
|
||||
for position, value in enumerate(('PN-A', 'PN-B'), start=1):
|
||||
line = ShippingOrderLine(extraction_id=extraction.id, position=position, line_key=str(position))
|
||||
line = ShippingOrderLine(extraction_id=extraction.id, position=position, line_no=position + 1)
|
||||
db.session.add(line); db.session.flush()
|
||||
decision = ShippingOrderFieldDecision(
|
||||
extraction_id=extraction.id, line_id=line.id, field_id=part.id,
|
||||
@@ -566,20 +625,27 @@ def test_export_matches_inventory_by_profile_keys_not_row_position(app, tmp_path
|
||||
extraction_id=extraction.id, line_id=line.id, field_id=part.id,
|
||||
value_text=value, decision_id=decision.id,
|
||||
))
|
||||
task = ShippingNotice(
|
||||
task_no='TASK-KEYED', current_order_extraction_id=extraction.id, inventory_source_code='KEYED',
|
||||
inventory_warehouse_id='DUB', inventory_lines_json=[
|
||||
confirm_test_customer_resolution(ot, customer=customer, extraction=extraction)
|
||||
inventory_file = ShippingInventoryFile(
|
||||
inventory_file_no='SIF-KEYED', user_id=None, source_file_name='keyed.xlsx',
|
||||
storage_path=str(tmp_path / 'keyed.xlsx'),
|
||||
inventory_lines_json=[
|
||||
{_inventory_field_id('part_number'): 'PN-B', _inventory_field_id('odm_id'): 'ODM-B'},
|
||||
{_inventory_field_id('part_number'): 'PN-A', _inventory_field_id('odm_id'): 'ODM-A'},
|
||||
],
|
||||
line_count=2, share_mode='task_only', warehouse_id='DUB', source_code='KEYED', snapshot_status='ready',
|
||||
)
|
||||
db.session.add(task); db.session.flush()
|
||||
_freeze_export_snapshot(
|
||||
task, extraction, version,
|
||||
db.session.add(inventory_file); db.session.flush()
|
||||
snapshot = _freeze_export_snapshot(
|
||||
ot, extraction, version,
|
||||
lines=[
|
||||
(1, part.id, 'PN-A', 1, '1000000002'),
|
||||
(2, part.id, 'PN-B', 1, '1000000003'),
|
||||
],
|
||||
customer_id=customer.id, inventory_file_id=inventory_file.id,
|
||||
)
|
||||
task = _materialize_export_notice(
|
||||
ot=ot, snapshot=snapshot, inventory_file=inventory_file, source_code='KEYED', notice_no='TASK-KEYED',
|
||||
)
|
||||
|
||||
path, _filename, _artifact = generate_shipment_excel(task)
|
||||
@@ -700,6 +766,8 @@ def test_export_uses_active_inventory_link_instead_of_stale_task_cache(app, tmp_
|
||||
"""导出应读取活动库存 Link;无 Link 时旧缓存冲突仍须阻断且不留半成品。"""
|
||||
app.config['SHIPPING_NOTICE_STORAGE_ROOT'] = str(tmp_path / 'shipping')
|
||||
with app.app_context():
|
||||
from tests.conftest import confirm_test_customer_resolution
|
||||
|
||||
schema = ShippingOrderSchema(schema_code=DEFAULT_SCHEMA_FAMILY_CODE, display_name='Order')
|
||||
db.session.add(schema); db.session.flush()
|
||||
part = _lookup_standard_fields('part_number')['part_number']
|
||||
@@ -726,9 +794,15 @@ def test_export_uses_active_inventory_link_instead_of_stale_task_cache(app, tmp_
|
||||
parsing_profile_id=parsing.id, excel_template_id=published['id'], warehouse_resolution_mode='fixed',
|
||||
fixed_warehouse_id='DUB', report_path_json={}, query_parameters_json=[],
|
||||
))
|
||||
extraction = ShippingOrderExtraction(schema_version_id=version.id, input_snapshot_hash='i' * 64, status='accepted')
|
||||
customer = ShippingCustomer(standard_name='模糊匹配客户', short_code='AMB', status='active')
|
||||
db.session.add(customer); db.session.flush()
|
||||
ot = ShippingOrderTask(task_no='OT-AMBIGUOUS', user_id=1, status='ready', current_step='customer_review')
|
||||
db.session.add(ot); db.session.flush()
|
||||
extraction = ShippingOrderExtraction(
|
||||
task_id=ot.id, schema_version_id=version.id, input_snapshot_hash='i' * 64, status='accepted',
|
||||
)
|
||||
db.session.add(extraction); db.session.flush()
|
||||
line = ShippingOrderLine(extraction_id=extraction.id, position=1, line_key='1')
|
||||
line = ShippingOrderLine(extraction_id=extraction.id, position=1, line_no=2)
|
||||
db.session.add(line); db.session.flush()
|
||||
decision = ShippingOrderFieldDecision(
|
||||
extraction_id=extraction.id, line_id=line.id, field_id=part.id,
|
||||
@@ -739,17 +813,25 @@ def test_export_uses_active_inventory_link_instead_of_stale_task_cache(app, tmp_
|
||||
extraction_id=extraction.id, line_id=line.id, field_id=part.id,
|
||||
value_text='PN-A', decision_id=decision.id,
|
||||
))
|
||||
task = ShippingNotice(
|
||||
task_no='TASK-AMBIGUOUS', current_order_extraction_id=extraction.id,
|
||||
inventory_source_code='AMBIGUOUS', inventory_warehouse_id='DUB', inventory_lines_json=[
|
||||
confirm_test_customer_resolution(ot, customer=customer, extraction=extraction)
|
||||
ambiguous_file = ShippingInventoryFile(
|
||||
inventory_file_no='SIF-AMBIGUOUS', user_id=None, source_file_name='ambiguous.xlsx',
|
||||
storage_path=str(tmp_path / 'ambiguous.xlsx'),
|
||||
inventory_lines_json=[
|
||||
{_inventory_field_id('part_number'): 'PN-A', _inventory_field_id('odm_id'): 'ODM-1'},
|
||||
{_inventory_field_id('part_number'): 'PN-A', _inventory_field_id('odm_id'): 'ODM-2'},
|
||||
],
|
||||
line_count=2, share_mode='task_only', warehouse_id='DUB', source_code='AMBIGUOUS',
|
||||
snapshot_status='ready',
|
||||
)
|
||||
db.session.add(task); db.session.flush()
|
||||
_freeze_export_snapshot(
|
||||
task, extraction, version,
|
||||
lines=[(1, part.id, 'PN-A', 1, '1000000004')],
|
||||
db.session.add(ambiguous_file); db.session.flush()
|
||||
snapshot = _freeze_export_snapshot(
|
||||
ot, extraction, version,
|
||||
lines=[(1, part.id, 'PN-A', 1, '1000000004')],
|
||||
customer_id=customer.id, inventory_file_id=ambiguous_file.id,
|
||||
)
|
||||
task = _materialize_export_notice(
|
||||
ot=ot, snapshot=snapshot, inventory_file=ambiguous_file, source_code='AMBIGUOUS', notice_no='TASK-AMBIGUOUS',
|
||||
)
|
||||
|
||||
with pytest.raises(ValueError, match='库存字段“ODM 编号”'):
|
||||
@@ -770,7 +852,9 @@ def test_export_uses_active_inventory_link_instead_of_stale_task_cache(app, tmp_
|
||||
db.session.add(inventory_file)
|
||||
db.session.flush()
|
||||
replace_task_inventory_link(task, inventory_file, role='owner')
|
||||
task.inventory_snapshot_id = inventory_file.id
|
||||
# 切换活动库存后,行级匹配事实须指向新快照;导出只读取活动 Link。
|
||||
for match in ShippingNoticeLineInventoryMatch.query.filter_by(task_id=ot.id, is_current=True).all():
|
||||
match.inventory_snapshot_id = inventory_file.id
|
||||
db.session.flush()
|
||||
|
||||
path, _filename_value, artifact = generate_shipment_excel(task)
|
||||
@@ -781,7 +865,6 @@ def test_export_uses_active_inventory_link_instead_of_stale_task_cache(app, tmp_
|
||||
finally:
|
||||
workbook.close()
|
||||
assert artifact.inventory_snapshot_id == inventory_file.id
|
||||
assert task.inventory_lines_json[0][_inventory_field_id('odm_id')] == 'ODM-1'
|
||||
|
||||
|
||||
def test_export_commit_failure_removes_generated_file(app, tmp_path, monkeypatch):
|
||||
|
||||
@@ -1,113 +1,48 @@
|
||||
"""
|
||||
backend/tests/test_shipping_order_line_no_resolver.py
|
||||
|
||||
用途:order_line_no_resolver 单一解析器契约测试(项次展示/导出同源)。
|
||||
依赖:pytest、resolver、field_rules 格式门禁。
|
||||
逻辑:覆盖 field 真实值 / customer_po_line_no 回退 / sequence 展示序号 / 全空缺失四级分支,
|
||||
并验证 field_id 键与历史 field_code 键两种入参格式;mark 为 pure_unit 跳过 Flask 创建。
|
||||
用途:验证订单项次解析器只读取系统持久化的 line_no。
|
||||
依赖:order_line_no_resolver。
|
||||
逻辑:line_no 缺失时显式返回空值;客户 PO 行号、field_id 映射和展示序号均不得成为回退来源。
|
||||
"""
|
||||
|
||||
import pytest
|
||||
|
||||
from app.services.shipping_notice.order_line_no_resolver import (
|
||||
LINE_NO_SOURCE_CUSTOMER_PO_LINE,
|
||||
LINE_NO_SOURCE_FIELD,
|
||||
LINE_NO_SOURCE_SEQUENCE,
|
||||
field_id_by_code_from_definition,
|
||||
resolve_order_line_no,
|
||||
)
|
||||
|
||||
LINE_NO_ID = 'line-no-id'
|
||||
CUSTOMER_PO_LINE_ID = 'customer-po-line-id'
|
||||
FIELD_ID_BY_CODE = {
|
||||
'line_no': LINE_NO_ID,
|
||||
'customer_po_line_no': CUSTOMER_PO_LINE_ID,
|
||||
}
|
||||
|
||||
@pytest.mark.pure_unit
|
||||
def test_resolver_reads_persisted_line_no_only():
|
||||
"""系统已持久化项次时原样返回,不进行派生或格式重排。"""
|
||||
assert resolve_order_line_no(
|
||||
field_values={'line_no': '00010', 'customer_po_line_no': '00020'},
|
||||
field_id_by_code={'line_no': 'line-no-id'},
|
||||
) == {'value': '00010', 'source': 'persisted'}
|
||||
|
||||
|
||||
@pytest.mark.pure_unit
|
||||
def test_resolver_prefers_real_line_no_field_value():
|
||||
"""line_no 字段真实值必须优先于客户 PO 行号与序号。"""
|
||||
def test_resolver_does_not_use_field_id_or_customer_po_line_no():
|
||||
"""项次不是 Schema 字段值,也不能从客户 PO 行号推断。"""
|
||||
assert resolve_order_line_no(
|
||||
field_values={LINE_NO_ID: '00010', CUSTOMER_PO_LINE_ID: '00020'},
|
||||
field_id_by_code=FIELD_ID_BY_CODE,
|
||||
fallback_sequence=1,
|
||||
) == {'value': '00010', 'source': LINE_NO_SOURCE_FIELD}
|
||||
|
||||
|
||||
@pytest.mark.pure_unit
|
||||
def test_resolver_prefers_real_line_no_over_sequence_and_customer():
|
||||
"""line_no 缺失时按顺序回退 customer_po_line_no,不直接落序号。"""
|
||||
assert resolve_order_line_no(
|
||||
field_values={CUSTOMER_PO_LINE_ID: '10'},
|
||||
field_id_by_code=FIELD_ID_BY_CODE,
|
||||
fallback_sequence=1,
|
||||
) == {'value': '10', 'source': LINE_NO_SOURCE_CUSTOMER_PO_LINE}
|
||||
|
||||
|
||||
@pytest.mark.pure_unit
|
||||
def test_resolver_rejects_invalid_customer_line_no_format():
|
||||
"""客户 PO 行号格式非法(非 1–6 位数字)时不得当业务项次,回退展示序号。"""
|
||||
assert resolve_order_line_no(
|
||||
field_values={'line_no': None, CUSTOMER_PO_LINE_ID: 'ABC'},
|
||||
field_id_by_code=FIELD_ID_BY_CODE,
|
||||
fallback_sequence=2,
|
||||
) == {'value': '2', 'source': LINE_NO_SOURCE_SEQUENCE}
|
||||
|
||||
|
||||
@pytest.mark.pure_unit
|
||||
def test_resolver_uses_sequence_when_business_fields_missing():
|
||||
"""line_no 与 customer_po_line_no 均缺失时仅能使用显式标注的展示序号。"""
|
||||
assert resolve_order_line_no(
|
||||
field_values={'line_no': None},
|
||||
field_id_by_code=FIELD_ID_BY_CODE,
|
||||
fallback_sequence=3,
|
||||
) == {'value': '3', 'source': LINE_NO_SOURCE_SEQUENCE}
|
||||
|
||||
|
||||
@pytest.mark.pure_unit
|
||||
def test_resolver_missing_everything_returns_none_source():
|
||||
"""全部事实缺失且无序号时返回 None/None,禁止伪造空字符串或 0。"""
|
||||
assert resolve_order_line_no(
|
||||
field_values={},
|
||||
field_id_by_code=FIELD_ID_BY_CODE,
|
||||
fallback_sequence=None,
|
||||
field_values={'line-no-id': '10', 'customer_po_line_no': '20'},
|
||||
field_id_by_code={'line_no': 'line-no-id'},
|
||||
) == {'value': None, 'source': None}
|
||||
|
||||
|
||||
@pytest.mark.pure_unit
|
||||
def test_resolver_supports_legacy_field_code_keys():
|
||||
"""历史/测试数据按 field_code 键存储时,解析器仍能正确读取。"""
|
||||
def test_resolver_does_not_fallback_to_display_sequence():
|
||||
"""缺少持久化项次时显式返回空值,展示层不能伪造业务项次。"""
|
||||
assert resolve_order_line_no(
|
||||
field_values={'line_no': '00020', 'customer_po_line_no': '00030'},
|
||||
field_id_by_code=None,
|
||||
fallback_sequence=5,
|
||||
) == {'value': '00020', 'source': LINE_NO_SOURCE_FIELD}
|
||||
field_values={},
|
||||
) == {'value': None, 'source': None}
|
||||
|
||||
|
||||
@pytest.mark.pure_unit
|
||||
def test_resolver_fallbacks_to_code_key_when_field_id_missing():
|
||||
"""field_id 键缺失时回退读取 field_code 键,避免跨格式读取失败。"""
|
||||
assert resolve_order_line_no(
|
||||
field_values={CUSTOMER_PO_LINE_ID: '10', 'customer_po_line_no': '20'},
|
||||
field_id_by_code=FIELD_ID_BY_CODE,
|
||||
fallback_sequence=1,
|
||||
) == {'value': '10', 'source': LINE_NO_SOURCE_CUSTOMER_PO_LINE}
|
||||
|
||||
|
||||
@pytest.mark.pure_unit
|
||||
def test_resolver_keeps_leading_zeros():
|
||||
"""项次原样输出,保留前导零(如 00010),禁止转 int 丢零。"""
|
||||
assert resolve_order_line_no(
|
||||
field_values={'line_no': '00010'},
|
||||
field_id_by_code=FIELD_ID_BY_CODE,
|
||||
fallback_sequence=1,
|
||||
) == {'value': '00010', 'source': LINE_NO_SOURCE_FIELD}
|
||||
|
||||
|
||||
@pytest.mark.pure_unit
|
||||
def test_field_id_by_code_from_definition_extracts_only_pair():
|
||||
"""Schema 定义中仅提取同时具备 field_code/field_id 的字段。"""
|
||||
def test_field_id_by_code_from_definition_is_metadata_only():
|
||||
"""Schema 映射仍可独立解析,但不会参与 line_no 读取。"""
|
||||
definition = {'fields': [
|
||||
{'field_code': 'line_no', 'field_id': 'a', 'scope': 'line'},
|
||||
{'field_code': 'customer_po_line_no', 'field_id': 'b'},
|
||||
|
||||
Reference in new issue
Block a user