Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/opds_server/db/access.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@
CALIBRE_DATETIME_FALLBACK = datetime(1970, 1, 1, tzinfo=UTC)


def unicode_casefold(value: str | None) -> str:
"""Return text folded for Unicode-aware caseless comparisons."""
return value.casefold() if value is not None else ""


def parse_calibre_datetime(value: object) -> datetime:
"""Parse a Calibre timestamp, falling back to a stable Atom-safe value."""
if not isinstance(value, str) or not value.strip():
Expand Down Expand Up @@ -79,6 +84,10 @@ async def connect_db(config: Config) -> AsyncIterator[aiosqlite.Connection]:
get_db_uri(config),
uri=True,
)
# SQLite's built-in NOCASE collation only folds ASCII characters.
await conn.create_function(
"unicode_casefold", 1, unicode_casefold, deterministic=True
)
try:
yield conn
finally:
Expand Down Expand Up @@ -350,7 +359,7 @@ async def search_books(
sql = """
SELECT id, title, last_modified
FROM books
WHERE title LIKE ? COLLATE NOCASE
WHERE unicode_casefold(title) LIKE unicode_casefold(?)
ORDER BY sort, id
"""

Expand Down
39 changes: 38 additions & 1 deletion tests/test_catalog_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from pydantic import ValidationError

from opds_server.core.config import Config
from opds_server.db.access import parse_calibre_datetime
from opds_server.db.access import parse_calibre_datetime, unicode_casefold

ATOM = "http://www.w3.org/2005/Atom"
OPENSEARCH = "http://a9.com/-/spec/opensearch/1.1/"
Expand Down Expand Up @@ -85,6 +85,15 @@ def test_calibre_datetime_parsing_is_stable(value, expected):
assert parse_calibre_datetime(value) == expected


@pytest.mark.parametrize(
("value", "expected"),
[("Механика", "механика"), ("МЕХАНИКА", "механика"), (None, "")],
)
def test_unicode_casefold_normalizes_cyrillic_case(value, expected):
"""Fold Cyrillic case consistently and safely handle SQLite nulls."""
assert unicode_casefold(value) == expected


@pytest.mark.parametrize("last_modified", [None, "", "not-a-date"])
def test_books_with_invalid_dates_remain_in_feeds(catalog_client, last_modified):
"""Keep usable books visible when their Calibre timestamp is invalid."""
Expand Down Expand Up @@ -261,6 +270,34 @@ def test_search_special_queries(catalog_client, query, page, expected_title):
assert parse_qs(urlsplit(links(feed, "self")[0].get("href")).query)["q"] == [query]


@pytest.mark.parametrize("query", ["кириллица поиск", "КИРИЛЛИЦА ПОИСК"])
def test_search_is_case_insensitive_for_cyrillic(catalog_client, query):
"""Find a mixed-case Cyrillic title regardless of the query's case."""
library, client = catalog_client
with sqlite3.connect(library / "metadata.db") as connection:
connection.execute(
"UPDATE books SET title = ? WHERE id = 2", ("Кириллица Поиск",)
)

feed = parse_atom(client.get("/opds/search", params={"q": query}))

assert [entry.findtext("atom:title", namespaces=NS) for entry in entries(feed)] == [
"Кириллица Поиск"
]


@pytest.mark.parametrize("query", ["Practical", "practical", "PRACTICAL"])
def test_search_remains_case_insensitive_for_latin(catalog_client, query):
"""Preserve the existing ASCII case-insensitive search behavior."""
_, client = catalog_client

feed = parse_atom(client.get("/opds/search", params={"q": query}))

assert [entry.findtext("atom:title", namespaces=NS) for entry in entries(feed)] == [
"A <Practical> Book"
]


def test_empty_catalog_returns_valid_empty_feeds(client_factory):
"""Return well-formed feeds without entries for a valid empty database."""
_, client = client_factory(populated=False)
Expand Down
Loading