Skip to content

Commit

Permalink
test: support PDFs with pages of varying sizes
Browse files Browse the repository at this point in the history
  • Loading branch information
bastienlc committed Apr 21, 2024
1 parent 5932a26 commit 4af760d
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 11 deletions.
Binary file added tests/fixtures/different_sizes_input.pdf
Binary file not shown.
Binary file added tests/fixtures/different_sizes_output.pdf
Binary file not shown.
12 changes: 1 addition & 11 deletions tests/test_add_watermark_from_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@

import os

import numpy as np
import pytest
from pdf2image import convert_from_path

from pdf_watermark.handler import add_watermark_from_options
from pdf_watermark.options import (
Expand All @@ -16,6 +14,7 @@
InsertOptions,
)
from pdf_watermark.watermark import DEFAULTS
from tests.utils import assert_pdfs_are_close

INPUT = "tests/fixtures/input.pdf"
OUTPUT = "output.pdf"
Expand Down Expand Up @@ -62,15 +61,6 @@ def cleanup():
]


def assert_pdfs_are_close(path_1: str, path_2: str, epsilon: float = 1e-10):
"""This function checks that two PDFs are close enough. We chose to convert the PDFs to images and then compare their L1 norms, because other techniques (hashing for instance) might break easily."""
images_1 = convert_from_path(path_1)
images_2 = convert_from_path(path_2)

for im1, im2 in zip(images_1, images_2):
assert np.sum(np.abs(np.array(im1) - np.array(im2))) < epsilon


def test_add_watermark_from_options():
index = 0
for files_options in FILES_OPTIONS_FIXTURES:
Expand Down
53 changes: 53 additions & 0 deletions tests/test_different_page_sizes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
"""
Test for PDFs with different page sizes.
"""

import os

import pytest

from pdf_watermark.handler import add_watermark_from_options
from pdf_watermark.options import DrawingOptions, FilesOptions, GridOptions
from pdf_watermark.watermark import DEFAULTS
from tests.utils import assert_pdfs_are_close

INPUT = "tests/fixtures/different_sizes_input.pdf"
OUTPUT = "output.pdf"
FIXTURE = "tests/fixtures/different_sizes_output.pdf"


@pytest.fixture(autouse=True)
def cleanup():
yield
os.remove(OUTPUT)


DRAWING_OPTIONS_FIXTURE = DrawingOptions(
watermark="watermark",
opacity=DEFAULTS.opacity,
angle=DEFAULTS.angle,
text_color=DEFAULTS.text_color,
text_font=DEFAULTS.text_font,
text_size=DEFAULTS.text_size,
unselectable=DEFAULTS.unselectable,
image_scale=DEFAULTS.image_scale,
save_as_image=DEFAULTS.save_as_image,
dpi=DEFAULTS.dpi,
)

FILES_OPTIONS_FIXTURE = FilesOptions(INPUT, OUTPUT)

GRID_OPTIONS_FIXTURE = GridOptions(
horizontal_boxes=DEFAULTS.horizontal_boxes,
vertical_boxes=DEFAULTS.vertical_boxes,
margin=DEFAULTS.margin,
)


def test_different_page_sizes():
add_watermark_from_options(
files_options=FILES_OPTIONS_FIXTURE,
drawing_options=DRAWING_OPTIONS_FIXTURE,
specific_options=GRID_OPTIONS_FIXTURE,
)
assert_pdfs_are_close(OUTPUT, FIXTURE)
11 changes: 11 additions & 0 deletions tests/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import numpy as np
from pdf2image import convert_from_path


def assert_pdfs_are_close(path_1: str, path_2: str, epsilon: float = 1e-10):
"""This function checks that two PDFs are close enough. We chose to convert the PDFs to images and then compare their L1 norms, because other techniques (hashing for instance) might break easily."""
images_1 = convert_from_path(path_1)
images_2 = convert_from_path(path_2)

for im1, im2 in zip(images_1, images_2):
assert np.sum(np.abs(np.array(im1) - np.array(im2))) < epsilon

0 comments on commit 4af760d

Please sign in to comment.