📝 新增完整项目文档、测试套件与构建配置
This commit is contained in:
150
tests/test_convert.py
Normal file
150
tests/test_convert.py
Normal file
@@ -0,0 +1,150 @@
|
||||
"""Tests for convertPPT class."""
|
||||
|
||||
import pytest
|
||||
from pathlib import Path
|
||||
from unittest.mock import Mock, MagicMock, patch
|
||||
from pptopic.lib import convertPPT
|
||||
|
||||
|
||||
class TestConvertPPTClass:
|
||||
"""Test convertPPT class initialization and properties."""
|
||||
|
||||
def test_ttypes_mapping(self):
|
||||
"""Test that TTYPES mapping is correct."""
|
||||
assert convertPPT.TTYPES["JPG"] == 17
|
||||
assert convertPPT.TTYPES["PNG"] == 18
|
||||
assert convertPPT.TTYPES["PDF"] == 32
|
||||
assert convertPPT.TTYPES["XPS"] == 33
|
||||
|
||||
@patch('pptopic.lib.sys.platform', 'win32')
|
||||
def test_init_with_valid_file(self, tmp_path):
|
||||
"""Test initialization with a valid file 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
|
||||
|
||||
ppt = convertPPT(test_file, trans="JPG")
|
||||
assert ppt.savetype == "JPG"
|
||||
mock_dispatch.assert_called_once_with('PowerPoint.Application')
|
||||
|
||||
@patch('pptopic.lib.sys.platform', 'win32')
|
||||
def test_init_with_path_object(self, tmp_path):
|
||||
"""Test initialization with Path object."""
|
||||
test_file = tmp_path / "test.pptx"
|
||||
test_file.touch()
|
||||
|
||||
with patch('pptopic.lib.wcc.Dispatch'):
|
||||
ppt = convertPPT(Path(test_file), trans="PNG")
|
||||
assert ppt.savetype == "PNG"
|
||||
|
||||
@patch('pptopic.lib.sys.platform', 'win32')
|
||||
def test_init_with_nonexistent_file(self):
|
||||
"""Test initialization with nonexistent file raises FileNotFoundError."""
|
||||
with pytest.raises(FileNotFoundError, match="File not found"):
|
||||
convertPPT("nonexistent.pptx")
|
||||
|
||||
@patch('pptopic.lib.sys.platform', 'win32')
|
||||
def test_init_with_invalid_trans_type(self, tmp_path):
|
||||
"""Test initialization with invalid save type raises ValueError."""
|
||||
test_file = tmp_path / "test.pptx"
|
||||
test_file.touch()
|
||||
|
||||
with pytest.raises(ValueError, match="Save type is not supported"):
|
||||
convertPPT(test_file, trans="BMP")
|
||||
|
||||
@patch('pptopic.lib.sys.platform', 'win32')
|
||||
def test_init_with_case_insensitive_trans(self, tmp_path):
|
||||
"""Test that trans parameter is case insensitive."""
|
||||
test_file = tmp_path / "test.pptx"
|
||||
test_file.touch()
|
||||
|
||||
with patch('pptopic.lib.wcc.Dispatch'):
|
||||
ppt = convertPPT(test_file, trans="jpg")
|
||||
assert ppt.savetype == "JPG"
|
||||
|
||||
ppt = convertPPT(test_file, trans="Pdf")
|
||||
assert ppt.savetype == "PDF"
|
||||
|
||||
|
||||
class TestConvertPTTNonWindows:
|
||||
"""Test convertPPT behavior on non-Windows systems."""
|
||||
|
||||
@patch('pptopic.lib.sys.platform', 'linux')
|
||||
def test_init_on_non_windows_raises_error(self):
|
||||
"""Test that initialization on non-Windows raises SystemError."""
|
||||
with pytest.raises(SystemError, match="Only support Windows system"):
|
||||
convertPPT("test.pptx")
|
||||
|
||||
|
||||
class TestConvertPPTContextManager:
|
||||
"""Test convertPPT as context manager."""
|
||||
|
||||
@patch('pptopic.lib.sys.platform', 'win32')
|
||||
def test_context_manager_enter_exit(self, tmp_path):
|
||||
"""Test __enter__ and __exit__ methods."""
|
||||
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()
|
||||
|
||||
|
||||
class TestConvertPPTSaveAs:
|
||||
"""Test saveAs method."""
|
||||
|
||||
@patch('pptopic.lib.sys.platform', 'win32')
|
||||
def test_saveas_default(self, tmp_path):
|
||||
"""Test saveAs with default parameter."""
|
||||
test_file = tmp_path / "test.pptx"
|
||||
test_file.touch()
|
||||
|
||||
with patch('pptopic.lib.wcc.Dispatch'):
|
||||
ppt = convertPPT(test_file, trans="PNG")
|
||||
ppt.saveAs()
|
||||
assert ppt.savetype == "JPG"
|
||||
|
||||
@patch('pptopic.lib.sys.platform', 'win32')
|
||||
def test_saveas_with_valid_type(self, tmp_path):
|
||||
"""Test saveAs with valid save type."""
|
||||
test_file = tmp_path / "test.pptx"
|
||||
test_file.touch()
|
||||
|
||||
with patch('pptopic.lib.wcc.Dispatch'):
|
||||
ppt = convertPPT(test_file, trans="JPG")
|
||||
ppt.saveAs("PDF")
|
||||
assert ppt.savetype == "PDF"
|
||||
|
||||
@patch('pptopic.lib.sys.platform', 'win32')
|
||||
def test_saveas_with_invalid_type(self, tmp_path):
|
||||
"""Test saveAs with invalid save type raises ValueError."""
|
||||
test_file = tmp_path / "test.pptx"
|
||||
test_file.touch()
|
||||
|
||||
with patch('pptopic.lib.wcc.Dispatch'):
|
||||
ppt = convertPPT(test_file, trans="JPG")
|
||||
with pytest.raises(ValueError, match="Save type is not supported"):
|
||||
ppt.saveAs("BMP")
|
||||
|
||||
|
||||
class TestConvertPTTOpenClassmethod:
|
||||
"""Test open class method."""
|
||||
|
||||
@patch('pptopic.lib.sys.platform', 'win32')
|
||||
def test_open_classmethod(self, tmp_path):
|
||||
"""Test open class method creates instance."""
|
||||
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)
|
||||
Reference in New Issue
Block a user