238 lines
8.4 KiB
Python
238 lines
8.4 KiB
Python
"""
|
|
tests/test_notice_intake.py
|
|
|
|
用途:验证发货通知附件统一接入服务的抽取、截断与失败分流语义。
|
|
依赖:notice_upload.intake_notice_attachment、truncate_notice_text。
|
|
逻辑:保证单票/批量/重传三条入口共用同一 intake 结果模型,避免行为漂移。
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from io import BytesIO
|
|
|
|
|
|
def test_truncate_notice_text_appends_warning(app):
|
|
"""正文超长时应截断并追加 notice_text_truncated warning。"""
|
|
from app.services.shipping_notice.notice_upload import truncate_notice_text
|
|
|
|
with app.app_context():
|
|
app.config["SHIPPING_NOTICE_NOTICE_TEXT_MAX_CHARS"] = 10
|
|
text, warnings = truncate_notice_text("abcdefghijklmnop", [])
|
|
|
|
assert text == "abcdefghij"
|
|
assert warnings == ["notice_text_truncated:10"]
|
|
|
|
|
|
def test_intake_notice_attachment_accepts_txt(app, tmp_path):
|
|
"""任务级接入应落盘、抽取并标记 accepted。"""
|
|
from app.services.shipping_notice.notice_upload import intake_notice_attachment
|
|
|
|
app.config["SHIPPING_NOTICE_STORAGE_ROOT"] = str(tmp_path)
|
|
with app.app_context():
|
|
result = intake_notice_attachment(
|
|
filename="notice-a.txt",
|
|
raw="客户:ACME\nA001 x 10".encode("utf-8"),
|
|
source_file_type="text/plain",
|
|
storage="task",
|
|
task_no="SN-INTAKE-001",
|
|
)
|
|
|
|
assert result.accepted is True
|
|
assert result.status == "accepted"
|
|
assert "A001" in result.text
|
|
assert result.to_notice_item()["source_file_name"] == "notice-a.txt"
|
|
assert result.to_notice_upload_meta()["filename"] == "notice-a.txt"
|
|
assert result.saved_path is not None
|
|
assert result.saved_path.exists()
|
|
|
|
|
|
def test_intake_notice_attachment_empty_text_is_rejected(app, tmp_path, monkeypatch):
|
|
"""抽取空文本时应标记 empty_text,供批量入口分流到 rejected_files。"""
|
|
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: ("", {"warnings": ["ocr_empty"], "file_type": "pdf"}),
|
|
)
|
|
|
|
with app.app_context():
|
|
result = notice_upload.intake_notice_attachment(
|
|
filename="scan.pdf",
|
|
raw=b"%PDF-1.4",
|
|
source_file_type="application/pdf",
|
|
storage="temp",
|
|
temp_request_id="req-empty",
|
|
temp_index=1,
|
|
)
|
|
|
|
assert result.accepted is False
|
|
assert result.status == "empty_text"
|
|
rejected = result.to_rejected_file_summary()
|
|
assert rejected["reason"] == "empty_text"
|
|
assert rejected["warnings"] == ["ocr_empty"]
|
|
|
|
|
|
def test_intake_temp_notice_uploads_splits_accepted_and_rejected(app, tmp_path, monkeypatch):
|
|
"""批量接入应分别返回 accepted / rejected,并生成统一 file_summary。"""
|
|
from app.services.shipping_notice import notice_upload
|
|
|
|
app.config["SHIPPING_NOTICE_STORAGE_ROOT"] = str(tmp_path)
|
|
|
|
def fake_extract(saved_path):
|
|
name = str(saved_path).replace("\\", "/")
|
|
if name.endswith("/notice-a.txt"):
|
|
return "客户:ACME\nA001 x 10", {"warnings": [], "file_type": "txt"}
|
|
return "", {"warnings": ["ocr_empty"], "file_type": "pdf"}
|
|
|
|
monkeypatch.setattr(notice_upload, "extract_notice_text", fake_extract)
|
|
|
|
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
|
|
|
|
uploads = [
|
|
_Upload("notice-a.txt", b"ok", "text/plain"),
|
|
_Upload("notice-b.pdf", b"%PDF-1.4", "application/pdf"),
|
|
]
|
|
|
|
with app.app_context():
|
|
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
|
|
assert len(rejected) == 1
|
|
assert summary["accepted_count"] == 1
|
|
assert summary["rejected_count"] == 1
|
|
assert summary["accepted_files"][0]["filename"] == "notice-a.txt"
|
|
assert summary["rejected_files"][0]["filename"] == "notice-b.pdf"
|
|
|
|
|
|
def test_intake_notice_attachment_extract_failure(app, tmp_path, monkeypatch):
|
|
"""抽取异常时应标记 extract_failed 并保留 notice_extract_failed warning。"""
|
|
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: (_ for _ in ()).throw(RuntimeError("ocr down")),
|
|
)
|
|
|
|
with app.app_context():
|
|
result = notice_upload.intake_notice_attachment(
|
|
filename="broken.pdf",
|
|
raw=BytesIO(b"%PDF").read(),
|
|
storage="task",
|
|
task_no="SN-INTAKE-FAIL",
|
|
)
|
|
|
|
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
|