From 7d61c3769f3bb596027c3ca51a3f757684e073dd Mon Sep 17 00:00:00 2001 From: stumpylog <797416+stumpylog@users.noreply.github.com> Date: Thu, 20 Aug 2026 11:56:42 -0700 Subject: [PATCH] test(search): assert the empty highlight query matches nothing test_empty_query_returns_empty_query and test_all_operators_returns_empty_query asserted isinstance(result, tantivy.Query), which parse_simple_text_highlight_query cannot fail to satisfy: it either returns a Query or raises. Replacing its `return tantivy.Query.empty_query()` with `all_query()` left both green, so the contract they were named for -- highlight nothing, rather than highlight every document -- was unpinned. They now count hits against an index holding one document. A third test asserts a real token matches that document, so an empty corpus cannot make the other two pass for a match-everything query. Under the empty_query -> all_query mutation both new tests fail; before this change the mutation killed nothing. Co-Authored-By: Claude Opus 5 --- src/documents/tests/search/test_query.py | 46 +++++++++++++++++++----- 1 file changed, 38 insertions(+), 8 deletions(-) diff --git a/src/documents/tests/search/test_query.py b/src/documents/tests/search/test_query.py index caa35f365..8b2962542 100644 --- a/src/documents/tests/search/test_query.py +++ b/src/documents/tests/search/test_query.py @@ -34,6 +34,27 @@ def query_index() -> tantivy.Index: return idx +@pytest.fixture(scope="module") +def populated_index() -> tantivy.Index: + """An index holding one document, so a query matching nothing is + distinguishable from one matching everything.""" + idx = tantivy.Index(build_schema(), path=None) + register_tokenizers(idx, "") + writer = idx.writer() + doc = tantivy.Document() + doc.add_unsigned("id", 1) + doc.add_text("content", "needle in indexed content") + writer.add_document(doc) + writer.commit() + idx.reload() + return idx + + +def _highlight_hit_count(index: tantivy.Index, raw_query: str) -> int: + query = parse_simple_text_highlight_query(index, raw_query) + return index.searcher().search(query, limit=1).count + + class TestParseUserQuery: """parse_user_query runs the full preprocessing pipeline.""" @@ -166,16 +187,25 @@ class TestParseSimpleTextHighlightQuery: tantivy.Query, ) - def test_empty_query_returns_empty_query(self, query_index: tantivy.Index) -> None: - result = parse_simple_text_highlight_query(query_index, "") - assert isinstance(result, tantivy.Query) - - def test_all_operators_returns_empty_query( + def test_a_real_token_matches_the_corpus( self, - query_index: tantivy.Index, + populated_index: tantivy.Index, ) -> None: - result = parse_simple_text_highlight_query(query_index, "- +") - assert isinstance(result, tantivy.Query) + """Without this, an empty corpus would make the two assertions below + pass for a query that matches every document.""" + assert _highlight_hit_count(populated_index, "needle") == 1 + + def test_empty_query_matches_no_document( + self, + populated_index: tantivy.Index, + ) -> None: + assert _highlight_hit_count(populated_index, "") == 0 + + def test_all_operators_query_matches_no_document( + self, + populated_index: tantivy.Index, + ) -> None: + assert _highlight_hit_count(populated_index, "- +") == 0 class TestPermissionFilter: