Files
syops/backend/tests/test_shipping_document_layout_evaluation.py
T

111 lines
4.1 KiB
Python

"""
backend/tests/test_shipping_document_layout_evaluation.py
用途:验证阶段 L0 人工真值契约和表格/单元格评测指标。
依赖:document_layout.evaluation、布局契约测试构造器。
逻辑:以不可逆构造样本模拟完整命中、漏单元格、字段错配和哈希漂移。
"""
from __future__ import annotations
from copy import deepcopy
import pytest
from pydantic import ValidationError
from app.services.shipping_notice.document_layout.evaluation import evaluate_layout
from tests.test_shipping_document_layout_contract import _document
def _annotation() -> dict:
"""为构造布局生成两页、两表、四单元格的人工真值。"""
tables = []
for page_no, suffix, continuation in ((1, 'p1', None), (2, 'p2', 'truth-p1')):
cells = []
for column, text, field_code in ((0, 'Part', 'part_number'), (1, 'Qty', 'qty')):
x0 = 10 + column * 40
cells.append({
'annotation_id': f'truth-cell-{suffix}-{column}',
'page_no': page_no,
'table_index': 0,
'row_index': 0,
'column_index': column,
'normalized_bbox': {
'x0': x0 / 100, 'y0': 0.1, 'x1': (x0 + 30) / 100, 'y1': 0.25,
},
'normalized_text': text,
'role': 'header',
'expected_field_code': field_code,
})
tables.append({
'annotation_id': f'truth-{suffix}',
'page_no': page_no,
'table_index': 0,
'normalized_bbox': {'x0': 0.05, 'y0': 0.05, 'x1': 0.95, 'y1': 0.55},
'cells': cells,
'continuation_of': continuation,
})
return {
'schema_version': 'shipping_document_layout_annotation.v1',
'layout_schema_version': 'shipping_document_layout.v1',
'sample_id': 'synthetic-two-page-table',
'content_hash': 'a' * 64,
'categories': ['native_pdf', 'cross_page_table'],
'tables': tables,
}
def test_evaluation_reports_perfect_layout_and_field_mapping():
"""几何、行列和字段映射全部正确时四项指标均为 1。"""
mapped = {
'cell-p1-0-0': 'part_number', 'cell-p1-0-1': 'qty',
'cell-p2-0-0': 'part_number', 'cell-p2-0-1': 'qty',
}
result = evaluate_layout(_annotation(), _document(), mapped_field_codes=mapped)
assert result.table_detection_recall == 1
assert result.cell_detection_recall == 1
assert result.cell_position_accuracy == 1
assert result.field_mapping_accuracy == 1
def test_evaluation_separates_detection_position_and_field_errors():
"""漏单元格与字段错配必须反映在不同指标,便于定位解析层或映射层问题。"""
candidate = _document()
candidate['pages'][1]['tables'][0]['rows'][0]['cells'].pop()
mapped = {
'cell-p1-0-0': 'qty',
'cell-p1-0-1': 'qty',
'cell-p2-0-0': 'part_number',
}
result = evaluate_layout(_annotation(), candidate, mapped_field_codes=mapped)
assert result.table_detection_recall == 1
assert result.cell_detection_recall == 0.75
assert result.cell_position_accuracy == 1
assert result.field_mapping_accuracy == 0.5
def test_evaluation_rejects_annotation_for_another_file():
"""人工真值与候选文件哈希不一致时不得产生误导性指标。"""
annotation = deepcopy(_annotation())
annotation['content_hash'] = 'b' * 64
with pytest.raises(ValueError, match='content_hash'):
evaluate_layout(annotation, _document())
def test_evaluation_rejects_invalid_annotation_geometry():
"""人工真值坐标越界或单元格超出父表时必须在评测前拒绝。"""
outside_page = _annotation()
outside_page['tables'][0]['normalized_bbox']['x1'] = 1.1
with pytest.raises(ValidationError, match='within 0..1'):
evaluate_layout(outside_page, _document())
outside_table = _annotation()
outside_table['tables'][0]['cells'][0]['normalized_bbox']['x0'] = 0.01
with pytest.raises(ValidationError, match='within parent table'):
evaluate_layout(outside_table, _document())