fix(设置): 恢复上传提交失败前的公司标志文件
数据库提交失败时恢复被覆盖的文件,或移除本次新文件。 文件恢复失败保留原始异常,并补充三种扩展名场景的回归测试。
This commit is contained in:
1 parent
67adecbf1b
commit
c684a44753
2 files changed
+46
-7
No files matched your search
@@ -0,0 +1,30 @@
|
||||
"""数据库提交失败时,上传文件恢复到原状且保留原始异常。"""
|
||||
|
||||
from contextlib import nullcontext
|
||||
from types import SimpleNamespace
|
||||
from unittest.mock import Mock
|
||||
|
||||
import pytest
|
||||
|
||||
from app.services import system_setting_service as service
|
||||
|
||||
|
||||
@pytest.mark.parametrize("old_ext", [None, "png", "jpg"])
|
||||
def test_提交失败不丢旧logo也不残留新文件(tmp_path, monkeypatch, old_ext):
|
||||
original = b"original-logo"
|
||||
if old_ext:
|
||||
(tmp_path / f"logo.{old_ext}").write_bytes(original)
|
||||
monkeypatch.setattr(service, "_logo_dir", lambda: tmp_path)
|
||||
setting = SimpleNamespace(logo_path=f"logo/logo.{old_ext}" if old_ext else None)
|
||||
monkeypatch.setattr(service, "get_setting", lambda _: setting)
|
||||
monkeypatch.setattr(service.audit_service, "audit", lambda *_, **__: nullcontext())
|
||||
session = Mock()
|
||||
session.commit.side_effect = RuntimeError("数据库提交失败")
|
||||
with pytest.raises(RuntimeError, match="数据库提交失败"):
|
||||
service.save_logo(session, service._MAGIC["png"] + b"new", "png", operator=Mock())
|
||||
if old_ext == "png":
|
||||
assert (tmp_path / "logo.png").read_bytes() == original
|
||||
else:
|
||||
assert not (tmp_path / "logo.png").exists()
|
||||
if old_ext:
|
||||
assert (tmp_path / f"logo.{old_ext}").read_bytes() == original
|
||||
Reference in new issue
Block a user