diff --git a/backend/src/chitai/services/metadata_extractor.py b/backend/src/chitai/services/metadata_extractor.py index e8dcd7f..1e6794f 100644 --- a/backend/src/chitai/services/metadata_extractor.py +++ b/backend/src/chitai/services/metadata_extractor.py @@ -571,7 +571,7 @@ class EpubExtractor(FileExtractor): @classmethod def _extract_publisher(cls, epub: epub.EpubBook) -> str | None: try: - epub.get_metadata("DC", "publisher")[0][0] + return epub.get_metadata("DC", "publisher")[0][0] except Exception: return None diff --git a/backend/tests/unit/test_metadata_extractor.py b/backend/tests/unit/test_metadata_extractor.py index e6c687b..7070776 100644 --- a/backend/tests/unit/test_metadata_extractor.py +++ b/backend/tests/unit/test_metadata_extractor.py @@ -1,4 +1,5 @@ import pytest +from ebooklib import epub from pathlib import Path from datetime import date from chitai.services.metadata_extractor import ( @@ -172,3 +173,20 @@ class TestEditionFromFiles: assert metadata["title"] == "The Project Gutenberg eBook #33283: Calculus Made Easy" assert metadata["edition"] == 2 + + +class TestEpubPublisher: + """The publisher was looked up and then dropped on the floor.""" + + def test_a_declared_publisher_is_returned(self) -> None: + """ + The lookup discarded its own result and fell off the end of the function, so + every EPUB reported no publisher no matter what it said. + """ + book = epub.EpubBook() + book.add_metadata("DC", "publisher", "No Starch Press") + + assert EpubExtractor._extract_publisher(book) == "No Starch Press" + + def test_no_publisher_is_none(self) -> None: + assert EpubExtractor._extract_publisher(epub.EpubBook()) is None