Skip to content

Commit

Permalink
TST: Test cat command with more parameters + validate result (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinThoma authored Dec 16, 2023
1 parent a15caf4 commit 21ebb2a
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
16 changes: 15 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

from fpdf import FPDF
import pytest

from pdfly.cli import entry_point

try:
Expand Down Expand Up @@ -47,3 +46,18 @@ def two_pages_pdf_filepath(tmp_path):
pdf_filepath = tmp_path / "two_pages.pdf"
pdf.output(pdf_filepath)
return pdf_filepath


@pytest.fixture
def pdf_file_100(tmp_path):
"""A PDF with 100 pages; each has only the page index on it."""
pdf = FPDF()

for i in range(100):
pdf.add_page()
pdf.set_font("Arial", size=12)
pdf.cell(200, 10, txt=f"{i}", ln=True, align="C")

pdf_filepath = tmp_path / "two_pages.pdf"
pdf.output(pdf_filepath)
return pdf_filepath
45 changes: 45 additions & 0 deletions tests/test_cat.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ def test_cat_incorrect_number_of_args(capsys, tmp_path):


def test_cat_two_files_ok(capsys, tmp_path):
# Act
with chdir(tmp_path):
exit_code = run_cli(
[
Expand All @@ -24,6 +25,8 @@ def test_cat_two_files_ok(capsys, tmp_path):
]
)
captured = capsys.readouterr()

# Assert
assert exit_code == 0, captured
assert not captured.err
reader = PdfReader(tmp_path / "out.pdf")
Expand Down Expand Up @@ -122,3 +125,45 @@ def extract_embedded_images(pdf_filepath):
for page in reader.pages:
images.extend(page.images)
return images


@pytest.mark.parametrize(
("page_range", "expected"),
[
("22", ["22"]),
("0:3", ["0", "1", "2"]),
(":3", ["0", "1", "2"]),
(":", [str(el) for el in range(100)]),
("5:", [str(el) for el in list(range(100))[5:]]),
("::2", [str(el) for el in list(range(100))[::2]]),
("1:10:2", [str(el) for el in list(range(100))[1:10:2]]),
("::1", [str(el) for el in list(range(100))[::1]]),
("::-1", [str(el) for el in list(range(100))[::-1]]),
],
)
def test_cat_commands(pdf_file_100, capsys, tmp_path, page_range, expected):
with chdir(tmp_path):
output_pdf_path = tmp_path / "out.pdf"

# Run pdfly cat command
exit_code = run_cli(
[
"cat",
str(pdf_file_100),
page_range,
"--output",
str(output_pdf_path),
]
)

# Check if the command was successful
assert exit_code == 0

# Extract text from the original and modified PDFs
extracted_pages = []
reader = PdfReader(output_pdf_path)
for page in reader.pages:
extracted_pages.append(page.extract_text())

# Compare the extracted text
assert extracted_pages == expected

0 comments on commit 21ebb2a

Please sign in to comment.