Skip to content

Commit

Permalink
TST: Add test for PdfMerger
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinThoma committed Jun 16, 2022
1 parent 1ccf484 commit ebfb01a
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions tests/test_merger.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import os
import sys

import pytest

import PyPDF2
from PyPDF2.generic import Destination

Expand All @@ -24,6 +26,7 @@ def test_merge():
merger.append(outline)
merger.append(pdf_path, pages=PyPDF2.pagerange.PageRange(slice(0, 0)))
merger.append(pdf_forms)
merger.merge(0, pdf_path, import_bookmarks=False)

# Merging an encrypted file
reader = PyPDF2.PdfReader(pdf_pw)
Expand Down Expand Up @@ -92,3 +95,25 @@ def test_merge():

# Clean up
os.remove(tmp_path)


def test_merge_page_exception():
merger = PyPDF2.PdfMerger()
pdf_path = os.path.join(RESOURCE_ROOT, "crazyones.pdf")
with pytest.raises(TypeError) as exc:
merger.merge(0, pdf_path, pages="a:b")
assert exc.value.args[0] == '"pages" must be a tuple of (start, stop[, step])'


def test_merge_write_closed_fh():
merger = PyPDF2.PdfMerger()
pdf_path = os.path.join(RESOURCE_ROOT, "crazyones.pdf")
merger.append(pdf_path)

merger.close()
with pytest.raises(RuntimeError) as exc:
merger.write("stream.pdf")
assert (
exc.value.args[0]
== "close() was called and thus the writer cannot be used anymore"
)

0 comments on commit ebfb01a

Please sign in to comment.