-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_pptx_builder.py
More file actions
286 lines (218 loc) · 8.76 KB
/
test_pptx_builder.py
File metadata and controls
286 lines (218 loc) · 8.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
"""
Tests for pptx_builder.core
Run with: pytest test_pptx_builder.py
"""
import pytest
from pptx.util import Emu
from pptx_builder.core import (
list_images,
detect_input_type,
confirm_overwrite,
emu_to_float_inches,
build_presentation,
ALLOWED_EXTS,
SLIDE_SIZES,
)
class TestImageListing:
"""Test image file detection and listing"""
def test_list_images_filters_correctly(self, tmp_path):
"""Should only return supported image formats"""
# Create test files
(tmp_path / "image.png").touch()
(tmp_path / "photo.jpg").touch()
(tmp_path / "document.txt").touch() # Should be ignored
(tmp_path / "data.json").touch() # Should be ignored
images = list_images(tmp_path)
assert len(images) == 2
assert all(img.suffix.lower() in ALLOWED_EXTS for img in images)
def test_list_images_case_insensitive(self, tmp_path):
"""Should handle uppercase extensions"""
(tmp_path / "IMAGE.PNG").touch()
(tmp_path / "PHOTO.JPG").touch()
images = list_images(tmp_path)
assert len(images) == 2
def test_list_images_sorted_alphabetically(self, tmp_path):
"""Should sort filenames case-insensitively"""
(tmp_path / "zebra.png").touch()
(tmp_path / "Apple.jpg").touch()
(tmp_path / "banana.png").touch()
images = list_images(tmp_path)
names = [img.name.lower() for img in images]
assert names == sorted(names)
class TestInputDetection:
"""Test input type detection (PDF vs folder vs unknown)"""
def test_detect_pdf(self, tmp_path):
"""Should detect PDF files"""
pdf_file = tmp_path / "test.pdf"
pdf_file.touch()
assert detect_input_type(pdf_file) == "pdf"
def test_detect_folder_with_images(self, tmp_path):
"""Should detect folder with images"""
(tmp_path / "image.png").touch()
assert detect_input_type(tmp_path) == "folder"
def test_detect_empty_folder(self, tmp_path):
"""Should return unknown for empty folder"""
assert detect_input_type(tmp_path) == "unknown"
def test_detect_unknown_file(self, tmp_path):
"""Should return unknown for non-PDF files"""
txt_file = tmp_path / "test.txt"
txt_file.touch()
assert detect_input_type(txt_file) == "unknown"
class TestSlidePresets:
"""Test slide size presets"""
def test_all_presets_valid(self):
"""All slide presets should have valid dimensions"""
for key, (label, width, height) in SLIDE_SIZES.items():
assert isinstance(width, (int, float))
assert isinstance(height, (int, float))
assert width > 0
assert height > 0
assert len(label) > 0
def test_standard_sizes_included(self):
"""Common sizes should be available"""
labels = [label for label, _, _ in SLIDE_SIZES.values()]
assert any("16:9" in label for label in labels)
assert any("4:3" in label for label in labels)
assert any("Letter" in label for label in labels)
assert any("A4" in label for label in labels)
class TestAllowedExtensions:
"""Test allowed file extensions"""
def test_common_formats_supported(self):
"""Common image formats should be supported"""
required = {".png", ".jpg", ".jpeg"}
assert required.issubset(ALLOWED_EXTS)
def test_heic_supported(self):
"""HEIC format should be supported"""
assert ".heic" in ALLOWED_EXTS or ".heif" in ALLOWED_EXTS
def test_extensions_lowercase(self):
"""All extensions should be lowercase for comparison"""
assert all(ext.islower() for ext in ALLOWED_EXTS)
# Integration test (requires PIL)
class TestIntegration:
"""Integration tests requiring actual image processing"""
@pytest.mark.integration
def test_create_simple_presentation(self, tmp_path):
"""End-to-end test creating a simple presentation"""
from PIL import Image
# Create test image
img_path = tmp_path / "test.png"
img = Image.new("RGB", (100, 100), color="red")
img.save(img_path)
# Create presentation
output = tmp_path / "output.pptx"
build_presentation(
images=[img_path],
output_path=output,
slide_width_in=10.0,
slide_height_in=7.5,
mode="fit",
)
assert output.exists()
assert output.stat().st_size > 0
@pytest.mark.integration
def test_multiple_images_presentation(self, tmp_path):
"""Test creating presentation with multiple images"""
from PIL import Image
# Create multiple test images
images = []
for i, color in enumerate(["red", "green", "blue"]):
img_path = tmp_path / f"image_{i}.png"
img = Image.new("RGB", (100, 100), color=color)
img.save(img_path)
images.append(img_path)
# Create presentation
output = tmp_path / "multi.pptx"
build_presentation(
images=images,
output_path=output,
slide_width_in=10.0,
slide_height_in=7.5,
mode="fill",
)
assert output.exists()
assert output.stat().st_size > 0
@pytest.mark.integration
def test_heic_format_supported(self, tmp_path):
"""Test that HEIC format is in allowed extensions"""
# HEIC requires pillow-heif which might not be installed
# Just test that it's in the allowed list
assert ".heic" in ALLOWED_EXTS or ".heif" in ALLOWED_EXTS
class TestUtilities:
"""Test utility functions"""
def test_emu_to_float_inches_conversion(self):
"""Should correctly convert EMU to inches"""
# 914400 EMU = 1 inch
assert abs(emu_to_float_inches(Emu(914400)) - 1.0) < 0.001
assert abs(emu_to_float_inches(Emu(1828800)) - 2.0) < 0.001
assert abs(emu_to_float_inches(Emu(0)) - 0.0) < 0.001
def test_confirm_overwrite_force_mode(self, tmp_path):
"""Force mode should always return True"""
existing_file = tmp_path / "existing.pptx"
existing_file.touch()
assert confirm_overwrite(existing_file, force=True) is True
def test_confirm_overwrite_quiet_mode(self, tmp_path):
"""Quiet mode with existing file should not prompt (returns True)"""
existing_file = tmp_path / "existing.pptx"
existing_file.touch()
assert confirm_overwrite(existing_file, quiet=True) is True
def test_confirm_overwrite_nonexistent_file(self, tmp_path):
"""Non-existent file should always return True"""
nonexistent = tmp_path / "new_file.pptx"
assert confirm_overwrite(nonexistent, quiet=True) is True
assert confirm_overwrite(nonexistent, force=True) is True
class TestBuildPresentation:
"""Test presentation building functionality"""
@pytest.mark.integration
def test_build_presentation_fit_mode(self, tmp_path):
"""Test presentation building with fit mode"""
from PIL import Image
img_path = tmp_path / "test.png"
img = Image.new("RGB", (200, 100), color="blue")
img.save(img_path)
output = tmp_path / "fit_test.pptx"
build_presentation(
images=[img_path],
output_path=output,
slide_width_in=10.0,
slide_height_in=7.5,
mode="fit",
)
assert output.exists()
# PPTX should have reasonable minimum size
assert output.stat().st_size > 10000
@pytest.mark.integration
def test_build_presentation_fill_mode(self, tmp_path):
"""Test presentation building with fill mode"""
from PIL import Image
img_path = tmp_path / "test.png"
img = Image.new("RGB", (300, 200), color="green")
img.save(img_path)
output = tmp_path / "fill_test.pptx"
build_presentation(
images=[img_path],
output_path=output,
slide_width_in=10.0,
slide_height_in=7.5,
mode="fill",
)
assert output.exists()
assert output.stat().st_size > 10000
@pytest.mark.integration
def test_build_presentation_custom_dimensions(self, tmp_path):
"""Test presentation with custom slide dimensions"""
from PIL import Image
img_path = tmp_path / "test.png"
img = Image.new("RGB", (100, 100), color="yellow")
img.save(img_path)
# Test A4 size
output = tmp_path / "a4_test.pptx"
build_presentation(
images=[img_path],
output_path=output,
slide_width_in=11.69,
slide_height_in=8.27,
mode="fit",
)
assert output.exists()
if __name__ == "__main__":
pytest.main([__file__, "-v"])