feat(shipping-notice): 添加订单文档 Profile 盲测集和样本管理功能,支持创建、执行和批准盲测,增强订单冻结和库存核验功能
This commit is contained in:
1 parent
c35223d49d
commit
abd8fc1e16
98 files changed
+4629
-528
No files matched your search
@@ -0,0 +1,153 @@
|
||||
"""
|
||||
backend/tests/test_shipping_document_profile_quality.py
|
||||
|
||||
用途:验证订单文档模板的独立盲测、批准资格和冻结 checksum 门禁。
|
||||
依赖:Profile 服务、布局快照、模板盲测服务与测试数据库。
|
||||
逻辑:管理员固化三份独立样本真值;全部通过且批准后才可放行确定性自动建单。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import datetime
|
||||
|
||||
import pytest
|
||||
|
||||
from app import db
|
||||
from app.models import (
|
||||
FileAsset,
|
||||
ShippingNoticeDocument,
|
||||
ShippingNoticeIntake,
|
||||
ShippingOrderDocumentProfile,
|
||||
ShippingOrderSchema,
|
||||
ShippingOrderSchemaVersion,
|
||||
User,
|
||||
)
|
||||
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
|
||||
|
||||
|
||||
def _published_profile_with_samples() -> tuple[ShippingOrderDocumentProfile, User, list[ShippingNoticeDocument]]:
|
||||
"""创建一份已发布 Profile 及三份不同文档盲测样本,真值不复用建模输入。"""
|
||||
user = User(username='profile-quality-admin', email='profile-quality-admin@example.com', role='admin')
|
||||
# 产品模型只允许唯一的 SHIPPING_ORDER Schema,测试仅创建其隔离版本。
|
||||
schema = ShippingOrderSchema(schema_code='SHIPPING_ORDER', display_name='Quality order')
|
||||
db.session.add_all([user, schema])
|
||||
db.session.flush()
|
||||
version = ShippingOrderSchemaVersion(
|
||||
schema_id=schema.id, version=1, status='published', checksum='a' * 64,
|
||||
published_at=datetime.utcnow(), definition_json={'fields': [
|
||||
{
|
||||
'field_id': 'part-id', 'field_code': 'part_number', 'scope': 'line',
|
||||
'value_type': 'string', 'enabled': True, 'required': True,
|
||||
'normalizer_key': 'trim', 'validator_keys': [],
|
||||
},
|
||||
{
|
||||
'field_id': 'qty-id', 'field_code': 'qty', 'scope': 'line',
|
||||
'value_type': 'decimal', 'enabled': True, 'required': True,
|
||||
'normalizer_key': 'decimal', 'validator_keys': ['positive_decimal'],
|
||||
},
|
||||
]},
|
||||
)
|
||||
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)
|
||||
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'}]},
|
||||
],
|
||||
},
|
||||
)
|
||||
db.session.add(profile)
|
||||
db.session.flush()
|
||||
documents = []
|
||||
for index in range(3):
|
||||
asset = FileAsset(
|
||||
asset_no=f'FA-QUALITY-{index}', owner_user_id=user.id, original_filename=f'q{index}.pdf',
|
||||
storage_provider='test', storage_bucket='test', storage_key=f'q{index}.pdf',
|
||||
content_hash=f'{index:x}' * 64, file_size=1, mime_type='application/pdf',
|
||||
upload_status='ready', lifecycle_status='temporary',
|
||||
)
|
||||
db.session.add(asset)
|
||||
db.session.flush()
|
||||
intake = ShippingNoticeIntake(
|
||||
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(
|
||||
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()
|
||||
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}],
|
||||
})
|
||||
documents.append(document)
|
||||
db.session.commit()
|
||||
return profile, user, documents
|
||||
|
||||
|
||||
def test_profile_auto_create_requires_three_passing_blind_samples_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:
|
||||
quality.approve_suite(suite_id=suite['id'], actor_id=user.id)
|
||||
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',
|
||||
expected_rows=[{'part_number': 'PN-1', 'qty': 3}], actor_id=user.id,
|
||||
)
|
||||
quality.execute_suite(suite_id=suite['id'], actor_id=user.id)
|
||||
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)
|
||||
with pytest.raises(Exception) as immutable:
|
||||
quality.add_sample(
|
||||
suite_id=suite['id'], document_id=documents[0].id, table_id='other',
|
||||
expected_rows=[{'part_number': 'PN-1'}], actor_id=user.id,
|
||||
)
|
||||
assert getattr(immutable.value, 'code', None) == 'SHIPPING.ORDER_DOCUMENT_PROFILE_TEST_IMMUTABLE'
|
||||
|
||||
|
||||
def test_profile_checksum_change_invalidates_approved_quality_credential(app):
|
||||
"""关键断言:Profile checksum 变更后,旧盲测凭证绝不能继续放行。"""
|
||||
with app.app_context():
|
||||
profile, user, documents = _published_profile_with_samples()
|
||||
suite = quality.create_suite(profile_id=profile.id, actor_id=user.id)
|
||||
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,
|
||||
)
|
||||
quality.execute_suite(suite_id=suite['id'], actor_id=user.id)
|
||||
quality.approve_suite(suite_id=suite['id'], actor_id=user.id)
|
||||
profile.checksum = 'c' * 64
|
||||
db.session.commit()
|
||||
assert not quality.profile_is_auto_create_approved(profile_id=profile.id, checksum=profile.checksum)
|
||||
Reference in new issue
Block a user