From 8ee962bf37b59310f5094be842ffcaa54c863595 Mon Sep 17 00:00:00 2001 From: Hiren Date: Wed, 25 Mar 2026 13:08:12 -0400 Subject: [PATCH 1/3] fix: stabilize test_text_query_word_weights with structural assertions Replace exact-string assertion (sensitive to non-deterministic token ordering) with component-based checks that verify: - Description clause is properly delimited - Weighted terms appear inside the clause - alpha occurs twice (matching input frequency) - Unweighted terms are present - Post-query modifiers (SCORER, WITHSCORES, DIALECT, LIMIT) follow Addresses review feedback on #524. --- tests/unit/test_query_types.py | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/tests/unit/test_query_types.py b/tests/unit/test_query_types.py index fa7ad69a..3f5ca5c6 100644 --- a/tests/unit/test_query_types.py +++ b/tests/unit/test_query_types.py @@ -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( @@ -344,10 +343,33 @@ 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 with structural guarantees, + # not exact token ordering (which is non-deterministic). + query_str = str(query) + + # Description clause is properly delimited + assert "@description:(" in query_str + desc_start = query_str.index("@description:(") + desc_close = query_str.index(")", desc_start) + desc_clause = query_str[desc_start + len("@description:(") : desc_close] + + # Weighted terms appear inside the description clause + assert "alpha=>{$weight:2}" in desc_clause + assert "delta=>{$weight:0.555}" in desc_clause + + # alpha appears twice (once per occurrence in input text) — count + assert desc_clause.count("alpha") == 2 + + # Unweighted terms are present + for term in ["query", "string", "bravo", "tango"]: + assert term in desc_clause + + # Post-query modifiers follow after the closing paren + suffix = query_str[desc_close + 1 :] + assert suffix.lstrip().startswith("SCORER BM25STD") + assert "WITHSCORES" in suffix + assert "DIALECT 2" in suffix + assert "LIMIT 0 10" in suffix # raise an error if weights are not positive floats with pytest.raises(ValueError): From 517c61fa59f5235f707f209bf61318560150ca60 Mon Sep 17 00:00:00 2001 From: Hiren Date: Wed, 25 Mar 2026 13:20:06 -0400 Subject: [PATCH 2/3] fix: strengthen test assertions per Copilot review - Verify both alpha occurrences are weighted (no unweighted alpha tokens) - Enforce modifier ordering: SCORER <= WITHSCORES <= DIALECT <= LIMIT --- tests/unit/test_query_types.py | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/tests/unit/test_query_types.py b/tests/unit/test_query_types.py index 3f5ca5c6..0d86bfff 100644 --- a/tests/unit/test_query_types.py +++ b/tests/unit/test_query_types.py @@ -354,22 +354,32 @@ def test_text_query_word_weights(): desc_clause = query_str[desc_start + len("@description:(") : desc_close] # Weighted terms appear inside the description clause - assert "alpha=>{$weight:2}" in desc_clause assert "delta=>{$weight:0.555}" in desc_clause - # alpha appears twice (once per occurrence in input text) — count - assert desc_clause.count("alpha") == 2 + # alpha appears twice and both occurrences are weighted + alpha_weighted = "alpha=>{$weight:2}" + assert desc_clause.count(alpha_weighted) == 2 + # Ensure no unweighted 'alpha' tokens slipped through + idx = 0 + while True: + idx = desc_clause.find("alpha", idx) + if idx == -1: + break + assert desc_clause.startswith(alpha_weighted, idx) + idx += len("alpha") # Unweighted terms are present for term in ["query", "string", "bravo", "tango"]: assert term in desc_clause - # Post-query modifiers follow after the closing paren - suffix = query_str[desc_close + 1 :] - assert suffix.lstrip().startswith("SCORER BM25STD") - assert "WITHSCORES" in suffix - assert "DIALECT 2" in suffix - assert "LIMIT 0 10" in suffix + # Post-query modifiers follow after the closing paren in expected order + suffix_stripped = suffix.lstrip() + assert suffix_stripped.startswith("SCORER BM25STD") + scorer_idx = suffix_stripped.index("SCORER BM25STD") + withscores_idx = suffix_stripped.index("WITHSCORES") + dialect_idx = suffix_stripped.index("DIALECT 2") + limit_idx = suffix_stripped.index("LIMIT 0 10") + assert scorer_idx <= withscores_idx <= dialect_idx <= limit_idx # raise an error if weights are not positive floats with pytest.raises(ValueError): From 96805235c881f3af0ad8287f430a8c3c20e5d8e4 Mon Sep 17 00:00:00 2001 From: Hiren Date: Wed, 25 Mar 2026 13:37:53 -0400 Subject: [PATCH 3/3] fix: define missing suffix variable in test --- tests/unit/test_query_types.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/unit/test_query_types.py b/tests/unit/test_query_types.py index 0d86bfff..a270951f 100644 --- a/tests/unit/test_query_types.py +++ b/tests/unit/test_query_types.py @@ -373,6 +373,7 @@ def test_text_query_word_weights(): assert term in desc_clause # Post-query modifiers follow after the closing paren in expected order + suffix = query_str[desc_close + 1 :] suffix_stripped = suffix.lstrip() assert suffix_stripped.startswith("SCORER BM25STD") scorer_idx = suffix_stripped.index("SCORER BM25STD")