148 lines
5.7 KiB
Python
148 lines
5.7 KiB
Python
"""
|
|
tests/test_shipping_xfa_profile_matcher.py
|
|
|
|
用途:阶段 3 XFA Profile 精确结构身份匹配验收(唯一命中 / 结构变化 / 多命中)。
|
|
依赖:xfa_profile_matcher、xfa_signature、xfa helpers、pytest。
|
|
逻辑:基于 annotation fixture 计算 live 指纹,构造 published 快照后断言匹配态;
|
|
草稿/retired 不得参与新 Run 匹配。
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import pytest
|
|
|
|
from app.services.shipping_notice.document_layout.xfa_dataset import parse_xfa_datasets
|
|
from app.services.shipping_notice.document_layout.xfa_profile_matcher import match_frozen_xfa_profiles
|
|
from app.services.shipping_notice.document_layout.xfa_signature import build_signature_summary
|
|
from tests.test_shipping_xfa_helpers import load_datasets_fixture
|
|
|
|
pytestmark = pytest.mark.pure_unit
|
|
|
|
|
|
def _live_identity() -> tuple[str, str]:
|
|
"""从 annotation fixture 计算 template/fingerprint(使用固定 template 占位)。"""
|
|
datasets = load_datasets_fixture()
|
|
analysis = parse_xfa_datasets(datasets)
|
|
summary = build_signature_summary(
|
|
template_bytes=b'<template><subform name="form"/></template>',
|
|
dataset_nodes=analysis.nodes,
|
|
)
|
|
return str(summary['template_packet_checksum']), str(summary['dataset_structure_fingerprint'])
|
|
|
|
|
|
def _published_profile(*, code: str = 'XFA_PO', version: int = 1, checksum: str = 'pub' * 16, **source_overrides):
|
|
"""构造冻结 published XFA Profile 快照。"""
|
|
template, fingerprint = _live_identity()
|
|
source = {
|
|
'template_packet_checksum': template,
|
|
'dataset_structure_fingerprint': fingerprint,
|
|
'dataset_root_path': '/data',
|
|
'authoritative_line_collection_path': '/data/ITEMS/DATA',
|
|
'mirror_collection_paths': ['/data/ITEMS1/DATA'],
|
|
}
|
|
source.update(source_overrides)
|
|
return {
|
|
'profile_id': 101,
|
|
'profile_code': code,
|
|
'version': version,
|
|
'checksum': checksum,
|
|
'status': 'published',
|
|
'template_mode': 'xfa_dataset',
|
|
'schema_version_id': 1,
|
|
'definition': {
|
|
'template_mode': 'xfa_dataset',
|
|
'source_contract': source,
|
|
'header_mappings': [{
|
|
'mapping_id': 'm-po', 'field_id': 'f-po', 'field_code': 'customer_po_no',
|
|
'data_path': '/data/HEADER/EBELN', 'path_id': 'path-po',
|
|
'value_type': 'string', 'normalizer_key': 'trim',
|
|
}],
|
|
'line_mapping': {
|
|
'row_path': '/data/ITEMS/DATA',
|
|
'fields': [{
|
|
'mapping_id': 'm-mat', 'field_id': 'f-mat', 'field_code': 'material_no',
|
|
'data_path': '/data/ITEMS/DATA/MATNR', 'path_id': 'path-mat',
|
|
'value_type': 'string', 'normalizer_key': 'trim',
|
|
}],
|
|
},
|
|
'cross_checks': {
|
|
'acroform_enabled': True,
|
|
'mirror_collection_policy': 'must_match_when_present',
|
|
'line_amount_formula': 'quantity_times_unit_price',
|
|
'total_amount_policy': 'must_equal_line_sum',
|
|
},
|
|
},
|
|
}
|
|
|
|
|
|
def test_unique_exact_structure_identity_match():
|
|
"""关键断言:精确结构身份唯一命中 published Profile。"""
|
|
template, fingerprint = _live_identity()
|
|
profiles = [_published_profile()]
|
|
result = match_frozen_xfa_profiles(
|
|
profiles=profiles,
|
|
template_packet_checksum=template,
|
|
dataset_structure_fingerprint=fingerprint,
|
|
)
|
|
assert result.status == 'unique_match'
|
|
assert result.profile_code == 'XFA_PO'
|
|
assert result.checksum == 'pub' * 16
|
|
assert result.reason_codes == []
|
|
|
|
|
|
def test_structure_change_is_no_match():
|
|
"""关键断言:结构指纹变化 → no_match / structure identity changed。"""
|
|
template, _fingerprint = _live_identity()
|
|
profiles = [_published_profile()]
|
|
result = match_frozen_xfa_profiles(
|
|
profiles=profiles,
|
|
template_packet_checksum=template,
|
|
dataset_structure_fingerprint='sha256:' + ('f' * 64),
|
|
)
|
|
assert result.status == 'no_match'
|
|
assert 'xfa_profile_not_matched' in result.reason_codes or any(
|
|
'xfa_structure_identity_changed' in c.reason_codes for c in result.candidates
|
|
)
|
|
|
|
|
|
def test_ambiguous_when_multiple_published_match():
|
|
"""关键断言:多 Profile 精确命中 → ambiguous。"""
|
|
template, fingerprint = _live_identity()
|
|
profiles = [
|
|
_published_profile(code='XFA_A', checksum='a' * 48),
|
|
_published_profile(code='XFA_B', checksum='b' * 48),
|
|
]
|
|
result = match_frozen_xfa_profiles(
|
|
profiles=profiles,
|
|
template_packet_checksum=template,
|
|
dataset_structure_fingerprint=fingerprint,
|
|
)
|
|
assert result.status == 'ambiguous'
|
|
assert 'xfa_profile_ambiguous' in result.reason_codes
|
|
|
|
|
|
def test_draft_and_retired_excluded_from_match():
|
|
"""关键断言:草稿/retired 不参与新 Run 匹配。"""
|
|
template, fingerprint = _live_identity()
|
|
draft = _published_profile(code='XFA_DRAFT', checksum='d' * 48)
|
|
draft['status'] = 'draft'
|
|
retired = _published_profile(code='XFA_RETIRED', checksum='r' * 48)
|
|
retired['status'] = 'retired'
|
|
result = match_frozen_xfa_profiles(
|
|
profiles=[draft, retired],
|
|
template_packet_checksum=template,
|
|
dataset_structure_fingerprint=fingerprint,
|
|
)
|
|
assert result.status == 'no_match'
|
|
assert result.candidates == []
|
|
|
|
|
|
def test_missing_live_identity_is_evidence_insufficient():
|
|
"""关键断言:缺少 live 结构身份 → evidence_insufficient。"""
|
|
result = match_frozen_xfa_profiles(
|
|
profiles=[_published_profile()],
|
|
template_packet_checksum=None,
|
|
dataset_structure_fingerprint=None,
|
|
)
|
|
assert result.status == 'evidence_insufficient'
|