Files
shelfmark/tests/metadata/test_openlibrary_parse.py
T
HeihoffJandClaude Opus 5.5 756bdec19b fix(metadata): show the translation a search matched on Open Library and Hardcover (#1451)
Searching a translated title on Open Library or Hardcover returned the
original-language work. "Der Weg der Könige" came back as "The Way of
Kings" (2010), and that English title then went into the release search
and the download name. Both providers keep one record per work, titled
after the original, and list translations underneath it.

#483 already searches releases per language with Hardcover's localized
titles, which fixed the release side of #360 when the matching language
is selected. This PR fixes the search result itself, so the book a user
picks is the translation they searched for, and it covers Open Library,
the default provider, which has no titles_by_language.

- Open Library: request the matched edition (editions.*) and prefer its
title, ISBNs, cover, language and publication year when its title
differs from the work's.
- Hardcover: use the alternative title named in the hit's highlights
(alternative_titles, best index first), without the original's subtitle.
A hit whose title also matched keeps the work title.
- /api/releases: when the modal sends a title that differs from the book
record's, it becomes search_title. Before, Hardcover's search_title from
the original won, so picking "Kinder des Nebels" still searched "The
Final Empire" first.
- Release cache: keyed on title and language as well as the provider ID.
All translations share that ID, so opening "The Way of Kings" after "Der
Weg der Könige" showed the German releases until a reload.

Known limitations: neither provider has descriptions per edition, so the
description stays the work's (usually English). Hardcover's search hits
carry no year or language for alternative titles, so those stay the
work's; Open Library's do not. Google Books is unaffected, since it
already returns one volume per edition.

Tests: Open Library edition parsing, Hardcover alternative-title
matching, the /api/releases title override, and the release cache, each
written to fail first. The Python suite passes apart from the 9
test_entrypoint_permissions.py tests, which fail the same way on an
unmodified main on macOS. The frontend typecheck, lint, format check and
unit tests pass. Checked on a live instance with Open Library, Hardcover
and Google Books.

Refs #360, #483

Disclaimer:
This was developed with the help of AI but reviewed and tested as well
as deployed on my server by a real human.

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-20 01:08:45 -04:00

119 lines
4.6 KiB
Python

from shelfmark.metadata_providers.openlibrary import OpenLibraryProvider
class TestOpenLibraryParseSearchDoc:
def test_prefers_matched_edition_over_work_title(self):
"""Searching a translated title shouldn't surface the work's original title.
Open Library groups every edition (including translations) under one work
record, and reports the work's own `title` - usually the original-language
edition - regardless of which translation's text actually matched the query.
Querying "Der Weg der Könige" matches the German edition of Brandon
Sanderson's "The Way of Kings" by title, but the work-level title still says
"The Way of Kings". The nested `editions` block carries the edition whose own
fields satisfied the query, so that edition's title/isbn/cover/language
should win - otherwise both the displayed result and the downstream release
search (which uses `search_title`, see search_plan._pick_search_title) stay
in English even though the user searched for the German edition by name.
"""
provider = OpenLibraryProvider()
result = provider._parse_search_doc(
{
"key": "/works/OL15358691W",
"title": "The Way of Kings",
"author_name": ["Brandon Sanderson"],
"isbn": ["9780765326355", "0765326353"],
"cover_i": 14658316,
"language": ["eng"],
"editions": {
"docs": [
{
"key": "/books/OL32771812M",
"title": "Der Weg der Könige",
"cover_i": 14658425,
"language": ["ger"],
"isbn": ["9783453317109", "3453317106"],
"publish_year": [2015],
}
]
},
"first_publish_year": 2010,
}
)
assert result is not None
assert result.title == "Der Weg der Könige"
assert result.publish_year == 2015
assert result.search_title == "Der Weg der Könige"
assert result.language == "ger"
assert result.isbn_13 == "9783453317109"
assert result.isbn_10 == "3453317106"
assert result.cover_url == "https://covers.openlibrary.org/b/id/14658425-L.jpg"
def test_keeps_work_year_when_matched_edition_has_none(self):
provider = OpenLibraryProvider()
result = provider._parse_search_doc(
{
"key": "/works/OL15358691W",
"title": "The Way of Kings",
"author_name": ["Brandon Sanderson"],
"first_publish_year": 2010,
"editions": {
"docs": [{"key": "/books/OL32771812M", "title": "Der Weg der Könige"}]
},
}
)
assert result is not None
assert result.title == "Der Weg der Könige"
assert result.publish_year == 2010
def test_falls_back_to_work_title_without_a_matched_edition(self):
provider = OpenLibraryProvider()
result = provider._parse_search_doc(
{
"key": "/works/OL15358691W",
"title": "The Way of Kings",
"author_name": ["Brandon Sanderson"],
"isbn": ["9780765326355"],
"cover_i": 14658316,
"language": ["eng"],
}
)
assert result is not None
assert result.title == "The Way of Kings"
assert result.search_title is None
assert result.language == "eng"
def test_ignores_matched_edition_identical_to_work_title(self):
"""An edition that happens to share the work's title adds nothing new."""
provider = OpenLibraryProvider()
result = provider._parse_search_doc(
{
"key": "/works/OL15358691W",
"title": "The Way of Kings",
"author_name": ["Brandon Sanderson"],
"isbn": ["9780765326355"],
"cover_i": 14658316,
"language": ["eng"],
"editions": {
"docs": [
{
"key": "/books/OL1M",
"title": "The Way of Kings",
"language": ["eng"],
}
]
},
}
)
assert result is not None
assert result.search_title is None
assert result.isbn_13 == "9780765326355"