feat(shipping-order): 迭代货代订单提取与履约全流程功能
添加订单字段自动标注配置,默认仅关键字段参与自动框选 完善字段规范化逻辑,支持更多日期格式解析 添加订单候选人工采纳、驳回的完整流程,包含API、前端交互与状态管理 完善盲测功能,支持订单级字段真值校验,调整盲测集唯一性约束 加固库存校验逻辑,防止重复预约与数量不一致问题 优化坐标匹配逻辑,支持表格外的锚点定位 新增上传结果未知的状态处理,完善任务状态机 更新多语言文案与文档说明,补充功能细节 修复订单合并、重解析的边缘情况问题 补充相关测试用例,完善质量保障
This commit is contained in:
1 parent
11079910ec
commit
90f77634d6
76 files changed
+5056
-338
No files matched your search
@@ -3,7 +3,7 @@ backend/tests/test_shipping_document_profile_quality.py
|
||||
|
||||
用途:验证订单文档模板的独立盲测、批准资格和冻结 checksum 门禁。
|
||||
依赖:Profile 服务、布局快照、模板盲测服务与测试数据库。
|
||||
逻辑:管理员固化三份独立样本真值;全部通过且批准后才可放行确定性自动建单。
|
||||
逻辑:管理员固化一份独立样本真值;通过且批准后才可放行确定性自动建单。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
@@ -111,17 +111,12 @@ def _published_profile_with_samples() -> tuple[ShippingOrderDocumentProfile, Use
|
||||
return profile, user, documents
|
||||
|
||||
|
||||
def test_profile_auto_create_requires_three_passing_blind_samples_and_approval(app):
|
||||
"""关键断言:三样本全通过和管理员批准缺一不可,批准后样本不可篡改。"""
|
||||
def test_profile_auto_create_requires_one_passing_blind_sample_and_approval(app):
|
||||
"""关键断言:一份独立样本通过并经批准后才能放行,批准后样本不可篡改。"""
|
||||
with app.app_context():
|
||||
profile, user, documents = _published_profile_with_samples()
|
||||
suite = quality.create_suite(profile_id=profile.id, actor_id=user.id)
|
||||
assert not quality.profile_is_auto_create_approved(profile_id=profile.id, checksum=profile.checksum)
|
||||
for document in documents[:2]:
|
||||
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,
|
||||
)
|
||||
executed = quality.execute_suite(suite_id=suite['id'], actor_id=user.id)
|
||||
assert executed['latest_run']['status'] == 'failed'
|
||||
with pytest.raises(Exception) as caught:
|
||||
@@ -129,10 +124,11 @@ def test_profile_auto_create_requires_three_passing_blind_samples_and_approval(a
|
||||
assert getattr(caught.value, 'code', None) == 'SHIPPING.ORDER_DOCUMENT_PROFILE_TEST_NOT_READY'
|
||||
|
||||
quality.add_sample(
|
||||
suite_id=suite['id'], document_id=documents[2].id, table_id='table-1',
|
||||
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,
|
||||
)
|
||||
quality.execute_suite(suite_id=suite['id'], actor_id=user.id)
|
||||
executed = quality.execute_suite(suite_id=suite['id'], actor_id=user.id)
|
||||
assert executed['latest_run']['status'] == 'passed'
|
||||
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)
|
||||
@@ -144,6 +140,43 @@ def test_profile_auto_create_requires_three_passing_blind_samples_and_approval(a
|
||||
assert getattr(immutable.value, 'code', None) == 'SHIPPING.ORDER_DOCUMENT_PROFILE_TEST_IMMUTABLE'
|
||||
|
||||
|
||||
def test_independent_sample_review_reuses_existing_truth_and_excludes_training_input(app):
|
||||
"""关键断言:只展示同类未建模样本,逐项确认后才将候选写为既有盲测真值。"""
|
||||
with app.app_context():
|
||||
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.add(ShippingOrderDocumentProfileSample(
|
||||
profile_id=profile.id, document_id=documents[0].id, table_id='table-1',
|
||||
sample_role='training', created_by_id=user.id,
|
||||
))
|
||||
db.session.commit()
|
||||
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 {item['document_id'] for item in available} == {documents[1].id, documents[2].id}
|
||||
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}]
|
||||
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,
|
||||
)
|
||||
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,
|
||||
)
|
||||
assert confirmed['sample_count'] == 1
|
||||
sample = db.session.get(ShippingDocumentProfileTestSuite, suite['id']).samples[0]
|
||||
assert sample.expected_rows_json == candidate['rows']
|
||||
|
||||
|
||||
def test_coordinate_v2_blind_suite_uses_frozen_coordinate_evidence_not_html_grid(app):
|
||||
"""v2 盲测直接回放坐标字段,HTML 网格即使不存在也不能成为质量执行依赖。"""
|
||||
with app.app_context():
|
||||
@@ -178,6 +211,44 @@ def test_coordinate_v2_blind_suite_uses_frozen_coordinate_evidence_not_html_grid
|
||||
assert executed['latest_run']['status'] == 'passed', executed['latest_run']['failure_codes']
|
||||
|
||||
|
||||
def test_coordinate_blind_preview_skips_header_only_regions(app):
|
||||
"""关键断言:仅含订单字段的页不重建物理行,候选预览不会因空布局报 500。"""
|
||||
with app.app_context():
|
||||
profile, user, documents = _published_profile_with_samples()
|
||||
profile.definition_json = {
|
||||
'contract_version': 'order_coordinate_profile.v2', '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': 'part-id', 'field_code': 'part_number',
|
||||
'region': {'x0': 0, 'y0': 0, 'x1': 1, 'y1': 1},
|
||||
'allowed_sources': ['pdf_coordinate']}],
|
||||
}]},
|
||||
{'region_id': 'header_only', 'page_scope': 'exact_page', 'page_no': 2,
|
||||
'region': {'x0': 0, 'y0': 0, 'x1': 1, 'y1': 1},
|
||||
'header_layouts': [{'layout_id': 'header', 'region': {'x0': 0, 'y0': 0, 'x1': 1, 'y1': .1}, 'field_regions': []}],
|
||||
'line_layouts': []},
|
||||
],
|
||||
}
|
||||
for document in documents:
|
||||
glyphs = [
|
||||
{'text': char, 'bbox': {'x0': 20 + index * 4, 'y0': 20, 'x1': 23 + index * 4, 'y1': 30}}
|
||||
for index, char in enumerate('PN-1')
|
||||
]
|
||||
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': 60}}},
|
||||
})
|
||||
db.session.commit()
|
||||
suite = quality.create_suite(profile_id=profile.id, actor_id=user.id)
|
||||
candidate = quality._candidate_for_sample(profile=profile, document_id=documents[0].id, table_id='table-1')
|
||||
assert suite['id'] > 0
|
||||
assert candidate['rows'] == []
|
||||
|
||||
|
||||
def test_profile_checksum_change_invalidates_approved_quality_credential(app):
|
||||
"""关键断言:Profile checksum 变更后,旧盲测凭证绝不能继续放行。"""
|
||||
with app.app_context():
|
||||
@@ -195,6 +266,21 @@ def test_profile_checksum_change_invalidates_approved_quality_credential(app):
|
||||
assert not quality.profile_is_auto_create_approved(profile_id=profile.id, checksum=profile.checksum)
|
||||
|
||||
|
||||
def test_profile_change_supersedes_stale_draft_suite_and_creates_current_suite(app):
|
||||
"""保存模板后必须建立新 checksum 的盲测集,旧草稿真值不得继续阻断或背书。"""
|
||||
with app.app_context():
|
||||
profile, user, _documents = _published_profile_with_samples()
|
||||
stale = quality.create_suite(profile_id=profile.id, actor_id=user.id)
|
||||
profile.checksum = 'd' * 64
|
||||
db.session.commit()
|
||||
|
||||
current = quality.create_suite(profile_id=profile.id, actor_id=user.id)
|
||||
assert current['id'] != stale['id']
|
||||
assert current['profile_checksum'] == 'd' * 64
|
||||
stale_suite = db.session.get(ShippingDocumentProfileTestSuite, stale['id'])
|
||||
assert stale_suite.status == 'superseded'
|
||||
|
||||
|
||||
def test_blind_test_sample_removal_is_audited_and_approved_suite_is_immutable(app):
|
||||
"""盲测草稿可受控删除样本并留痕,批准后的真值不得再被篡改。"""
|
||||
with app.app_context():
|
||||
@@ -205,8 +291,15 @@ def test_blind_test_sample_removal_is_audited_and_approved_suite_is_immutable(ap
|
||||
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']))
|
||||
# 关键断言:工作台只得到移除定位信息,绝不返回人工确认的字段真值。
|
||||
assert serialized['samples'] == [{
|
||||
'id': sample_id, 'document_id': documents[0].id, 'table_id': 'table-1',
|
||||
'filename': 'q0.pdf', 'page_no': 1,
|
||||
}]
|
||||
removed = quality.remove_sample(suite_id=suite['id'], sample_id=sample_id, actor_id=user.id)
|
||||
assert removed['sample_count'] == 0
|
||||
assert removed['samples'] == []
|
||||
audit = ShippingOrderDocumentProfileAudit.query.filter_by(profile_id=profile.id).order_by(
|
||||
ShippingOrderDocumentProfileAudit.id.desc(),
|
||||
).first()
|
||||
@@ -244,7 +337,7 @@ def test_draft_publish_requires_independent_training_and_multiline_blind_gate(ap
|
||||
assert getattr(missing_suite.value, 'code', None) == 'SHIPPING.ORDER_DOCUMENT_PROFILE_PUBLISH_GATE_BLIND_TEST'
|
||||
|
||||
suite = quality.create_suite(profile_id=profile.id, actor_id=user.id)
|
||||
# Use different documents than the training sample. One sample is real multi-line evidence.
|
||||
# One independent sample is enough, but it must still be genuine multi-line evidence.
|
||||
record_inline_layout_snapshot(documents[1], {
|
||||
'snapshot_version': 'shipping_document_table_snapshot.v1', 'status': 'ready',
|
||||
'tables': [{'table_id': 'table-1', 'page_no': 1, 'html': '<table><tr><th>Part</th><th>Qty</th></tr><tr><td>PN-1</td><td>3</td></tr><tr><td>PN-2</td><td>4</td></tr></table>'}],
|
||||
@@ -252,28 +345,9 @@ def test_draft_publish_requires_independent_training_and_multiline_blind_gate(ap
|
||||
quality.add_sample(suite_id=suite['id'], document_id=documents[1].id, table_id='table-1', expected_rows=[
|
||||
{'part_number': 'PN-1', 'qty': 3}, {'part_number': 'PN-2', 'qty': 4},
|
||||
], actor_id=user.id)
|
||||
quality.add_sample(suite_id=suite['id'], document_id=documents[2].id, table_id='table-1', expected_rows=[{'part_number': 'PN-1', 'qty': 3}], actor_id=user.id)
|
||||
assert quality.execute_suite(suite_id=suite['id'], actor_id=user.id)['latest_run']['status'] == 'failed'
|
||||
|
||||
# A third independent document is necessary even when one sample is multi-line.
|
||||
extra = ShippingNoticeDocument(
|
||||
intake_id=documents[2].intake_id, asset_id=documents[2].asset_id, document_type='attachment',
|
||||
attachment_key='quality-extra.pdf', filename='quality-extra.pdf', content_hash='e' * 64,
|
||||
classification='order_notice', parse_status='parsed', extracted_text='PN-1 3',
|
||||
)
|
||||
db.session.add(extra)
|
||||
db.session.flush()
|
||||
record_inline_layout_snapshot(extra, {
|
||||
'snapshot_version': 'shipping_document_table_snapshot.v1', 'status': 'ready',
|
||||
'tables': [{'table_id': 'table-1', 'page_no': 1, 'html': '<table><tr><th>Part</th><th>Qty</th></tr><tr><td>PN-1</td><td>3</td></tr></table>'}],
|
||||
})
|
||||
db.session.commit()
|
||||
quality.add_sample(suite_id=suite['id'], document_id=extra.id, table_id='table-1', 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', executed['latest_run']['failure_codes']
|
||||
quality.approve_suite(suite_id=suite['id'], actor_id=user.id)
|
||||
# This legacy fixture cannot be republished under the v2-only write contract,
|
||||
# but its current approved credential proves the draft gate accepts independent evidence.
|
||||
assert quality.profile_is_auto_create_approved(profile_id=profile.id, checksum=profile.structure_fingerprint)
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user