闭合拆分预览令牌单次消费、样本 checksum 绑定与确认审计;正式提取改为 bindings-only。 同步统一标注工作台交互,并补齐进度投影/任务中心与迁移文档。 Co-authored-by: Cursor <cursoragent@cursor.com>
181 lines
7.4 KiB
Python
181 lines
7.4 KiB
Python
"""
|
||
tests/test_shipping_xfa_unified_migration.py
|
||
|
||
用途:验收统一 binding 一次性迁移(坐标 + XFA Dry Run;路径保留 100%;正式字段差异 0)。
|
||
依赖:order_profile_unified_migration、pytest。
|
||
逻辑:使用合成 definition,不接真实 PDF;断言报告指标与 convert 稳定性。
|
||
"""
|
||
|
||
from __future__ import annotations
|
||
|
||
from app.services.shipping_notice.order_profile_unified_migration import (
|
||
_clear_xfa_legacy_mappings,
|
||
build_migration_report,
|
||
convert_coordinate_definition_to_unified_bindings,
|
||
convert_xfa_definition_to_unified_bindings,
|
||
)
|
||
|
||
|
||
def _coordinate_definition() -> dict:
|
||
"""构造最小合法坐标 definition(含 order_field_layouts)。"""
|
||
return {
|
||
'template_mode': 'coordinate',
|
||
'document_match': {
|
||
'required_anchors': [],
|
||
'optional_anchors': [],
|
||
'negative_anchors': [],
|
||
},
|
||
'table_regions': [{
|
||
'region_id': 'table_main',
|
||
'page_scope': 'first_page',
|
||
'region': {'x0': 0.1, 'y0': 0.2, 'x1': 0.9, 'y1': 0.8},
|
||
'header_layouts': [],
|
||
'line_layouts': [{
|
||
'layout_id': 'line_1',
|
||
'field_regions': [{
|
||
'region_id': 'line_part',
|
||
'field_id': 'fld-part',
|
||
'field_code': 'part_number',
|
||
'scope': 'line',
|
||
'region': {'x0': 0.2, 'y0': 0.3, 'x1': 0.4, 'y1': 0.35},
|
||
'allowed_sources': ['pdf_text'],
|
||
'extractor_key': 'direct',
|
||
}],
|
||
}],
|
||
'continuation_rule': {'required_anchor_ids': []},
|
||
}],
|
||
'order_field_layouts': [{
|
||
'layout_id': 'hdr_po',
|
||
'field_id': 'fld-po',
|
||
'field_code': 'customer_po_number',
|
||
'scope': 'header',
|
||
'page_scope': 'first_page',
|
||
'absolute_value_region': {'x0': 0.1, 'y0': 0.1, 'x1': 0.3, 'y1': 0.14},
|
||
'minimum_evidence_level': 'coordinate',
|
||
}],
|
||
'draft_unbound_order_field_layouts': [],
|
||
}
|
||
|
||
|
||
def _xfa_definition() -> dict:
|
||
"""构造最小合法 XFA definition(header + line mapping)。"""
|
||
return {
|
||
'template_mode': 'xfa_dataset',
|
||
'source_contract': {
|
||
'template_packet_checksum': 'sha256:' + ('a' * 64),
|
||
'dataset_structure_fingerprint': 'sha256:' + ('b' * 64),
|
||
'dataset_root_path': '/data',
|
||
'authoritative_line_collection_path': '/data/ITEMS/DATA',
|
||
'mirror_collection_paths': ['/data/ITEMS1/DATA'],
|
||
},
|
||
'header_mappings': [{
|
||
'mapping_id': 'map-po',
|
||
'field_id': 'fld-po',
|
||
'field_code': 'customer_po_number',
|
||
'data_path': '/data/HEADER/EBELN',
|
||
'path_id': 'path-ebeln',
|
||
'value_type': 'string',
|
||
}],
|
||
'line_mapping': {
|
||
'row_path': '/data/ITEMS/DATA',
|
||
'fields': [{
|
||
'mapping_id': 'map-matnr',
|
||
'field_id': 'fld-part',
|
||
'field_code': 'part_number',
|
||
'data_path': '/data/ITEMS/DATA/MATNR',
|
||
'path_id': 'path-matnr',
|
||
'value_type': 'string',
|
||
}],
|
||
},
|
||
'bindings': [],
|
||
'split_rules': [],
|
||
}
|
||
|
||
|
||
def test_coordinate_dry_run_path_preserve_and_zero_field_diff():
|
||
"""坐标迁移:正式字段差异 0;bindings 覆盖 order + line 字段。"""
|
||
before = _coordinate_definition()
|
||
after, stats = convert_coordinate_definition_to_unified_bindings(before)
|
||
report = build_migration_report(before=before, after=after, stats=stats)
|
||
assert report['field_diff_count'] == 0
|
||
assert report['path_preserve_rate'] == 100.0
|
||
assert report['binding_count'] >= 2
|
||
assert report['pending_visual_count'] == 0
|
||
assert all(
|
||
item['value_binding']['type'] in {'pdf_coordinate', 'ocr_coordinate'}
|
||
for item in after['bindings']
|
||
)
|
||
assert all(item['visual_binding']['status'] == 'confirmed' for item in after['bindings'])
|
||
# 原 coordinate 键保留
|
||
assert after['order_field_layouts']
|
||
assert after['table_regions']
|
||
|
||
|
||
def test_xfa_dry_run_path_preserve_100_and_zero_field_diff():
|
||
"""XFA 迁移:path_id 保留率 100%;正式字段差异 0;视觉框为 suggested。"""
|
||
before = _xfa_definition()
|
||
after, stats = convert_xfa_definition_to_unified_bindings(before, widgets=[])
|
||
report = build_migration_report(before=before, after=after, stats=stats)
|
||
assert report['path_preserve_rate'] == 100.0
|
||
assert report['field_diff_count'] == 0
|
||
assert report['binding_count'] == 2
|
||
assert report['pending_visual_count'] == 2
|
||
assert report['legacy_will_be_cleared'] is True
|
||
assert report['legacy_mapping_counts'] == {'header': 1, 'line': 1}
|
||
assert after['header_mappings'] == before['header_mappings']
|
||
assert after['line_mapping'] == before['line_mapping']
|
||
assert after['source_contract'] == before['source_contract']
|
||
assert all(item['value_binding']['type'] == 'xfa_dataset' for item in after['bindings'])
|
||
assert {item['value_binding']['path_id'] for item in after['bindings']} == {
|
||
'path-ebeln', 'path-matnr',
|
||
}
|
||
assert all(item['visual_binding']['status'] == 'suggested' for item in after['bindings'])
|
||
|
||
|
||
def test_xfa_execute_clears_legacy_mappings():
|
||
"""Execute 后须清除 header_mappings / line_mapping.fields,仅保留 bindings。"""
|
||
before = _xfa_definition()
|
||
after, _stats = convert_xfa_definition_to_unified_bindings(before, widgets=[])
|
||
cleared = _clear_xfa_legacy_mappings(after)
|
||
assert cleared['bindings']
|
||
assert cleared['header_mappings'] == []
|
||
assert cleared['line_mapping']['fields'] == []
|
||
assert cleared['line_mapping']['row_path'] == before['source_contract']['authoritative_line_collection_path']
|
||
|
||
|
||
def test_xfa_migrated_pending_visual_blocks_publish_gate():
|
||
"""关键断言:迁移 Execute 清除 legacy 后 suggested 视觉框仍阻断发布。"""
|
||
from app.services.shipping_notice.order_document_profile_publish_check_service import (
|
||
_blocking_xfa_visual_bindings,
|
||
)
|
||
|
||
before = _xfa_definition()
|
||
after, _stats = convert_xfa_definition_to_unified_bindings(before, widgets=[])
|
||
migrated = _clear_xfa_legacy_mappings(after)
|
||
schema_field_ids = {'fld-po', 'fld-part'}
|
||
blocking = _blocking_xfa_visual_bindings(
|
||
definition=migrated,
|
||
schema_field_ids=schema_field_ids,
|
||
)
|
||
assert any(item.get('key') == 'xfa_visual_binding_incomplete' for item in blocking)
|
||
|
||
|
||
def test_scan_unified_migration_profiles_synthetic():
|
||
"""scan 返回逐族状态;无 DB 时用 summarize 逻辑(纯函数部分)。"""
|
||
from app.services.shipping_notice.order_profile_unified_migration import (
|
||
scan_unified_migration_profiles,
|
||
summarize_unified_migration_status,
|
||
)
|
||
|
||
coord = summarize_unified_migration_status(_coordinate_definition(), template_mode='coordinate')
|
||
assert coord['unified_binding_status'] == 'needs_migration'
|
||
assert coord['field_diff_count'] == 0
|
||
|
||
xfa = summarize_unified_migration_status(_xfa_definition(), template_mode='xfa_dataset')
|
||
assert xfa['unified_binding_status'] == 'needs_migration'
|
||
assert xfa['pending_visual_count'] == 2
|
||
assert xfa['path_preserve_rate'] == 100.0
|
||
|
||
# scan 需 DB;此处只验证函数可导入且 summarize 与 convert 一致
|
||
assert callable(scan_unified_migration_profiles)
|