Skip to content

Commit

Permalink
TST: cat with two files (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinThoma authored Dec 16, 2023
1 parent 21ebb2a commit 2deee1c
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 2 deletions.
19 changes: 17 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,24 @@ def pdf_file_100(tmp_path):

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

pdf_filepath = tmp_path / "two_pages.pdf"
pdf_filepath = tmp_path / "pdf_file_100.pdf"
pdf.output(pdf_filepath)
return pdf_filepath


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

for char in [chr(i) for i in range(ord("a"), ord("z") + 1)]:
pdf.add_page()
pdf.set_font("helvetica", size=12)
pdf.cell(200, 10, txt=f"{char}", ln=True, align="C")

pdf_filepath = tmp_path / "abc.pdf"
pdf.output(pdf_filepath)
return pdf_filepath
65 changes: 65 additions & 0 deletions tests/test_cat.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,71 @@ def extract_embedded_images(pdf_filepath):
return images


def test_cat_combine_files(pdf_file_100, pdf_file_abc, tmp_path, capsys):
with chdir(tmp_path):
output_pdf_path = tmp_path / "out.pdf"

# Run pdfly cat command
exit_code = run_cli(
[
"cat",
str(pdf_file_100),
"1:10:2",
str(pdf_file_abc),
"::2",
str(pdf_file_abc),
"1::2",
"--output",
str(output_pdf_path),
]
)
captured = capsys.readouterr()

# Check if the command was successful
assert exit_code == 0, captured.out

# 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 == [
"1",
"3",
"5",
"7",
"9",
"a",
"c",
"e",
"g",
"i",
"k",
"m",
"o",
"q",
"s",
"u",
"w",
"y",
"b",
"d",
"f",
"h",
"j",
"l",
"n",
"p",
"r",
"t",
"v",
"x",
"z",
]


@pytest.mark.parametrize(
("page_range", "expected"),
[
Expand Down

0 comments on commit 2deee1c

Please sign in to comment.