Skip to content

Commit

Permalink
PPF-750: Improve Integer and Float Input Handling in PdfWrapper.fill(…
Browse files Browse the repository at this point in the history
…) Method (#750)

* PPF-749: add integer and float input handling to PdfWrapper.fill() method

* PPF-749: add new filled sample pdf files

* PPF-749: add new tests in 'tests/test_fill_method.py'
  • Loading branch information
alexperedel authored Oct 23, 2024
1 parent b851988 commit 65c5c8d
Show file tree
Hide file tree
Showing 8 changed files with 126 additions and 0 deletions.
4 changes: 4 additions & 0 deletions PyPDFForm/wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,10 @@ def fill(
if key in self.widgets:
self.widgets[key].value = value

for key, widget in self.widgets.items():
if isinstance(widget, Text) and isinstance(widget.value, (int, float)):
self.widgets[key].value = str(widget.value)

for key, value in self.widgets.items():
if isinstance(value, Dropdown):
self.widgets[key] = dropdown_to_text(value)
Expand Down
Binary file added pdf_samples/sample_filled_boolean_and_ints.pdf
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added pdf_samples/sample_filled_varied_float_values.pdf
Binary file not shown.
Binary file added pdf_samples/sample_filled_varied_ints.pdf
Binary file not shown.
Binary file not shown.
122 changes: 122 additions & 0 deletions tests/test_fill_method.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
import os
from PyPDFForm import PdfWrapper


def test_fill_with_varied_int_values(pdf_samples, request):
expected_path = os.path.join(pdf_samples, "sample_filled_varied_ints.pdf")
with open(expected_path, "rb+") as f:
obj = PdfWrapper(os.path.join(pdf_samples, "sample_template.pdf")).fill(
{
"test": 100,
"test_2": -250,
"test_3": 0,
}
)

request.config.results["expected_path"] = expected_path
request.config.results["stream"] = obj.read()

expected = f.read()

assert len(obj.read()) == len(expected)
assert obj.read() == expected


def test_fill_with_boolean_and_int_values(pdf_samples, request):
expected_path = os.path.join(pdf_samples, "sample_filled_boolean_and_ints.pdf")
with open(expected_path, "rb+") as f:
obj = PdfWrapper(os.path.join(pdf_samples, "sample_template.pdf")).fill(
{
"test": 42,
"test_2": True,
"test_3": False,
}
)

request.config.results["expected_path"] = expected_path
request.config.results["stream"] = obj.read()

expected = f.read()

assert len(obj.read()) == len(expected)
assert obj.read() == expected


def test_fill_with_empty_string_and_int(pdf_samples, request):
expected_path = os.path.join(pdf_samples, "sample_filled_with_empty_string_and_int.pdf")
with open(expected_path, "rb+") as f:
obj = PdfWrapper(os.path.join(pdf_samples, "sample_template.pdf")).fill(
{
"test": 42,
"test_2": "",
"test_3": 33,
}
)

request.config.results["expected_path"] = expected_path
request.config.results["stream"] = obj.read()

expected = f.read()

assert len(obj.read()) == len(expected)
assert obj.read() == expected


def test_fill_with_large_and_small_ints(pdf_samples, request):
expected_path = os.path.join(pdf_samples, "sample_filled_large_and_small_ints.pdf")
with open(expected_path, "rb+") as f:
obj = PdfWrapper(os.path.join(pdf_samples, "sample_template.pdf")).fill(
{
"test": 999999999999,
"test_2": -999999999999,
"test_3": 1,
}
)

request.config.results["expected_path"] = expected_path
request.config.results["stream"] = obj.read()

expected = f.read()

assert len(obj.read()) == len(expected)
assert obj.read() == expected


def test_fill_with_varied_float_values(pdf_samples, request):
expected_path = os.path.join(pdf_samples, "sample_filled_varied_float_values.pdf")
with open(expected_path, "rb+") as f:
obj = PdfWrapper(os.path.join(pdf_samples, "sample_template.pdf")).fill(
{
"test": 1.5,
"test_2": 13.8,
"test_3": 543,
}
)

request.config.results["expected_path"] = expected_path
request.config.results["stream"] = obj.read()

expected = f.read()

assert len(obj.read()) == len(expected)
assert obj.read() == expected


def test_fill_with_negative_and_positive_floats_and_int(pdf_samples, request):
expected_path = os.path.join(pdf_samples, "sample_filled_negative_and_positive_floats_and_int.pdf")
with open(expected_path, "rb+") as f:
obj = PdfWrapper(os.path.join(pdf_samples, "sample_template.pdf")).fill(
{
"test": -1.5,
"test_2": -6.9,
"test_3": 22,
}
)

request.config.results["expected_path"] = expected_path
request.config.results["stream"] = obj.read()

expected = f.read()

assert len(obj.read()) == len(expected)
assert obj.read() == expected

0 comments on commit 65c5c8d

Please sign in to comment.