Files
pptopic/tests/test_convert.py
Sidney Zhang 97b30ace84 feat(lib): 添加批量图片优化功能与实时进度显示
- 新增 `batchImageOptimization` 函数支持批量处理图片
- 为图片优化添加 spinner 进度指示器和实时计时显示
- 更新版本号至 0.3.2
2026-06-18 16:17:38 +08:00

103 lines
3.6 KiB
Python

"""Tests for convertPPT class."""
from pathlib import Path
from unittest.mock import MagicMock, patch
import pytest
from pptopic.lib import convertPPT
class TestConvertPPT:
"""Test convertPPT class."""
TTYPE_VALUES = [("JPG", 17), ("PNG", 18), ("PDF", 32), ("XPS", 33)]
@pytest.mark.parametrize("ttype,expected", TTYPE_VALUES)
def test_ttypes_mapping(self, ttype, expected):
assert convertPPT.TTYPES[ttype] == expected
@pytest.mark.parametrize("as_path", [False, True])
@patch("pptopic.lib.sys.platform", "win32")
def test_init_valid(self, as_path, tmp_path):
test_file = tmp_path / "test.pptx"
test_file.touch()
if as_path:
test_file = Path(test_file)
with patch("pptopic.lib.wcc.Dispatch"):
ppt = convertPPT(test_file, trans="JPG")
assert ppt.savetype == "JPG"
@patch("pptopic.lib.sys.platform", "win32")
def test_init_nonexistent_file(self):
with pytest.raises(FileNotFoundError, match="File not found"):
convertPPT("nonexistent.pptx")
@patch("pptopic.lib.sys.platform", "win32")
def test_init_invalid_trans_type(self, tmp_path):
test_file = tmp_path / "test.pptx"
test_file.touch()
with pytest.raises(ValueError, match="Save type is not supported"):
convertPPT(test_file, trans="BMP")
@pytest.mark.parametrize("ttype,expected", [("jpg", "JPG"), ("Pdf", "PDF")])
@patch("pptopic.lib.sys.platform", "win32")
def test_init_case_insensitive_trans(self, ttype, expected, tmp_path):
test_file = tmp_path / "test.pptx"
test_file.touch()
with patch("pptopic.lib.wcc.Dispatch"):
ppt = convertPPT(test_file, trans=ttype)
assert ppt.savetype == expected
@patch("pptopic.lib.sys.platform", "linux")
def test_init_non_windows_raises_error(self):
with pytest.raises(SystemError, match="Only support Windows system"):
convertPPT("test.pptx")
@patch("pptopic.lib.sys.platform", "win32")
def test_context_manager(self, tmp_path):
test_file = tmp_path / "test.pptx"
test_file.touch()
with patch("pptopic.lib.wcc.Dispatch") as mock_dispatch:
mock_app = MagicMock()
mock_dispatch.return_value = mock_app
with convertPPT(test_file) as ppt:
assert ppt.savetype == "JPG"
mock_app.Quit.assert_called_once()
@pytest.mark.parametrize(
"save_as,expected",
[(None, "JPG"), ("PDF", "PDF")],
ids=["default_jpg", "explicit_pdf"],
)
@patch("pptopic.lib.sys.platform", "win32")
def test_saveas(self, save_as, expected, tmp_path):
test_file = tmp_path / "test.pptx"
test_file.touch()
with patch("pptopic.lib.wcc.Dispatch"):
ppt = convertPPT(test_file, trans="PNG")
ppt.saveAs(save_as)
assert ppt.savetype == expected
@patch("pptopic.lib.sys.platform", "win32")
def test_saveas_invalid_type(self, tmp_path):
test_file = tmp_path / "test.pptx"
test_file.touch()
with patch("pptopic.lib.wcc.Dispatch"):
ppt = convertPPT(test_file)
with pytest.raises(ValueError, match="Save type is not supported"):
ppt.saveAs("BMP")
@patch("pptopic.lib.sys.platform", "win32")
def test_open_classmethod(self, tmp_path):
test_file = tmp_path / "test.pptx"
test_file.touch()
with patch("pptopic.lib.wcc.Dispatch"):
ppt = convertPPT.open(test_file, trans="PNG")
assert ppt.savetype == "PNG"
assert isinstance(ppt, convertPPT)