Skip to content

Commit

Permalink
Merge pull request #723 from chinapandaman/PPF-722
Browse files Browse the repository at this point in the history
PPF-722: make middleware sample value respect max schema boundary
  • Loading branch information
chinapandaman authored Sep 15, 2024
2 parents e8d4726 + a4e9ba2 commit 0da01c5
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 5 deletions.
2 changes: 1 addition & 1 deletion PyPDFForm/middleware/dropdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ def schema_definition(self) -> dict:
def sample_value(self) -> int:
"""Sample value of the dropdown."""

return 0
return len(self.choices) - 1
2 changes: 1 addition & 1 deletion PyPDFForm/middleware/radio.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ def schema_definition(self) -> dict:
def sample_value(self) -> int:
"""Sample value of the radiobutton."""

return 0
return self.number_of_options - 1
2 changes: 1 addition & 1 deletion PyPDFForm/middleware/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,4 @@ def schema_definition(self) -> dict:
def sample_value(self) -> str:
"""Sample value of the text field."""

return self.name
return self.name[:self.max_length] if self.max_length is not None else self.name
Binary file modified pdf_samples/scenario/issues/437_expected.pdf
Binary file not shown.
Binary file modified pdf_samples/widget/create_widget_sejda.pdf
Binary file not shown.
6 changes: 4 additions & 2 deletions tests/test_dropdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@


def test_schema(sample_template_with_dropdown):
schema = PdfWrapper(sample_template_with_dropdown).schema
obj = PdfWrapper(sample_template_with_dropdown)

for key, value in schema["properties"].items():
for key, value in obj.schema["properties"].items():
if key == "dropdown_1":
assert value["maximum"] == 3

assert obj.sample_data["dropdown_1"] == 3


def test_dropdown_not_specified(sample_template_with_dropdown):
assert (
Expand Down
11 changes: 11 additions & 0 deletions tests/test_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,17 @@ def test_sample_data(sejda_template_complex):
pass


def test_sample_data_max_boundary(sample_template_with_comb_text_field):
obj = PdfWrapper(sample_template_with_comb_text_field)
try:
validate(instance=obj.sample_data, schema=obj.schema)
except ValidationError:
raise AssertionError from ValidationError

assert obj.sample_data["LastName"] == "LastNam"
assert obj.sample_data["Gender"] == 1


def test_fill_right_aligned(
sample_template_with_right_aligned_text_field, pdf_samples, request
):
Expand Down

0 comments on commit 0da01c5

Please sign in to comment.