清理废弃的shipping xfa ocr automark相关代码和job配置 新增收料报表管理的前端页面、路由和侧边栏入口 新增后端收料报表配置接口、数据库模型和RPA脚本 优化发货通知流程的重试策略,将shipping相关job默认不自动重试 移除废弃的库存源确认相关逻辑,更新流程门禁规则 重构订单模板的值适配器模块,新增坐标OCR值适配器 清理前端shipping菜单和action的废弃选项 更新相关文档和测试用例,调整.gitignore忽略规则
348 lines
13 KiB
Python
348 lines
13 KiB
Python
"""
|
|
tests/test_shipping_xfa_validation.py
|
|
|
|
用途:阶段 3 AcroForm / 镜像集合 / 金额恒等式校验验收。
|
|
依赖:xfa_validation、xfa_profile_extraction、xfa helpers、pytest。
|
|
逻辑:正向 golden 通过;分别注入镜像不一致、金额公式失败、总计不一致断言 fail-closed。
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from copy import deepcopy
|
|
from decimal import Decimal
|
|
|
|
import pytest
|
|
|
|
from app.services.shipping_notice.document_layout.xfa_acroform import XfaAcroformField
|
|
from app.services.shipping_notice.document_layout.xfa_dataset import parse_xfa_datasets
|
|
from app.services.shipping_notice.document_layout.xfa_profile_extraction import extract_xfa_profile
|
|
from app.services.shipping_notice.document_layout.xfa_validation import (
|
|
_line_amount_failures,
|
|
_normalize_comparable,
|
|
validate_xfa_extraction,
|
|
)
|
|
from tests.test_shipping_xfa_helpers import load_datasets_fixture
|
|
from tests.test_shipping_xfa_profile_extraction import _raw_profile, _schema_definition
|
|
|
|
pytestmark = pytest.mark.pure_unit
|
|
|
|
|
|
def _extract_ok():
|
|
"""提取成功的 golden 结果。"""
|
|
analysis = parse_xfa_datasets(load_datasets_fixture())
|
|
raw_profile = _raw_profile()
|
|
extraction = extract_xfa_profile(
|
|
raw_profile=raw_profile,
|
|
dataset_analysis=analysis,
|
|
schema_definition=_schema_definition(),
|
|
document_id=85,
|
|
packet_checksum='sha256:' + ('a' * 64),
|
|
dataset_structure_fingerprint=raw_profile['definition']['source_contract']['dataset_structure_fingerprint'],
|
|
)
|
|
return raw_profile, analysis, extraction
|
|
|
|
|
|
def test_golden_cross_checks_pass_without_acroform_fields():
|
|
"""关键断言:镜像一致 + 金额公式通过;无 PDF/AcroForm 时 acroform=unavailable。"""
|
|
raw_profile, analysis, extraction = _extract_ok()
|
|
assert extraction.status == 'parsed'
|
|
result = validate_xfa_extraction(
|
|
raw_profile=raw_profile,
|
|
dataset_analysis=analysis,
|
|
extraction=extraction,
|
|
pdf_bytes=None,
|
|
)
|
|
assert result.passed is True
|
|
assert result.reason_codes == []
|
|
assert result.cross_check_summary['mirror_status'] == 'matched'
|
|
assert result.cross_check_summary['acroform_status'] == 'unavailable'
|
|
assert result.cross_check_summary['line_amount_status'] == 'passed'
|
|
assert result.cross_check_summary['total_amount_status'] == 'passed'
|
|
|
|
|
|
def test_mirror_collection_mismatch_fails_closed(monkeypatch):
|
|
"""关键断言:镜像集合标准化后不同 → xfa_mirror_collection_mismatch。"""
|
|
raw_profile, analysis, extraction = _extract_ok()
|
|
from app.services.shipping_notice.document_layout.xfa_dataset import read_collection_rows as real_read
|
|
|
|
def _fake_rows(dataset_analysis, collection_path):
|
|
rows = real_read(dataset_analysis, collection_path)
|
|
if str(collection_path).endswith('ITEMS1/DATA') and rows:
|
|
mutated = deepcopy(rows)
|
|
matnr_key = '/data/ITEMS1/DATA/MATNR'
|
|
assert matnr_key in mutated[0]
|
|
mutated[0][matnr_key] = 'DIFFERENT-MATERIAL'
|
|
return mutated
|
|
return rows
|
|
|
|
monkeypatch.setattr(
|
|
'app.services.shipping_notice.document_layout.xfa_validation.read_collection_rows',
|
|
_fake_rows,
|
|
)
|
|
result = validate_xfa_extraction(
|
|
raw_profile=raw_profile,
|
|
dataset_analysis=analysis,
|
|
extraction=extraction,
|
|
pdf_bytes=None,
|
|
)
|
|
assert result.passed is False
|
|
assert 'xfa_mirror_collection_mismatch' in result.reason_codes
|
|
|
|
|
|
def test_line_amount_formula_failure():
|
|
"""关键断言:明细金额与数量×单价不一致 → business_validation_failed。"""
|
|
raw_profile, analysis, extraction = _extract_ok()
|
|
# 篡改提取结果中的 amount。
|
|
for row in extraction.rows:
|
|
for item in row.get('fields') or []:
|
|
if item.get('field_code') == 'amount':
|
|
item['value'] = Decimal('1.00')
|
|
result = validate_xfa_extraction(
|
|
raw_profile=raw_profile,
|
|
dataset_analysis=analysis,
|
|
extraction=extraction,
|
|
pdf_bytes=None,
|
|
)
|
|
assert result.passed is False
|
|
assert 'xfa_business_validation_failed' in result.reason_codes
|
|
|
|
|
|
def test_current_schema_amount_aliases_do_not_implicitly_split_composite_prices():
|
|
"""交叉校验不得把未显式拆分的 NETPR 复合展示值当作单价。"""
|
|
assert _normalize_comparable('898.72 / 1') == '898.72 / 1'
|
|
assert _line_amount_failures([{
|
|
'qty': '30',
|
|
'unit_price': '898.72',
|
|
'price_unit': '1',
|
|
'line_amount_with_tax': '26,961.60',
|
|
}]) == []
|
|
|
|
|
|
def test_line_amount_formula_never_uses_price_unit_as_an_implicit_divisor():
|
|
"""金额公式只使用数量、单价和金额;price_unit 不能成为后端特殊除数。"""
|
|
assert _line_amount_failures([{
|
|
'qty': '30',
|
|
'unit_price': '898.72',
|
|
'line_amount_with_tax': '26,961.60',
|
|
}]) == []
|
|
|
|
# 即使原始行残留同名值,未注册字段也不能作为后台隐式除数。
|
|
assert _line_amount_failures([{
|
|
'qty': '30',
|
|
'unit_price': '898.72',
|
|
'price_unit': 'not-a-number',
|
|
'line_amount_with_tax': '26,961.60',
|
|
}]) == []
|
|
|
|
# 即使 Schema 中存在同名字段,通用金额公式也不得按字段名切换为除法。
|
|
failures = _line_amount_failures([{
|
|
'qty': '10',
|
|
'unit_price': '20',
|
|
'price_unit': '10',
|
|
'line_amount_with_tax': '20',
|
|
}])
|
|
assert failures and failures[0]['reason'] == 'amount_mismatch'
|
|
|
|
|
|
def test_header_total_vs_line_sum_failure():
|
|
"""关键断言:Header 总金额与明细合计不一致 → business_validation_failed。"""
|
|
raw_profile, analysis, extraction = _extract_ok()
|
|
for header in extraction.header_fields:
|
|
if header.field_code == 'total_amount':
|
|
header.normalized_value = Decimal('99999.00')
|
|
result = validate_xfa_extraction(
|
|
raw_profile=raw_profile,
|
|
dataset_analysis=analysis,
|
|
extraction=extraction,
|
|
pdf_bytes=None,
|
|
)
|
|
assert result.passed is False
|
|
assert 'xfa_business_validation_failed' in result.reason_codes
|
|
|
|
|
|
def test_acroform_mismatch_fails_closed(monkeypatch):
|
|
"""关键断言:datasets 与 AcroForm 关键字段标准化后不同 → xfa_acroform_mismatch。"""
|
|
raw_profile, analysis, extraction = _extract_ok()
|
|
|
|
def _fake_acroform(_pdf_bytes):
|
|
return [
|
|
XfaAcroformField(
|
|
qualified_name='form[0].EBELN[0]',
|
|
short_name='EBELN',
|
|
value='WRONG-PO',
|
|
value_masked='W***O',
|
|
),
|
|
]
|
|
|
|
monkeypatch.setattr(
|
|
'app.services.shipping_notice.document_layout.xfa_validation.read_acroform_fields',
|
|
_fake_acroform,
|
|
)
|
|
result = validate_xfa_extraction(
|
|
raw_profile=raw_profile,
|
|
dataset_analysis=analysis,
|
|
extraction=extraction,
|
|
pdf_bytes=b'%PDF-fake',
|
|
)
|
|
assert result.passed is False
|
|
assert 'xfa_acroform_mismatch' in result.reason_codes
|
|
|
|
|
|
def test_acroform_display_format_difference_is_ok(monkeypatch):
|
|
"""关键断言:AcroForm 展示格式不同但标准化相同则通过(30 vs 30.000)。"""
|
|
raw_profile, analysis, extraction = _extract_ok()
|
|
|
|
def _fake_acroform(_pdf_bytes):
|
|
return [
|
|
XfaAcroformField('MENGE', 'MENGE', '30', '30'),
|
|
XfaAcroformField('AMOUNT', 'AMOUNT', '26,961.6', '26***6'),
|
|
XfaAcroformField('EINDT', 'EINDT', '14/07/2026', '14***6'),
|
|
XfaAcroformField('EBELN', 'EBELN', '4510039227', '451***227'),
|
|
XfaAcroformField('MATNR', 'MATNR', 'TEMP-S000218922', 'TEM***922'),
|
|
XfaAcroformField('NETPR', 'NETPR', '898.72', '898.72'),
|
|
XfaAcroformField('PEINH', 'PEINH', '1', '1'),
|
|
XfaAcroformField('TOTAL_AMOUNT', 'TOTAL_AMOUNT', '26,961.60', '26***0'),
|
|
]
|
|
|
|
monkeypatch.setattr(
|
|
'app.services.shipping_notice.document_layout.xfa_validation.read_acroform_fields',
|
|
_fake_acroform,
|
|
)
|
|
result = validate_xfa_extraction(
|
|
raw_profile=raw_profile,
|
|
dataset_analysis=analysis,
|
|
extraction=extraction,
|
|
pdf_bytes=b'%PDF-fake',
|
|
)
|
|
assert result.passed is True
|
|
assert result.cross_check_summary['acroform_status'] == 'matched'
|
|
|
|
|
|
def test_acroform_composite_value_requires_an_explicit_split_binding(monkeypatch):
|
|
"""复合 Widget 只有明确 split_rule_ref 才可跳过原文对账。"""
|
|
raw_profile, analysis, extraction = _extract_ok()
|
|
raw_profile['definition']['split_rules'] = []
|
|
price_binding = next(
|
|
item for item in raw_profile['definition']['bindings']
|
|
if item['field_code'] == 'unit_price'
|
|
)
|
|
price_binding.pop('split_rule_ref', None)
|
|
# 运行期模型只信 binding 明确引用;清除引用后使用直取值以暴露不一致。
|
|
for row in extraction.rows:
|
|
for field in row.get('fields') or []:
|
|
if field.get('field_code') == 'unit_price':
|
|
field['value'] = '898.72'
|
|
|
|
monkeypatch.setattr(
|
|
'app.services.shipping_notice.document_layout.xfa_validation.read_acroform_fields',
|
|
lambda _pdf: [XfaAcroformField('NETPR', 'NETPR', '898.72 / 1', '898.72 / 1')],
|
|
)
|
|
result = validate_xfa_extraction(
|
|
raw_profile=raw_profile,
|
|
dataset_analysis=analysis,
|
|
extraction=extraction,
|
|
pdf_bytes=b'%PDF-fake',
|
|
)
|
|
|
|
assert result.passed is False
|
|
assert 'xfa_acroform_mismatch' in result.reason_codes
|
|
|
|
|
|
def test_acroform_english_month_date_matches_normalized_dataset_date(monkeypatch):
|
|
"""AcroForm 的英文月名日期与 datasets ISO 日期一致时不得误判为冲突。"""
|
|
raw_profile, analysis, extraction = _extract_ok()
|
|
|
|
def _fake_acroform(_pdf_bytes):
|
|
return [
|
|
XfaAcroformField('EINDT', 'EINDT', '14-Jul-2026', '14***6'),
|
|
]
|
|
|
|
monkeypatch.setattr(
|
|
'app.services.shipping_notice.document_layout.xfa_validation.read_acroform_fields',
|
|
_fake_acroform,
|
|
)
|
|
result = validate_xfa_extraction(
|
|
raw_profile=raw_profile,
|
|
dataset_analysis=analysis,
|
|
extraction=extraction,
|
|
pdf_bytes=b'%PDF-fake',
|
|
)
|
|
|
|
# 关键断言:只覆盖一个字段应为 partial,而不是格式假冲突导致 mismatched。
|
|
assert result.passed is True
|
|
assert result.cross_check_summary['acroform_status'] == 'partial'
|
|
|
|
|
|
def test_acroform_empty_fields_is_unavailable(monkeypatch):
|
|
"""关键断言:PDF 有调用但无 AcroForm Fields → unavailable,不得记 matched。"""
|
|
raw_profile, analysis, extraction = _extract_ok()
|
|
|
|
monkeypatch.setattr(
|
|
'app.services.shipping_notice.document_layout.xfa_validation.read_acroform_fields',
|
|
lambda _pdf: [],
|
|
)
|
|
result = validate_xfa_extraction(
|
|
raw_profile=raw_profile,
|
|
dataset_analysis=analysis,
|
|
extraction=extraction,
|
|
pdf_bytes=b'%PDF-fake',
|
|
)
|
|
assert result.passed is True
|
|
assert result.cross_check_summary['acroform_status'] == 'unavailable'
|
|
|
|
|
|
def test_acroform_partial_coverage_is_partial(monkeypatch):
|
|
"""关键断言:仅部分映射字段存在 AcroForm → partial。"""
|
|
raw_profile, analysis, extraction = _extract_ok()
|
|
|
|
def _fake_acroform(_pdf_bytes):
|
|
return [
|
|
XfaAcroformField('EBELN', 'EBELN', '4510039227', '451***227'),
|
|
]
|
|
|
|
monkeypatch.setattr(
|
|
'app.services.shipping_notice.document_layout.xfa_validation.read_acroform_fields',
|
|
_fake_acroform,
|
|
)
|
|
result = validate_xfa_extraction(
|
|
raw_profile=raw_profile,
|
|
dataset_analysis=analysis,
|
|
extraction=extraction,
|
|
pdf_bytes=b'%PDF-fake',
|
|
)
|
|
assert result.passed is True
|
|
assert result.cross_check_summary['acroform_status'] == 'partial'
|
|
|
|
|
|
def test_acroform_multirow_aligns_by_instance_index(monkeypatch):
|
|
"""关键断言:多行明细按 Widget 实例下标对齐,不得用首行值覆盖第二行。"""
|
|
raw_profile, analysis, extraction = _extract_ok()
|
|
# 复制第二行,模拟双行提取;第二行数量改为 99。
|
|
assert extraction.rows
|
|
second = deepcopy(extraction.rows[0])
|
|
for item in second.get('fields') or []:
|
|
if item.get('field_code') == 'quantity':
|
|
item['value'] = Decimal('99')
|
|
extraction.rows.append(second)
|
|
|
|
def _fake_acroform(_pdf_bytes):
|
|
return [
|
|
XfaAcroformField('form[0].MENGE[0]', 'MENGE', '30', '30'),
|
|
XfaAcroformField('form[0].MENGE[1]', 'MENGE', '50', '50'), # 与第二行 99 不一致
|
|
XfaAcroformField('EBELN', 'EBELN', '4510039227', '451***227'),
|
|
]
|
|
|
|
monkeypatch.setattr(
|
|
'app.services.shipping_notice.document_layout.xfa_validation.read_acroform_fields',
|
|
_fake_acroform,
|
|
)
|
|
result = validate_xfa_extraction(
|
|
raw_profile=raw_profile,
|
|
dataset_analysis=analysis,
|
|
extraction=extraction,
|
|
pdf_bytes=b'%PDF-fake',
|
|
)
|
|
assert result.passed is False
|
|
assert 'xfa_acroform_mismatch' in result.reason_codes
|
|
assert result.cross_check_summary['acroform_status'] == 'mismatched'
|