245 lines
9.8 KiB
Python
245 lines
9.8 KiB
Python
"""Tests for PPT_longPic function."""
|
|
|
|
import pytest
|
|
from pathlib import Path
|
|
from unittest.mock import Mock, MagicMock, patch
|
|
from PIL import Image
|
|
from pptopic.lib import PPT_longPic
|
|
|
|
|
|
class TestPPTLongPic:
|
|
"""Test PPT_longPic function."""
|
|
|
|
@patch('pptopic.lib.convertPPT')
|
|
def test_ppt_longpic_with_save_name(self, mock_convert_ppt, tmp_path):
|
|
"""Test PPT_longPic with saveName parameter."""
|
|
ppt_file = tmp_path / "test.pptx"
|
|
ppt_file.touch()
|
|
|
|
mock_ppt = MagicMock()
|
|
mock_convert_ppt.return_value.__enter__.return_value = mock_ppt
|
|
|
|
with patch('pptopic.lib.Path.glob') as mock_glob:
|
|
mock_img_path = MagicMock()
|
|
mock_img_path.suffix = '.jpg'
|
|
mock_glob.return_value = [mock_img_path]
|
|
|
|
with patch('pptopic.lib.Image.open') as mock_open:
|
|
mock_img = MagicMock()
|
|
mock_img.mode = 'RGB'
|
|
mock_img.width = 100
|
|
mock_img.height = 100
|
|
mock_img.resize.return_value = mock_img
|
|
mock_open.return_value.__enter__.return_value = mock_img
|
|
|
|
with patch('pptopic.lib.Image.new') as mock_new:
|
|
mock_canvas = MagicMock()
|
|
mock_new.return_value = mock_canvas
|
|
|
|
PPT_longPic(ppt_file, saveName="output.jpg", saveto=tmp_path)
|
|
|
|
mock_canvas.save.assert_called_once()
|
|
|
|
@patch('pptopic.lib.convertPPT')
|
|
def test_ppt_longpic_without_save_name(self, mock_convert_ppt, tmp_path):
|
|
"""Test PPT_longPic without saveName uses default JPG."""
|
|
ppt_file = tmp_path / "test.pptx"
|
|
ppt_file.touch()
|
|
|
|
mock_ppt = MagicMock()
|
|
mock_convert_ppt.return_value.__enter__.return_value = mock_ppt
|
|
|
|
with patch('pptopic.lib.Path.glob') as mock_glob:
|
|
mock_img_path = MagicMock()
|
|
mock_img_path.suffix = '.jpg'
|
|
mock_glob.return_value = [mock_img_path]
|
|
|
|
with patch('pptopic.lib.Image.open') as mock_open:
|
|
mock_img = MagicMock()
|
|
mock_img.mode = 'RGB'
|
|
mock_img.width = 100
|
|
mock_img.height = 100
|
|
mock_img.resize.return_value = mock_img
|
|
mock_open.return_value.__enter__.return_value = mock_img
|
|
|
|
with patch('pptopic.lib.Image.new') as mock_new:
|
|
mock_canvas = MagicMock()
|
|
mock_new.return_value = mock_canvas
|
|
|
|
PPT_longPic(ppt_file, saveto=tmp_path)
|
|
|
|
mock_canvas.save.assert_called_once()
|
|
|
|
def test_ppt_longpic_invalid_image_type(self, tmp_path):
|
|
"""Test PPT_longPic with invalid image type raises ValueError."""
|
|
ppt_file = tmp_path / "test.pptx"
|
|
ppt_file.touch()
|
|
|
|
with pytest.raises(ValueError, match="Unable to save this type"):
|
|
PPT_longPic(ppt_file, saveName="output.bmp")
|
|
|
|
@patch('pptopic.lib.convertPPT')
|
|
def test_ppt_longpic_no_images_generated(self, mock_convert_ppt, tmp_path):
|
|
"""Test PPT_longPic when no images are generated raises ValueError."""
|
|
ppt_file = tmp_path / "test.pptx"
|
|
ppt_file.touch()
|
|
|
|
mock_ppt = MagicMock()
|
|
mock_convert_ppt.return_value.__enter__.return_value = mock_ppt
|
|
|
|
with patch('pptopic.lib.Path.glob', return_value=[]):
|
|
with pytest.raises(ValueError, match="No images generated"):
|
|
PPT_longPic(ppt_file)
|
|
|
|
@patch('pptopic.lib.convertPPT')
|
|
def test_ppt_longpic_with_percentage_width(self, mock_convert_ppt, tmp_path):
|
|
"""Test PPT_longPic with percentage width string."""
|
|
ppt_file = tmp_path / "test.pptx"
|
|
ppt_file.touch()
|
|
|
|
mock_ppt = MagicMock()
|
|
mock_convert_ppt.return_value.__enter__.return_value = mock_ppt
|
|
|
|
with patch('pptopic.lib.Path.glob') as mock_glob:
|
|
mock_img_path = MagicMock()
|
|
mock_img_path.suffix = '.jpg'
|
|
mock_glob.return_value = [mock_img_path]
|
|
|
|
with patch('pptopic.lib.Image.open') as mock_open:
|
|
mock_img = MagicMock()
|
|
mock_img.mode = 'RGB'
|
|
mock_img.width = 100
|
|
mock_img.height = 100
|
|
mock_img.resize.return_value = mock_img
|
|
mock_open.return_value.__enter__.return_value = mock_img
|
|
|
|
with patch('pptopic.lib.Image.new') as mock_new:
|
|
mock_canvas = MagicMock()
|
|
mock_new.return_value = mock_canvas
|
|
|
|
PPT_longPic(ppt_file, width="50%", saveto=tmp_path)
|
|
|
|
mock_new.assert_called_once()
|
|
|
|
@patch('pptopic.lib.convertPPT')
|
|
def test_ppt_longpic_with_pixel_width(self, mock_convert_ppt, tmp_path):
|
|
"""Test PPT_longPic with pixel width."""
|
|
ppt_file = tmp_path / "test.pptx"
|
|
ppt_file.touch()
|
|
|
|
mock_ppt = MagicMock()
|
|
mock_convert_ppt.return_value.__enter__.return_value = mock_ppt
|
|
|
|
with patch('pptopic.lib.Path.glob') as mock_glob:
|
|
mock_img_path = MagicMock()
|
|
mock_img_path.suffix = '.jpg'
|
|
mock_glob.return_value = [mock_img_path]
|
|
|
|
with patch('pptopic.lib.Image.open') as mock_open:
|
|
mock_img = MagicMock()
|
|
mock_img.mode = 'RGB'
|
|
mock_img.width = 100
|
|
mock_img.height = 100
|
|
mock_img.resize.return_value = mock_img
|
|
mock_open.return_value.__enter__.return_value = mock_img
|
|
|
|
with patch('pptopic.lib.Image.new') as mock_new:
|
|
mock_canvas = MagicMock()
|
|
mock_new.return_value = mock_canvas
|
|
|
|
PPT_longPic(ppt_file, width=50, saveto=tmp_path)
|
|
|
|
mock_new.assert_called_once()
|
|
|
|
@patch('pptopic.lib.convertPPT')
|
|
def test_ppt_longpic_with_none_width(self, mock_convert_ppt, tmp_path):
|
|
"""Test PPT_longPic with None width (no scaling)."""
|
|
ppt_file = tmp_path / "test.pptx"
|
|
ppt_file.touch()
|
|
|
|
mock_ppt = MagicMock()
|
|
mock_convert_ppt.return_value.__enter__.return_value = mock_ppt
|
|
|
|
with patch('pptopic.lib.Path.glob') as mock_glob:
|
|
mock_img_path = MagicMock()
|
|
mock_img_path.suffix = '.jpg'
|
|
mock_glob.return_value = [mock_img_path]
|
|
|
|
with patch('pptopic.lib.Image.open') as mock_open:
|
|
mock_img = MagicMock()
|
|
mock_img.mode = 'RGB'
|
|
mock_img.width = 100
|
|
mock_img.height = 100
|
|
mock_img.resize.return_value = mock_img
|
|
mock_open.return_value.__enter__.return_value = mock_img
|
|
|
|
with patch('pptopic.lib.Image.new') as mock_new:
|
|
mock_canvas = MagicMock()
|
|
mock_new.return_value = mock_canvas
|
|
|
|
PPT_longPic(ppt_file, width=None, saveto=tmp_path)
|
|
|
|
mock_new.assert_called_once()
|
|
|
|
@patch('pptopic.lib.convertPPT')
|
|
def test_ppt_longpic_save_to_current_dir(self, mock_convert_ppt, tmp_path):
|
|
"""Test PPT_longPic saving to current directory."""
|
|
ppt_file = tmp_path / "test.pptx"
|
|
ppt_file.touch()
|
|
|
|
mock_ppt = MagicMock()
|
|
mock_convert_ppt.return_value.__enter__.return_value = mock_ppt
|
|
|
|
with patch('pptopic.lib.Path.glob') as mock_glob:
|
|
mock_img_path = MagicMock()
|
|
mock_img_path.suffix = '.jpg'
|
|
mock_glob.return_value = [mock_img_path]
|
|
|
|
with patch('pptopic.lib.Image.open') as mock_open:
|
|
mock_img = MagicMock()
|
|
mock_img.mode = 'RGB'
|
|
mock_img.width = 100
|
|
mock_img.height = 100
|
|
mock_img.resize.return_value = mock_img
|
|
mock_open.return_value.__enter__.return_value = mock_img
|
|
|
|
with patch('pptopic.lib.Image.new') as mock_new:
|
|
mock_canvas = MagicMock()
|
|
mock_new.return_value = mock_canvas
|
|
|
|
PPT_longPic(ppt_file, saveto=".")
|
|
|
|
mock_canvas.save.assert_called_once()
|
|
|
|
@patch('pptopic.lib.convertPPT')
|
|
def test_ppt_longpic_multiple_images(self, mock_convert_ppt, tmp_path):
|
|
"""Test PPT_longPic with multiple images."""
|
|
ppt_file = tmp_path / "test.pptx"
|
|
ppt_file.touch()
|
|
|
|
mock_ppt = MagicMock()
|
|
mock_convert_ppt.return_value.__enter__.return_value = mock_ppt
|
|
|
|
with patch('pptopic.lib.Path.glob') as mock_glob:
|
|
mock_img_path1 = MagicMock()
|
|
mock_img_path1.suffix = '.jpg'
|
|
mock_img_path2 = MagicMock()
|
|
mock_img_path2.suffix = '.jpg'
|
|
mock_glob.return_value = [mock_img_path1, mock_img_path2]
|
|
|
|
with patch('pptopic.lib.Image.open') as mock_open:
|
|
mock_img = MagicMock()
|
|
mock_img.mode = 'RGB'
|
|
mock_img.width = 100
|
|
mock_img.height = 100
|
|
mock_img.resize.return_value = mock_img
|
|
mock_open.return_value.__enter__.return_value = mock_img
|
|
|
|
with patch('pptopic.lib.Image.new') as mock_new:
|
|
mock_canvas = MagicMock()
|
|
mock_new.return_value = mock_canvas
|
|
|
|
PPT_longPic(ppt_file, saveto=tmp_path)
|
|
|
|
mock_canvas.paste.assert_called()
|