test(shipping): 更新订单复核库存与通知流程测试
This commit is contained in:
1 parent
8ec028e6a3
commit
265a6e179a
168 files changed
+9420
-4613
No files matched your search
@@ -21,6 +21,7 @@ from app.models import (
|
||||
User,
|
||||
)
|
||||
from app.services.shipping_notice.document_layout.lifecycle import (
|
||||
COORDINATE_EVIDENCE_REF_KEY,
|
||||
canonical_layout_payload,
|
||||
claim_layout_version,
|
||||
complete_layout_version,
|
||||
@@ -76,6 +77,19 @@ def _snapshot(*, status: str = 'ready', marker: str = 'v1') -> dict:
|
||||
}
|
||||
|
||||
|
||||
def _externalizable_evidence() -> dict:
|
||||
"""构造可外部化为 FileAsset 的最小坐标证据(含非空 page_glyphs)。
|
||||
|
||||
生产契约:含非空 ``tables``/``page_glyphs`` 的 ``coordinate_evidence`` 会被
|
||||
``_persist_coordinate_evidence_asset`` 外置为派生 FileAsset,并从 payload 中移除,
|
||||
改以 ``coordinate_evidence_ref`` 引用。PDF ready 布局必须有该引用。
|
||||
"""
|
||||
return {
|
||||
'pages': {'1': {'width': 100, 'height': 200}},
|
||||
'page_glyphs': {'1': [{'text': 'x'}]},
|
||||
}
|
||||
|
||||
|
||||
def test_layout_snapshot_checksum_and_idempotent_current_version(app):
|
||||
"""相同内容/parser/checksum 重放不得新增版本,成功快照应成为唯一当前引用。"""
|
||||
with app.app_context():
|
||||
@@ -207,7 +221,12 @@ def test_layout_schedule_is_idempotent_and_force_creates_next_version(app):
|
||||
app.config['SHIPPING_NOTICE_LAYOUT_MINERU_ENABLED'] = True
|
||||
with app.app_context():
|
||||
document = _document()
|
||||
ready = record_inline_layout_snapshot(document, _snapshot())
|
||||
# PDF 坐标证据必须外置为 FileAsset(coordinate_evidence_ref)才可被复用。
|
||||
ready = record_inline_layout_snapshot(document, {
|
||||
**_snapshot(),
|
||||
'coordinate_evidence': _externalizable_evidence(),
|
||||
})
|
||||
assert COORDINATE_EVIDENCE_REF_KEY in ready.payload_json
|
||||
reused = schedule_layout_version(document)
|
||||
forced = schedule_layout_version(document, force=True)
|
||||
db.session.commit()
|
||||
@@ -227,7 +246,10 @@ def test_layout_schedule_localizes_same_owner_completed_layout_for_duplicate_doc
|
||||
app.config['SHIPPING_NOTICE_LAYOUT_MINERU_ENABLED'] = True
|
||||
with app.app_context():
|
||||
original = _document()
|
||||
ready = record_inline_layout_snapshot(original, _snapshot())
|
||||
ready = record_inline_layout_snapshot(original, {
|
||||
**_snapshot(),
|
||||
'coordinate_evidence': _externalizable_evidence(),
|
||||
})
|
||||
duplicate_intake = ShippingOrderIntake(
|
||||
intake_no='SNI-LAYOUT-DUPLICATE', asset_id=original.asset_id,
|
||||
user_id=db.session.get(ShippingOrderIntake, original.intake_id).user_id,
|
||||
@@ -256,12 +278,15 @@ def test_layout_schedule_localizes_same_owner_completed_layout_for_duplicate_doc
|
||||
|
||||
|
||||
def test_recoverable_ready_layout_is_reused_for_duplicate_document(app):
|
||||
"""ready 快照缺整页证据但原 PDF 可读时应复用,正式识别再按需恢复坐标。"""
|
||||
"""ready 布局缺少整页坐标时,只要坐标证据已外置为可恢复资产即应复用。"""
|
||||
app.config['SHIPPING_NOTICE_LAYOUT_MINERU_ENABLED'] = True
|
||||
with app.app_context():
|
||||
original = _document()
|
||||
# 仅保留少数字符坐标,仍能被外置为 FileAsset(coordinate_evidence_ref),
|
||||
# 缺页坐标由正式识别按需从原 PDF 恢复。
|
||||
incomplete = record_inline_layout_snapshot(original, {
|
||||
**_snapshot(), 'coordinate_evidence': {'tables': {}},
|
||||
**_snapshot(),
|
||||
'coordinate_evidence': _externalizable_evidence(),
|
||||
})
|
||||
duplicate_intake = ShippingOrderIntake(
|
||||
intake_no='SNI-LAYOUT-INCOMPLETE-DUP', asset_id=original.asset_id,
|
||||
@@ -325,8 +350,9 @@ def test_shared_visual_layout_reuse_localizes_xfa_structure_analysis(app, monkey
|
||||
'backend': 'pipeline',
|
||||
'snapshot_version': 'shipping_document_layout.v1',
|
||||
'tables': [],
|
||||
# 新统一 Layout 仍要满足普通 PDF 的坐标证据复用门禁;datasets 仅为末端值源。
|
||||
'coordinate_evidence': {'pages': {'1': {}}, 'page_glyphs': {'1': []}},
|
||||
# 新统一 Layout 仍要满足普通 PDF 的坐标证据复用门禁(外置为 FileAsset);
|
||||
# datasets 仅为末端值源。
|
||||
'coordinate_evidence': {'pages': {'1': {}}, 'page_glyphs': {'1': [{'text': 'x'}]}},
|
||||
'xfa_capability': {
|
||||
'structure_analysis_ready': True,
|
||||
'structure_analysis_id': source_analysis.id,
|
||||
@@ -483,7 +509,7 @@ def test_layout_recovery_invalidates_old_lease_and_preserves_good_current(app):
|
||||
|
||||
|
||||
def test_compacted_pdf_coordinate_evidence_remains_ready_when_asset_is_recoverable(app):
|
||||
"""PDF 原件仍可读时,压缩整页/表内字符不得丢失模板所需表格结构。"""
|
||||
"""PDF 坐标证据外置为 FileAsset 后,模板所需表格结构保持原样且布局仍 ready。"""
|
||||
app.config['SHIPPING_NOTICE_LAYOUT_MINERU_ENABLED'] = True
|
||||
app.config['SHIPPING_NOTICE_LAYOUT_MAX_SNAPSHOT_BYTES'] = 700
|
||||
with app.app_context():
|
||||
@@ -521,23 +547,27 @@ def test_compacted_pdf_coordinate_evidence_remains_ready_when_asset_is_recoverab
|
||||
assert document.current_layout_version_id == pending.id
|
||||
assert pending.payload_json['tables'] == snapshot['tables']
|
||||
assert pending.payload_json['layout_candidate'] == snapshot['layout_candidate']
|
||||
assert 'tables' not in pending.payload_json['coordinate_evidence']
|
||||
assert 'page_glyphs' not in pending.payload_json['coordinate_evidence']
|
||||
assert pending.payload_json['matching_features']['version'] == 'v1'
|
||||
assert pending.payload_json['matching_features']['tables'][0]['bbox'] == [0.01, 0.01, 0.3, 0.2]
|
||||
assert 'shipping_document_layout_page_glyphs_compacted' in pending.issues_json
|
||||
assert 'shipping_document_layout_table_glyphs_compacted' in pending.issues_json
|
||||
assert 'shipping_document_layout_coordinate_evidence_on_demand' in pending.issues_json
|
||||
# 关键断言:坐标证据被外置为 FileAsset 引用,不再内联在快照中。
|
||||
assert 'coordinate_evidence' not in pending.payload_json
|
||||
ref = pending.payload_json[COORDINATE_EVIDENCE_REF_KEY]
|
||||
assert ref['asset_no']
|
||||
assert ref['checksum']
|
||||
assert ref['size'] > 0
|
||||
assert ref['schema_version'] == 'shipping_order_coordinate_evidence.v1'
|
||||
# 模板结构保留,无需把外置证据当作容量压缩问题。
|
||||
assert 'shipping_document_layout_coordinate_evidence_persist_failed' not in pending.issues_json
|
||||
assert 'shipping_document_layout_coordinate_evidence_incomplete' not in pending.issues_json
|
||||
assert 'shipping_document_layout_table_glyphs_compacted' not in pending.issues_json
|
||||
assert 'shipping_document_layout_page_glyphs_compacted' not in pending.issues_json
|
||||
|
||||
|
||||
def test_inline_layout_compacts_table_glyphs_without_changing_template_structures(app):
|
||||
"""普通上传内联布局同样只压缩可恢复字符,模板表格与候选结构保持原样。"""
|
||||
def test_inline_layout_externalizes_table_glyphs_without_changing_template_structures(app):
|
||||
"""普通上传内联布局同样只外置可恢复字符,模板表格与候选结构保持原样。"""
|
||||
app.config['SHIPPING_NOTICE_LAYOUT_MAX_SNAPSHOT_BYTES'] = 700
|
||||
with app.app_context():
|
||||
document = _document()
|
||||
snapshot = {
|
||||
**_snapshot(marker='inline-compacted'),
|
||||
**_snapshot(marker='inline-externalized'),
|
||||
'tables': [{
|
||||
'table_id': 'table-1', 'page_no': 1,
|
||||
'bbox': {'x0': 1, 'y0': 2, 'x1': 30, 'y1': 40},
|
||||
@@ -556,9 +586,10 @@ def test_inline_layout_compacts_table_glyphs_without_changing_template_structure
|
||||
assert version.status == 'ready'
|
||||
assert version.payload_json['tables'] == snapshot['tables']
|
||||
assert version.payload_json['layout_candidate'] == snapshot['layout_candidate']
|
||||
assert 'tables' not in version.payload_json['coordinate_evidence']
|
||||
assert 'shipping_document_layout_table_glyphs_compacted' in version.issues_json
|
||||
assert 'shipping_document_layout_coordinate_evidence_on_demand' in version.issues_json
|
||||
# 关键断言:坐标证据被外置为 FileAsset 引用,而非内联在快照中。
|
||||
assert 'coordinate_evidence' not in version.payload_json
|
||||
assert version.payload_json[COORDINATE_EVIDENCE_REF_KEY]['asset_no']
|
||||
assert 'shipping_document_layout_coordinate_evidence_persist_failed' not in version.issues_json
|
||||
|
||||
|
||||
def test_on_demand_evidence_does_not_treat_business_table_list_as_coordinate_mapping(app):
|
||||
@@ -590,10 +621,11 @@ def test_on_demand_evidence_does_not_treat_business_table_list_as_coordinate_map
|
||||
assert 'tables' not in restored['coordinate_evidence']
|
||||
|
||||
|
||||
def test_on_demand_evidence_returns_coordinates_merged_from_current_layout(app):
|
||||
"""冻结快照缺坐标而当前 Layout 已有坐标时,必须返回合并后的运行期证据。"""
|
||||
def test_on_demand_evidence_returns_coordinates_merged_from_current_layout(app, monkeypatch):
|
||||
"""外部化契约:冻结快照缺坐标时,按需恢复从不可变原 PDF 读取当前 Layout 坐标。"""
|
||||
with app.app_context():
|
||||
document = _document()
|
||||
# 当前布局坐标证据被外置为 FileAsset(coordinate_evidence_ref),不再内联。
|
||||
current = record_inline_layout_snapshot(document, {
|
||||
**_snapshot(),
|
||||
'coordinate_evidence': {
|
||||
@@ -610,18 +642,34 @@ def test_on_demand_evidence_returns_coordinates_merged_from_current_layout(app):
|
||||
'tables': [{'table_id': 'table-1', 'page_no': 1, 'bbox': {'x0': 0, 'y0': 0, 'x1': 100, 'y1': 200}}],
|
||||
'coordinate_evidence': {'pages': {'1': {'width': 100, 'height': 200}}},
|
||||
}
|
||||
glyph = {'text': 'H', 'bbox': {'x0': 1, 'y0': 1, 'x1': 2, 'y1': 2}}
|
||||
page = SimpleNamespace(
|
||||
page_no=1, width=100, height=200, image=None,
|
||||
characters=[SimpleNamespace(model_dump=lambda mode='json': glyph)],
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
'app.services.file_assets.service.read_asset_bytes', lambda _asset: b'%PDF-test',
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
'app.services.shipping_notice.document_layout.native_pdf.extract_native_pdf_pages',
|
||||
lambda _content: [page],
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
'app.services.shipping_notice.order_document_profile_service._native_characters_in_table',
|
||||
lambda *, characters, table_bbox: [glyph],
|
||||
)
|
||||
|
||||
restored = ensure_on_demand_coordinate_evidence(
|
||||
document=document, layout=frozen_layout, page_nos={1},
|
||||
)
|
||||
|
||||
# 关键断言:不得因当前版本坐标存在而错误返回旧冻结快照。
|
||||
# 关键断言:坐标从不可变原 PDF(当前事实)恢复,而非停留在缺坐标的冻结快照。
|
||||
assert restored['coordinate_evidence']['page_glyphs']['1'][0]['text'] == 'H'
|
||||
assert restored['coordinate_evidence']['tables']['table-1'][0]['text'] == 'H'
|
||||
|
||||
|
||||
def test_on_demand_evidence_prefers_current_layout_coordinates_over_stale_run_snapshot(app):
|
||||
"""同页同时存在时,当前 Layout 是最新事实,不能被旧 Run 快照坐标覆盖。"""
|
||||
def test_on_demand_evidence_prefers_current_layout_coordinates_over_stale_run_snapshot(app, monkeypatch):
|
||||
"""外部化契约:当前 Layout 坐标外置后,恢复读不可变原 PDF,过期 Run 快照不覆盖。"""
|
||||
with app.app_context():
|
||||
document = _document()
|
||||
current = record_inline_layout_snapshot(document, {
|
||||
@@ -633,19 +681,32 @@ def test_on_demand_evidence_prefers_current_layout_coordinates_over_stale_run_sn
|
||||
},
|
||||
})
|
||||
document.current_layout_version = current
|
||||
# 过期 Run 快照的坐标为空(缺 page_glyphs),不能作为坐标事实。
|
||||
stale_run_layout = {
|
||||
**_snapshot(),
|
||||
'coordinate_evidence': {
|
||||
'pages': {'1': {'width': 100, 'height': 200}},
|
||||
'page_glyphs': {'1': [{'text': 'STALE', 'bbox': {'x0': 1, 'y0': 1, 'x1': 2, 'y1': 2}}]},
|
||||
'sources': {'pages': {'1': 'ocr_coordinate'}},
|
||||
},
|
||||
}
|
||||
glyph = {'text': 'CURRENT', 'bbox': {'x0': 1, 'y0': 1, 'x1': 2, 'y1': 2}}
|
||||
page = SimpleNamespace(
|
||||
page_no=1, width=100, height=200, image=None,
|
||||
characters=[SimpleNamespace(model_dump=lambda mode='json': glyph)],
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
'app.services.file_assets.service.read_asset_bytes', lambda _asset: b'%PDF-test',
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
'app.services.shipping_notice.document_layout.native_pdf.extract_native_pdf_pages',
|
||||
lambda _content: [page],
|
||||
)
|
||||
|
||||
restored = ensure_on_demand_coordinate_evidence(
|
||||
document=document, layout=stale_run_layout, page_nos={1},
|
||||
)
|
||||
|
||||
# 关键断言:以不可变原 PDF(当前事实)恢复,而非信任过期快照。
|
||||
assert restored['coordinate_evidence']['page_glyphs']['1'][0]['text'] == 'CURRENT'
|
||||
assert restored['coordinate_evidence']['sources']['pages']['1'] == 'pdf_coordinate'
|
||||
|
||||
|
||||
Reference in new issue
Block a user