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 <noreply@anthropic.com>
This commit is contained in:
stumpylog
2026-08-20 11:56:42 -07:00
co-authored by Claude Opus 5
parent 42c4f648a4
commit 7d61c3769f
+38 -8
View File
@@ -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: