diff options
| author | historia <historiavg@proton.me> | 2026-08-18 02:17:39 -0400 |
|---|---|---|
| committer | historia <historiavg@proton.me> | 2026-08-18 02:17:39 -0400 |
| commit | e650d17b07cceb660ef8686fd1f122d50fa3a05c (patch) | |
| tree | 75ef197e4d654b17459d3aa16c00e24b936da54d /tests/test_extractors.py | |
| parent | 8760626f744d79a05904e692119ffa72fc0c4e54 (diff) | |
| download | tts-audiobook-generator-e650d17b07cceb660ef8686fd1f122d50fa3a05c.tar.gz | |
refactor(converter): deduplicate epub extraction
Diffstat (limited to 'tests/test_extractors.py')
| -rw-r--r-- | tests/test_extractors.py | 40 |
1 files changed, 27 insertions, 13 deletions
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 = "<html><body><p>First chapter text.</p></body></html>" - chapter2 = epub.EpubHtml(title="Two", file_name="chap2.xhtml", lang="en") - chapter2.content = "<html><body><p>Second chapter text.</p></body></html>" + 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"<html><body><p>{text}</p></body></html>" + 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() |
