Skip to content

Commit 0c7295f

Browse files
authored
test: Add comprehensive test coverage for MariaDBFilterExpressionConverter (#3943)
Added new test cases covering edge cases and complex scenarios Refactored existing tests to use neutral domain terminology Improved test coverage for: - Empty/single-item lists - Null values and empty strings - Nested JSON paths and array indexing - Mixed data types (boolean, numeric, string) - Complex nested groupings - Special characters and numeric strings Details - Enhanced robustness of filter expression conversion - Better validation of edge cases - Domain-neutral test data for broader applicability Signed-off-by: Alex Klimenko <[email protected]>
1 parent b8d8235 commit 0c7295f

File tree

1 file changed

+103
-0
lines changed

1 file changed

+103
-0
lines changed

vector-stores/spring-ai-mariadb-store/src/test/java/org/springframework/ai/vectorstore/mariadb/MariaDBFilterExpressionConverterTests.java

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,4 +123,107 @@ public void testComplexIdentifiers() {
123123
assertThat(vectorExpr).isEqualTo("JSON_VALUE(metadata, '$.\"country 1 2 3\"') = 'BG'");
124124
}
125125

126+
@Test
127+
public void testEmptyList() {
128+
// category IN []
129+
String vectorExpr = this.converter
130+
.convertExpression(new Expression(IN, new Key("category"), new Value(List.of())));
131+
assertThat(vectorExpr).isEqualTo("JSON_VALUE(metadata, '$.category') IN ()");
132+
}
133+
134+
@Test
135+
public void testSingleItemList() {
136+
// status IN ["active"]
137+
String vectorExpr = this.converter
138+
.convertExpression(new Expression(IN, new Key("status"), new Value(List.of("active"))));
139+
assertThat(vectorExpr).isEqualTo("JSON_VALUE(metadata, '$.status') IN ('active')");
140+
}
141+
142+
@Test
143+
public void testNullValue() {
144+
// description == null
145+
String vectorExpr = this.converter
146+
.convertExpression(new Expression(EQ, new Key("description"), new Value(null)));
147+
assertThat(vectorExpr).isEqualTo("JSON_VALUE(metadata, '$.description') = null");
148+
}
149+
150+
@Test
151+
public void testNestedJsonPath() {
152+
// entity.profile.name == "EntityA"
153+
String vectorExpr = this.converter
154+
.convertExpression(new Expression(EQ, new Key("entity.profile.name"), new Value("EntityA")));
155+
assertThat(vectorExpr).isEqualTo("JSON_VALUE(metadata, '$.entity.profile.name') = 'EntityA'");
156+
}
157+
158+
@Test
159+
public void testNumericStringValue() {
160+
// id == "1"
161+
String vectorExpr = this.converter.convertExpression(new Expression(EQ, new Key("id"), new Value("1")));
162+
assertThat(vectorExpr).isEqualTo("JSON_VALUE(metadata, '$.id') = '1'");
163+
}
164+
165+
@Test
166+
public void testZeroValue() {
167+
// count == 0
168+
String vectorExpr = this.converter.convertExpression(new Expression(EQ, new Key("count"), new Value(0)));
169+
assertThat(vectorExpr).isEqualTo("JSON_VALUE(metadata, '$.count') = 0");
170+
}
171+
172+
@Test
173+
public void testComplexNestedGroups() {
174+
// ((fieldA >= 100 AND fieldB == "X1") OR (fieldA >= 50 AND fieldB == "Y2")) AND
175+
// fieldC != "inactive"
176+
String vectorExpr = this.converter.convertExpression(new Expression(AND,
177+
new Group(new Expression(OR,
178+
new Group(new Expression(AND, new Expression(GTE, new Key("fieldA"), new Value(100)),
179+
new Expression(EQ, new Key("fieldB"), new Value("X1")))),
180+
new Group(new Expression(AND, new Expression(GTE, new Key("fieldA"), new Value(50)),
181+
new Expression(EQ, new Key("fieldB"), new Value("Y2")))))),
182+
new Expression(NE, new Key("fieldC"), new Value("inactive"))));
183+
184+
assertThat(vectorExpr)
185+
.isEqualTo("((JSON_VALUE(metadata, '$.fieldA') >= 100 AND JSON_VALUE(metadata, '$.fieldB') = 'X1') OR "
186+
+ "(JSON_VALUE(metadata, '$.fieldA') >= 50 AND JSON_VALUE(metadata, '$.fieldB') = 'Y2')) AND "
187+
+ "JSON_VALUE(metadata, '$.fieldC') != 'inactive'");
188+
}
189+
190+
@Test
191+
public void testMixedDataTypes() {
192+
// active == true AND score >= 1.5 AND tags IN ["featured", "premium"] AND
193+
// version == 1
194+
String vectorExpr = this.converter.convertExpression(new Expression(AND,
195+
new Expression(AND,
196+
new Expression(AND, new Expression(EQ, new Key("active"), new Value(true)),
197+
new Expression(GTE, new Key("score"), new Value(1.5))),
198+
new Expression(IN, new Key("tags"), new Value(List.of("featured", "premium")))),
199+
new Expression(EQ, new Key("version"), new Value(1))));
200+
201+
assertThat(vectorExpr)
202+
.isEqualTo("JSON_VALUE(metadata, '$.active') = true AND JSON_VALUE(metadata, '$.score') >= 1.5 AND "
203+
+ "JSON_VALUE(metadata, '$.tags') IN ('featured','premium') AND JSON_VALUE(metadata, '$.version') = 1");
204+
}
205+
206+
@Test
207+
public void testNinWithMixedTypes() {
208+
// status NIN ["A", "B", "C"]
209+
String vectorExpr = this.converter
210+
.convertExpression(new Expression(NIN, new Key("status"), new Value(List.of("A", "B", "C"))));
211+
assertThat(vectorExpr).isEqualTo("JSON_VALUE(metadata, '$.status') NOT IN ('A','B','C')");
212+
}
213+
214+
@Test
215+
public void testEmptyStringValue() {
216+
// description != ""
217+
String vectorExpr = this.converter.convertExpression(new Expression(NE, new Key("description"), new Value("")));
218+
assertThat(vectorExpr).isEqualTo("JSON_VALUE(metadata, '$.description') != ''");
219+
}
220+
221+
@Test
222+
public void testArrayIndexAccess() {
223+
// tags[0] == "important"
224+
String vectorExpr = this.converter
225+
.convertExpression(new Expression(EQ, new Key("tags[0]"), new Value("important")));
226+
assertThat(vectorExpr).isEqualTo("JSON_VALUE(metadata, '$.tags[0]') = 'important'");
227+
}
228+
126229
}

0 commit comments

Comments
 (0)