Skip to content

Commit

Permalink
TST: Basic test for PdfFileMerger
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinThoma committed Apr 6, 2022
1 parent abed048 commit 2cc1bce
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
build
.idea/*
.coverage
dont_commit_merged.pdf
20 changes: 20 additions & 0 deletions Tests/test_merger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import os
import binascii
import sys

import PyPDF2

TESTS_ROOT = os.path.abspath(os.path.dirname(__file__))
PROJECT_ROOT = os.path.dirname(TESTS_ROOT)
RESOURCE_ROOT = os.path.join(PROJECT_ROOT, "Resources")

sys.path.append(PROJECT_ROOT)


def test_merge():
file_merger = PyPDF2.PdfFileMerger()
pdf_path = os.path.join(RESOURCE_ROOT, "crazyones.pdf")
for path in [pdf_path, pdf_path, pdf_path]:
file_merger.append(PyPDF2.PdfFileReader(path, "rb"))

file_merger.write("dont_commit_merged.pdf")
24 changes: 22 additions & 2 deletions Tests/test_workflows.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os
import binascii
import sys
import pytest

from PyPDF2 import PdfFileReader, PdfFileWriter

Expand All @@ -10,6 +11,7 @@

sys.path.append(PROJECT_ROOT)


def test_PdfReaderFileLoad():
"""
Test loading and parsing of a file. Extract text of the file and compare to expected
Expand Down Expand Up @@ -60,12 +62,30 @@ def test_PdfReaderJpegImage():


def test_decrypt():
with open(os.path.join(RESOURCE_ROOT, "libreoffice-writer-password.pdf"), "rb") as inputfile:
with open(
os.path.join(RESOURCE_ROOT, "libreoffice-writer-password.pdf"), "rb"
) as inputfile:
ipdf = PdfFileReader(inputfile)
assert ipdf.isEncrypted == True
ipdf.decrypt('openpassword')
ipdf.decrypt("openpassword")
assert ipdf.getNumPages() == 1
assert ipdf.isEncrypted == True

# Is extractText() broken for encrypted files?
# assert ipdf.getPage(0).extractText().replace('\n', '') == "\n˘\n\u02c7\u02c6˙\n\n\n˘\u02c7\u02c6˙\n\n"


@pytest.mark.parametrize("degree", [0, 90, 180, 270, 360, -90])
def test_rotate(degree):
with open(os.path.join(RESOURCE_ROOT, "crazyones.pdf"), "rb") as inputfile:
ipdf = PdfFileReader(inputfile)
page = ipdf.getPage(0)
page.rotateCounterClockwise(degree)


def test_rotate_45():
with open(os.path.join(RESOURCE_ROOT, "crazyones.pdf"), "rb") as inputfile:
ipdf = PdfFileReader(inputfile)
page = ipdf.getPage(0)
with pytest.raises(AssertionError):
page.rotateCounterClockwise(45)

0 comments on commit 2cc1bce

Please sign in to comment.