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
@@ -0,0 +1,115 @@
|
||||
"""
|
||||
tests/test_shipping_notice_intake_helpers.py
|
||||
|
||||
用途:ShippingOrderIntake 测试共用 fixture / 种子 / PDF 合成工具。
|
||||
依赖:pytest、FileStorage、FileAsset、ShippingOrderIntake、upload_file_asset。
|
||||
逻辑:供 intakes_lifecycle / parse / jobs 三个拆分模块 import;不包含断言用例。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import threading
|
||||
import time
|
||||
from datetime import datetime
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
from werkzeug.datastructures import FileStorage
|
||||
from io import BytesIO
|
||||
|
||||
from app import db
|
||||
from app.core.errors import ConflictError, ForbiddenError, NotFoundError, ValidationError
|
||||
from app.models import (
|
||||
FileAsset,
|
||||
ShippingDocumentLayoutVersion,
|
||||
ShippingOrderCandidate,
|
||||
ShippingOrderDocument,
|
||||
ShippingOrderIntake,
|
||||
ShippingOrderExtraction,
|
||||
ShippingOrderSchema,
|
||||
ShippingOrderSchemaVersion,
|
||||
ShippingNotice,
|
||||
User,
|
||||
)
|
||||
from app.services.file_assets.service import upload_file_asset
|
||||
from app.services.shipping_notice.intake_service import create_shipping_notice_intake
|
||||
from app.services.shipping_notice.business_fingerprint import (
|
||||
build_business_fingerprint,
|
||||
build_content_fingerprint,
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def shipping_user(app):
|
||||
with app.app_context():
|
||||
user = User(username='intake-user', email='intake@example.com', password_hash='x')
|
||||
db.session.add(user)
|
||||
db.session.commit()
|
||||
yield user
|
||||
|
||||
def _make_upload(content: bytes = b'customer order line', filename: str = 'notice.txt') -> FileStorage:
|
||||
return FileStorage(stream=BytesIO(content), filename=filename, content_type='text/plain')
|
||||
|
||||
def _seed_parsed_intake(user_id: int, *, intake_no: str = 'SNI-TEST-0001', hash_value: str = 'abc123'):
|
||||
asset = FileAsset(
|
||||
asset_no=f'FA-{intake_no}',
|
||||
owner_user_id=user_id,
|
||||
original_filename='notice.txt',
|
||||
storage_provider='minio',
|
||||
storage_bucket='hxops-file-assets',
|
||||
storage_key=f'temporary/{user_id}/test/{intake_no}.txt',
|
||||
content_hash=hash_value,
|
||||
file_size=12,
|
||||
mime_type='text/plain',
|
||||
upload_status='ready',
|
||||
lifecycle_status='temporary',
|
||||
reference_count=0,
|
||||
)
|
||||
db.session.add(asset)
|
||||
db.session.flush()
|
||||
intake = ShippingOrderIntake(
|
||||
intake_no=intake_no,
|
||||
asset_id=asset.id,
|
||||
user_id=user_id,
|
||||
purpose='shipping_notice',
|
||||
parse_status='parsed',
|
||||
parser_version='v1',
|
||||
extracted_text='SKU001 qty 10',
|
||||
)
|
||||
db.session.add(intake)
|
||||
db.session.commit()
|
||||
return asset, intake
|
||||
|
||||
def _minimal_pdf_bytes(marker: str) -> bytes:
|
||||
"""构造含可见文本的最小 PDF(与 extract 测试口径一致)。"""
|
||||
safe = str(marker).replace('\\', '\\\\').replace('(', '\\(').replace(')', '\\)')
|
||||
stream = f'BT /F1 24 Tf 72 720 Td ({safe}) Tj ET'.encode('latin-1')
|
||||
objects = [
|
||||
b'1 0 obj<< /Type /Catalog /Pages 2 0 R >>endobj\n',
|
||||
b'2 0 obj<< /Type /Pages /Kids [3 0 R] /Count 1 >>endobj\n',
|
||||
(
|
||||
b'3 0 obj<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] '
|
||||
b'/Contents 4 0 R /Resources<< /Font<< /F1 5 0 R >> >> >>endobj\n'
|
||||
),
|
||||
f'4 0 obj<< /Length {len(stream)} >>stream\n'.encode('latin-1')
|
||||
+ stream
|
||||
+ b'\nendstream\nendobj\n',
|
||||
b'5 0 obj<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica >>endobj\n',
|
||||
]
|
||||
out = bytearray(b'%PDF-1.4\n')
|
||||
offsets = [0]
|
||||
for obj in objects:
|
||||
offsets.append(len(out))
|
||||
out.extend(obj)
|
||||
xref_pos = len(out)
|
||||
out.extend(f'xref\n0 {len(offsets)}\n'.encode('latin-1'))
|
||||
out.extend(b'0000000000 65535 f \n')
|
||||
for off in offsets[1:]:
|
||||
out.extend(f'{off:010d} 00000 n \n'.encode('latin-1'))
|
||||
out.extend(
|
||||
f'trailer<< /Size {len(offsets)} /Root 1 0 R >>\nstartxref\n{xref_pos}\n%%EOF\n'.encode(
|
||||
'latin-1'
|
||||
)
|
||||
)
|
||||
return bytes(out)
|
||||
|
||||
Reference in new issue
Block a user