Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests: look for regressions when converting PDFs #1089

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def sample_pdf() -> str:

test_docs = [
p
for p in test_docs_dir.rglob("*")
for p in test_docs_dir.glob("*")
if p.is_file()
and not (p.name.endswith(SAFE_EXTENSION) or p.name.startswith("sample_bad"))
]
Expand Down
22 changes: 20 additions & 2 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from pathlib import Path
from typing import Optional, Sequence

import fitz
import pytest
from click.testing import CliRunner, Result
from pytest_mock import MockerFixture
Expand Down Expand Up @@ -191,9 +192,26 @@ def test_invalid_lang(self, sample_pdf: str) -> None:
result.assert_failure()

@for_each_doc
def test_formats(self, doc: Path) -> None:
result = self.run_cli(str(doc))
def test_formats(self, doc: Path, tmp_path_factory: pytest.TempPathFactory) -> None:
reference = (doc.parent / "reference" / doc.stem).with_suffix(".pdf")
destination = tmp_path_factory.mktemp(doc.stem).with_suffix(".pdf")

result = self.run_cli([str(doc), "--output-filename", str(destination)])
result.assert_success()
# When needed, regenerate the reference PDFs by uncommenting the following line:
# reference.parent.mkdir(parents=True, exist_ok=True)
# shutil.copy(destination, reference)

converted = fitz.open(destination)
ref = fitz.open(reference)
assert len(converted) == len(ref), "different number of pages"

for page, ref_page in zip(converted, ref):
page.get_pixmap(dpi=150)
ref_page.get_pixmap(dpi=150)
assert page.get_pixmap().tobytes() == ref_page.get_pixmap().tobytes(), (
f"different page content for page {page.number}"
)

def test_output_filename(self, sample_pdf: str) -> None:
temp_dir = tempfile.mkdtemp(prefix="dangerzone-")
Expand Down
Binary file added tests/test_docs/reference/sample-bmp.pdf
Binary file not shown.
Binary file added tests/test_docs/reference/sample-doc.pdf
Binary file not shown.
Binary file added tests/test_docs/reference/sample-docm.pdf
Binary file not shown.
Binary file added tests/test_docs/reference/sample-docx.pdf
Binary file not shown.
Binary file added tests/test_docs/reference/sample-epub.pdf
Binary file not shown.
Binary file added tests/test_docs/reference/sample-gif.pdf
Binary file not shown.
Binary file added tests/test_docs/reference/sample-jpg.pdf
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added tests/test_docs/reference/sample-odg.pdf
Binary file not shown.
Binary file added tests/test_docs/reference/sample-odp.pdf
Binary file not shown.
Binary file added tests/test_docs/reference/sample-ods.pdf
Binary file not shown.
Binary file added tests/test_docs/reference/sample-odt-mp4.pdf
Binary file not shown.
Binary file added tests/test_docs/reference/sample-odt.pdf
Binary file not shown.
Binary file added tests/test_docs/reference/sample-pbm.pdf
Binary file not shown.
Binary file added tests/test_docs/reference/sample-pdf.pdf
Binary file not shown.
Binary file added tests/test_docs/reference/sample-png.pdf
Binary file not shown.
Binary file added tests/test_docs/reference/sample-pnm.pdf
Binary file not shown.
Binary file added tests/test_docs/reference/sample-ppm.pdf
Binary file not shown.
Binary file added tests/test_docs/reference/sample-ppt.pdf
Binary file not shown.
Binary file added tests/test_docs/reference/sample-pptx.pdf
Binary file not shown.
Binary file added tests/test_docs/reference/sample-svg.pdf
Binary file not shown.
Binary file added tests/test_docs/reference/sample-xls.pdf
Binary file not shown.
Binary file added tests/test_docs/reference/sample-xlsx.pdf
Binary file not shown.
Loading