Adds no cover on some defensive error handling, cover a few other cases more directly

This commit is contained in:
Trenton H
2026-03-31 11:14:26 -07:00
parent 9003bfdeea
commit c981fb26f7
3 changed files with 26 additions and 5 deletions
+5 -5
View File
@@ -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()
@@ -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."""
+15
View File
@@ -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."""