feat(shipping-notice): 增加发货通知建单幂等性支持及内容哈希校验

This commit is contained in:
hugo committed 2026-07-14 00:28:52 +08:00
1 parent 0a9bd6415d
commit 143e054d86
39 files changed
+1750 -80

No files matched your search

+96 -1
View File
@@ -103,11 +103,12 @@ def test_intake_temp_notice_uploads_splits_accepted_and_rejected(app, tmp_path,
]
with app.app_context():
accepted, rejected = notice_upload.intake_temp_notice_uploads("req-batch", uploads)
accepted, rejected, duplicate_files = notice_upload.intake_temp_notice_uploads("req-batch", uploads)
summary = notice_upload.build_notice_file_summary(
total_files=len(uploads),
accepted=accepted,
rejected=rejected,
duplicate_files=duplicate_files,
)
assert len(accepted) == 1
@@ -140,3 +141,97 @@ def test_intake_notice_attachment_extract_failure(app, tmp_path, monkeypatch):
assert result.accepted is False
assert result.status == "extract_failed"
assert result.warnings[0].startswith("notice_extract_failed:")
def test_intake_notice_attachment_same_bytes_same_hash(app, tmp_path):
"""相同字节内容应得到相同 SHA-256(与文件名无关)。"""
from app.services.shipping_notice.notice_upload import intake_notice_attachment
app.config["SHIPPING_NOTICE_STORAGE_ROOT"] = str(tmp_path)
raw = "客户:ACME\nA001 x 10".encode("utf-8")
with app.app_context():
first = intake_notice_attachment(
filename="notice-a.txt",
raw=raw,
storage="temp",
temp_request_id="req-hash-a",
temp_index=1,
)
second = intake_notice_attachment(
filename="notice-b.txt",
raw=raw,
storage="temp",
temp_request_id="req-hash-b",
temp_index=1,
)
assert first.content_hash == second.content_hash
assert len(first.content_hash) == 64
assert first.content_hash == first.content_hash.lower()
def test_intake_notice_attachment_different_bytes_different_hash(app, tmp_path):
"""不同字节内容应得到不同 SHA-256。"""
from app.services.shipping_notice.notice_upload import intake_notice_attachment
app.config["SHIPPING_NOTICE_STORAGE_ROOT"] = str(tmp_path)
with app.app_context():
first = intake_notice_attachment(
filename="notice-a.txt",
raw=b"content-a",
storage="temp",
temp_request_id="req-diff-a",
temp_index=1,
)
second = intake_notice_attachment(
filename="notice-b.txt",
raw=b"content-b",
storage="temp",
temp_request_id="req-diff-b",
temp_index=1,
)
assert first.content_hash != second.content_hash
def test_intake_temp_notice_uploads_dedupes_same_content_in_one_request(app, tmp_path, monkeypatch):
"""同一请求内相同内容文件只进入 accepted 一次,duplicate 不计入 rejected。"""
from app.services.shipping_notice import notice_upload
app.config["SHIPPING_NOTICE_STORAGE_ROOT"] = str(tmp_path)
monkeypatch.setattr(
notice_upload,
"extract_notice_text",
lambda saved_path: ("客户:ACME\nA001 x 10", {"warnings": [], "file_type": "txt"}),
)
class _Upload:
def __init__(self, name: str, raw: bytes, mimetype: str):
self.filename = name
self._raw = raw
self.mimetype = mimetype
def read(self):
return self._raw
raw = "客户:ACME\nA001 x 10".encode("utf-8")
uploads = [
_Upload("original.pdf", raw, "application/pdf"),
_Upload("copy.pdf", raw, "application/pdf"),
_Upload("another-copy.txt", raw, "text/plain"),
]
with app.app_context():
accepted, rejected, duplicate_files = notice_upload.intake_temp_notice_uploads("req-dedupe", uploads)
summary = notice_upload.build_notice_file_summary(
total_files=len(uploads),
accepted=accepted,
rejected=rejected,
duplicate_files=duplicate_files,
)
assert len(accepted) == 1
assert len(rejected) == 0
assert summary["duplicate_count"] == 2
assert summary["duplicate_files"][0]["duplicate_of"] == "original.pdf"
assert accepted[0].to_notice_item()["source_content_hash"] == accepted[0].content_hash