Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

STY: Apply black and isort #950

Merged
merged 18 commits into from
Jun 6, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Minor test improvements
  • Loading branch information
MartinThoma committed Jun 6, 2022
commit af00e73446fef216d8885bdf4f8dd5298cb84a9a
4 changes: 2 additions & 2 deletions tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ def get_pdf_from_url(url: str, name: str) -> bytes:
This function is a last resort for PDF files where we are uncertain if
we may add it for testing purposes to https://github.com/py-pdf/sample-files

URL: location of the PDF file
name: unique name accross all files
:param str url: location of the PDF file
:param str name: unique name accross all files
"""
cache_dir = os.path.join(os.path.dirname(__file__), "pdf_cache")
if not os.path.exists(cache_dir):
Expand Down
6 changes: 3 additions & 3 deletions tests/test_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def test_FlateDecode_unsupported_predictor():


@pytest.mark.parametrize(
("input", "expected"),
("data", "expected"),
[
(">", ""),
(
Expand Down Expand Up @@ -88,7 +88,7 @@ def test_FlateDecode_unsupported_predictor():
"whitespace",
],
)
def test_ASCIIHexDecode(input, expected):
def test_ASCIIHexDecode(data, expected):
"""
Feeds a bunch of values to ASCIIHexDecode.decode() and ensures the
correct output is returned.
Expand All @@ -97,7 +97,7 @@ def test_ASCIIHexDecode(input, expected):
is currently raised.)
"""

assert ASCIIHexDecode.decode(input) == expected
assert ASCIIHexDecode.decode(data) == expected


def test_ASCIIHexDecode_no_eod():
Expand Down
5 changes: 2 additions & 3 deletions tests/test_generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,6 @@ def test_readStringFromStream_not_in_escapedict_no_digit():
with pytest.raises(PdfReadError) as exc:
readStringFromStream(stream)
assert exc.value.args[0] == "Stream has ended unexpectedly"
# "Unexpected escaped string: y"


def test_readStringFromStream_multichar_eol():
Expand Down Expand Up @@ -334,10 +333,10 @@ def test_DictionaryObject_read_from_stream_stream_stream_valid(
):
stream = BytesIO(b"<< /S /GoTo /Length %d >>stream\nBT /F1\nendstream\n" % length)

class tst: # to replace pdf
class Tst: # to replace pdf
strict = True

pdf = tst()
pdf = Tst()
pdf.strict = strict
with pytest.raises(PdfReadError) as exc:
do = DictionaryObject.read_from_stream(stream, pdf)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_javascript.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def pdf_file_writer():
reader = PdfReader(os.path.join(RESOURCE_ROOT, "crazyones.pdf"))
writer = PdfWriter()
writer.append_pages_from_reader(reader)
yield writer
return writer


def test_add_js(pdf_file_writer):
Expand Down
1 change: 0 additions & 1 deletion tests/test_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ def test_read(meta):
[
("crazyones.pdf", None),
("attachment.pdf", None),
# ("side-by-side-subfig.pdf", None),
(
"libreoffice-writer-password.pdf",
"openpassword",
Expand Down
42 changes: 17 additions & 25 deletions tests/test_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,11 @@ def test_get_images(src, nb_images):
images_extracted = []

if RES.XOBJECT in page[PG.RESOURCES]:
xObject = page[PG.RESOURCES][RES.XOBJECT].get_object()
x_object = page[PG.RESOURCES][RES.XOBJECT].get_object()

for obj in xObject:
if xObject[obj][IA.SUBTYPE] == "/Image":
extension, byte_stream = _xobj_to_image(xObject[obj])
for obj in x_object:
if x_object[obj][IA.SUBTYPE] == "/Image":
extension, byte_stream = _xobj_to_image(x_object[obj])
if extension is not None:
filename = obj[1:] + ".png"
with open(filename, "wb") as img:
Expand Down Expand Up @@ -229,9 +229,8 @@ def test_get_images_raw(strict, with_prev_0, startx_correction, should_fail):
)
pdf_stream = io.BytesIO(pdf_data)
if should_fail:
with pytest.raises(PdfReadError) as exc:
with pytest.warns(PdfReadWarning):
PdfReader(pdf_stream, strict=strict)
with pytest.raises(PdfReadError) as exc, pytest.warns(PdfReadWarning):
PdfReader(pdf_stream, strict=strict)
assert exc.type == PdfReadError
if startx_correction == -1:
assert (
Expand All @@ -245,9 +244,8 @@ def test_get_images_raw(strict, with_prev_0, startx_correction, should_fail):

def test_issue297():
path = os.path.join(RESOURCE_ROOT, "issue-297.pdf")
with pytest.raises(PdfReadError) as exc:
with pytest.warns(PdfReadWarning):
reader = PdfReader(path, strict=True)
with pytest.raises(PdfReadError) as exc, pytest.warns(PdfReadWarning):
reader = PdfReader(path, strict=True)
assert "Broken xref table" in exc.value.args[0]
with pytest.warns(PdfReadWarning):
reader = PdfReader(path, strict=False)
Expand Down Expand Up @@ -437,9 +435,8 @@ def test_read_prev_0_trailer():
pdf_data.find(b"xref") - 1,
)
pdf_stream = io.BytesIO(pdf_data)
with pytest.raises(PdfReadError) as exc:
with pytest.warns(PdfReadWarning):
PdfReader(pdf_stream, strict=True)
with pytest.raises(PdfReadError) as exc, pytest.warns(PdfReadWarning):
PdfReader(pdf_stream, strict=True)
assert exc.value.args[0] == "/Prev=0 in the trailer (try opening with strict=False)"


Expand Down Expand Up @@ -511,16 +508,14 @@ def test_read_unknown_zero_pages():
pdf_stream = io.BytesIO(pdf_data)
with pytest.warns(PdfReadWarning):
reader = PdfReader(pdf_stream, strict=True)
with pytest.raises(PdfReadError) as exc:
with pytest.warns(PdfReadWarning):
len(reader.pages)
with pytest.raises(PdfReadError) as exc, pytest.warns(PdfReadWarning):
len(reader.pages)

assert exc.value.args[0] == "Could not find object."
with pytest.warns(PdfReadWarning):
reader = PdfReader(pdf_stream, strict=False)
with pytest.raises(AttributeError) as exc:
with pytest.warns(PdfReadWarning):
len(reader.pages)
with pytest.raises(AttributeError) as exc, pytest.warns(PdfReadWarning):
len(reader.pages)
assert exc.value.args[0] == "'NoneType' object has no attribute 'get_object'"


Expand Down Expand Up @@ -588,10 +583,9 @@ def test_issue604(strict):
pdf = None
bookmarks = None
if strict:
with pytest.raises(PdfReadError) as exc:
pdf = PdfReader(f, strict=strict)
with pytest.warns(PdfReadWarning):
bookmarks = pdf._get_outlines()
pdf = PdfReader(f, strict=strict)
with pytest.raises(PdfReadError) as exc, pytest.warns(PdfReadWarning):
bookmarks = pdf._get_outlines()
if "Unknown Destination" not in exc.value.args[0]:
raise Exception("Expected exception not raised")
return # bookmarks not correct
Expand All @@ -601,7 +595,6 @@ def test_issue604(strict):
bookmarks = pdf._get_outlines()

def get_dest_pages(x):
# print(x)
if isinstance(x, list):
r = [get_dest_pages(y) for y in x]
return r
Expand All @@ -613,7 +606,6 @@ def get_dest_pages(x):
b
) in bookmarks: # b can be destination or a list:preferred to just print them
out.append(get_dest_pages(b))
# print(out)


def test_decode_permissions():
Expand Down