feat(shipping-notice): 添加 MinerU 订单附件表格结构化功能,更新环境配置和数据模型以支持新布局解析和文档模板管理
This commit is contained in:
1 parent
c4095b08da
commit
c35223d49d
148 files changed
+15709
-1353
No files matched your search
@@ -0,0 +1,209 @@
|
||||
"""
|
||||
backend/tests/test_shipping_document_layout_lifecycle.py
|
||||
|
||||
用途:验证 L1 版面快照 checksum、版本幂等、容量门禁和当前成功版本保护。
|
||||
依赖:ShippingNoticeDocument/LayoutVersion、document_layout.lifecycle、Flask 测试库。
|
||||
逻辑:相同快照复用;新失败/超限版本保留审计但不覆盖旧成功引用。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from app import db
|
||||
from app.models import Job, ShippingDocumentLayoutVersion, ShippingNoticeDocument, ShippingNoticeIntake, FileAsset, User
|
||||
from app.services.shipping_notice.document_layout.lifecycle import (
|
||||
canonical_layout_payload,
|
||||
claim_layout_version,
|
||||
complete_layout_version,
|
||||
current_layout_snapshot,
|
||||
record_inline_layout_snapshot,
|
||||
schedule_layout_version,
|
||||
schedule_document_layouts,
|
||||
sync_layout_after_job_recovery,
|
||||
)
|
||||
|
||||
|
||||
def _document() -> ShippingNoticeDocument:
|
||||
"""创建带稳定内容哈希的最小附件 Document。"""
|
||||
user = User(username='layout-owner', email='layout-owner@example.com', role='user', status='active')
|
||||
db.session.add(user)
|
||||
db.session.flush()
|
||||
asset = FileAsset(
|
||||
asset_no='FA-LAYOUT-L1', owner_user_id=user.id, original_filename='sample.pdf',
|
||||
storage_provider='minio', storage_bucket='test', storage_key='sample.pdf',
|
||||
content_hash='a' * 64, file_size=10, mime_type='application/pdf',
|
||||
upload_status='ready', lifecycle_status='referenced',
|
||||
)
|
||||
db.session.add(asset)
|
||||
db.session.flush()
|
||||
intake = ShippingNoticeIntake(
|
||||
intake_no='SNI-LAYOUT-L1', asset_id=asset.id, user_id=user.id,
|
||||
purpose='shipping_notice', parse_status='parsed', parser_version='v6',
|
||||
)
|
||||
db.session.add(intake)
|
||||
db.session.flush()
|
||||
document = ShippingNoticeDocument(
|
||||
intake_id=intake.id, asset_id=asset.id, document_type='attachment',
|
||||
attachment_key='sample.pdf', filename='sample.pdf', content_hash='a' * 64,
|
||||
classification='order_notice', parse_status='parsed',
|
||||
)
|
||||
db.session.add(document)
|
||||
db.session.flush()
|
||||
return document
|
||||
|
||||
|
||||
def _snapshot(*, status: str = 'ready', marker: str = 'v1') -> dict:
|
||||
"""构造不含正文的紧凑布局快照。"""
|
||||
return {
|
||||
'snapshot_version': 'shipping_document_table_snapshot.v1',
|
||||
'provider': 'mineru', 'provider_version': '3.1.14', 'backend': 'pipeline',
|
||||
'status': status, 'tables': [], 'issues': [], 'marker': marker,
|
||||
}
|
||||
|
||||
|
||||
def test_layout_snapshot_checksum_and_idempotent_current_version(app):
|
||||
"""相同内容/parser/checksum 重放不得新增版本,成功快照应成为唯一当前引用。"""
|
||||
with app.app_context():
|
||||
document = _document()
|
||||
checksum, size = canonical_layout_payload(_snapshot())
|
||||
first = record_inline_layout_snapshot(document, _snapshot())
|
||||
replay = record_inline_layout_snapshot(document, _snapshot())
|
||||
db.session.commit()
|
||||
|
||||
assert first.id == replay.id
|
||||
assert first.payload_checksum == checksum
|
||||
assert first.payload_size_bytes == size
|
||||
assert ShippingDocumentLayoutVersion.query.count() == 1
|
||||
assert document.current_layout_version_id == first.id
|
||||
assert current_layout_snapshot(document)['marker'] == 'v1'
|
||||
|
||||
|
||||
def test_failed_or_oversized_layout_does_not_replace_last_good_version(app):
|
||||
"""失败/超限版本必须可审计,但绝不能覆盖 Document 上一次成功布局。"""
|
||||
app.config['SHIPPING_NOTICE_LAYOUT_MAX_SNAPSHOT_BYTES'] = 200
|
||||
with app.app_context():
|
||||
document = _document()
|
||||
good = record_inline_layout_snapshot(document, _snapshot())
|
||||
failed = record_inline_layout_snapshot(document, _snapshot(status='failed', marker='failed'))
|
||||
oversized = record_inline_layout_snapshot(document, {
|
||||
**_snapshot(marker='large'), 'tables': [{'html': 'X' * 500}],
|
||||
})
|
||||
db.session.commit()
|
||||
|
||||
assert failed.status == 'failed'
|
||||
assert oversized.status == 'failed'
|
||||
assert oversized.payload_json is None
|
||||
assert 'shipping_document_layout_snapshot_size_exceeded' in oversized.issues_json
|
||||
assert document.current_layout_version_id == good.id
|
||||
assert current_layout_snapshot(document)['marker'] == 'v1'
|
||||
assert ShippingDocumentLayoutVersion.query.count() == 3
|
||||
|
||||
|
||||
def test_layout_schedule_is_idempotent_and_force_creates_next_version(app):
|
||||
"""同 parser 成功版本默认复用;显式重解析创建下一 queued 版本及独立 Job。"""
|
||||
app.config['SHIPPING_NOTICE_LAYOUT_MINERU_ENABLED'] = True
|
||||
with app.app_context():
|
||||
document = _document()
|
||||
ready = record_inline_layout_snapshot(document, _snapshot())
|
||||
reused = schedule_layout_version(document)
|
||||
forced = schedule_layout_version(document, force=True)
|
||||
db.session.commit()
|
||||
|
||||
assert reused.id == ready.id
|
||||
assert forced.id != ready.id
|
||||
assert forced.version == 2
|
||||
assert forced.status == 'queued'
|
||||
assert forced.job_id is not None
|
||||
job = db.session.get(Job, forced.job_id)
|
||||
assert job.job_type == 'shipping_document_layout'
|
||||
assert job.payload_json == {'layout_version_id': forced.id}
|
||||
|
||||
|
||||
def test_layout_parser_revision_change_creates_new_version_without_force(app):
|
||||
"""运维提升 parser revision 后不得复用旧 Provider/模型结果。"""
|
||||
app.config['SHIPPING_NOTICE_LAYOUT_MINERU_ENABLED'] = True
|
||||
with app.app_context():
|
||||
document = _document()
|
||||
old = record_inline_layout_snapshot(
|
||||
document, _snapshot(),
|
||||
parser_key='mineru:pipeline:shipping_document_table_snapshot.v1:v1',
|
||||
)
|
||||
app.config['SHIPPING_NOTICE_LAYOUT_PARSER_REVISION'] = 'v2'
|
||||
new = schedule_layout_version(document)
|
||||
db.session.commit()
|
||||
|
||||
assert new.id != old.id
|
||||
assert new.version == 2
|
||||
assert new.parser_key.endswith(':v2')
|
||||
|
||||
|
||||
def test_layout_recovery_invalidates_old_lease_and_preserves_good_current(app):
|
||||
"""Job 回收后版本回 queued 并清 lease,旧 Worker 结果不得覆盖当前成功版本。"""
|
||||
app.config['SHIPPING_NOTICE_LAYOUT_MINERU_ENABLED'] = True
|
||||
with app.app_context():
|
||||
document = _document()
|
||||
ready = record_inline_layout_snapshot(document, _snapshot())
|
||||
pending = schedule_layout_version(document, force=True)
|
||||
job = db.session.get(Job, pending.job_id)
|
||||
job.status = 'running'
|
||||
job.attempts = 1
|
||||
db.session.commit()
|
||||
claimed = claim_layout_version(pending.id, job_id=job.id, owned_attempt=1)
|
||||
assert claimed is not None
|
||||
_version, old_lease = claimed
|
||||
|
||||
job.status = 'queued'
|
||||
sync_layout_after_job_recovery(job)
|
||||
db.session.commit()
|
||||
|
||||
assert pending.status == 'queued'
|
||||
assert pending.lease_token is None
|
||||
assert complete_layout_version(
|
||||
pending.id, lease_token=old_lease, snapshot=_snapshot(marker='stale'),
|
||||
) is False
|
||||
db.session.refresh(document)
|
||||
assert document.current_layout_version_id == ready.id
|
||||
|
||||
|
||||
def test_layout_document_budget_creates_unsupported_audit_versions(app):
|
||||
"""超过单 Intake 配额的支持附件必须有 unsupported 审计,不能静默跳过。"""
|
||||
app.config['SHIPPING_NOTICE_LAYOUT_MINERU_ENABLED'] = True
|
||||
app.config['SHIPPING_NOTICE_LAYOUT_MINERU_MAX_DOCUMENTS_PER_INTAKE'] = 1
|
||||
with app.app_context():
|
||||
first = _document()
|
||||
second = ShippingNoticeDocument(
|
||||
intake_id=first.intake_id, asset_id=first.asset_id, document_type='attachment',
|
||||
attachment_key='second.pdf', filename='second.pdf', content_hash='b' * 64,
|
||||
classification='order_notice', parse_status='parsed',
|
||||
)
|
||||
text = ShippingNoticeDocument(
|
||||
intake_id=first.intake_id, asset_id=first.asset_id, document_type='attachment',
|
||||
attachment_key='notes.txt', filename='notes.txt', content_hash='c' * 64,
|
||||
classification='order_notice', parse_status='parsed',
|
||||
)
|
||||
db.session.add_all([second, text])
|
||||
db.session.flush()
|
||||
|
||||
versions = schedule_document_layouts([first, text, second])
|
||||
db.session.commit()
|
||||
|
||||
assert len(versions) == 2
|
||||
assert versions[0].status == 'queued'
|
||||
assert versions[1].status == 'unsupported'
|
||||
assert versions[1].issues_json == ['mineru_intake_document_budget_exceeded']
|
||||
assert ShippingDocumentLayoutVersion.query.filter_by(document_id=text.id).count() == 0
|
||||
|
||||
|
||||
def test_deleting_document_cascades_layout_versions(app):
|
||||
"""Document 删除必须先解除 current 引用,并级联清理其全部布局版本。"""
|
||||
with app.app_context():
|
||||
document = _document()
|
||||
version = record_inline_layout_snapshot(document, _snapshot())
|
||||
document_id = document.id
|
||||
version_id = version.id
|
||||
db.session.commit()
|
||||
|
||||
db.session.delete(document)
|
||||
db.session.commit()
|
||||
|
||||
assert db.session.get(ShippingNoticeDocument, document_id) is None
|
||||
assert db.session.get(ShippingDocumentLayoutVersion, version_id) is None
|
||||
Reference in new issue
Block a user