Files
syops/backend/tests/test_shipping_pdf_automark.py
T

74 lines
3.3 KiB
Python

"""
tests/test_shipping_pdf_automark.py
用途:验证统一 PDF 自动标记 Job 对不同 PDF 能力输出同一候选契约。
依赖:order_document_profile_pdf_automark_service、pytest mock。
逻辑:普通 PDF 复用坐标重建与 OCR 回退;XFA 复用多源融合执行器,但调用方不传类型。
"""
from __future__ import annotations
from types import SimpleNamespace
from unittest.mock import MagicMock, patch
import pytest
from app.core.errors import ValidationError
from app.services.shipping_notice.order_document_profile_pdf_automark_service import (
execute_pdf_automark_job,
)
pytestmark = pytest.mark.pure_unit
def test_coordinate_pdf_automark_retries_with_ocr_and_returns_candidates():
"""关键断言:普通 PDF 在坐标不足时由统一 Job 触发 OCR 后仍返回 candidates[]。"""
profile = SimpleNamespace(template_mode='coordinate', definition_json={}, status='draft')
reconstruction = {'subrow_layouts': [{'row_offset': 0}], 'header_regions': []}
fields = {'order_field_candidates': [], 'page_no': 1}
coordinate = MagicMock()
coordinate.suggest_coordinate_reconstruction.side_effect = [
ValidationError('missing', code='SHIPPING.ORDER_DOCUMENT_PROFILE_RECONSTRUCTION_UNAVAILABLE'),
reconstruction,
]
coordinate.suggest_order_field_layouts.return_value = fields
with patch(
'app.services.shipping_notice.order_document_profile_pdf_automark_service._profile',
return_value=profile,
), patch(
'app.services.shipping_notice.order_document_profile_service',
coordinate,
create=True,
):
# execute 函数内部延迟导入,直接替换模块缓存中的服务对象。
with patch.dict('sys.modules', {
'app.services.shipping_notice.order_document_profile_service': coordinate,
}):
result = execute_pdf_automark_job({
'profile_id': 12, 'document_id': 77, 'table_id': 'table-1', 'schema_version_id': 3, 'page_no': 1,
})
assert result['kind'] == 'coordinate_regions'
assert [item['candidate_kind'] for item in result['candidates']] == ['coordinate_reconstruction', 'order_fields']
coordinate.generate_ocr_coordinate_evidence.assert_called_once_with(document_id=77, table_id='table-1', actor_id=None)
def test_xfa_pdf_automark_delegates_multi_source_evidence_to_same_contract():
"""关键断言:XFA 由 Profile 能力自动识别,不要求客户端传类型且仍返回 candidates[]。"""
profile = SimpleNamespace(template_mode='xfa_dataset', definition_json={'template_mode': 'xfa_dataset'}, status='draft')
xfa = MagicMock()
xfa.execute_xfa_ocr_automark_job.return_value = {
'status': 'success', 'suggestions': [{'suggestion_id': 's-1'}], 'error_code': None,
}
with patch(
'app.services.shipping_notice.order_document_profile_pdf_automark_service._profile',
return_value=profile,
), patch.dict('sys.modules', {
'app.services.shipping_notice.order_document_profile_xfa_automark_service': xfa,
}):
result = execute_pdf_automark_job({'profile_id': 12, 'document_id': 77})
assert result['kind'] == 'visual_binding'
assert result['candidates'] == [{'suggestion_id': 's-1'}]
assert result['diagnostics']['evidence'] == ['pdfium', 'acroform_widget', 'ocr', 'xfa_datasets']