Skip to content
Open
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
18 changes: 13 additions & 5 deletions tests/unit/test_query_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,6 @@ def test_text_query_with_string_filter():
assert "AND" not in query_string_wildcard


@pytest.mark.skip("Test is flaking")
def test_text_query_word_weights():
# verify word weights get added into the raw Redis query syntax
query = TextQuery(
Expand All @@ -344,10 +343,19 @@ def test_text_query_word_weights():
text_weights={"alpha": 2, "delta": 0.555, "gamma": 0.95},
)

assert (
str(query)
== "@description:(query | string | alpha=>{$weight:2} | bravo | delta=>{$weight:0.555} | tango | alpha=>{$weight:2}) SCORER BM25STD WITHSCORES DIALECT 2 LIMIT 0 10"
)
# Check query components without relying on exact token ordering
query_str = str(query)
assert "@description:(" in query_str
assert "alpha=>{$weight:2}" in query_str
assert "delta=>{$weight:0.555}" in query_str
assert "query" in query_str
assert "string" in query_str
assert "bravo" in query_str
assert "tango" in query_str
Comment on lines +346 to +354
Copy link

Copilot AI Mar 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These substring assertions are much less strict than the previous exact-match and could pass even if key semantics regress (e.g., only one of the two alpha occurrences is weighted, or the @description:(...) clause is malformed). Consider adding stable-but-strong checks like counting weighted occurrences (e.g., alpha appears twice), and asserting structure (e.g., startswith @description:( and that the text clause closes before SCORER).

Suggested change
# Check query components without relying on exact token ordering
query_str = str(query)
assert "@description:(" in query_str
assert "alpha=>{$weight:2}" in query_str
assert "delta=>{$weight:0.555}" in query_str
assert "query" in query_str
assert "string" in query_str
assert "bravo" in query_str
assert "tango" in query_str
# Check query components without relying on exact token ordering,
# but with stronger structural guarantees.
query_str = str(query)
# The description clause should be present and properly delimited
assert "@description:(" in query_str
description_start = query_str.index("@description:(")
description_close = query_str.index(")", description_start)
scorer_pos = query_str.index("SCORER")
# Ensure the text clause closes before the SCORER section
assert description_close < scorer_pos
# The weighted tokens should appear the expected number of times
assert query_str.count("alpha=>{$weight:2}") == 2
assert query_str.count("delta=>{$weight:0.555}") == 1
# Ensure the main text tokens appear inside the description clause
description_clause = query_str[description_start:description_close]
assert "query" in description_clause
assert "string" in description_clause
assert "bravo" in description_clause
assert "tango" in description_clause
# Other expected query modifiers

Copilot uses AI. Check for mistakes.
assert "SCORER BM25STD" in query_str
assert "WITHSCORES" in query_str
assert "DIALECT 2" in query_str
assert "LIMIT 0 10" in query_str

# raise an error if weights are not positive floats
with pytest.raises(ValueError):
Expand Down