31 lines
1.3 KiB
Python
31 lines
1.3 KiB
Python
"""数据库提交失败时,上传文件恢复到原状且保留原始异常。"""
|
|
|
|
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
|