Skip to content

Commit

Permalink
STY: Make variable naming more consistent in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinThoma committed Apr 16, 2022
1 parent d58a849 commit a5875c5
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 59 deletions.
8 changes: 4 additions & 4 deletions PyPDF2/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,10 +197,10 @@ def markLocation(stream):
# Mainly for debugging
RADIUS = 5000
stream.seek(-RADIUS, 1)
with open('PyPDF2_pdfLocation.txt', 'wb') as outputDoc:
outputDoc.write(stream.read(RADIUS))
outputDoc.write(b'HERE')
outputDoc.write(stream.read(RADIUS))
with open('PyPDF2_pdfLocation.txt', 'wb') as output_fh:
output_fh.write(stream.read(RADIUS))
output_fh.write(b'HERE')
output_fh.write(stream.read(RADIUS))
stream.seek(-RADIUS, 1)


Expand Down
10 changes: 7 additions & 3 deletions Tests/test_basic_features.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@


def test_basic_features():
writer = PdfFileWriter()
pdf_path = os.path.join(RESOURCE_ROOT, "crazyones.pdf")
reader = PdfFileReader(pdf_path)
writer = PdfFileWriter()

# print how many pages input1 has:
print("document1.pdf has %d pages." % reader.getNumPages())
Expand Down Expand Up @@ -54,8 +54,12 @@ def test_basic_features():
writer.encrypt(password)

# finally, write "output" to PyPDF2-output.pdf
with open("PyPDF2-output.pdf", "wb") as outputStream:
writer.write(outputStream)
tmp_path = "PyPDF2-output.pdf"
with open(tmp_path, "wb") as output_stream:
writer.write(output_stream)

# cleanup
os.remove(tmp_path)


def test_convertToInt():
Expand Down
6 changes: 3 additions & 3 deletions Tests/test_javascript.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
@pytest.fixture
def pdf_file_writer():
reader = PdfFileReader(os.path.join(RESOURCE_ROOT, "crazyones.pdf"))
pdf_file_writer = PdfFileWriter()
pdf_file_writer.appendPagesFromReader(reader)
yield pdf_file_writer
writer = PdfFileWriter()
writer.appendPagesFromReader(reader)
yield writer


def test_add_js(pdf_file_writer):
Expand Down
6 changes: 5 additions & 1 deletion Tests/test_merger.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,9 @@ def test_merge():
file_merger.setPageLayout("/SinglePage")
file_merger.setPageMode("/UseThumbs")

file_merger.write("dont_commit_merged.pdf")
tmp_path = "dont_commit_merged.pdf"
file_merger.write(tmp_path)
file_merger.close()

# Clean up
os.remove(tmp_path)
1 change: 1 addition & 0 deletions Tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ def test_matrixMultiply(a, b, expected):
def test_markLocation():
stream = io.BytesIO(b"abde" * 6000)
PyPDF2.utils.markLocation(stream)
os.remove("PyPDF2_pdfLocation.txt") # cleanup


def test_ConvertFunctionsToVirtualList():
Expand Down
38 changes: 19 additions & 19 deletions Tests/test_workflows.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,19 @@ def test_PdfReaderFileLoad():

with open(os.path.join(RESOURCE_ROOT, "crazyones.pdf"), "rb") as inputfile:
# Load PDF file from file
ipdf = PdfFileReader(inputfile)
ipdf_p1 = ipdf.getPage(0)
reader = PdfFileReader(inputfile)
page = reader.getPage(0)

# Retrieve the text of the PDF
with open(os.path.join(RESOURCE_ROOT, "crazyones.txt"), "rb") as pdftext_file:
pdftext = pdftext_file.read()

ipdf_p1_text = ipdf_p1.extractText().replace("\n", "").encode("utf-8")
text = page.extractText().replace("\n", "").encode("utf-8")

