648 lines
23 KiB
Python
648 lines
23 KiB
Python
"""
|
||
backend/tests/test_shipping_notice_business_dedup.py
|
||
|
||
用途:业务指纹与 Intake/Candidate 重复检测核心用例(co8 已移除建批门闩用例)。
|
||
依赖:pytest、business_fingerprint、duplicate_detection、intake_service。
|
||
逻辑:覆盖指纹稳定性、候选阻断与 refresh。
|
||
"""
|
||
|
||
from __future__ import annotations
|
||
|
||
from datetime import datetime
|
||
from unittest.mock import patch
|
||
|
||
import pytest
|
||
|
||
from app import db
|
||
from app.core.errors import ConflictError
|
||
from app.models import (
|
||
FileAsset,
|
||
ShippingOrderCandidate,
|
||
ShippingOrderDocument,
|
||
ShippingOrderIntake,
|
||
ShippingNotice,
|
||
ShippingOrderTask,
|
||
User,
|
||
)
|
||
from app.services.shipping_notice.business_fingerprint import (
|
||
build_business_fingerprint,
|
||
build_content_fingerprint,
|
||
)
|
||
from app.services.shipping_notice.duplicate_detection import (
|
||
apply_duplicate_result_to_candidate,
|
||
detect_duplicates_for_candidate,
|
||
detect_duplicates_for_intake,
|
||
)
|
||
from app.services.shipping_notice.intake_service import resolve_intake_duplicate
|
||
|
||
|
||
@pytest.fixture
|
||
def dedup_user(app):
|
||
with app.app_context():
|
||
user = User(username='dedup-user', email='dedup@example.com', password_hash='x', role='user')
|
||
db.session.add(user)
|
||
db.session.commit()
|
||
yield user
|
||
|
||
|
||
def _seed_parsed_intake(
|
||
user_id: int,
|
||
*,
|
||
intake_no: str,
|
||
hash_value: str,
|
||
business_summary: dict,
|
||
fingerprint: dict,
|
||
):
|
||
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',
|
||
structured_result_json={'business_summary': business_summary},
|
||
business_fingerprint=fingerprint.get('fingerprint'),
|
||
fingerprint_version=fingerprint.get('version'),
|
||
fingerprint_strength=fingerprint.get('strength'),
|
||
duplicate_status='none',
|
||
duplicate_matches_json=[],
|
||
)
|
||
db.session.add(intake)
|
||
db.session.commit()
|
||
return asset, intake
|
||
|
||
|
||
def _seed_existing_notice_task(
|
||
user_id: int,
|
||
*,
|
||
task_no: str,
|
||
business_fingerprint: str,
|
||
fingerprint_version: str,
|
||
content_fingerprint: str | None = None,
|
||
content_fingerprint_version: str | None = None,
|
||
source_content_hash: str | None = None,
|
||
document_version: int = 1,
|
||
) -> ShippingNotice:
|
||
"""
|
||
构造一个已建单的正式发货通知,作为业务指纹去重的命中目标。
|
||
|
||
新契约:ShippingNotice 不再保存指纹投影;business/content 指纹转移到被消费的
|
||
ShippingOrderCandidate(consumed_task_id -> 建单前 OT),Notice 经 order_task_id
|
||
挂接该 OT。只有建立 Notice→Task→Candidate 这条链,duplicate_detection 的
|
||
Notice→Task(consumed_task_id)→Candidate 查询才能命中——这是 key 断言,
|
||
不是只为了消除构造参数,而是让「已有任务重复」真正可被探测。
|
||
"""
|
||
ot = ShippingOrderTask(
|
||
task_no=f'OT-{task_no}',
|
||
user_id=user_id,
|
||
source_type='eml',
|
||
lifecycle_status='active',
|
||
)
|
||
db.session.add(ot)
|
||
db.session.flush()
|
||
|
||
asset = FileAsset(
|
||
asset_no=f'FA-{task_no}',
|
||
owner_user_id=user_id,
|
||
original_filename='existing.txt',
|
||
storage_provider='minio',
|
||
storage_bucket='hxops-file-assets',
|
||
storage_key=f'temporary/{user_id}/{task_no}.txt',
|
||
content_hash=source_content_hash or f'hash-{task_no}',
|
||
file_size=1,
|
||
mime_type='text/plain',
|
||
upload_status='ready',
|
||
lifecycle_status='temporary',
|
||
)
|
||
db.session.add(asset)
|
||
db.session.flush()
|
||
|
||
intake = ShippingOrderIntake(
|
||
intake_no=f'SNI-{task_no}',
|
||
asset_id=asset.id,
|
||
user_id=user_id,
|
||
purpose='shipping_notice',
|
||
parse_status='parsed',
|
||
parser_version='v1',
|
||
duplicate_status='none',
|
||
)
|
||
db.session.add(intake)
|
||
db.session.flush()
|
||
|
||
doc = ShippingOrderDocument(
|
||
intake_id=intake.id,
|
||
attachment_key=f'att-{task_no}',
|
||
document_type='attachment',
|
||
filename=f'{task_no}.pdf',
|
||
content_hash=source_content_hash or f'hash-{task_no}',
|
||
classification='order_notice',
|
||
parse_status='parsed',
|
||
)
|
||
db.session.add(doc)
|
||
db.session.flush()
|
||
|
||
candidate = ShippingOrderCandidate(
|
||
candidate_no=f'SNC-{task_no}',
|
||
intake_id=intake.id,
|
||
primary_document_id=doc.id,
|
||
candidate_key=f'test:{task_no}',
|
||
business_fingerprint=business_fingerprint,
|
||
fingerprint_version=fingerprint_version,
|
||
content_fingerprint=content_fingerprint,
|
||
content_fingerprint_version=content_fingerprint_version,
|
||
consumed_task_id=ot.id,
|
||
duplicate_status='none',
|
||
candidate_status='accepted',
|
||
selected=True,
|
||
)
|
||
db.session.add(candidate)
|
||
db.session.flush()
|
||
ot.candidate_id = candidate.id
|
||
|
||
notice = ShippingNotice(
|
||
task_no=task_no,
|
||
user_id=user_id,
|
||
source_type='manual',
|
||
document_version=document_version,
|
||
order_task_id=ot.id,
|
||
lifecycle_status='active',
|
||
)
|
||
db.session.add(notice)
|
||
db.session.commit()
|
||
return notice
|
||
|
||
|
||
def test_business_fingerprint_ignores_internal_hex_reservation_id():
|
||
"""内部 32 位 hex 不得增强业务身份指纹,避免把数据库主键当成订单真值。"""
|
||
summary = {
|
||
'customer_name': 'ACME',
|
||
'reservation_ids': ['1f1a682dfdc446a393415dee93eb36ce'],
|
||
'lines': [
|
||
{
|
||
'part_number': 'A001',
|
||
'qty': 1,
|
||
'reservation_id': '1f1a682dfdc446a393415dee93eb36ce',
|
||
}
|
||
],
|
||
}
|
||
|
||
fingerprint = build_business_fingerprint(summary)
|
||
|
||
assert fingerprint['strength'] == 'weak'
|
||
assert 'reservation_ids' not in fingerprint['components']
|
||
|
||
|
||
def test_fingerprint_stable_across_line_order():
|
||
base = {
|
||
'customer_code': 'C1',
|
||
'destination': {'code': 'D', 'city': 'SH', 'country': 'CN'},
|
||
'required_date': '2026-01-01',
|
||
'lines': [
|
||
{'part_number': 'B', 'qty': 2, 'reservation_id': '1000000001'},
|
||
{'part_number': 'A', 'qty': 1, 'reservation_id': '1000000002'},
|
||
],
|
||
}
|
||
swapped = {
|
||
**base,
|
||
'lines': [
|
||
{'part_number': 'A', 'qty': 1, 'reservation_id': '1000000002'},
|
||
{'part_number': 'B', 'qty': 2, 'reservation_id': '1000000001'},
|
||
],
|
||
}
|
||
a = build_business_fingerprint(base)
|
||
b = build_business_fingerprint(swapped)
|
||
assert a['strength'] == 'medium'
|
||
assert a['fingerprint'] == b['fingerprint']
|
||
|
||
|
||
def test_fingerprint_changes_with_qty():
|
||
base = {
|
||
'customer_code': 'C1',
|
||
'destination': {'code': 'D', 'city': 'SH', 'country': 'CN'},
|
||
'required_date': '2026-01-01',
|
||
'lines': [{'part_number': 'A', 'qty': 1, 'reservation_id': '1000000002'}],
|
||
}
|
||
changed = {**base, 'lines': [{'part_number': 'A', 'qty': 9, 'reservation_id': '1000000002'}]}
|
||
assert build_business_fingerprint(base)['fingerprint'] != build_business_fingerprint(changed)['fingerprint']
|
||
|
||
|
||
|
||
|
||
|
||
def test_fingerprint_strong_with_customer_name_po_reservation():
|
||
"""启发式常只有客户名:客户名 + PO + Reservation 应产出 strong 指纹。"""
|
||
summary = {
|
||
'customer_name': 'ACME',
|
||
'customer_code': None,
|
||
'po_number': 'PO-20260714',
|
||
'reservation_ids': ['1000140351'],
|
||
'lines': [{'part_number': 'SKU001', 'qty': 10, 'reservation_id': '1000140351'}],
|
||
}
|
||
fp = build_business_fingerprint(summary)
|
||
assert fp['strength'] == 'strong'
|
||
assert fp['fingerprint']
|
||
assert fp['components'].get('customer_name') == 'ACME'
|
||
|
||
|
||
def test_fingerprint_keeps_same_customer_po_when_reservation_changes():
|
||
"""关键断言:Reservation/数量变化是同一 PO 的内容修订,不能改变版本链身份。"""
|
||
base = {'customer_code': 'C1', 'po_number': 'PO-VERSION', 'reservation_ids': ['10000001'], 'lines': [{'reservation_id': '10000001', 'part_number': 'A', 'qty': 1}]}
|
||
resend = {'customer_code': 'C1', 'po_number': 'PO-VERSION', 'reservation_ids': ['10000002'], 'lines': [{'reservation_id': '10000002', 'part_number': 'A', 'qty': 2}]}
|
||
assert build_business_fingerprint(base)['fingerprint'] == build_business_fingerprint(resend)['fingerprint']
|
||
assert build_content_fingerprint(base)['fingerprint'] != build_content_fingerprint(resend)['fingerprint']
|
||
|
||
|
||
def test_pending_review_hit_is_same_active_blocking(app, dedup_user):
|
||
"""pending_review 属活跃态:业务指纹命中应为 same_active_task 且 blocking。"""
|
||
with app.app_context():
|
||
summary = {
|
||
'customer_code': 'C1',
|
||
'po_number': 'PO-PENDING',
|
||
'reservation_ids': ['1000140399'],
|
||
'lines': [],
|
||
}
|
||
fp = build_business_fingerprint(summary)
|
||
asset, intake = _seed_parsed_intake(
|
||
dedup_user.id,
|
||
intake_no='SNI-PENDING-ACTIVE',
|
||
hash_value='hash-pending-1',
|
||
business_summary=summary,
|
||
fingerprint=fp,
|
||
)
|
||
_seed_existing_notice_task(
|
||
dedup_user.id,
|
||
task_no='SN-PENDING-1',
|
||
business_fingerprint=fp['fingerprint'],
|
||
fingerprint_version=fp['version'],
|
||
document_version=1,
|
||
)
|
||
|
||
result = detect_duplicates_for_intake(
|
||
intake=intake,
|
||
asset=asset,
|
||
business_summary=summary,
|
||
fingerprint_result=fp,
|
||
)
|
||
assert result['status'] == 'same_active_task'
|
||
assert result['blocking'] is True
|
||
|
||
|
||
|
||
def test_serialize_intake_possible_duplicate_is_blocking(app, dedup_user):
|
||
"""API 投影:possible_duplicate 必须 blocking,供前端弹窗确认。"""
|
||
from app.services.shipping_notice.intake_service import serialize_intake
|
||
|
||
with app.app_context():
|
||
asset, intake = _seed_parsed_intake(
|
||
dedup_user.id,
|
||
intake_no='SNI-POSSIBLE-1',
|
||
hash_value='hash-possible-1',
|
||
business_summary={'customer_name': 'X', 'lines': [{'part_number': 'A', 'qty': 1}]},
|
||
fingerprint={'fingerprint': 'weakfp', 'version': 'shipping-v1', 'strength': 'weak'},
|
||
)
|
||
intake.duplicate_status = 'possible_duplicate'
|
||
intake.fingerprint_strength = 'weak'
|
||
intake.duplicate_matches_json = [
|
||
{
|
||
'task_no': 'SN-OLD',
|
||
'document_version': 1,
|
||
'match_type': 'weak_fingerprint',
|
||
'matched_fields': ['line_digest'],
|
||
}
|
||
]
|
||
db.session.commit()
|
||
payload = serialize_intake(intake, asset=asset)
|
||
assert payload['duplicate']['status'] == 'possible_duplicate'
|
||
assert payload['duplicate']['blocking'] is True
|
||
assert 'force_create' in payload['duplicate']['allowed_actions']
|
||
|
||
|
||
|
||
def test_candidate_duplicate_blocks_only_matching_candidate(app, dedup_user):
|
||
"""
|
||
阶段二:同一邮件中候选 A 与已有任务重复时,仅 A 被阻断;B/C 仍可为 none。
|
||
"""
|
||
with app.app_context():
|
||
existing_fp = build_business_fingerprint(
|
||
{
|
||
'customer_code': 'C1',
|
||
'po_number': '4510039227',
|
||
'reservation_ids': ['1000140351'],
|
||
'lines': [],
|
||
}
|
||
)
|
||
_seed_existing_notice_task(
|
||
dedup_user.id,
|
||
task_no='SN-EXISTING-PO-A',
|
||
business_fingerprint=existing_fp['fingerprint'],
|
||
fingerprint_version=existing_fp['version'],
|
||
source_content_hash='hash-other-file',
|
||
)
|
||
|
||
parent = FileAsset(
|
||
asset_no='FA-STAGE2-DEDUP-EML',
|
||
owner_user_id=dedup_user.id,
|
||
original_filename='multi.eml',
|
||
storage_provider='minio',
|
||
storage_bucket='hxops-file-assets',
|
||
storage_key=f'temporary/{dedup_user.id}/stage2.eml',
|
||
content_hash='eml-parent-hash',
|
||
file_size=100,
|
||
mime_type='message/rfc822',
|
||
upload_status='ready',
|
||
lifecycle_status='temporary',
|
||
asset_role='upload',
|
||
)
|
||
db.session.add(parent)
|
||
db.session.flush()
|
||
intake = ShippingOrderIntake(
|
||
intake_no='SNI-STAGE2-DEDUP',
|
||
asset_id=parent.id,
|
||
user_id=dedup_user.id,
|
||
purpose='shipping_notice',
|
||
parse_status='parsed',
|
||
parser_version='v6',
|
||
extracted_text='multi',
|
||
duplicate_status='unchecked',
|
||
)
|
||
db.session.add(intake)
|
||
db.session.flush()
|
||
|
||
docs = []
|
||
for idx, po in enumerate(('4510039227', '4510039228', '4510039229'), start=1):
|
||
doc = ShippingOrderDocument(
|
||
intake_id=intake.id,
|
||
attachment_key=f'att-00{idx}',
|
||
document_type='attachment',
|
||
filename=f'{po}.pdf',
|
||
content_hash=f'pdf-hash-{po}',
|
||
classification='order_notice',
|
||
parse_status='parsed',
|
||
extracted_text=f'PO {po}',
|
||
)
|
||
db.session.add(doc)
|
||
docs.append(doc)
|
||
db.session.flush()
|
||
|
||
results = []
|
||
for doc, po in zip(docs, ('4510039227', '4510039228', '4510039229')):
|
||
summary = {
|
||
'customer_code': 'C1',
|
||
'po_number': po,
|
||
'reservation_ids': ['1000140351'] if po == '4510039227' else [],
|
||
'lines': [],
|
||
}
|
||
if po != '4510039227':
|
||
summary['reservation_ids'] = [f'100014035{po[-1]}']
|
||
fp = build_business_fingerprint(summary)
|
||
dup = detect_duplicates_for_candidate(
|
||
intake=intake,
|
||
document=doc,
|
||
business_summary=summary,
|
||
fingerprint_result=fp,
|
||
)
|
||
cand = ShippingOrderCandidate(
|
||
candidate_no=f'SNC-{po}',
|
||
intake_id=intake.id,
|
||
primary_document_id=doc.id,
|
||
candidate_key=f'test:SNC-{po}',
|
||
po_number=po,
|
||
structured_result_json={'business_summary': summary},
|
||
business_fingerprint=fp.get('fingerprint'),
|
||
fingerprint_version=fp.get('version'),
|
||
fingerprint_strength=fp.get('strength'),
|
||
duplicate_status=str(dup.get('status') or 'none'),
|
||
duplicate_matches_json=list(dup.get('matches') or []),
|
||
selected=True,
|
||
validation_status='pending',
|
||
)
|
||
db.session.add(cand)
|
||
results.append(dup)
|
||
db.session.commit()
|
||
|
||
assert results[0]['blocking'] is True
|
||
assert results[0]['status'] in {'same_active_task', 'exact_file', 'possible_duplicate'}
|
||
assert results[1]['blocking'] is False
|
||
assert results[1]['status'] == 'none'
|
||
assert results[2]['blocking'] is False
|
||
assert results[2]['status'] == 'none'
|
||
|
||
refreshed = (
|
||
db.session.query(ShippingOrderCandidate)
|
||
.filter_by(intake_id=intake.id)
|
||
.order_by(ShippingOrderCandidate.id.asc())
|
||
.all()
|
||
)
|
||
assert refreshed[0].duplicate_status != 'none'
|
||
assert refreshed[1].duplicate_status == 'none'
|
||
assert refreshed[2].duplicate_status == 'none'
|
||
|
||
|
||
def test_refresh_duplicate_updates_candidate_statuses(app, dedup_user):
|
||
"""刷新重复检测须重写各 Candidate.duplicate_status,再聚合到 Intake。"""
|
||
from app.services.shipping_notice.intake_service import refresh_intake_duplicate_detection
|
||
|
||
with app.app_context():
|
||
existing_fp = build_business_fingerprint(
|
||
{
|
||
'customer_code': 'C1',
|
||
'po_number': '4510099999',
|
||
'reservation_ids': ['1000199999'],
|
||
'lines': [],
|
||
}
|
||
)
|
||
_seed_existing_notice_task(
|
||
dedup_user.id,
|
||
task_no='SN-REFRESH-EXISTING',
|
||
business_fingerprint=existing_fp['fingerprint'],
|
||
fingerprint_version=existing_fp['version'],
|
||
source_content_hash='hash-refresh-other',
|
||
)
|
||
|
||
parent = FileAsset(
|
||
asset_no='FA-REFRESH-DEDUP',
|
||
owner_user_id=dedup_user.id,
|
||
original_filename='refresh.eml',
|
||
storage_provider='minio',
|
||
storage_bucket='hxops-file-assets',
|
||
storage_key=f'temporary/{dedup_user.id}/refresh.eml',
|
||
content_hash='eml-refresh-hash',
|
||
file_size=100,
|
||
mime_type='message/rfc822',
|
||
upload_status='ready',
|
||
lifecycle_status='temporary',
|
||
asset_role='upload',
|
||
)
|
||
db.session.add(parent)
|
||
db.session.flush()
|
||
intake = ShippingOrderIntake(
|
||
intake_no='SNI-REFRESH-DEDUP',
|
||
asset_id=parent.id,
|
||
user_id=dedup_user.id,
|
||
purpose='shipping_notice',
|
||
parse_status='parsed',
|
||
parser_version='v6',
|
||
extracted_text='refresh',
|
||
duplicate_status='none',
|
||
structured_result_json={'business_summary': {'customer_code': 'C1'}},
|
||
)
|
||
db.session.add(intake)
|
||
db.session.flush()
|
||
|
||
summary = {
|
||
'customer_code': 'C1',
|
||
'po_number': '4510099999',
|
||
'reservation_ids': ['1000199999'],
|
||
'lines': [],
|
||
}
|
||
fp = build_business_fingerprint(summary)
|
||
doc = ShippingOrderDocument(
|
||
intake_id=intake.id,
|
||
attachment_key='att-001',
|
||
document_type='attachment',
|
||
filename='dup.pdf',
|
||
content_hash='pdf-refresh-1',
|
||
classification='order_notice',
|
||
parse_status='parsed',
|
||
extracted_text='PO 4510099999',
|
||
)
|
||
db.session.add(doc)
|
||
db.session.flush()
|
||
cand = ShippingOrderCandidate(
|
||
candidate_no='SNC-REFRESH-1',
|
||
intake_id=intake.id,
|
||
primary_document_id=doc.id,
|
||
candidate_key='test:SNC-REFRESH-1',
|
||
po_number='4510099999',
|
||
structured_result_json={'business_summary': summary},
|
||
business_fingerprint=fp.get('fingerprint'),
|
||
fingerprint_version=fp.get('version'),
|
||
fingerprint_strength=fp.get('strength'),
|
||
# 模拟解析后任务尚未存在时的旧结果;刷新时应变为阻断
|
||
duplicate_status='none',
|
||
duplicate_matches_json=[],
|
||
selected=True,
|
||
validation_status='pending',
|
||
)
|
||
db.session.add(cand)
|
||
db.session.commit()
|
||
|
||
payload = refresh_intake_duplicate_detection(
|
||
user_id=dedup_user.id,
|
||
intake_no='SNI-REFRESH-DEDUP',
|
||
)
|
||
assert payload['duplicate']['blocking'] is True
|
||
db.session.expire_all()
|
||
cand2 = db.session.query(ShippingOrderCandidate).filter_by(candidate_no='SNC-REFRESH-1').one()
|
||
assert cand2.duplicate_status != 'none'
|
||
assert list(cand2.duplicate_matches_json or [])
|
||
|
||
|
||
def test_candidate_content_fingerprint_distinguishes_same_content_and_revision(app, dedup_user):
|
||
"""身份相同且内容相同应判重复内容;数量变化应判修订。"""
|
||
with app.app_context():
|
||
base = {
|
||
'customer_code': 'C1',
|
||
'po_number': '4510039227',
|
||
'reservation_ids': ['2000485916'],
|
||
'lines': [
|
||
{
|
||
'reservation_id': '2000485916',
|
||
'part_number': '504-006043-001',
|
||
'qty': 30,
|
||
'uom': 'EA',
|
||
}
|
||
],
|
||
}
|
||
identity = build_business_fingerprint(base)
|
||
content = build_content_fingerprint(base)
|
||
_seed_existing_notice_task(
|
||
dedup_user.id,
|
||
task_no='SN-CONTENT-EXISTING',
|
||
business_fingerprint=identity['fingerprint'],
|
||
fingerprint_version=identity['version'],
|
||
content_fingerprint=content['fingerprint'],
|
||
content_fingerprint_version=content['version'],
|
||
)
|
||
parent, intake = _seed_parsed_intake(
|
||
dedup_user.id,
|
||
intake_no='SNI-CONTENT-CHECK',
|
||
hash_value='content-parent-hash',
|
||
business_summary=base,
|
||
fingerprint=identity,
|
||
)
|
||
doc = ShippingOrderDocument(
|
||
intake_id=intake.id,
|
||
attachment_key='att-content',
|
||
document_type='attachment',
|
||
filename='4510039227.pdf',
|
||
content_hash='different-file-hash',
|
||
classification='order_notice',
|
||
parse_status='parsed',
|
||
)
|
||
db.session.add(doc)
|
||
db.session.commit()
|
||
|
||
same = detect_duplicates_for_candidate(
|
||
intake=intake,
|
||
document=doc,
|
||
business_summary=base,
|
||
fingerprint_result=identity,
|
||
content_fingerprint_result=content,
|
||
)
|
||
changed = {**base, 'lines': [{**base['lines'][0], 'qty': 25}]}
|
||
revision = detect_duplicates_for_candidate(
|
||
intake=intake,
|
||
document=doc,
|
||
business_summary=changed,
|
||
fingerprint_result=identity,
|
||
content_fingerprint_result=build_content_fingerprint(changed),
|
||
)
|
||
assert same['status'] == 'same_business_content'
|
||
assert revision['status'] == 'possible_revision'
|
||
candidate = ShippingOrderCandidate(
|
||
candidate_no='SNC-CONTENT-WRITEBACK',
|
||
intake_id=intake.id,
|
||
primary_document_id=doc.id,
|
||
candidate_key='test:SNC-CONTENT-WRITEBACK',
|
||
structured_result_json={'business_summary': changed},
|
||
content_fingerprint=content['fingerprint'],
|
||
content_fingerprint_version=content['version'],
|
||
)
|
||
db.session.add(candidate)
|
||
apply_duplicate_result_to_candidate(candidate, revision)
|
||
assert candidate.content_fingerprint == revision['content_fingerprint']
|
||
assert candidate.content_fingerprint != content['fingerprint']
|
||
|
||
inventory_source_changed = {
|
||
**base,
|
||
'lines': [{**base['lines'][0], 'inventory_source_hint': 'DBS DUB'}],
|
||
}
|
||
assert build_content_fingerprint(inventory_source_changed)['fingerprint'] != content['fingerprint']
|
||
|
||
header_date_changed = {**base, 'required_date': '2026-07-20'}
|
||
assert build_content_fingerprint(header_date_changed)['fingerprint'] != content['fingerprint']
|
||
|
||
allocation_changed = {
|
||
**base,
|
||
'lines': [{**base['lines'][0], 'allocation_id': 'ALLOC-2'}],
|
||
}
|
||
assert build_content_fingerprint(allocation_changed)['fingerprint'] != content['fingerprint']
|