feat(shipping-notice): 落地 XFA 拆分、版面签名、统一进度与处理域履约
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
3a9fbac2fc
commit
d2b3d74def
571 files changed
+78064
-24681
No files matched your search
@@ -8,7 +8,8 @@ backend/tests/test_shipping_document_profile_quality.py
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import datetime
|
||||
from datetime import date, datetime
|
||||
from decimal import Decimal
|
||||
from types import SimpleNamespace
|
||||
|
||||
import pytest
|
||||
@@ -16,8 +17,8 @@ import pytest
|
||||
from app import db
|
||||
from app.models import (
|
||||
FileAsset,
|
||||
ShippingNoticeDocument,
|
||||
ShippingNoticeIntake,
|
||||
ShippingOrderDocument,
|
||||
ShippingOrderIntake,
|
||||
ShippingCustomer,
|
||||
ShippingOrderDocumentProfile,
|
||||
ShippingOrderDocumentProfileAudit,
|
||||
@@ -28,9 +29,27 @@ from app.models import (
|
||||
ShippingOrderDocumentProfileSample,
|
||||
)
|
||||
from app.services.shipping_notice import document_profile_quality_service as quality
|
||||
from app.services.shipping_notice.document_layout.html_table import parse_html_grid
|
||||
from app.services.shipping_notice.document_layout.lifecycle import record_inline_layout_snapshot
|
||||
from app.services.shipping_notice.document_layout.order_profile import order_profile_structure_fingerprint
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
('actual', 'expected', 'config'),
|
||||
[
|
||||
('2026-06-09', date(2026, 6, 9), {'value_type': 'date'}),
|
||||
('898.72000', Decimal('898.72000'), {'value_type': 'decimal'}),
|
||||
(' Quanta Computer Inc. ', 'Quanta Computer Inc.', {'value_type': 'string', 'normalizer_key': 'trim'}),
|
||||
],
|
||||
)
|
||||
def test_blind_test_order_field_comparison_normalizes_both_values(actual, expected, config):
|
||||
"""关键断言:API 序列化后的字符串与冻结类型值按同一 Schema 比较,不产生伪 mismatch。"""
|
||||
assert quality._order_field_values_match(actual=actual, expected=expected, config=config)
|
||||
|
||||
|
||||
def test_blind_test_order_field_comparison_rejects_different_values():
|
||||
"""关键断言:双向规范化只消除类型差异,真实日期差异仍必须失败。"""
|
||||
assert not quality._order_field_values_match(
|
||||
actual='2026-06-09', expected='2026-06-10', config={'value_type': 'date'},
|
||||
)
|
||||
|
||||
|
||||
def test_adaptive_quality_region_uses_page_glyphs_without_changing_default_source():
|
||||
@@ -50,7 +69,7 @@ def test_adaptive_quality_region_uses_page_glyphs_without_changing_default_sourc
|
||||
) == [{'text': 'PAGE'}]
|
||||
|
||||
|
||||
def _published_profile_with_samples() -> tuple[ShippingOrderDocumentProfile, User, list[ShippingNoticeDocument]]:
|
||||
def _published_profile_with_samples() -> tuple[ShippingOrderDocumentProfile, User, list[ShippingOrderDocument]]:
|
||||
"""创建一份已发布 Profile 及三份不同文档盲测样本,真值不复用建模输入。"""
|
||||
user = User(username='profile-quality-admin', email='profile-quality-admin@example.com', role='admin')
|
||||
# 产品模型只允许唯一的 SHIPPING_ORDER Schema,测试仅创建其隔离版本。
|
||||
@@ -80,20 +99,39 @@ def _published_profile_with_samples() -> tuple[ShippingOrderDocumentProfile, Use
|
||||
db.session.add(version)
|
||||
db.session.flush()
|
||||
html = '<table><tr><th>Part</th><th>Qty</th></tr><tr><td>PN-1</td><td>3</td></tr></table>'
|
||||
fingerprint = order_profile_structure_fingerprint(parse_html_grid(html, table_id='table-1'), header_row_index=0)
|
||||
definition = {
|
||||
'document_match': {
|
||||
'document_type': 'purchase_order', 'page_pattern': 'one_or_more',
|
||||
'required_anchors': [], 'optional_anchors': [], 'minimum_score': 0, 'minimum_margin': 0,
|
||||
},
|
||||
'table_regions': [{
|
||||
'region_id': 'lines', 'page_scope': 'first_page',
|
||||
'line_start_field_code': 'qty',
|
||||
'region': {'x0': 0, 'y0': 0, 'x1': 1, 'y1': 1},
|
||||
'header_layouts': [], 'termination_rules': [], 'exclusion_regions': [],
|
||||
'continuation_rule': {'allow_cross_page': False, 'required_anchor_ids': []},
|
||||
'line_layouts': [{
|
||||
'layout_id': 'main', 'row_offset': 0, 'role': 'main',
|
||||
'new_line_anchor': None,
|
||||
'region': {'x0': 0, 'y0': 0, 'x1': 1, 'y1': 1},
|
||||
'field_regions': [
|
||||
{'field_id': 'part-id', 'field_code': 'part_number', 'scope': 'line',
|
||||
'region': {'x0': 0, 'y0': 0, 'x1': .6, 'y1': 1},
|
||||
'allowed_sources': ['pdf_coordinate']},
|
||||
{'field_id': 'qty-id', 'field_code': 'qty', 'scope': 'line',
|
||||
'region': {'x0': .6, 'y0': 0, 'x1': 1, 'y1': 1},
|
||||
'allowed_sources': ['pdf_coordinate']},
|
||||
],
|
||||
'continuation_conditions': [], 'required_fields': ['part_number', 'qty'],
|
||||
'allowed_empty_fields': [],
|
||||
}],
|
||||
}],
|
||||
'order_field_layouts': [], 'draft_unbound_order_field_layouts': [],
|
||||
}
|
||||
profile = ShippingOrderDocumentProfile(
|
||||
profile_code='QUALITY_TEMPLATE', display_name='Quality template', version=1, status='published',
|
||||
schema_version_id=version.id, structure_fingerprint=fingerprint, checksum='b' * 64,
|
||||
published_at=datetime.utcnow(), definition_json={
|
||||
'contract_version': 'shipping_order_document_profile.v1', 'structure_fingerprint': fingerprint,
|
||||
'header_row_index': 0, 'data_start_row_index': 1, 'row_structure': 'preserved',
|
||||
'drop_zero_qty': True, 'column_rules': [
|
||||
{'column_index': 0, 'extractor_key': 'direct', 'target_field_codes': ['part_number'],
|
||||
'target_fields': [{'field_id': 'part-id', 'field_code': 'part_number'}]},
|
||||
{'column_index': 1, 'extractor_key': 'direct', 'target_field_codes': ['qty'],
|
||||
'target_fields': [{'field_id': 'qty-id', 'field_code': 'qty'}]},
|
||||
],
|
||||
},
|
||||
schema_version_id=version.id, structure_fingerprint='a' * 64, checksum='b' * 64,
|
||||
published_at=datetime.utcnow(), definition_json=definition,
|
||||
)
|
||||
db.session.add(profile)
|
||||
db.session.flush()
|
||||
@@ -107,23 +145,36 @@ def _published_profile_with_samples() -> tuple[ShippingOrderDocumentProfile, Use
|
||||
)
|
||||
db.session.add(asset)
|
||||
db.session.flush()
|
||||
intake = ShippingNoticeIntake(
|
||||
intake = ShippingOrderIntake(
|
||||
intake_no=f'SNI-QUALITY-{index}', asset_id=asset.id, user_id=user.id,
|
||||
purpose='shipping_notice', parse_status='parsed', parser_version='test',
|
||||
)
|
||||
db.session.add(intake)
|
||||
db.session.flush()
|
||||
document = ShippingNoticeDocument(
|
||||
document = ShippingOrderDocument(
|
||||
intake_id=intake.id, asset_id=asset.id, document_type='attachment', attachment_key=asset.storage_key,
|
||||
filename=asset.original_filename, content_hash=asset.content_hash, classification='order_notice',
|
||||
parse_status='parsed', extracted_text='PN-1 3',
|
||||
)
|
||||
db.session.add(document)
|
||||
db.session.flush()
|
||||
glyphs = []
|
||||
for text, x0 in [('PN-1', 30), ('3', 150)]:
|
||||
for char_index, char in enumerate(text):
|
||||
glyphs.append({'text': char, 'bbox': {
|
||||
'x0': x0 + char_index * 5, 'y0': 20, 'x1': x0 + char_index * 5 + 4, 'y1': 30,
|
||||
}})
|
||||
record_inline_layout_snapshot(document, {
|
||||
'snapshot_version': 'shipping_document_table_snapshot.v1', 'provider': 'mineru',
|
||||
'backend': 'test', 'status': 'ready', 'issues': [],
|
||||
'tables': [{'table_id': 'table-1', 'page_no': 1, 'html': html}],
|
||||
'tables': [{'table_id': 'table-1', 'page_no': 1, 'html': html,
|
||||
'bbox': {'x0': 0, 'y0': 0, 'x1': 200, 'y1': 60}}],
|
||||
'coordinate_evidence': {
|
||||
'tables': {'table-1': glyphs},
|
||||
'pages': {'1': {'width': 200, 'height': 60}},
|
||||
'page_glyphs': {'1': glyphs},
|
||||
'sources': {'pages': {'1': 'pdf_coordinate'}},
|
||||
},
|
||||
})
|
||||
documents.append(document)
|
||||
db.session.commit()
|
||||
@@ -138,7 +189,6 @@ def _publishable_v2_draft(profile: ShippingOrderDocumentProfile) -> None:
|
||||
profile.checksum = None
|
||||
profile.published_at = None
|
||||
profile.definition_json = {
|
||||
'contract_version': 'order_coordinate_profile.v2',
|
||||
'document_match': {'required_anchors': [{'anchor_id': 'plant_name', 'label': 'PLANT NAME:', 'value': 'FACTORY A'}]},
|
||||
'table_regions': [{
|
||||
'region_id': 'lines', 'region': {'x0': 0, 'y0': 0, 'x1': 1, 'y1': 1},
|
||||
@@ -148,9 +198,9 @@ def _publishable_v2_draft(profile: ShippingOrderDocumentProfile) -> None:
|
||||
'layout_id': 'main', 'row_offset': 0, 'role': 'main',
|
||||
'region': {'x0': 0, 'y0': 0, 'x1': 1, 'y1': 1},
|
||||
'field_regions': [
|
||||
{'field_id': 'line-id', 'field_code': 'customer_po_line_no', 'region': {'x0': 0, 'y0': 0, 'x1': .2, 'y1': 1}, 'allowed_sources': ['pdf_coordinate']},
|
||||
{'field_id': 'part-id', 'field_code': 'part_number', 'region': {'x0': .2, 'y0': 0, 'x1': .6, 'y1': 1}, 'allowed_sources': ['pdf_coordinate']},
|
||||
{'field_id': 'qty-id', 'field_code': 'qty', 'region': {'x0': .6, 'y0': 0, 'x1': 1, 'y1': 1}, 'allowed_sources': ['pdf_coordinate']},
|
||||
{'field_id': 'line-id', 'field_code': 'customer_po_line_no', 'scope': 'line', 'region': {'x0': 0, 'y0': 0, 'x1': .2, 'y1': 1}, 'allowed_sources': ['pdf_coordinate']},
|
||||
{'field_id': 'part-id', 'field_code': 'part_number', 'scope': 'line', 'region': {'x0': .2, 'y0': 0, 'x1': .6, 'y1': 1}, 'allowed_sources': ['pdf_coordinate']},
|
||||
{'field_id': 'qty-id', 'field_code': 'qty', 'scope': 'line', 'region': {'x0': .6, 'y0': 0, 'x1': 1, 'y1': 1}, 'allowed_sources': ['pdf_coordinate']},
|
||||
],
|
||||
'continuation_conditions': [], 'required_fields': ['part_number', 'qty'], 'allowed_empty_fields': [],
|
||||
}],
|
||||
@@ -177,10 +227,10 @@ def test_profile_blind_test_approval_enables_current_auto_freeze_qualification(a
|
||||
|
||||
quality.add_sample(
|
||||
suite_id=suite['id'], document_id=documents[0].id, table_id='table-1',
|
||||
expected_rows=[{'part_number': 'PN-1', 'qty': 3}], actor_id=user.id,
|
||||
expected_rows=[{'part_number': 'PN-1', 'qty': '3'}], actor_id=user.id,
|
||||
)
|
||||
executed = quality.execute_suite(suite_id=suite['id'], actor_id=user.id)
|
||||
assert executed['latest_run']['status'] == 'passed'
|
||||
assert executed['latest_run']['status'] == 'passed', executed['latest_run']
|
||||
approved = quality.approve_suite(suite_id=suite['id'], actor_id=user.id)
|
||||
assert approved['status'] == 'approved'
|
||||
assert quality.profile_is_auto_create_approved(profile_id=profile.id, checksum=profile.checksum)
|
||||
@@ -203,7 +253,7 @@ def test_independent_sample_review_reuses_existing_truth_and_excludes_training_i
|
||||
profile, user, documents = _published_profile_with_samples()
|
||||
# 仅专用模板样本可供日常盲测选择;训练文档必须始终被排除。
|
||||
for document in documents:
|
||||
db.session.get(ShippingNoticeIntake, document.intake_id).purpose = 'order_document_profile_sample'
|
||||
db.session.get(ShippingOrderIntake, document.intake_id).purpose = 'order_document_profile_sample'
|
||||
db.session.add(ShippingOrderDocumentProfileSample(
|
||||
profile_id=profile.id, document_id=documents[0].id, table_id='table-1',
|
||||
sample_role='training', created_by_id=user.id,
|
||||
@@ -221,22 +271,201 @@ def test_independent_sample_review_reuses_existing_truth_and_excludes_training_i
|
||||
candidate = quality.preview_independent_sample(
|
||||
suite_id=suite['id'], document_id=documents[1].id, table_id='table-1', actor_id=user.id,
|
||||
)
|
||||
assert candidate['rows'] == [{'part_number': 'PN-1', 'qty': 3}]
|
||||
assert candidate['rows'] == [{'part_number': 'PN-1', 'qty': '3'}]
|
||||
with pytest.raises(Exception) as incomplete:
|
||||
quality.confirm_sample_reviews(
|
||||
suite_id=suite['id'], document_id=documents[1].id, table_id='table-1',
|
||||
row_reviews=[{'correct': None, 'values': candidate['rows'][0]}],
|
||||
order_field_reviews={}, actor_id=user.id,
|
||||
order_field_reviews={}, layout_version_id=candidate['layout_version_id'],
|
||||
layout_payload_checksum=candidate['layout_payload_checksum'], actor_id=user.id,
|
||||
)
|
||||
assert getattr(incomplete.value, 'code', None) == 'SHIPPING.ORDER_DOCUMENT_PROFILE_TEST_INVALID'
|
||||
|
||||
confirmed = quality.confirm_sample_reviews(
|
||||
suite_id=suite['id'], document_id=documents[1].id, table_id='table-1',
|
||||
row_reviews=[{'correct': True, 'values': {}}], order_field_reviews={}, actor_id=user.id,
|
||||
row_reviews=[{'correct': True, 'values': {}}], order_field_reviews={},
|
||||
layout_version_id=candidate['layout_version_id'],
|
||||
layout_payload_checksum=candidate['layout_payload_checksum'], actor_id=user.id,
|
||||
)
|
||||
assert confirmed['sample_count'] == 1
|
||||
sample = db.session.get(ShippingDocumentProfileTestSuite, suite['id']).samples[0]
|
||||
assert sample.expected_rows_json == candidate['rows']
|
||||
assert sample.layout_version_id == candidate['layout_version_id']
|
||||
assert sample.layout_payload_checksum == candidate['layout_payload_checksum']
|
||||
|
||||
# 当前指针变化不得改变已确认样本的回放输入;盲测必须继续读取冻结版本。
|
||||
current_version = documents[1].current_layout_version
|
||||
replacement = dict(current_version.payload_json or {})
|
||||
replacement['tables'] = []
|
||||
from app.services.shipping_notice.document_layout.lifecycle import record_inline_layout_snapshot
|
||||
record_inline_layout_snapshot(documents[1], replacement, parser_key='changed-parser-key')
|
||||
db.session.commit()
|
||||
executed = quality.execute_suite(suite_id=suite['id'], actor_id=user.id)
|
||||
assert executed['latest_run']['status'] == 'passed'
|
||||
|
||||
|
||||
def test_layout_failed_document_is_diagnostic_only(app, monkeypatch):
|
||||
"""关键断言:失败 Layout 可从原 PDF 诊断,但不能确认成盲测真值。"""
|
||||
with app.app_context():
|
||||
profile, user, documents = _published_profile_with_samples()
|
||||
document = documents[1]
|
||||
db.session.get(ShippingOrderIntake, document.intake_id).purpose = 'order_document_profile_sample'
|
||||
current_version = document.current_layout_version
|
||||
current_version.status = 'failed'
|
||||
current_version.payload_json = {
|
||||
'snapshot_version': 'shipping_document_table_snapshot.v1',
|
||||
'status': 'failed', 'tables': [],
|
||||
'issues': ['mineru_cell_coordinates_missing'],
|
||||
}
|
||||
current_version.issues_json = ['mineru_cell_coordinates_missing']
|
||||
db.session.commit()
|
||||
suite = quality.create_suite(profile_id=profile.id, actor_id=user.id)
|
||||
|
||||
def recover_document_coordinates(*, document, layout, page_nos, table_by_page=None):
|
||||
"""模拟从原始 PDF 恢复整页字符,验证盲测不依赖 MinerU table_id。"""
|
||||
assert page_nos is None
|
||||
recovered = dict(layout)
|
||||
recovered['coordinate_evidence'] = {
|
||||
'pages': {'1': {'width': 200, 'height': 60}},
|
||||
'page_glyphs': {'1': [
|
||||
{'text': char, 'bbox': {
|
||||
'x0': x0 + char_index * 5, 'y0': 20,
|
||||
'x1': x0 + char_index * 5 + 4, 'y1': 30,
|
||||
}}
|
||||
for text, x0 in [('PN-1', 30), ('3', 150)]
|
||||
for char_index, char in enumerate(text)
|
||||
]},
|
||||
'sources': {'pages': {'1': 'pdf_coordinate'}},
|
||||
'tables': {},
|
||||
}
|
||||
return recovered
|
||||
|
||||
monkeypatch.setattr(quality, 'ensure_on_demand_coordinate_evidence', recover_document_coordinates)
|
||||
|
||||
available = quality.list_independent_samples(suite_id=suite['id'], actor_id=user.id)['samples']
|
||||
candidate_option = next(item for item in available if item['document_id'] == document.id)
|
||||
assert candidate_option['table_id'] == quality.DOCUMENT_CANDIDATE_TABLE_ID
|
||||
assert candidate_option['review_mode'] == 'diagnostic'
|
||||
assert candidate_option['confirmable'] is False
|
||||
assert candidate_option['layout_issue_codes'] == ['mineru_cell_coordinates_missing']
|
||||
|
||||
candidate = quality.preview_independent_sample(
|
||||
suite_id=suite['id'], document_id=document.id,
|
||||
table_id=quality.DOCUMENT_CANDIDATE_TABLE_ID, actor_id=user.id,
|
||||
)
|
||||
assert candidate['rows'] == [{'part_number': 'PN-1', 'qty': '3'}]
|
||||
with pytest.raises(Exception) as bypassed_review:
|
||||
quality.add_sample(
|
||||
suite_id=suite['id'], document_id=document.id,
|
||||
table_id=quality.DOCUMENT_CANDIDATE_TABLE_ID,
|
||||
expected_rows=candidate['rows'], actor_id=user.id,
|
||||
)
|
||||
assert getattr(bypassed_review.value, 'code', None) == 'SHIPPING.ORDER_DOCUMENT_PROFILE_TEST_INVALID'
|
||||
assert candidate['confirmable'] is False
|
||||
with pytest.raises(Exception) as diagnostic_confirmation:
|
||||
quality.confirm_sample_reviews(
|
||||
suite_id=suite['id'], document_id=document.id,
|
||||
table_id=quality.DOCUMENT_CANDIDATE_TABLE_ID,
|
||||
row_reviews=[{'correct': True}], order_field_reviews={},
|
||||
layout_version_id=current_version.id,
|
||||
layout_payload_checksum=current_version.payload_checksum, actor_id=user.id,
|
||||
)
|
||||
assert getattr(diagnostic_confirmation.value, 'code', None) == 'SHIPPING.ORDER_DOCUMENT_PROFILE_TEST_PREVIEW_STALE'
|
||||
|
||||
|
||||
def test_running_layout_without_payload_is_not_reviewable(app, monkeypatch):
|
||||
"""关键断言:后台 Layout 尚在运行时只显示准备状态,不启动临时 PDF 复核。"""
|
||||
with app.app_context():
|
||||
profile, user, documents = _published_profile_with_samples()
|
||||
document = documents[1]
|
||||
db.session.get(ShippingOrderIntake, document.intake_id).purpose = 'order_document_profile_sample'
|
||||
current_version = document.current_layout_version
|
||||
current_version.status = 'running'
|
||||
current_version.payload_json = None
|
||||
current_version.issues_json = []
|
||||
document.current_layout_version_id = None
|
||||
db.session.commit()
|
||||
suite = quality.create_suite(profile_id=profile.id, actor_id=user.id)
|
||||
|
||||
def recover_document_coordinates(*, document, layout, page_nos, table_by_page=None):
|
||||
"""模拟正在预热时直接从原 PDF 恢复整页坐标,空布局不得阻断复核。"""
|
||||
assert layout == {'status': 'pending', 'tables': [], 'issues': []}
|
||||
assert page_nos is None
|
||||
return {
|
||||
**layout,
|
||||
'coordinate_evidence': {
|
||||
'pages': {'1': {'width': 200, 'height': 60}},
|
||||
'page_glyphs': {'1': [
|
||||
{'text': char, 'bbox': {
|
||||
'x0': x0 + char_index * 5, 'y0': 20,
|
||||
'x1': x0 + char_index * 5 + 4, 'y1': 30,
|
||||
}}
|
||||
for text, x0 in [('PN-1', 30), ('3', 150)]
|
||||
for char_index, char in enumerate(text)
|
||||
]},
|
||||
'sources': {'pages': {'1': 'pdf_coordinate'}},
|
||||
'tables': {},
|
||||
},
|
||||
}
|
||||
|
||||
monkeypatch.setattr(quality, 'ensure_on_demand_coordinate_evidence', recover_document_coordinates)
|
||||
|
||||
available = quality.list_independent_samples(suite_id=suite['id'], actor_id=user.id)['samples']
|
||||
candidate_option = next(item for item in available if item['document_id'] == document.id)
|
||||
assert candidate_option['table_id'] == quality.DOCUMENT_CANDIDATE_TABLE_ID
|
||||
assert candidate_option['review_mode'] == 'preparing'
|
||||
assert candidate_option['confirmable'] is False
|
||||
|
||||
with pytest.raises(Exception) as preparing_preview:
|
||||
quality.preview_independent_sample(
|
||||
suite_id=suite['id'], document_id=document.id,
|
||||
table_id=quality.DOCUMENT_CANDIDATE_TABLE_ID, actor_id=user.id,
|
||||
)
|
||||
assert getattr(preparing_preview.value, 'code', None) == 'SHIPPING.ORDER_DOCUMENT_PROFILE_TEST_NOT_READY'
|
||||
|
||||
|
||||
def test_document_candidate_identity_expires_after_background_layout_completion(app, monkeypatch):
|
||||
"""关键断言:后台产生真实 table_id 后,旧文档候选必须刷新并重新正式预览。"""
|
||||
with app.app_context():
|
||||
profile, user, documents = _published_profile_with_samples()
|
||||
document = documents[1]
|
||||
db.session.get(ShippingOrderIntake, document.intake_id).purpose = 'order_document_profile_sample'
|
||||
suite = quality.create_suite(profile_id=profile.id, actor_id=user.id)
|
||||
|
||||
# 当前列表已经转成真实表格候选,模拟页面仍持有上传完成时的文档级身份。
|
||||
available = quality.list_independent_samples(suite_id=suite['id'], actor_id=user.id)['samples']
|
||||
assert any(item['document_id'] == document.id and item['table_id'] == 'table-1' for item in available)
|
||||
assert not any(
|
||||
item['document_id'] == document.id and item['table_id'] == quality.DOCUMENT_CANDIDATE_TABLE_ID
|
||||
for item in available
|
||||
)
|
||||
|
||||
def recover_document_coordinates(*, document, layout, page_nos, table_by_page=None):
|
||||
"""文档级复核使用整页证据,不依赖后台刚生成的表格身份。"""
|
||||
assert page_nos is None
|
||||
recovered = dict(layout)
|
||||
recovered['coordinate_evidence'] = {
|
||||
'pages': {'1': {'width': 200, 'height': 60}},
|
||||
'page_glyphs': {'1': [
|
||||
{'text': char, 'bbox': {
|
||||
'x0': x0 + char_index * 5, 'y0': 20,
|
||||
'x1': x0 + char_index * 5 + 4, 'y1': 30,
|
||||
}}
|
||||
for text, x0 in [('PN-1', 30), ('3', 150)]
|
||||
for char_index, char in enumerate(text)
|
||||
]},
|
||||
'sources': {'pages': {'1': 'pdf_coordinate'}},
|
||||
'tables': {},
|
||||
}
|
||||
return recovered
|
||||
|
||||
monkeypatch.setattr(quality, 'ensure_on_demand_coordinate_evidence', recover_document_coordinates)
|
||||
with pytest.raises(Exception) as stale_preview:
|
||||
quality.preview_independent_sample(
|
||||
suite_id=suite['id'], document_id=document.id,
|
||||
table_id=quality.DOCUMENT_CANDIDATE_TABLE_ID, actor_id=user.id,
|
||||
)
|
||||
assert getattr(stale_preview.value, 'code', None) == 'SHIPPING.ORDER_DOCUMENT_PROFILE_TEST_SAMPLE_NOT_INDEPENDENT'
|
||||
|
||||
|
||||
def test_independent_samples_include_historical_blind_documents(app):
|
||||
@@ -246,7 +475,7 @@ def test_independent_samples_include_historical_blind_documents(app):
|
||||
historical_suite = quality.create_suite(profile_id=profile.id, actor_id=user.id)
|
||||
quality.add_sample(
|
||||
suite_id=historical_suite['id'], document_id=documents[0].id, table_id='table-1',
|
||||
expected_rows=[{'part_number': 'PN-1', 'qty': 3}], actor_id=user.id,
|
||||
expected_rows=[{'part_number': 'PN-1', 'qty': '3'}], actor_id=user.id,
|
||||
)
|
||||
draft = ShippingOrderDocumentProfile(
|
||||
profile_code=profile.profile_code, display_name=profile.display_name, version=2, status='draft',
|
||||
@@ -292,7 +521,7 @@ def test_independent_samples_deduplicate_reuploaded_content(app):
|
||||
with app.app_context():
|
||||
profile, user, documents = _published_profile_with_samples()
|
||||
for document in documents[:2]:
|
||||
db.session.get(ShippingNoticeIntake, document.intake_id).purpose = 'order_document_profile_sample'
|
||||
db.session.get(ShippingOrderIntake, document.intake_id).purpose = 'order_document_profile_sample'
|
||||
documents[1].content_hash = documents[0].content_hash
|
||||
db.session.commit()
|
||||
suite = quality.create_suite(profile_id=profile.id, actor_id=user.id)
|
||||
@@ -309,13 +538,13 @@ def test_coordinate_v2_blind_suite_uses_frozen_coordinate_evidence_not_html_grid
|
||||
with app.app_context():
|
||||
profile, user, documents = _published_profile_with_samples()
|
||||
profile.definition_json = {
|
||||
'contract_version': 'order_coordinate_profile.v2', 'document_match': {'required_anchors': []},
|
||||
'document_match': {'required_anchors': []},
|
||||
'table_regions': [{'region_id': 'lines', 'region': {'x0': 0, 'y0': 0, 'x1': 1, 'y1': 1}, 'line_layouts': [{
|
||||
'layout_id': 'main', 'row_offset': 0, 'role': 'main', 'region': {'x0': 0, 'y0': 0, 'x1': 1, 'y1': 1},
|
||||
'field_regions': [
|
||||
{'field_id': 'line-id', 'field_code': 'customer_po_line_no', 'region': {'x0': 0, 'y0': 0, 'x1': .15, 'y1': 1}, 'allowed_sources': ['pdf_coordinate']},
|
||||
{'field_id': 'part-id', 'field_code': 'part_number', 'region': {'x0': .15, 'y0': 0, 'x1': .6, 'y1': 1}, 'allowed_sources': ['pdf_coordinate']},
|
||||
{'field_id': 'qty-id', 'field_code': 'qty', 'region': {'x0': .6, 'y0': 0, 'x1': 1, 'y1': 1}, 'allowed_sources': ['pdf_coordinate']},
|
||||
{'field_id': 'line-id', 'field_code': 'customer_po_line_no', 'scope': 'line', 'region': {'x0': 0, 'y0': 0, 'x1': .15, 'y1': 1}, 'allowed_sources': ['pdf_coordinate']},
|
||||
{'field_id': 'part-id', 'field_code': 'part_number', 'scope': 'line', 'region': {'x0': .15, 'y0': 0, 'x1': .6, 'y1': 1}, 'allowed_sources': ['pdf_coordinate']},
|
||||
{'field_id': 'qty-id', 'field_code': 'qty', 'scope': 'line', 'region': {'x0': .6, 'y0': 0, 'x1': 1, 'y1': 1}, 'allowed_sources': ['pdf_coordinate']},
|
||||
],
|
||||
}]}],
|
||||
}
|
||||
@@ -343,11 +572,11 @@ def test_coordinate_blind_candidate_list_does_not_prepare_missing_evidence(app,
|
||||
with app.app_context():
|
||||
profile, user, documents = _published_profile_with_samples()
|
||||
profile.definition_json = {
|
||||
'contract_version': 'order_coordinate_profile.v2', 'document_match': {'required_anchors': []},
|
||||
'document_match': {'required_anchors': []},
|
||||
'table_regions': [], 'order_field_layouts': [],
|
||||
}
|
||||
for document in documents:
|
||||
db.session.get(ShippingNoticeIntake, document.intake_id).purpose = 'order_document_profile_sample'
|
||||
db.session.get(ShippingOrderIntake, document.intake_id).purpose = 'order_document_profile_sample'
|
||||
db.session.add(ShippingOrderDocumentProfileSample(
|
||||
profile_id=profile.id, document_id=documents[0].id, table_id='table-1',
|
||||
sample_role='training', created_by_id=user.id,
|
||||
@@ -369,17 +598,17 @@ def test_coordinate_blind_preview_skips_header_only_regions(app):
|
||||
with app.app_context():
|
||||
profile, user, documents = _published_profile_with_samples()
|
||||
profile.definition_json = {
|
||||
'contract_version': 'order_coordinate_profile.v2', 'document_match': {'required_anchors': []},
|
||||
'document_match': {'required_anchors': []},
|
||||
'table_regions': [
|
||||
{'region_id': 'lines', 'page_scope': 'exact_page', 'page_no': 1,
|
||||
'region': {'x0': 0, 'y0': 0, 'x1': 1, 'y1': 1}, 'line_layouts': [{
|
||||
'layout_id': 'main', 'row_offset': 0, 'role': 'main',
|
||||
'region': {'x0': 0, 'y0': 0, 'x1': 1, 'y1': 1},
|
||||
'field_regions': [
|
||||
{'field_id': 'line-id', 'field_code': 'customer_po_line_no',
|
||||
{'field_id': 'line-id', 'field_code': 'customer_po_line_no', 'scope': 'line',
|
||||
'region': {'x0': 0, 'y0': 0, 'x1': .3, 'y1': 1},
|
||||
'allowed_sources': ['pdf_coordinate']},
|
||||
{'field_id': 'part-id', 'field_code': 'part_number',
|
||||
{'field_id': 'part-id', 'field_code': 'part_number', 'scope': 'line',
|
||||
'region': {'x0': .3, 'y0': 0, 'x1': 1, 'y1': 1},
|
||||
'allowed_sources': ['pdf_coordinate']},
|
||||
],
|
||||
@@ -415,7 +644,7 @@ def test_profile_checksum_change_invalidates_approved_quality_credential(app):
|
||||
for document in documents:
|
||||
quality.add_sample(
|
||||
suite_id=suite['id'], document_id=document.id, table_id='table-1',
|
||||
expected_rows=[{'part_number': 'PN-1', 'qty': 3}], actor_id=user.id,
|
||||
expected_rows=[{'part_number': 'PN-1', 'qty': '3'}], actor_id=user.id,
|
||||
)
|
||||
quality.execute_suite(suite_id=suite['id'], actor_id=user.id)
|
||||
quality.approve_suite(suite_id=suite['id'], actor_id=user.id)
|
||||
@@ -518,7 +747,7 @@ def test_blind_test_sample_removal_is_audited_and_approved_suite_is_immutable(ap
|
||||
suite = quality.create_suite(profile_id=profile.id, actor_id=user.id)
|
||||
quality.add_sample(
|
||||
suite_id=suite['id'], document_id=documents[0].id, table_id='table-1',
|
||||
expected_rows=[{'part_number': 'PN-1', 'qty': 3}], actor_id=user.id,
|
||||
expected_rows=[{'part_number': 'PN-1', 'qty': '3'}], actor_id=user.id,
|
||||
)
|
||||
sample_id = db.session.get(ShippingDocumentProfileTestSuite, suite['id']).samples[0].id
|
||||
serialized = quality.serialize_suite(db.session.get(ShippingDocumentProfileTestSuite, suite['id']))
|
||||
@@ -539,7 +768,7 @@ def test_blind_test_sample_removal_is_audited_and_approved_suite_is_immutable(ap
|
||||
for document in documents:
|
||||
quality.add_sample(
|
||||
suite_id=suite['id'], document_id=document.id, table_id='table-1',
|
||||
expected_rows=[{'part_number': 'PN-1', 'qty': 3}], actor_id=user.id,
|
||||
expected_rows=[{'part_number': 'PN-1', 'qty': '3'}], actor_id=user.id,
|
||||
)
|
||||
quality.execute_suite(suite_id=suite['id'], actor_id=user.id)
|
||||
quality.approve_suite(suite_id=suite['id'], actor_id=user.id)
|
||||
@@ -566,8 +795,8 @@ def test_draft_publish_requires_current_approved_blind_test(app):
|
||||
assert getattr(caught.value, 'code', None) == 'SHIPPING.ORDER_DOCUMENT_PROFILE_PUBLISH_GATE_BLIND_TEST'
|
||||
|
||||
|
||||
def test_draft_publish_requires_static_document_anchor(app):
|
||||
"""关键断言:零必填锚点的模板不能发布,避免成为匹配所有 PDF 的通配模板。"""
|
||||
def test_draft_publish_does_not_force_static_document_anchor(app):
|
||||
"""关键断言:是否配置必需锚点由用户决定,发布门禁不再强制添加。"""
|
||||
from app.services.shipping_notice import document_profile_quality_service as quality
|
||||
|
||||
with app.app_context():
|
||||
@@ -578,7 +807,7 @@ def test_draft_publish_requires_static_document_anchor(app):
|
||||
with pytest.raises(Exception) as caught:
|
||||
quality.assert_profile_publishable(profile)
|
||||
|
||||
assert getattr(caught.value, 'code', None) == 'SHIPPING.ORDER_DOCUMENT_PROFILE_PUBLISH_GATE_REQUIRED_ANCHOR'
|
||||
assert getattr(caught.value, 'code', None) != 'SHIPPING.ORDER_DOCUMENT_PROFILE_PUBLISH_GATE_REQUIRED_ANCHOR'
|
||||
|
||||
|
||||
def test_coordinate_v2_publish_promotes_approved_independent_suite(app):
|
||||
@@ -591,7 +820,6 @@ def test_coordinate_v2_publish_promotes_approved_independent_suite(app):
|
||||
profile.checksum = None
|
||||
profile.published_at = None
|
||||
profile.definition_json = {
|
||||
'contract_version': 'order_coordinate_profile.v2',
|
||||
'document_match': {'required_anchors': [{'anchor_id': 'po_title', 'label': 'DOCUMENT TYPE:', 'value': 'PURCHASE ORDER', 'value_position': 'right'}]},
|
||||
'table_regions': [{
|
||||
'region_id': 'lines', 'region': {'x0': 0, 'y0': 0, 'x1': 1, 'y1': 1},
|
||||
@@ -601,9 +829,9 @@ def test_coordinate_v2_publish_promotes_approved_independent_suite(app):
|
||||
'layout_id': 'main', 'row_offset': 0, 'role': 'main',
|
||||
'region': {'x0': 0, 'y0': .16, 'x1': 1, 'y1': 1},
|
||||
'field_regions': [
|
||||
{'field_id': 'line-id', 'field_code': 'customer_po_line_no', 'region': {'x0': 0, 'y0': .16, 'x1': .15, 'y1': 1}, 'allowed_sources': ['pdf_coordinate']},
|
||||
{'field_id': 'part-id', 'field_code': 'part_number', 'region': {'x0': .15, 'y0': .16, 'x1': .6, 'y1': 1}, 'allowed_sources': ['pdf_coordinate']},
|
||||
{'field_id': 'qty-id', 'field_code': 'qty', 'region': {'x0': .6, 'y0': 0, 'x1': 1, 'y1': 1}, 'allowed_sources': ['pdf_coordinate']},
|
||||
{'field_id': 'line-id', 'field_code': 'customer_po_line_no', 'scope': 'line', 'region': {'x0': 0, 'y0': .16, 'x1': .15, 'y1': 1}, 'allowed_sources': ['pdf_coordinate']},
|
||||
{'field_id': 'part-id', 'field_code': 'part_number', 'scope': 'line', 'region': {'x0': .15, 'y0': .16, 'x1': .6, 'y1': 1}, 'allowed_sources': ['pdf_coordinate']},
|
||||
{'field_id': 'qty-id', 'field_code': 'qty', 'scope': 'line', 'region': {'x0': .6, 'y0': 0, 'x1': 1, 'y1': 1}, 'allowed_sources': ['pdf_coordinate']},
|
||||
],
|
||||
'continuation_conditions': [], 'required_fields': ['part_number', 'qty'], 'allowed_empty_fields': [],
|
||||
}],
|
||||
@@ -615,7 +843,7 @@ def test_coordinate_v2_publish_promotes_approved_independent_suite(app):
|
||||
)
|
||||
profile.structure_fingerprint = profiles._definition_hash(profile.definition_json)
|
||||
|
||||
def set_coordinate_rows(document: ShippingNoticeDocument, rows: list[tuple[str, str]]) -> None:
|
||||
def set_coordinate_rows(document: ShippingOrderDocument, rows: list[tuple[str, str]]) -> None:
|
||||
"""冻结每个样本的字符坐标,让 v2 盲测不依赖 HTML 网格。"""
|
||||
glyphs = []
|
||||
for row_index, (part, qty) in enumerate(rows):
|
||||
@@ -628,12 +856,18 @@ def test_coordinate_v2_publish_promotes_approved_independent_suite(app):
|
||||
record_inline_layout_snapshot(document, {
|
||||
'snapshot_version': 'shipping_document_table_snapshot.v1', 'status': 'ready',
|
||||
'tables': [{'table_id': 'table-1', 'page_no': 1}],
|
||||
'coordinate_evidence': {'tables': {'table-1': glyphs}, 'pages': {'1': {'width': 200, 'height': 100}}},
|
||||
# 发布冻结要求 page_glyphs + sources,避免测试误走 MinIO 按需补证据。
|
||||
'coordinate_evidence': {
|
||||
'tables': {'table-1': glyphs},
|
||||
'pages': {'1': {'width': 200, 'height': 100}},
|
||||
'page_glyphs': {'1': glyphs},
|
||||
'sources': {'pages': {'1': 'pdf_coordinate'}},
|
||||
},
|
||||
})
|
||||
|
||||
for document in documents:
|
||||
set_coordinate_rows(document, [('PN-1', '3')])
|
||||
extra = ShippingNoticeDocument(
|
||||
extra = ShippingOrderDocument(
|
||||
intake_id=documents[2].intake_id, asset_id=documents[2].asset_id, document_type='attachment',
|
||||
attachment_key='quality-v2-extra.pdf', filename='quality-v2-extra.pdf', content_hash='f' * 64,
|
||||
classification='order_notice', parse_status='parsed', extracted_text='PN-3 5',
|
||||
|
||||
Reference in new issue
Block a user