feat: 增强资料包压缩导入异常提示

This commit is contained in:
2026-06-04 01:37:11 +08:00
parent e7e3202714
commit 0b7322aa65
2 changed files with 109 additions and 12 deletions

View File

@@ -334,3 +334,81 @@ def test_import_submission_batch_supports_7z_package_and_preserves_relative_path
"CH1/注册申请表.txt",
"CH1/目标产品说明书.txt",
]
def test_import_submission_batch_records_warnings_for_unsupported_zip_entries(db):
archive = BytesIO()
with ZipFile(archive, "w") as zip_file:
zip_file.writestr("CH1/注册申请表.txt", "产品名称产品A")
zip_file.writestr("CH1/忽略图片.png", b"binary-image-data")
archive.seek(0)
package = SimpleUploadedFile(
"registration-package.zip",
archive.read(),
content_type="application/zip",
)
result = import_submission_batch("document_review", [package])
batch = SubmissionBatch.objects.get(batch_id=result["batch_id"])
warnings = result["registration_overview_report"]["warnings"]
assert batch.file_count == 1
assert batch.exception_count == 1
assert "跳过不支持的文件" in warnings[0]
assert "CH1/忽略图片.png" in warnings[0]
def test_import_submission_batch_marks_failed_when_zip_has_no_supported_files(db):
archive = BytesIO()
with ZipFile(archive, "w") as zip_file:
zip_file.writestr("assets/readme.png", b"binary-image-data")
archive.seek(0)
package = SimpleUploadedFile(
"empty-registration-package.zip",
archive.read(),
content_type="application/zip",
)
result = import_submission_batch("document_review", [package])
batch = SubmissionBatch.objects.get(batch_id=result["batch_id"])
warnings = result["registration_overview_report"]["warnings"]
assert batch.file_count == 0
assert batch.import_status == SubmissionBatch.STATUS_FAILED
assert batch.exception_count == len(warnings)
assert any("未发现可导入的支持文件" in warning for warning in warnings)
def test_import_submission_batch_records_warnings_for_unsupported_7z_entries(db, monkeypatch):
package = SimpleUploadedFile(
"registration-package.7z",
b"fake-7z-bytes",
content_type="application/x-7z-compressed",
)
class FakeSevenZipFile:
def __init__(self, _file_obj, mode="r"):
self.mode = mode
def __enter__(self):
return self
def __exit__(self, exc_type, exc, tb):
return False
def extractall(self, path):
target = Path(path)
(target / "CH1").mkdir(parents=True, exist_ok=True)
(target / "CH1" / "注册申请表.txt").write_text("产品名称产品A", encoding="utf-8")
(target / "CH1" / "忽略图片.png").write_bytes(b"binary-image-data")
fake_module = types.SimpleNamespace(SevenZipFile=FakeSevenZipFile)
monkeypatch.setitem(sys.modules, "py7zr", fake_module)
result = import_submission_batch("document_review", [package])
batch = SubmissionBatch.objects.get(batch_id=result["batch_id"])
warnings = result["registration_overview_report"]["warnings"]
assert batch.file_count == 1
assert batch.exception_count == 1
assert any("CH1/忽略图片.png" in warning for warning in warnings)