# Compare the text of the PDF to a known source
assert ipdf_p1_text == pdftext, (
assert text == pdftext, (
"PDF extracted text differs from expected value.\n\nExpected:\n\n%r\n\nExtracted:\n\n%r\n\n"
% (pdftext, ipdf_p1_text)
% (pdftext, text)
)


Expand All @@ -47,15 +47,15 @@ def test_PdfReaderJpegImage():

with open(os.path.join(RESOURCE_ROOT, "jpeg.pdf"), "rb") as inputfile:
# Load PDF file from file
ipdf = PdfFileReader(inputfile)
reader = PdfFileReader(inputfile)

# Retrieve the text of the image
with open(os.path.join(RESOURCE_ROOT, "jpeg.txt"), "r") as pdftext_file:
imagetext = pdftext_file.read()

ipdf_p0 = ipdf.getPage(0)
xObject = ipdf_p0["/Resources"]["/XObject"].getObject()
data = xObject["/Im4"].getData()
page = reader.getPage(0)
x_object = page["/Resources"]["/XObject"].getObject()
data = x_object["/Im4"].getData()

# Compare the text of the PDF to a known source
assert binascii.hexlify(data).decode() == imagetext, (
Expand All @@ -68,12 +68,12 @@ def test_decrypt():
with open(
os.path.join(RESOURCE_ROOT, "libreoffice-writer-password.pdf"), "rb"
) as inputfile:
ipdf = PdfFileReader(inputfile)
assert ipdf.isEncrypted == True
ipdf.decrypt("openpassword")
assert ipdf.getNumPages() == 1
assert ipdf.isEncrypted == True
metadict = ipdf.getDocumentInfo()
reader = PdfFileReader(inputfile)
assert reader.isEncrypted == True
reader.decrypt("openpassword")
assert reader.getNumPages() == 1
assert reader.isEncrypted == True
metadict = reader.getDocumentInfo()
assert dict(metadict) == {
"/CreationDate": "D:20220403203552+02'00'",
"/Creator": "Writer",
Expand All @@ -86,14 +86,14 @@ def test_decrypt():
@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)
reader = PdfFileReader(inputfile)
page = reader.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)
reader = PdfFileReader(inputfile)
page = reader.getPage(0)
with pytest.raises(AssertionError):
page.rotateCounterClockwise(45)
58 changes: 31 additions & 27 deletions Tests/test_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,56 +24,60 @@ def test_writer_operations():
reader = PdfFileReader(pdf_path)
reader_outline = PdfFileReader(pdf_outline_path)

output = PdfFileWriter()
writer = PdfFileWriter()
page = reader.pages[0]
with pytest.raises(PageSizeNotDefinedError):
output.addBlankPage()
output.insertPage(page, 1)
output.removeText()
output.insertPage(reader_outline.pages[0], 0)
output.addBookmarkDestination(page)
output.addBookmark("A bookmark", 0)
writer.addBlankPage()
writer.insertPage(page, 1)
writer.removeText()
writer.insertPage(reader_outline.pages[0], 0)
writer.addBookmarkDestination(page)
writer.addBookmark("A bookmark", 0)
# output.addNamedDestination("A named destination", 1)
output.removeLinks()
writer.removeLinks()
# assert output.getNamedDestRoot() == ['A named destination', IndirectObject(9, 0, output)]
output.addBlankPage()
output.addURI(2, "https://example.com", RectangleObject([0, 0, 100, 100]))
output.addLink(2, 1, RectangleObject([0, 0, 100, 100]))
assert output.getPageLayout() is None
output.setPageLayout("SinglePage")
assert output.getPageLayout() == "SinglePage"
assert output.getPageMode() is None
output.setPageMode("UseNone")
assert output.getPageMode() == "UseNone"
output.insertBlankPage(width=100, height=100)
output.insertBlankPage() # without parameters
writer.addBlankPage()
writer.addURI(2, "https://example.com", RectangleObject([0, 0, 100, 100]))
writer.addLink(2, 1, RectangleObject([0, 0, 100, 100]))
assert writer.getPageLayout() is None
writer.setPageLayout("SinglePage")
assert writer.getPageLayout() == "SinglePage"
assert writer.getPageMode() is None
writer.setPageMode("UseNone")
assert writer.getPageMode() == "UseNone"
writer.insertBlankPage(width=100, height=100)
writer.insertBlankPage() # without parameters

# This gives "KeyError: '/Contents'" - is that a bug?
# output.removeImages()

output.addMetadata({"author": "Martin Thoma"})
writer.addMetadata({"author": "Martin Thoma"})

output.addAttachment("foobar.gif", b"foobarcontent")
writer.addAttachment("foobar.gif", b"foobarcontent")

# finally, write "output" to PyPDF2-output.pdf
with open("dont_commit_writer.pdf", "wb") as output_stream:
output.write(output_stream)
tmp_path = "dont_commit_writer.pdf"
with open(tmp_path, "wb") as output_stream:
writer.write(output_stream)

# cleanup
os.remove(tmp_path)


def test_remove_images():
pdf_path = os.path.join(RESOURCE_ROOT, "side-by-side-subfig.pdf")

reader = PdfFileReader(pdf_path)
output = PdfFileWriter()
writer = PdfFileWriter()

page = reader.pages[0]
output.insertPage(page, 0)
output.removeImages()
writer.insertPage(page, 0)
writer.removeImages()

# finally, write "output" to PyPDF2-output.pdf
tmp_filename = "dont_commit_writer_removed_image.pdf"
with open(tmp_filename, "wb") as output_stream:
output.write(output_stream)
writer.write(output_stream)

with open(tmp_filename, "rb") as input_stream:
reader = PdfFileReader(input_stream)
Expand Down
4 changes: 2 additions & 2 deletions docs/user/cropping-and-transforming.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ writer.addPage(page3)
# comment the the encription lines, if that's the case, to try this out:
writer.addJS("this.print({bUI:true,bSilent:false,bShrinkToFit:true});")

# finally, write to document-output.pdf
# write to document-output.pdf
with open("PyPDF2-output.pdf", "wb") as fp:
output.write(fp)
writer.write(fp)
```

0 comments on commit a5875c5

Please sign in to comment.