22
33"""Regression: negative numeric literals in vector-store filter lambdas (#14571)."""
44
5- from ast import Constant , USub , UnaryOp , parse
6-
5+ from ast import Constant , UnaryOp , dump , parse
76from unittest .mock import MagicMock
87
98import pytest
109from chromadb .api import ClientAPI
10+ from qdrant_client .async_qdrant_client import AsyncQdrantClient
1111from qdrant_client .models import FieldCondition , Range
1212
1313from semantic_kernel .connectors .chroma import ChromaCollection
@@ -30,46 +30,57 @@ def numeric_definition() -> VectorStoreCollectionDefinition:
3030 )
3131
3232
33- def test_fold_unary_numeric_constants_negates_literal ():
34- tree = parse ("lambda x: x.price > -5" )
33+ @pytest .mark .parametrize ("literal,expected" , [("-5" , - 5 ), ("-5.5" , - 5.5 )])
34+ def test_fold_unary_numeric_constants_negates_literal (literal , expected ):
35+ tree = parse (f"lambda x: x.price > { literal } " )
3536 folded = _FoldUnaryNumericConstants ().visit (tree )
3637 # Body of the lambda should now compare against Constant(-5), not UnaryOp(USub, ...).
3738 compare = folded .body [0 ].value .body
3839 right = compare .comparators [0 ]
3940 assert isinstance (right , Constant )
40- assert right .value == - 5
41+ assert right .value == expected
4142 assert not isinstance (right , UnaryOp )
4243
4344
44- def test_chroma_build_filter_accepts_negative_literal (numeric_definition ):
45+ @pytest .mark .parametrize ("expression" , ["-True" , "-False" , "+True" , "+False" , "+5" , "+5.5" , "~5" , "-x.price" ])
46+ def test_fold_unary_numeric_constants_preserves_other_unary_expressions (expression ):
47+ tree = parse (f"lambda x: x.price > { expression } " )
48+ original = dump (tree )
49+ folded = _FoldUnaryNumericConstants ().visit (tree )
50+ assert dump (folded ) == original
51+
52+
53+ @pytest .mark .parametrize ("literal,expected" , [("-5" , - 5 ), ("-5.5" , - 5.5 )])
54+ def test_chroma_build_filter_accepts_negative_literal (numeric_definition , literal , expected ):
4555 collection = ChromaCollection (
4656 collection_name = "temps" ,
4757 record_type = dict ,
4858 definition = numeric_definition ,
4959 client = MagicMock (spec = ClientAPI ),
5060 )
51- parsed = collection ._build_filter ("lambda x: x.price > -5 " )
52- assert parsed == {"price" : {"$gt" : - 5 }}
61+ parsed = collection ._build_filter (f "lambda x: x.price > { literal } " )
62+ assert parsed == {"price" : {"$gt" : expected }}
5363
5464
55- def test_chroma_build_filter_accepts_positive_unary_literal (numeric_definition ):
65+ @pytest .mark .parametrize ("expression" , ["-True" , "-False" , "+True" , "+False" , "+5" , "+5.5" , "~5" , "-x.price" ])
66+ def test_chroma_build_filter_rejects_other_unary_expressions (numeric_definition , expression ):
5667 collection = ChromaCollection (
5768 collection_name = "temps" ,
5869 record_type = dict ,
5970 definition = numeric_definition ,
6071 client = MagicMock (spec = ClientAPI ),
6172 )
62- parsed = collection . _build_filter ( "lambda x: x.price > +5" )
63- assert parsed == { "price" : { "$gt" : 5 }}
73+ with pytest . raises ( NotImplementedError , match = "Unary" ):
74+ collection . _build_filter ( f"lambda x: x.price > { expression } " )
6475
6576
66- def test_qdrant_build_filter_accepts_negative_literal ( numeric_definition , monkeypatch ):
67- monkeypatch . setenv ( "QDRANT_LOCATION" , "http://localhost:6333" )
77+ @ pytest . mark . parametrize ( "literal,expected" , [( "-5" , - 5 ), ( "-5.5" , - 5.5 )])
78+ def test_qdrant_build_filter_accepts_negative_literal ( numeric_definition , literal , expected ):
6879 collection = QdrantCollection (
6980 record_type = dict ,
7081 collection_name = "temps" ,
7182 definition = numeric_definition ,
72- env_file_path = "test.env" ,
83+ client = MagicMock ( spec = AsyncQdrantClient ) ,
7384 )
74- parsed = collection ._build_filter ("lambda x: x.price > -5 " )
75- assert parsed == FieldCondition (key = "price" , range = Range (gt = - 5 ))
85+ parsed = collection ._build_filter (f "lambda x: x.price > { literal } " )
86+ assert parsed == FieldCondition (key = "price" , range = Range (gt = expected ))
0 commit comments