feat(shipping-notice): 增强发货通知候选管理功能,支持候选选择、重复检测与业务摘要修订
This commit is contained in:
1 parent
70f684b94a
commit
b595e157e9
83 files changed
+5857
-200
No files matched your search
@@ -29,6 +29,10 @@ from app.models import (
|
||||
from app.services.file_assets.service import upload_file_asset
|
||||
from app.services.shipping_notice.batch_from_intakes import create_shipping_batch_from_intakes
|
||||
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
|
||||
@@ -44,6 +48,538 @@ def _make_upload(content: bytes = b'customer order line', filename: str = 'notic
|
||||
return FileStorage(stream=BytesIO(content), filename=filename, content_type='text/plain')
|
||||
|
||||
|
||||
def test_candidate_review_duplicate_api_and_access_guards(app, client):
|
||||
"""Candidate API 应支持修订、刷新和决策,并阻止越权或已消费候选修改。"""
|
||||
from app.core.auth.jwt import issue_local_access_token
|
||||
|
||||
with app.app_context():
|
||||
owner = User(username='candidate-owner', email='candidate-owner@example.com', password_hash='x')
|
||||
other = User(username='candidate-other', email='candidate-other@example.com', password_hash='x')
|
||||
db.session.add_all([owner, other])
|
||||
db.session.flush()
|
||||
asset = FileAsset(
|
||||
asset_no='FA-CANDIDATE-API-001',
|
||||
owner_user_id=owner.id,
|
||||
original_filename='order-7001.pdf',
|
||||
storage_provider='minio',
|
||||
storage_bucket='hxops-file-assets',
|
||||
storage_key=f'temporary/{owner.id}/order-7001.pdf',
|
||||
content_hash='candidate-api-content-hash',
|
||||
file_size=100,
|
||||
mime_type='application/pdf',
|
||||
upload_status='ready',
|
||||
lifecycle_status='temporary',
|
||||
)
|
||||
db.session.add(asset)
|
||||
db.session.flush()
|
||||
intake = ShippingNoticeIntake(
|
||||
intake_no='SNI-CANDIDATE-API-001',
|
||||
asset_id=asset.id,
|
||||
user_id=owner.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='attachment-1',
|
||||
filename='order-7001.pdf',
|
||||
content_hash=asset.content_hash,
|
||||
classification='order_notice',
|
||||
parse_status='parsed',
|
||||
)
|
||||
existing = ShippingTask(
|
||||
task_no='SN-CANDIDATE-EXISTING-001',
|
||||
user_id=owner.id,
|
||||
source_content_hash=asset.content_hash,
|
||||
process_status='created',
|
||||
)
|
||||
db.session.add_all([document, existing])
|
||||
db.session.flush()
|
||||
candidate = ShippingNoticeCandidate(
|
||||
candidate_no='SNC-CANDIDATE-API-001',
|
||||
intake_id=intake.id,
|
||||
primary_document_id=document.id,
|
||||
candidate_key='test:SNC-CANDIDATE-API-001',
|
||||
po_number='7001',
|
||||
structured_result_json={
|
||||
'business_summary': {
|
||||
'po_number': '7001',
|
||||
'lines': [{'reservation_id': 'R-1', 'sku': 'SKU-1', 'qty': 1}],
|
||||
},
|
||||
},
|
||||
validation_status='needs_review',
|
||||
conflicts_json=[{'field': 'qty'}],
|
||||
selected=True,
|
||||
duplicate_resolution='open_existing',
|
||||
duplicate_target_task_no=existing.task_no,
|
||||
duplicate_resolved_by_id=owner.id,
|
||||
duplicate_resolved_at=datetime.utcnow(),
|
||||
)
|
||||
db.session.add(candidate)
|
||||
db.session.commit()
|
||||
owner_headers = {'Authorization': f'Bearer {issue_local_access_token(owner)}'}
|
||||
other_headers = {'Authorization': f'Bearer {issue_local_access_token(other)}'}
|
||||
|
||||
patch_response = client.patch(
|
||||
'/api/workspace/shipping-notice/candidates/SNC-CANDIDATE-API-001',
|
||||
json={
|
||||
'selected': False,
|
||||
'business_summary': {
|
||||
'po_number': '7001',
|
||||
'lines': [{'reservation_id': 'R-1', 'sku': 'SKU-1', 'qty': 2}],
|
||||
},
|
||||
},
|
||||
headers=owner_headers,
|
||||
)
|
||||
assert patch_response.status_code == 200
|
||||
patched = patch_response.get_json()['data']
|
||||
assert patched['selected'] is False
|
||||
assert patched['validation_status'] == 'ready'
|
||||
assert patched['conflicts'] == []
|
||||
assert patched['content_fingerprint']
|
||||
assert patched['duplicate_resolution'] is None
|
||||
|
||||
forbidden = client.patch(
|
||||
'/api/workspace/shipping-notice/candidates/SNC-CANDIDATE-API-001',
|
||||
json={'selected': True},
|
||||
headers=other_headers,
|
||||
)
|
||||
assert forbidden.status_code == 404
|
||||
|
||||
refresh = client.post(
|
||||
'/api/workspace/shipping-notice/candidates/SNC-CANDIDATE-API-001/duplicate-refresh',
|
||||
headers=owner_headers,
|
||||
)
|
||||
assert refresh.status_code == 200
|
||||
refreshed = refresh.get_json()['data']
|
||||
assert refreshed['duplicate']['status'] == 'exact_file'
|
||||
assert 'open_existing' in refreshed['duplicate']['allowed_actions']
|
||||
|
||||
resolution = client.post(
|
||||
'/api/workspace/shipping-notice/candidates/SNC-CANDIDATE-API-001/duplicate-resolution',
|
||||
json={'action': 'open_existing', 'target_task_no': 'SN-CANDIDATE-EXISTING-001'},
|
||||
headers=owner_headers,
|
||||
)
|
||||
assert resolution.status_code == 200
|
||||
assert resolution.get_json()['data']['duplicate_resolution']['action'] == 'open_existing'
|
||||
|
||||
with app.app_context():
|
||||
row = db.session.query(ShippingNoticeCandidate).filter_by(
|
||||
candidate_no='SNC-CANDIDATE-API-001',
|
||||
).one()
|
||||
row.consumed_task_id = db.session.query(ShippingTask).filter_by(
|
||||
task_no='SN-CANDIDATE-EXISTING-001',
|
||||
).one().id
|
||||
db.session.commit()
|
||||
|
||||
consumed = client.patch(
|
||||
'/api/workspace/shipping-notice/candidates/SNC-CANDIDATE-API-001',
|
||||
json={'selected': True},
|
||||
headers=owner_headers,
|
||||
)
|
||||
assert consumed.status_code == 409
|
||||
|
||||
|
||||
def test_candidate_review_rejects_empty_or_invalid_order_lines(app, client):
|
||||
"""人工修订不得以空摘要或无效 SKU/Qty 绕过 Candidate 审核门禁。"""
|
||||
from app.core.auth.jwt import issue_local_access_token
|
||||
|
||||
with app.app_context():
|
||||
user = User(username='candidate-validation', email='candidate-validation@example.com', password_hash='x')
|
||||
db.session.add(user)
|
||||
db.session.flush()
|
||||
asset = FileAsset(
|
||||
asset_no='FA-CANDIDATE-VALIDATION',
|
||||
owner_user_id=user.id,
|
||||
original_filename='invalid-order.pdf',
|
||||
storage_provider='minio',
|
||||
storage_bucket='hxops-file-assets',
|
||||
storage_key=f'temporary/{user.id}/invalid-order.pdf',
|
||||
content_hash='candidate-validation-hash',
|
||||
file_size=10,
|
||||
mime_type='application/pdf',
|
||||
upload_status='ready',
|
||||
lifecycle_status='temporary',
|
||||
)
|
||||
db.session.add(asset)
|
||||
db.session.flush()
|
||||
intake = ShippingNoticeIntake(
|
||||
intake_no='SNI-CANDIDATE-VALIDATION',
|
||||
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='invalid-order',
|
||||
filename='invalid-order.pdf',
|
||||
content_hash=asset.content_hash,
|
||||
classification='order_notice',
|
||||
parse_status='parsed',
|
||||
)
|
||||
db.session.add(document)
|
||||
db.session.flush()
|
||||
candidate = ShippingNoticeCandidate(
|
||||
candidate_no='SNC-CANDIDATE-VALIDATION',
|
||||
intake_id=intake.id,
|
||||
primary_document_id=document.id,
|
||||
candidate_key='test:SNC-CANDIDATE-VALIDATION',
|
||||
structured_result_json={'business_summary': {'lines': [{'sku': 'SKU-1', 'qty': 1}]}},
|
||||
validation_status='needs_review',
|
||||
selected=True,
|
||||
)
|
||||
db.session.add(candidate)
|
||||
db.session.commit()
|
||||
headers = {'Authorization': f'Bearer {issue_local_access_token(user)}'}
|
||||
|
||||
empty = client.patch(
|
||||
'/api/workspace/shipping-notice/candidates/SNC-CANDIDATE-VALIDATION',
|
||||
json={'business_summary': {}},
|
||||
headers=headers,
|
||||
)
|
||||
assert empty.status_code == 400
|
||||
assert empty.get_json()['error']['code'] == 'SHIPPING_NOTICE.CANDIDATE_REVIEW_REQUIRED'
|
||||
|
||||
invalid_qty = client.patch(
|
||||
'/api/workspace/shipping-notice/candidates/SNC-CANDIDATE-VALIDATION',
|
||||
json={'business_summary': {'lines': [{'sku': 'SKU-1', 'qty': 0}]}},
|
||||
headers=headers,
|
||||
)
|
||||
assert invalid_qty.status_code == 400
|
||||
|
||||
invalid_reservation = client.patch(
|
||||
'/api/workspace/shipping-notice/candidates/SNC-CANDIDATE-VALIDATION',
|
||||
json={
|
||||
'business_summary': {
|
||||
'lines': [
|
||||
{
|
||||
'sku': 'SKU-1',
|
||||
'qty': 1,
|
||||
'reservation_id': '1f1a682dfdc446a393415dee93eb36ce',
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
headers=headers,
|
||||
)
|
||||
assert invalid_reservation.status_code == 400
|
||||
assert invalid_reservation.get_json()['error']['code'] == 'SHIPPING_NOTICE.CANDIDATE_REVIEW_REQUIRED'
|
||||
|
||||
invalid_selected = client.patch(
|
||||
'/api/workspace/shipping-notice/candidates/SNC-CANDIDATE-VALIDATION',
|
||||
json={'selected': 'false'},
|
||||
headers=headers,
|
||||
)
|
||||
assert invalid_selected.status_code == 400
|
||||
|
||||
null_summary = client.patch(
|
||||
'/api/workspace/shipping-notice/candidates/SNC-CANDIDATE-VALIDATION',
|
||||
json={'business_summary': None},
|
||||
headers=headers,
|
||||
)
|
||||
assert null_summary.status_code == 400
|
||||
|
||||
|
||||
def test_candidate_batch_gate_rejects_incomplete_expired_and_selection_drift(app, shipping_user):
|
||||
"""Candidate 建批必须继承附件完整性/过期门禁,并要求传入集合等于服务端已选集合。"""
|
||||
with app.app_context():
|
||||
parent = FileAsset(
|
||||
asset_no='FA-CANDIDATE-GATE',
|
||||
owner_user_id=shipping_user.id,
|
||||
original_filename='gate.eml',
|
||||
storage_provider='minio',
|
||||
storage_bucket='hxops-file-assets',
|
||||
storage_key=f'temporary/{shipping_user.id}/gate.eml',
|
||||
content_hash='candidate-gate-parent',
|
||||
file_size=100,
|
||||
mime_type='message/rfc822',
|
||||
upload_status='ready',
|
||||
lifecycle_status='temporary',
|
||||
)
|
||||
db.session.add(parent)
|
||||
db.session.flush()
|
||||
intake = ShippingNoticeIntake(
|
||||
intake_no='SNI-CANDIDATE-GATE',
|
||||
asset_id=parent.id,
|
||||
user_id=shipping_user.id,
|
||||
purpose='shipping_notice',
|
||||
parse_status='parsed',
|
||||
parser_version='v6',
|
||||
structured_result_json={
|
||||
'attachment_count': 1,
|
||||
'attachments': [{'parse_status': 'failed', 'filename': 'gate.pdf'}],
|
||||
},
|
||||
)
|
||||
db.session.add(intake)
|
||||
db.session.flush()
|
||||
candidates = []
|
||||
for index in (1, 2):
|
||||
document = ShippingNoticeDocument(
|
||||
intake_id=intake.id,
|
||||
document_type='attachment',
|
||||
attachment_key=f'gate-{index}',
|
||||
filename=f'gate-{index}.pdf',
|
||||
content_hash=f'candidate-gate-{index}',
|
||||
classification='order_notice',
|
||||
parse_status='parsed',
|
||||
)
|
||||
db.session.add(document)
|
||||
db.session.flush()
|
||||
summary = {'po_number': f'PO-{index}', 'lines': [{'sku': f'SKU-{index}', 'qty': index}]}
|
||||
candidate = ShippingNoticeCandidate(
|
||||
candidate_no=f'SNC-CANDIDATE-GATE-{index}',
|
||||
intake_id=intake.id,
|
||||
primary_document_id=document.id,
|
||||
candidate_key=f'test:SNC-CANDIDATE-GATE-{index}',
|
||||
structured_result_json={'business_summary': summary},
|
||||
validation_status='ready',
|
||||
conflicts_json=[],
|
||||
selected=True,
|
||||
duplicate_status='none',
|
||||
)
|
||||
db.session.add(candidate)
|
||||
candidates.append(candidate)
|
||||
db.session.commit()
|
||||
|
||||
with pytest.raises(ConflictError) as incomplete:
|
||||
create_shipping_batch_from_intakes(
|
||||
user_id=shipping_user.id,
|
||||
conversation_id='conv-gate',
|
||||
intake_nos=[intake.intake_no],
|
||||
candidate_nos=[candidate.candidate_no for candidate in candidates],
|
||||
)
|
||||
assert incomplete.value.code == 'SHIPPING_NOTICE.INTAKE_ATTACHMENTS_INCOMPLETE'
|
||||
db.session.rollback()
|
||||
|
||||
intake.structured_result_json = {'attachment_count': 0, 'attachments': []}
|
||||
intake.expires_at = datetime(2020, 1, 1)
|
||||
db.session.commit()
|
||||
with pytest.raises(ConflictError) as expired:
|
||||
create_shipping_batch_from_intakes(
|
||||
user_id=shipping_user.id,
|
||||
conversation_id='conv-gate',
|
||||
intake_nos=[intake.intake_no],
|
||||
candidate_nos=[candidate.candidate_no for candidate in candidates],
|
||||
)
|
||||
assert expired.value.code == 'SHIPPING_NOTICE.INTAKE_EXPIRED'
|
||||
db.session.rollback()
|
||||
|
||||
intake.expires_at = None
|
||||
db.session.commit()
|
||||
with pytest.raises(ConflictError) as changed:
|
||||
create_shipping_batch_from_intakes(
|
||||
user_id=shipping_user.id,
|
||||
conversation_id='conv-gate',
|
||||
intake_nos=[intake.intake_no],
|
||||
candidate_nos=[candidates[0].candidate_no],
|
||||
)
|
||||
assert changed.value.code == 'SHIPPING_NOTICE.CANDIDATE_SELECTION_CHANGED'
|
||||
|
||||
for candidate in candidates:
|
||||
candidate.selected = False
|
||||
db.session.commit()
|
||||
with pytest.raises(ValidationError) as none_selected:
|
||||
create_shipping_batch_from_intakes(
|
||||
user_id=shipping_user.id,
|
||||
conversation_id='conv-gate',
|
||||
intake_nos=[intake.intake_no],
|
||||
candidate_nos=None,
|
||||
)
|
||||
assert none_selected.value.code == 'SHIPPING_NOTICE.CANDIDATE_NOT_SELECTED'
|
||||
|
||||
|
||||
def test_candidate_era_intake_without_candidates_cannot_fallback_to_single_eml_task(app, shipping_user):
|
||||
"""v6 Intake 无 Candidate 时必须阻断,不得退回整封 EML 建一个旧式 Task。"""
|
||||
with app.app_context():
|
||||
asset = FileAsset(
|
||||
asset_no='FA-CANDIDATE-MISSING',
|
||||
owner_user_id=shipping_user.id,
|
||||
original_filename='missing-candidates.eml',
|
||||
storage_provider='minio',
|
||||
storage_bucket='hxops-file-assets',
|
||||
storage_key=f'temporary/{shipping_user.id}/missing-candidates.eml',
|
||||
content_hash='candidate-missing-hash',
|
||||
file_size=100,
|
||||
mime_type='message/rfc822',
|
||||
upload_status='ready',
|
||||
lifecycle_status='temporary',
|
||||
)
|
||||
db.session.add(asset)
|
||||
db.session.flush()
|
||||
intake = ShippingNoticeIntake(
|
||||
intake_no='SNI-CANDIDATE-MISSING',
|
||||
asset_id=asset.id,
|
||||
user_id=shipping_user.id,
|
||||
purpose='shipping_notice',
|
||||
parse_status='parsed',
|
||||
parser_version='v6',
|
||||
structured_result_json={'attachment_count': 0, 'attachments': []},
|
||||
)
|
||||
db.session.add(intake)
|
||||
db.session.commit()
|
||||
|
||||
with pytest.raises(ValidationError) as missing:
|
||||
create_shipping_batch_from_intakes(
|
||||
user_id=shipping_user.id,
|
||||
conversation_id='conv-missing-candidate',
|
||||
intake_nos=[intake.intake_no],
|
||||
)
|
||||
assert missing.value.code == 'SHIPPING_NOTICE.CANDIDATE_REVIEW_REQUIRED'
|
||||
assert db.session.query(ShippingTask).filter_by(source_intake_id=intake.id).count() == 0
|
||||
|
||||
|
||||
def test_one_eml_intake_candidates_create_one_batch_multiple_tasks(app, shipping_user, monkeypatch):
|
||||
"""一封邮件的三个已审核 Candidate 应创建一个批次和三个独立 Task,并支持幂等重放。"""
|
||||
from app.services.shipping_notice.batch_from_intakes import create_shipping_batch_from_intakes
|
||||
|
||||
with app.app_context():
|
||||
parent = FileAsset(
|
||||
asset_no='FA-CANDIDATE-BATCH',
|
||||
owner_user_id=shipping_user.id,
|
||||
original_filename='three-orders.eml',
|
||||
storage_provider='minio',
|
||||
storage_bucket='hxops-file-assets',
|
||||
storage_key=f'temporary/{shipping_user.id}/three-orders.eml',
|
||||
content_hash='eml-three-orders',
|
||||
file_size=1000,
|
||||
mime_type='message/rfc822',
|
||||
upload_status='ready',
|
||||
lifecycle_status='temporary',
|
||||
)
|
||||
db.session.add(parent)
|
||||
db.session.flush()
|
||||
intake = ShippingNoticeIntake(
|
||||
intake_no='SNI-CANDIDATE-BATCH',
|
||||
asset_id=parent.id,
|
||||
user_id=shipping_user.id,
|
||||
purpose='shipping_notice',
|
||||
parse_status='parsed',
|
||||
parser_version='v6',
|
||||
extracted_text='three orders',
|
||||
structured_result_json={
|
||||
'attachment_count': 3,
|
||||
'attachment_registered_count': 3,
|
||||
'attachments': [
|
||||
{'filename': f'{po}.pdf', 'parse_status': 'parsed'}
|
||||
for po in ('4510039227', '4510039228', '4510039229')
|
||||
],
|
||||
},
|
||||
)
|
||||
db.session.add(intake)
|
||||
db.session.flush()
|
||||
|
||||
candidate_nos = []
|
||||
hashes = []
|
||||
for index, po in enumerate(('4510039227', '4510039228', '4510039229'), start=1):
|
||||
attachment = FileAsset(
|
||||
asset_no=f'FA-CAND-{index}',
|
||||
owner_user_id=shipping_user.id,
|
||||
parent_asset_id=parent.id,
|
||||
asset_role='derived_attachment',
|
||||
source_attachment_key=f'att-{index}',
|
||||
original_filename=f'{po}.pdf',
|
||||
storage_provider='minio',
|
||||
storage_bucket='hxops-file-assets',
|
||||
storage_key=f'temporary/{shipping_user.id}/{po}.pdf',
|
||||
content_hash=f'pdf-hash-{po}',
|
||||
file_size=100,
|
||||
mime_type='application/pdf',
|
||||
upload_status='ready',
|
||||
lifecycle_status='referenced',
|
||||
)
|
||||
db.session.add(attachment)
|
||||
db.session.flush()
|
||||
document = ShippingNoticeDocument(
|
||||
intake_id=intake.id,
|
||||
asset_id=attachment.id,
|
||||
attachment_key=f'att-{index}',
|
||||
document_type='attachment',
|
||||
filename=f'{po}.pdf',
|
||||
content_hash=attachment.content_hash,
|
||||
classification='order_notice',
|
||||
parse_status='parsed',
|
||||
extracted_text=f'PO Number: {po} SKU-{index} x {index}',
|
||||
)
|
||||
db.session.add(document)
|
||||
db.session.flush()
|
||||
summary = {
|
||||
'customer_code': 'C1',
|
||||
'po_number': po,
|
||||
'reservation_ids': [f'20004859{index:02d}'],
|
||||
'lines': [
|
||||
{
|
||||
'reservation_id': f'20004859{index:02d}',
|
||||
'sku': f'SKU-{index}',
|
||||
'qty': index,
|
||||
'uom': 'EA',
|
||||
}
|
||||
],
|
||||
}
|
||||
identity = build_business_fingerprint(summary)
|
||||
content = build_content_fingerprint(summary)
|
||||
candidate = ShippingNoticeCandidate(
|
||||
candidate_no=f'SNC-CAND-BATCH-{index}',
|
||||
intake_id=intake.id,
|
||||
primary_document_id=document.id,
|
||||
candidate_key=f'test:SNC-CAND-BATCH-{index}',
|
||||
po_number=po,
|
||||
structured_result_json={'business_summary': summary},
|
||||
validation_status='ready',
|
||||
conflicts_json=[],
|
||||
selected=True,
|
||||
business_fingerprint=identity['fingerprint'],
|
||||
fingerprint_version=identity['version'],
|
||||
fingerprint_strength=identity['strength'],
|
||||
content_fingerprint=content['fingerprint'],
|
||||
content_fingerprint_version=content['version'],
|
||||
duplicate_status='none',
|
||||
)
|
||||
db.session.add(candidate)
|
||||
candidate_nos.append(candidate.candidate_no)
|
||||
hashes.append(attachment.content_hash)
|
||||
db.session.commit()
|
||||
|
||||
monkeypatch.setattr(
|
||||
'app.services.shipping_notice.batch_orchestrator.start_batch_orchestrator',
|
||||
lambda _batch_no: False,
|
||||
)
|
||||
first = create_shipping_batch_from_intakes(
|
||||
user_id=shipping_user.id,
|
||||
conversation_id='conv-candidate-batch',
|
||||
intake_nos=[intake.intake_no],
|
||||
candidate_nos=candidate_nos,
|
||||
create_idempotency_key='candidate-batch-idem',
|
||||
)
|
||||
assert first['batch_no']
|
||||
batch = db.session.query(ShippingBatch).filter_by(batch_no=first['batch_no']).one()
|
||||
tasks = db.session.query(ShippingTask).filter_by(batch_id=batch.id).order_by(ShippingTask.id).all()
|
||||
assert len(tasks) == 3
|
||||
assert {task.source_content_hash for task in tasks} == set(hashes)
|
||||
assert all(task.source_candidate_id for task in tasks)
|
||||
assert len({task.source_candidate_id for task in tasks}) == 3
|
||||
|
||||
replay = create_shipping_batch_from_intakes(
|
||||
user_id=shipping_user.id,
|
||||
conversation_id='conv-candidate-batch',
|
||||
intake_nos=[intake.intake_no],
|
||||
candidate_nos=candidate_nos,
|
||||
create_idempotency_key='candidate-batch-idem',
|
||||
)
|
||||
assert replay['idempotent_replay'] is True
|
||||
assert replay['batch_no'] == first['batch_no']
|
||||
|
||||
|
||||
def test_upload_file_asset_creates_ready_record(app, shipping_user):
|
||||
with app.app_context():
|
||||
with patch('app.services.file_assets.service.file_asset_storage.upload_bytes'):
|
||||
@@ -2740,12 +3276,12 @@ def _minimal_pdf_bytes(marker: str) -> bytes:
|
||||
return bytes(out)
|
||||
|
||||
|
||||
def test_run_intake_parse_three_pdfs_creates_documents_candidates_and_assets(
|
||||
def test_run_intake_parse_three_pdfs_creates_documents_and_assets_without_candidates(
|
||||
app, shipping_user, monkeypatch,
|
||||
):
|
||||
"""
|
||||
阶段二验收:3 个含真实 PO 的 PDF → 正文 Document(email_context) + 3 PDF Document + 3 Candidate;
|
||||
正文订单表不额外生成第 4 候选;各 PDF 独立 FileAsset;重试不膨胀。
|
||||
上传预处理验收:3 个含真实 PO 的 PDF → 正文 Document(email_context) + 3 PDF Document;
|
||||
点击发送前不生成 Candidate;各 PDF 独立 FileAsset;重试不膨胀。
|
||||
"""
|
||||
from email.message import EmailMessage
|
||||
|
||||
@@ -2845,14 +3381,13 @@ def test_run_intake_parse_three_pdfs_creates_documents_candidates_and_assets(
|
||||
assert len(pdf_docs) == 3
|
||||
assert all(d.classification == 'order_notice' for d in pdf_docs)
|
||||
assert all(d.asset_id for d in pdf_docs)
|
||||
assert len(cands) == 3
|
||||
assert all(c.primary_document_id != body_docs[0].id for c in cands)
|
||||
assert len(cands) == 0
|
||||
assert len(children) == 3
|
||||
assert len({c.content_hash for c in children}) == 3
|
||||
|
||||
detail = serialize_intake(refreshed_intake, asset=parent, include_parse_texts=True)
|
||||
assert detail['document_count'] == 4
|
||||
assert detail['candidate_count'] == 3
|
||||
assert detail['candidate_count'] == 0
|
||||
|
||||
intake2 = (
|
||||
db.session.query(ShippingNoticeIntake)
|
||||
@@ -2871,7 +3406,7 @@ def test_run_intake_parse_three_pdfs_creates_documents_candidates_and_assets(
|
||||
.id
|
||||
)
|
||||
assert db.session.query(ShippingNoticeDocument).filter_by(intake_id=intake_id).count() == 4
|
||||
assert db.session.query(ShippingNoticeCandidate).filter_by(intake_id=intake_id).count() == 3
|
||||
assert db.session.query(ShippingNoticeCandidate).filter_by(intake_id=intake_id).count() == 0
|
||||
assert (
|
||||
db.session.query(FileAsset)
|
||||
.filter(FileAsset.parent_asset_id == parent.id, FileAsset.deleted_at.is_(None))
|
||||
@@ -3002,10 +3537,10 @@ def test_stage2_persist_failure_marks_parse_failed_and_allows_retry(
|
||||
assert retried['parse_status'] == 'queued'
|
||||
|
||||
|
||||
def test_candidate_dedup_savepoint_failure_keeps_stage2_and_unchecked(
|
||||
def test_upload_preprocess_does_not_invoke_candidate_dedup(
|
||||
app, shipping_user, monkeypatch,
|
||||
):
|
||||
"""Candidate 重复检测异常仅 SAVEPOINT 回滚:Document/Candidate 保留,去重为 unchecked。"""
|
||||
"""上传预处理只落 Document;订单 Candidate 判重函数即使不可用也不影响预处理。"""
|
||||
from email.message import EmailMessage
|
||||
|
||||
from app.services.shipping_notice.intake_parse_workflow import run_intake_parse
|
||||
@@ -3055,16 +3590,17 @@ def test_candidate_dedup_savepoint_failure_keeps_stage2_and_unchecked(
|
||||
with patch('app.services.file_assets.service.file_asset_storage.upload_bytes'):
|
||||
result = run_intake_parse(intake, parent)
|
||||
assert result['status'] == 'parsed'
|
||||
assert result.get('duplicate_status') == 'unchecked'
|
||||
assert result.get('candidate_count') == 0
|
||||
assert result.get('order_extraction_status') == 'not_started'
|
||||
db.session.expire_all()
|
||||
refreshed = db.session.query(ShippingNoticeIntake).filter_by(
|
||||
intake_no='SNI-DEDUP-SAVEPOINT'
|
||||
).one()
|
||||
assert refreshed.parse_status == 'parsed'
|
||||
assert refreshed.duplicate_status == 'unchecked'
|
||||
assert refreshed.order_extraction_status == 'not_started'
|
||||
assert refreshed.duplicate_matches_json in (None, [])
|
||||
assert db.session.query(ShippingNoticeDocument).filter_by(intake_id=refreshed.id).count() >= 1
|
||||
assert db.session.query(ShippingNoticeCandidate).filter_by(intake_id=refreshed.id).count() >= 1
|
||||
assert db.session.query(ShippingNoticeCandidate).filter_by(intake_id=refreshed.id).count() == 0
|
||||
|
||||
|
||||
def test_stage2_fail_cleanup_clears_prior_duplicate_resolution(
|
||||
|
||||
Reference in new issue
Block a user