4
4
import subprocess
5
5
from io import BytesIO
6
6
from pathlib import Path
7
+ from tempfile import NamedTemporaryFile
7
8
from typing import Any
8
9
9
10
import pytest
@@ -213,18 +214,20 @@ def writer_operate(writer: PdfWriter) -> None:
213
214
assert k in objects_hash , f"Missing { v } "
214
215
215
216
216
- tmp_path = "dont_commit_writer.pdf"
217
-
218
-
219
217
@pytest .mark .parametrize (
220
- ("write_data_here " , "needs_cleanup" ),
218
+ ("convert " , "needs_cleanup" ),
221
219
[
222
- ("dont_commit_writer.pdf" , True ),
223
- (Path ( "dont_commit_writer.pdf" ) , True ),
220
+ (str , True ),
221
+ (Path , True ),
224
222
(BytesIO (), False ),
225
223
],
226
224
)
227
- def test_writer_operations_by_traditional_usage (write_data_here , needs_cleanup ):
225
+ def test_writer_operations_by_traditional_usage (convert , needs_cleanup ):
226
+ if callable (convert ):
227
+ write_data_here = convert (NamedTemporaryFile (suffix = ".pdf" , delete = False ).name )
228
+ else :
229
+ write_data_here = convert
230
+
228
231
writer = PdfWriter ()
229
232
230
233
writer_operate (writer )
@@ -242,14 +245,19 @@ def test_writer_operations_by_traditional_usage(write_data_here, needs_cleanup):
242
245
243
246
244
247
@pytest .mark .parametrize (
245
- ("write_data_here " , "needs_cleanup" ),
248
+ ("convert " , "needs_cleanup" ),
246
249
[
247
- ("dont_commit_writer.pdf" , True ),
248
- (Path ( "dont_commit_writer.pdf" ) , True ),
250
+ (str , True ),
251
+ (Path , True ),
249
252
(BytesIO (), False ),
250
253
],
251
254
)
252
- def test_writer_operations_by_semi_traditional_usage (write_data_here , needs_cleanup ):
255
+ def test_writer_operations_by_semi_traditional_usage (convert , needs_cleanup ):
256
+ if callable (convert ):
257
+ write_data_here = convert (NamedTemporaryFile (suffix = ".pdf" , delete = False ).name )
258
+ else :
259
+ write_data_here = convert
260
+
253
261
with PdfWriter () as writer :
254
262
writer_operate (writer )
255
263
@@ -266,16 +274,21 @@ def test_writer_operations_by_semi_traditional_usage(write_data_here, needs_clea
266
274
267
275
268
276
@pytest .mark .parametrize (
269
- ("write_data_here " , "needs_cleanup" ),
277
+ ("convert " , "needs_cleanup" ),
270
278
[
271
- ("dont_commit_writer.pdf" , True ),
272
- (Path ( "dont_commit_writer.pdf" ) , True ),
279
+ (str , True ),
280
+ (Path , True ),
273
281
(BytesIO (), False ),
274
282
],
275
283
)
276
284
def test_writer_operations_by_semi_new_traditional_usage (
277
- write_data_here , needs_cleanup
285
+ convert , needs_cleanup
278
286
):
287
+ if callable (convert ):
288
+ write_data_here = convert (NamedTemporaryFile (suffix = ".pdf" , delete = False ).name )
289
+ else :
290
+ write_data_here = convert
291
+
279
292
with PdfWriter () as writer :
280
293
writer_operate (writer )
281
294
@@ -287,14 +300,19 @@ def test_writer_operations_by_semi_new_traditional_usage(
287
300
288
301
289
302
@pytest .mark .parametrize (
290
- ("write_data_here " , "needs_cleanup" ),
303
+ ("convert " , "needs_cleanup" ),
291
304
[
292
- ("dont_commit_writer.pdf" , True ),
293
- (Path ( "dont_commit_writer.pdf" ) , True ),
305
+ (str , True ),
306
+ (Path , True ),
294
307
(BytesIO (), False ),
295
308
],
296
309
)
297
- def test_writer_operation_by_new_usage (write_data_here , needs_cleanup ):
310
+ def test_writer_operation_by_new_usage (convert , needs_cleanup ):
311
+ if callable (convert ):
312
+ write_data_here = convert (NamedTemporaryFile (suffix = ".pdf" , delete = False ).name )
313
+ else :
314
+ write_data_here = convert
315
+
298
316
# This includes write "output" to pypdf-output.pdf
299
317
with PdfWriter (write_data_here ) as writer :
300
318
writer_operate (writer )
@@ -1439,16 +1457,8 @@ def test_named_dest_page_number():
1439
1457
assert len (writer ._root_object ["/Names" ]["/Dests" ]["/Names" ]) == 6
1440
1458
1441
1459
1442
- @pytest .mark .parametrize (
1443
- ("write_data_here" , "needs_cleanup" ),
1444
- [
1445
- (
1446
- "dont_commit_writer.pdf" ,
1447
- True ,
1448
- )
1449
- ],
1450
- )
1451
- def test_update_form_fields (write_data_here , needs_cleanup ):
1460
+ def test_update_form_fields (tmp_path ):
1461
+ write_data_here = tmp_path / "out.pdf"
1452
1462
writer = PdfWriter (clone_from = RESOURCE_ROOT / "FormTestFromOo.pdf" )
1453
1463
writer .update_page_form_field_values (
1454
1464
writer .pages [0 ],
@@ -1472,8 +1482,8 @@ def test_update_form_fields(write_data_here, needs_cleanup):
1472
1482
auto_regenerate = False ,
1473
1483
)
1474
1484
1475
- writer .write ("dont_commit_writer.pdf" )
1476
- reader = PdfReader ("dont_commit_writer.pdf" )
1485
+ writer .write (write_data_here )
1486
+ reader = PdfReader (write_data_here )
1477
1487
flds = reader .get_fields ()
1478
1488
assert flds ["CheckBox1" ]["/V" ] == "/Yes"
1479
1489
assert flds ["CheckBox1" ].indirect_reference .get_object ()["/AS" ] == "/Yes"
@@ -1495,8 +1505,7 @@ def test_update_form_fields(write_data_here, needs_cleanup):
1495
1505
assert all (x in flds ["RadioGroup1" ]["/_States_" ] for x in ["/1" , "/2" , "/3" ])
1496
1506
assert all (x in flds ["Liste1" ]["/_States_" ] for x in ["Liste1" , "Liste2" , "Liste3" ])
1497
1507
1498
- if needs_cleanup :
1499
- Path (write_data_here ).unlink ()
1508
+ Path (write_data_here ).unlink ()
1500
1509
1501
1510
1502
1511
@pytest .mark .enable_socket ()
0 commit comments