From e650d17b07cceb660ef8686fd1f122d50fa3a05c Mon Sep 17 00:00:00 2001 From: historia Date: Tue, 18 Aug 2026 02:17:39 -0400 Subject: refactor(converter): deduplicate epub extraction --- tests/test_extractors.py | 40 +++++++++++++++++++++++++++------------- 1 file changed, 27 insertions(+), 13 deletions(-) (limited to 'tests') diff --git a/tests/test_extractors.py b/tests/test_extractors.py index 09b688e..7b307c0 100644 --- a/tests/test_extractors.py +++ b/tests/test_extractors.py @@ -35,7 +35,8 @@ class TxtExtractionTests(unittest.TestCase): extract_text(path) -def _build_test_epub(path: Path) -> None: +def _build_test_epub(path: Path, chapters=(("One", "First chapter text."), + ("Two", "Second chapter text."))) -> None: from ebooklib import epub book = epub.EpubBook() @@ -43,15 +44,15 @@ def _build_test_epub(path: Path) -> None: book.set_title("Test Book") book.set_language("en") - chapter1 = epub.EpubHtml(title="One", file_name="chap1.xhtml", lang="en") - chapter1.content = "

First chapter text.

" - chapter2 = epub.EpubHtml(title="Two", file_name="chap2.xhtml", lang="en") - chapter2.content = "

Second chapter text.

" + items = [] + for index, (title, text) in enumerate(chapters, 1): + chapter = epub.EpubHtml(title=title, file_name=f"chap{index}.xhtml", lang="en") + chapter.content = f"

{text}

" + book.add_item(chapter) + items.append(chapter) - book.add_item(chapter1) - book.add_item(chapter2) - book.toc = (chapter1, chapter2) - book.spine = ["nav", chapter1, chapter2] + book.toc = tuple(items) + book.spine = ["nav", *items] book.add_item(epub.EpubNcx()) book.add_item(epub.EpubNav()) @@ -68,15 +69,16 @@ class EpubExtractionTests(unittest.TestCase): def test_ebooklib_extraction(self): # Regression test: the ebooklib path used to silently return "" due to # isinstance(item, ebooklib.ITEM_DOCUMENT) (an int, not a class). - from converter.extractors import _extract_epub_ebooklib + from converter.extractors import _read_epub_ebooklib with tempfile.TemporaryDirectory() as tmp: path = Path(tmp) / "book.epub" _build_test_epub(path) - text = _extract_epub_ebooklib(path) + items = _read_epub_ebooklib(path) - self.assertIn("First chapter text.", text) - self.assertIn("Second chapter text.", text) + html = "\n".join(content for _, content in items) + self.assertIn("First chapter text.", html) + self.assertIn("Second chapter text.", html) def test_epub_extraction_follows_spine_order(self): with tempfile.TemporaryDirectory() as tmp: @@ -123,6 +125,18 @@ class ExtractSectionsTests(unittest.TestCase): self.assertEqual(sections[0].title, "book") self.assertEqual(sections[0].text, "Hello world.") + def test_single_chapter_epub_keeps_chapter_title(self): + from converter.extractors import extract_sections + + with tempfile.TemporaryDirectory() as tmp: + path = Path(tmp) / "book.epub" + _build_test_epub(path, chapters=(("Only", "Just one chapter."),)) + sections = extract_sections(path) + + self.assertEqual(len(sections), 1) + self.assertEqual(sections[0].title, "Only") + self.assertIn("Just one chapter.", sections[0].text) + if __name__ == "__main__": unittest.main() -- cgit v1.2.3