See Ash Code logo See Ash Code

I added real PDF and EPUB files to the converter tests

Ashlynn Antrobus
• • 3 min read

I’ve added PDF and EPUB files to ebook2text’s test directory. That sounds like an obvious thing for a book converter to have, but it tests something different from handing a chapter detector a string.

I already had tests for deciding whether text looked like a chapter heading. Those go back to May. They pass strings to functions and check the answers. They don’t ask whether I can get those strings out of a book file in the first place.

The PDF fixtures arrived on November 30, followed by the EPUB fixtures on December 3. Each format has a text version and a version with an image.

Something for the parser to open

In the previous post, I looked at image interpretation and a missing separator between pieces of PDF text. A small test can exercise either helper. A file gives the helpers something to work on together.

The PDF files are:

tests/test_files/test_pdf.pdf
tests/test_files/test_pdf_with_image.pdf

The tests use a fixture that builds the path relative to the test directory. That matters because the test shouldn’t depend on which directory I happened to run it from.

One PDF test asks the reader for its pages and expects seven. Others inspect the parsed text: the sample title and introduction should be removed, while the first chapter’s paragraph should remain. There is also a check for a chapter separator.

Those are different questions. Getting seven pages back doesn’t establish that the converter kept the right text. Finding a separator doesn’t establish that it went between the right chapters. The assertions have to describe the output I care about.

The tests also cover writing that text to a temporary file. That takes the check past the in-memory result and into the output somebody would receive.

A file can be real while part of the test is fake

For image text, I don’t need every test to call an OCR service. One of the PDF tests replaces run_ocr with a function that returns a known chapter heading. That lets the test check how the converter combines OCR text with the rest of the page.

It does leave a boundary. The test can tell me whether my code handles that supplied response. It can’t tell me whether an OCR model would read the image correctly.

I want both kinds of checks, but I want to know which one I’m looking at. Otherwise, a test that uses a real PDF can sound like a test of everything that happens to a real PDF.

The EPUB files add another format to open and parse. Alongside the file-based checks, the test module has small HTML elements for checking paragraph and image extraction separately. Keeping those smaller tests is useful when a larger conversion fails and I need to find the part responsible.

An illustration of an e-book reader with a stylus.

E-book reader illustration by netalloy, CC0.

Adding the files is not the same as passing the tests

There is a mismatch in the December snapshot that I need to fix. The committed image fixture is named:

test_epub_with_image.epub

The test fixture asks for:

test_epub_with_images.epub

One extra letter is enough to stop that test before it exercises the converter. There are also test calls that need to stay in step with the converter’s interfaces. A directory full of sample files doesn’t check either of those things for me.

So this change gives the tests their input documents. It doesn’t, by itself, establish a passing suite, and it certainly doesn’t establish that every PDF or EPUB will convert correctly.

I want to check for a recognizable passage from each fixture. An empty string would satisfy a type assertion.

AA

Written by Ashlynn Antrobus

Software developer, author, and builder. Sharing technical lessons learned from building tools like Ebook2Text, IndexedDict, and rag-mcp, and navigating life as a programmer with dyslexia.