From c981fb26f7829a25b144522dcbcf870429f051b9 Mon Sep 17 00:00:00 2001 From: Trenton H <797416+stumpylog@users.noreply.github.com> Date: Tue, 31 Mar 2026 11:14:26 -0700 Subject: [PATCH] Adds no cover on some defensive error handling, cover a few other cases more directly --- src/documents/search/_backend.py | 10 +++++----- src/documents/tests/search/test_backend.py | 6 ++++++ src/documents/tests/search/test_query.py | 15 +++++++++++++++ 3 files changed, 26 insertions(+), 5 deletions(-) diff --git a/src/documents/search/_backend.py b/src/documents/search/_backend.py index 563940ff9..b97957f21 100644 --- a/src/documents/search/_backend.py +++ b/src/documents/search/_backend.py @@ -251,7 +251,7 @@ class TantivyBackend: Safe to call multiple times - subsequent calls are no-ops. """ if self._index is not None: - return + return # pragma: no cover if self._path is not None: self._index = open_or_rebuild_index(self._path) else: @@ -271,7 +271,7 @@ class TantivyBackend: def _ensure_open(self) -> None: """Ensure the index is open before operations.""" if self._index is None: - self.open() + self.open() # pragma: no cover def _build_tantivy_doc( self, @@ -559,7 +559,7 @@ class TantivyBackend: if notes_snippet: highlights["notes"] = str(notes_snippet) - except Exception: + except Exception: # pragma: no cover logger.debug("Failed to generate highlights for doc %s", doc_id) hits.append( @@ -796,7 +796,7 @@ class TantivyBackend: writer.add_document(doc) writer.commit() new_index.reload() - except BaseException: + except BaseException: # pragma: no cover # Restore old index on failure so the backend remains usable self._index = old_index self._schema = old_schema @@ -832,7 +832,7 @@ def get_backend() -> TantivyBackend: with _backend_lock: # Double-check after acquiring lock — another thread may have beaten us if _backend is not None and _backend_path == current_path: - return _backend + return _backend # pragma: no cover if _backend is not None: _backend.close() diff --git a/src/documents/tests/search/test_backend.py b/src/documents/tests/search/test_backend.py index 3c9a1124f..4211611a0 100644 --- a/src/documents/tests/search/test_backend.py +++ b/src/documents/tests/search/test_backend.py @@ -280,6 +280,12 @@ class TestMoreLikeThis: # private_doc is owned by other, so viewer cannot see it assert 53 not in returned_ids + def test_document_not_in_index_returns_empty(self, backend: TantivyBackend): + """more_like_this for a doc_id absent from the index must return empty results.""" + results = backend.more_like_this(doc_id=9999, user=None, page=1, page_size=10) + assert results.hits == [] + assert results.total == 0 + class TestSingleton: """Test get_backend() and reset_backend() singleton lifecycle.""" diff --git a/src/documents/tests/search/test_query.py b/src/documents/tests/search/test_query.py index 57a41c0e1..74a064dbb 100644 --- a/src/documents/tests/search/test_query.py +++ b/src/documents/tests/search/test_query.py @@ -11,6 +11,9 @@ import pytest import tantivy import time_machine +from documents.search._query import _date_only_range +from documents.search._query import _datetime_range +from documents.search._query import _rewrite_compact_date from documents.search._query import build_permission_filter from documents.search._query import normalize_query from documents.search._query import parse_user_query @@ -154,6 +157,10 @@ class TestCreatedDateField: assert lo == "2025-12-01T00:00:00Z" assert hi == "2026-01-01T00:00:00Z" + def test_unknown_keyword_raises(self) -> None: + with pytest.raises(ValueError, match="Unknown keyword"): + _date_only_range("bogus_keyword", UTC) + class TestDateTimeFields: """ @@ -258,6 +265,10 @@ class TestDateTimeFields: assert lo == "2025-12-01T00:00:00Z" assert hi == "2026-01-01T00:00:00Z" + def test_unknown_keyword_raises(self) -> None: + with pytest.raises(ValueError, match="Unknown keyword"): + _datetime_range("bogus_keyword", UTC) + class TestWhooshQueryRewriting: """All Whoosh query syntax variants must be rewritten to ISO 8601 before Tantivy parses them.""" @@ -363,6 +374,10 @@ class TestWhooshQueryRewriting: def test_8digit_invalid_date_passes_through_unchanged(self) -> None: assert rewrite_natural_date_keywords("added:20231340", UTC) == "added:20231340" + def test_compact_14digit_invalid_date_passes_through_unchanged(self) -> None: + # Month=13 makes datetime() raise ValueError; the token must be left as-is + assert _rewrite_compact_date("20231300120000") == "20231300120000" + class TestParseUserQuery: """parse_user_query runs the full preprocessing pipeline."""