-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathtest_pdf_operations.py
More file actions
187 lines (165 loc) · 5.96 KB
/
Copy pathtest_pdf_operations.py
File metadata and controls
187 lines (165 loc) · 5.96 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
"""Tests for pdf_operations module."""
import os
import tempfile
import pikepdf
import pytest
from bigocrpdf.services.pdf_operations import (
PDFInfo,
extract_pages,
get_pdf_info,
merge_pdfs,
reorder_pages,
reverse_pages,
rotate_pages,
split_by_pages,
)
def _create_test_pdf(path: str, num_pages: int = 3) -> str:
"""Create a simple test PDF with the given number of pages."""
pdf = pikepdf.Pdf.new()
for i in range(num_pages):
page = pikepdf.Page(
pikepdf.Dictionary(
Type=pikepdf.Name.Page,
MediaBox=[0, 0, 612, 792],
Contents=pdf.make_stream(f"BT /F1 12 Tf 100 700 Td (Page {i + 1}) Tj ET".encode()),
)
)
pdf.pages.append(page)
pdf.save(path)
return path
class TestGetPdfInfo:
def test_basic_info(self):
with tempfile.NamedTemporaryFile(suffix=".pdf", delete=False) as f:
path = f.name
try:
_create_test_pdf(path, 5)
info = get_pdf_info(path)
assert isinstance(info, PDFInfo)
assert info.page_count == 5
assert info.file_size_bytes > 0
assert info.path == path
finally:
os.unlink(path)
def test_nonexistent_file_raises(self):
with pytest.raises((FileNotFoundError, OSError)):
get_pdf_info("/nonexistent/file.pdf")
class TestSplitByPages:
def test_split_3_pages_into_1_per_file(self):
with tempfile.NamedTemporaryFile(suffix=".pdf", delete=False) as f:
path = f.name
with tempfile.TemporaryDirectory() as out_dir:
try:
_create_test_pdf(path, 3)
result = split_by_pages(path, out_dir, pages_per_file=1)
assert len(result.output_files) == 3
assert result.total_pages == 3
for out_file in result.output_files:
assert os.path.exists(out_file)
finally:
os.unlink(path)
def test_split_all_in_one(self):
with tempfile.NamedTemporaryFile(suffix=".pdf", delete=False) as f:
path = f.name
with tempfile.TemporaryDirectory() as out_dir:
try:
_create_test_pdf(path, 3)
result = split_by_pages(path, out_dir, pages_per_file=10)
assert len(result.output_files) == 1
finally:
os.unlink(path)
class TestExtractPages:
def test_extract_single_page(self):
with tempfile.NamedTemporaryFile(suffix=".pdf", delete=False) as f:
src = f.name
with tempfile.NamedTemporaryFile(suffix=".pdf", delete=False) as f:
dst = f.name
try:
_create_test_pdf(src, 5)
result = extract_pages(src, dst, [2])
assert result.success is True
assert result.pages_affected == 1
info = get_pdf_info(dst)
assert info.page_count == 1
finally:
os.unlink(src)
if os.path.exists(dst):
os.unlink(dst)
def test_extract_multiple_pages(self):
with tempfile.NamedTemporaryFile(suffix=".pdf", delete=False) as f:
src = f.name
with tempfile.NamedTemporaryFile(suffix=".pdf", delete=False) as f:
dst = f.name
try:
_create_test_pdf(src, 5)
result = extract_pages(src, dst, [1, 3, 5])
assert result.success is True
assert result.pages_affected == 3
finally:
os.unlink(src)
if os.path.exists(dst):
os.unlink(dst)
class TestMergePdfs:
def test_merge_two_pdfs(self):
with tempfile.NamedTemporaryFile(suffix=".pdf", delete=False) as f1:
p1 = f1.name
with tempfile.NamedTemporaryFile(suffix=".pdf", delete=False) as f2:
p2 = f2.name
with tempfile.NamedTemporaryFile(suffix=".pdf", delete=False) as fo:
out = fo.name
try:
_create_test_pdf(p1, 2)
_create_test_pdf(p2, 3)
result = merge_pdfs([p1, p2], out)
assert result.success is True
info = get_pdf_info(out)
assert info.page_count == 5
finally:
for p in (p1, p2, out):
if os.path.exists(p):
os.unlink(p)
class TestRotatePages:
def test_rotate_90(self):
with tempfile.NamedTemporaryFile(suffix=".pdf", delete=False) as f:
src = f.name
with tempfile.NamedTemporaryFile(suffix=".pdf", delete=False) as f:
dst = f.name
try:
_create_test_pdf(src, 2)
result = rotate_pages(src, dst, [1], 90)
assert result.success is True
finally:
os.unlink(src)
if os.path.exists(dst):
os.unlink(dst)
class TestReorderPages:
def test_reverse_order(self):
with tempfile.NamedTemporaryFile(suffix=".pdf", delete=False) as f:
src = f.name
with tempfile.NamedTemporaryFile(suffix=".pdf", delete=False) as f:
dst = f.name
try:
_create_test_pdf(src, 3)
result = reorder_pages(src, dst, [3, 2, 1])
assert result.success is True
info = get_pdf_info(dst)
assert info.page_count == 3
finally:
os.unlink(src)
if os.path.exists(dst):
os.unlink(dst)
class TestReversePages:
def test_reverse(self):
with tempfile.NamedTemporaryFile(suffix=".pdf", delete=False) as f:
src = f.name
with tempfile.NamedTemporaryFile(suffix=".pdf", delete=False) as f:
dst = f.name
try:
_create_test_pdf(src, 4)
result = reverse_pages(src, dst)
assert result.success is True
info = get_pdf_info(dst)
assert info.page_count == 4
finally:
os.unlink(src)
if os.path.exists(dst):
os.unlink(dst)