- 新增行字段补充合并与订单模板匹配能力 - 优化运行时解析策略、签名校验及资料验证 - 完善库存 RPA 归档、收尾与 Worker 启动流程 - 补充前后端测试、多语言文案及功能文档
310 lines
12 KiB
Python
310 lines
12 KiB
Python
"""
|
|
tests/test_line_field_supplement_merge.py
|
|
|
|
用途:覆盖同文档 COO/明细字段补充表按 LN 合并的契约。
|
|
依赖:line_field_supplement_merge、order_profile.TableRegionProfile、
|
|
order_extraction_workflow._pick_overlapping_layout_table。
|
|
逻辑:断言空补、前缀补全、冲突不覆盖、LN 歧义/缺失与禁止改 qty;
|
|
成功补齐仅表级 reasons、不写入 issues;以及补充区与 MinerU 表重叠时的物理表选取。
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import pytest
|
|
from pydantic import ValidationError
|
|
|
|
from app.services.shipping_notice.document_layout.order_profile import TableRegionProfile
|
|
from app.services.shipping_notice.line_field_supplement_merge import (
|
|
REASON_APPLIED,
|
|
REASON_CONFLICT,
|
|
REASON_LN_AMBIGUOUS,
|
|
merge_line_field_supplement,
|
|
normalize_line_start_key,
|
|
normalize_part_value,
|
|
)
|
|
from app.services.shipping_notice.order_extraction_workflow import (
|
|
_pick_overlapping_layout_table,
|
|
)
|
|
|
|
|
|
def test_normalize_line_start_and_part_values():
|
|
"""关键断言:行号去前导零对齐;料号仅去空白。"""
|
|
assert normalize_line_start_key(' 02 ') == '2'
|
|
assert normalize_line_start_key('00010') == '10'
|
|
assert normalize_part_value('WTP-B3- A08') == 'WTP-B3-A08'
|
|
|
|
|
|
def test_merge_fills_empty_and_prefix_without_touching_qty():
|
|
"""空值直补、截断前缀补全,数量保持主表原值。"""
|
|
main = [
|
|
{'customer_po_line_no': '1', 'customer_part_number': '', 'part_number': 'B3-', 'qty': '30'},
|
|
{
|
|
'customer_po_line_no': '2',
|
|
'customer_part_number': 'WTP-B3-',
|
|
'part_number': '',
|
|
'qty': '3',
|
|
},
|
|
]
|
|
supplement = [
|
|
{
|
|
'customer_po_line_no': '1',
|
|
'customer_part_number': 'WTP-B3-A08AR11000001',
|
|
'part_number': 'B3-A08AR11000001',
|
|
},
|
|
{
|
|
'customer_po_line_no': '2',
|
|
'customer_part_number': 'WTP-B3-A08AR10000017-A6',
|
|
'part_number': 'B3-A08AR10000017-A6',
|
|
},
|
|
]
|
|
|
|
merged, reasons = merge_line_field_supplement(main, supplement)
|
|
|
|
assert merged[0]['customer_part_number'] == 'WTP-B3-A08AR11000001'
|
|
assert merged[0]['part_number'] == 'B3-A08AR11000001'
|
|
assert merged[0]['qty'] == '30'
|
|
assert merged[1]['customer_part_number'] == 'WTP-B3-A08AR10000017-A6'
|
|
assert merged[1]['part_number'] == 'B3-A08AR10000017-A6'
|
|
assert merged[1]['qty'] == '3'
|
|
assert REASON_APPLIED in reasons
|
|
assert REASON_CONFLICT not in reasons
|
|
# 关键断言:成功补齐不得进入行 issues,否则正式建单会被误判为 row_needs_review。
|
|
assert REASON_APPLIED not in (merged[0].get('issues') or [])
|
|
assert REASON_APPLIED not in (merged[1].get('issues') or [])
|
|
|
|
|
|
def test_merge_conflict_does_not_overwrite():
|
|
"""两边完整且不同时保留主表并记冲突。"""
|
|
main = [{'customer_po_line_no': '1', 'part_number': 'MAIN-ONLY', 'qty': '1'}]
|
|
supplement = [{'customer_po_line_no': '1', 'part_number': 'COO-OTHER'}]
|
|
|
|
merged, reasons = merge_line_field_supplement(main, supplement)
|
|
|
|
assert merged[0]['part_number'] == 'MAIN-ONLY'
|
|
assert merged[0]['qty'] == '1'
|
|
assert REASON_CONFLICT in reasons
|
|
assert REASON_CONFLICT in merged[0]['issues']
|
|
|
|
|
|
def test_merge_skips_ambiguous_line_numbers():
|
|
"""主表或补充表 LN 重复时不补该键。"""
|
|
main = [
|
|
{'customer_po_line_no': '1', 'part_number': ''},
|
|
{'customer_po_line_no': '1', 'part_number': ''},
|
|
]
|
|
supplement = [{'customer_po_line_no': '1', 'part_number': 'B3-A08'}]
|
|
|
|
merged, reasons = merge_line_field_supplement(main, supplement)
|
|
|
|
assert merged[0]['part_number'] == ''
|
|
assert merged[1]['part_number'] == ''
|
|
assert REASON_LN_AMBIGUOUS in reasons
|
|
|
|
|
|
def test_merge_workflow_shaped_rows():
|
|
"""workflow fields 列表形态同样可补齐。"""
|
|
main = [{
|
|
'status': 'needs_review',
|
|
'fields': [
|
|
{'field_code': 'customer_po_line_no', 'value': '2'},
|
|
{'field_code': 'customer_part_number', 'value': 'WTP-B3-'},
|
|
{'field_code': 'qty', 'value': '3'},
|
|
],
|
|
'issues': [],
|
|
}]
|
|
supplement = [{
|
|
'fields': [
|
|
{'field_code': 'customer_po_line_no', 'value': '2'},
|
|
{'field_code': 'customer_part_number', 'value': 'WTP-B3-A08AR10000017-A6'},
|
|
{'field_code': 'part_number', 'value': 'B3-A08AR10000017-A6'},
|
|
],
|
|
}]
|
|
|
|
merged, reasons = merge_line_field_supplement(main, supplement)
|
|
values = {item['field_code']: item['value'] for item in merged[0]['fields']}
|
|
|
|
assert values['customer_part_number'] == 'WTP-B3-A08AR10000017-A6'
|
|
assert values['part_number'] == 'B3-A08AR10000017-A6'
|
|
assert values['qty'] == '3'
|
|
assert REASON_APPLIED in reasons
|
|
assert REASON_APPLIED not in (merged[0].get('issues') or [])
|
|
|
|
|
|
def test_apply_line_field_supplement_uses_reconstructed_coo_rows(monkeypatch):
|
|
"""关键断言:正式路径在主表截断后用补充表重建行补全料号。"""
|
|
from app.services.shipping_notice import order_extraction_workflow as workflow
|
|
|
|
def _fake_reconstruct(**_kwargs):
|
|
return (
|
|
[{
|
|
'fields': [
|
|
{'field_code': 'customer_po_line_no', 'value': '2'},
|
|
{'field_code': 'customer_part_number', 'value': 'WTP-B3-A08AR10000017-A6'},
|
|
{'field_code': 'part_number', 'value': 'B3-A08AR10000017-A6'},
|
|
],
|
|
'issues': [],
|
|
}],
|
|
'customer_po_line_no',
|
|
)
|
|
|
|
monkeypatch.setattr(workflow, '_reconstruct_line_field_supplement_rows', _fake_reconstruct)
|
|
parsed = [{
|
|
'status': 'parsed',
|
|
'fields': [
|
|
{'field_code': 'customer_po_line_no', 'value': '2'},
|
|
{'field_code': 'customer_part_number', 'value': 'WTP-B3-'},
|
|
{'field_code': 'part_number', 'value': ''},
|
|
{'field_code': 'qty', 'value': '3'},
|
|
],
|
|
'issues': [],
|
|
}]
|
|
merged, reasons = workflow._apply_line_field_supplement(
|
|
raw_profile={'definition': {}},
|
|
layout={},
|
|
parsed_rows=parsed,
|
|
schema_fields={},
|
|
)
|
|
values = {item['field_code']: item['value'] for item in merged[0]['fields']}
|
|
assert values['customer_part_number'] == 'WTP-B3-A08AR10000017-A6'
|
|
assert values['part_number'] == 'B3-A08AR10000017-A6'
|
|
assert values['qty'] == '3'
|
|
assert REASON_APPLIED in reasons
|
|
|
|
|
|
def test_project_supplement_onto_validation_evidence():
|
|
"""关键断言:验证证据 lines 可被 COO 补齐且保留其它列。"""
|
|
from app.services.shipping_notice.line_field_supplement_merge import (
|
|
project_line_field_supplement_onto_validation_evidence,
|
|
)
|
|
|
|
evidence = {
|
|
'header_fields': [],
|
|
'lines': [{
|
|
'row_index': 0,
|
|
'fields': [
|
|
{'field_code': 'customer_po_line_no', 'raw_value': '2', 'normalized_value': '2', 'status': 'passed', 'issues': []},
|
|
{'field_code': 'customer_part_number', 'raw_value': 'WTP-B3-', 'normalized_value': 'WTP-B3-', 'status': 'passed', 'issues': []},
|
|
{'field_code': 'qty', 'raw_value': '3', 'normalized_value': '3', 'status': 'passed', 'issues': []},
|
|
],
|
|
}],
|
|
'cross_checks': {},
|
|
}
|
|
updated, reasons = project_line_field_supplement_onto_validation_evidence(
|
|
evidence,
|
|
supplement_rows=[{
|
|
'fields': [
|
|
{'field_code': 'customer_po_line_no', 'value': '2'},
|
|
{'field_code': 'customer_part_number', 'value': 'WTP-B3-A08AR10000017-A6'},
|
|
],
|
|
}],
|
|
)
|
|
part = next(item for item in updated['lines'][0]['fields'] if item['field_code'] == 'customer_part_number')
|
|
qty = next(item for item in updated['lines'][0]['fields'] if item['field_code'] == 'qty')
|
|
assert part['raw_value'] == 'WTP-B3-A08AR10000017-A6'
|
|
assert qty['raw_value'] == '3'
|
|
assert REASON_APPLIED in reasons
|
|
|
|
|
|
def test_line_field_supplement_region_rejects_non_part_bindings():
|
|
"""补充表绑定数量等业务字段必须失败。"""
|
|
payload = {
|
|
'region_id': 'coo_lines',
|
|
'page_scope': 'exact_page',
|
|
'page_no': 1,
|
|
'region': {'x0': 0, 'y0': 0.6, 'x1': 1, 'y1': 0.9},
|
|
'region_role': 'line_field_supplement',
|
|
'line_layouts': [{
|
|
'layout_id': 'main', 'row_offset': 0, 'role': 'main',
|
|
'region': {'x0': 0, 'y0': 0, 'x1': 1, 'y1': 1},
|
|
'field_regions': [
|
|
{
|
|
'field_id': 'ln', 'field_code': 'customer_po_line_no', 'scope': 'line',
|
|
'region': {'x0': 0, 'y0': 0, 'x1': .2, 'y1': 1},
|
|
'allowed_sources': ['pdf_coordinate'],
|
|
},
|
|
{
|
|
'field_id': 'qty', 'field_code': 'qty', 'scope': 'line',
|
|
'region': {'x0': .2, 'y0': 0, 'x1': .4, 'y1': 1},
|
|
'allowed_sources': ['pdf_coordinate'],
|
|
},
|
|
],
|
|
}],
|
|
}
|
|
with pytest.raises(ValidationError, match='line_field_supplement may only bind'):
|
|
TableRegionProfile.model_validate(payload)
|
|
|
|
|
|
def test_line_field_supplement_region_accepts_part_bindings():
|
|
"""补充表仅绑 LN + 料号字段可通过。"""
|
|
region = TableRegionProfile.model_validate({
|
|
'region_id': 'coo_lines',
|
|
'page_scope': 'exact_page',
|
|
'page_no': 1,
|
|
'region': {'x0': 0, 'y0': 0.6, 'x1': 1, 'y1': 0.9},
|
|
'region_role': 'line_field_supplement',
|
|
'line_layouts': [{
|
|
'layout_id': 'main', 'row_offset': 0, 'role': 'main',
|
|
'region': {'x0': 0, 'y0': 0, 'x1': 1, 'y1': 1},
|
|
'field_regions': [
|
|
{
|
|
'field_id': 'ln', 'field_code': 'customer_po_line_no', 'scope': 'line',
|
|
'region': {'x0': 0, 'y0': 0, 'x1': .2, 'y1': 1},
|
|
'allowed_sources': ['pdf_coordinate'],
|
|
},
|
|
{
|
|
'field_id': 'cpn', 'field_code': 'customer_part_number', 'scope': 'line',
|
|
'region': {'x0': .2, 'y0': 0, 'x1': .6, 'y1': 1},
|
|
'allowed_sources': ['pdf_coordinate'],
|
|
},
|
|
{
|
|
'field_id': 'pn', 'field_code': 'part_number', 'scope': 'line',
|
|
'region': {'x0': .6, 'y0': 0, 'x1': 1, 'y1': 1},
|
|
'allowed_sources': ['pdf_coordinate'],
|
|
},
|
|
],
|
|
}],
|
|
})
|
|
assert region.region_role == 'line_field_supplement'
|
|
|
|
|
|
def test_pick_overlapping_layout_table_prefers_max_vertical_overlap():
|
|
"""关键断言:同页重叠表取纵向重叠面积最大者;无重叠返回空。"""
|
|
page_size = {'width': 100.0, 'height': 100.0}
|
|
region = {'x0': 0.05, 'y0': 0.67, 'x1': 0.9, 'y1': 0.70}
|
|
tables = [
|
|
{
|
|
'table_id': 'header-ish',
|
|
'page_no': 1,
|
|
'bbox': {'x0': 5.0, 'y0': 10.0, 'x1': 90.0, 'y1': 40.0},
|
|
},
|
|
{
|
|
'table_id': 'coo-table',
|
|
'page_no': 1,
|
|
'bbox': {'x0': 5.0, 'y0': 66.0, 'x1': 90.0, 'y1': 78.0},
|
|
},
|
|
{
|
|
'table_id': 'other-page',
|
|
'page_no': 2,
|
|
'bbox': {'x0': 5.0, 'y0': 66.0, 'x1': 90.0, 'y1': 78.0},
|
|
},
|
|
]
|
|
|
|
table_id, bbox = _pick_overlapping_layout_table(
|
|
layout_tables=tables,
|
|
page_no=1,
|
|
region_relative=region,
|
|
page_size=page_size,
|
|
)
|
|
|
|
assert table_id == 'coo-table'
|
|
assert bbox == {'x0': 5.0, 'y0': 66.0, 'x1': 90.0, 'y1': 78.0}
|
|
|
|
empty_id, empty_bbox = _pick_overlapping_layout_table(
|
|
layout_tables=tables,
|
|
page_no=1,
|
|
region_relative={'x0': 0.05, 'y0': 0.90, 'x1': 0.9, 'y1': 0.95},
|
|
page_size=page_size,
|
|
)
|
|
assert empty_id is None
|
|
assert empty_bbox is None
|