Skip to content

Commit 196a91b

Browse files
authored
test: Add edge case tests for tool result conversion and Redis filter expressions (#4043)
Signed-off-by: Alex Klimenko <[email protected]>
1 parent a1b7d5e commit 196a91b

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed

spring-ai-model/src/test/java/org/springframework/ai/tool/execution/DefaultToolCallResultConverterTests.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,37 @@ void convertImageShouldReturnBase64Image() throws IOException {
123123
assertThat(imgRes.getRGB(0, 0)).isEqualTo(img.getRGB(0, 0));
124124
}
125125

126+
@Test
127+
void convertEmptyCollectionsShouldReturnEmptyJson() {
128+
assertThat(this.converter.convert(List.of(), List.class)).isEqualTo("[]");
129+
assertThat(this.converter.convert(Map.of(), Map.class)).isEqualTo("{}");
130+
assertThat(this.converter.convert(new String[0], String[].class)).isEqualTo("[]");
131+
}
132+
133+
@Test
134+
void convertRecordReturnTypeShouldReturnJson() {
135+
TestRecord record = new TestRecord("recordName", 1);
136+
String result = this.converter.convert(record, TestRecord.class);
137+
138+
assertThat(result).containsIgnoringWhitespaces("\"recordName\"");
139+
assertThat(result).containsIgnoringWhitespaces("1");
140+
}
141+
142+
@Test
143+
void convertSpecialCharactersInStringsShouldEscapeJson() {
144+
String specialChars = "Test with \"quotes\", newlines\n, tabs\t, and backslashes\\";
145+
String result = this.converter.convert(specialChars, String.class);
146+
147+
// Should properly escape JSON special characters
148+
assertThat(result).contains("\\\"quotes\\\"");
149+
assertThat(result).contains("\\n");
150+
assertThat(result).contains("\\t");
151+
assertThat(result).contains("\\\\");
152+
}
153+
154+
record TestRecord(String name, int value) {
155+
}
156+
126157
record Base64Wrapper(MimeType mimeType, String data) {
127158
}
128159

vector-stores/spring-ai-redis-store/src/test/java/org/springframework/ai/vectorstore/redis/RedisFilterExpressionConverterTests.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,4 +129,46 @@ void testComplexIdentifiers() {
129129
assertThat(vectorExpr).isEqualTo("@'country 1 2 3':{BG}");
130130
}
131131

132+
@Test
133+
void testSpecialCharactersInValues() {
134+
// Test values with Redis special characters that need escaping
135+
String vectorExpr = converter(RedisVectorStore.MetadataField.tag("description"))
136+
.convertExpression(new Expression(EQ, new Key("description"), new Value("test@value{with}special|chars")));
137+
138+
// Should properly escape special Redis characters
139+
assertThat(vectorExpr).isEqualTo("@description:{test@value{with}special|chars}");
140+
}
141+
142+
@Test
143+
void testEmptyStringValues() {
144+
String vectorExpr = converter(RedisVectorStore.MetadataField.tag("status"))
145+
.convertExpression(new Expression(EQ, new Key("status"), new Value("")));
146+
147+
assertThat(vectorExpr).isEqualTo("@status:{}");
148+
}
149+
150+
@Test
151+
void testSingleItemInList() {
152+
String vectorExpr = converter(RedisVectorStore.MetadataField.tag("status"))
153+
.convertExpression(new Expression(IN, new Key("status"), new Value(List.of("active"))));
154+
155+
assertThat(vectorExpr).isEqualTo("@status:{active}");
156+
}
157+
158+
@Test
159+
void testWhitespaceInFieldNames() {
160+
String vectorExpr = converter(RedisVectorStore.MetadataField.tag("value with spaces"))
161+
.convertExpression(new Expression(EQ, new Key("\"value with spaces\""), new Value("test")));
162+
163+
assertThat(vectorExpr).isEqualTo("@\"value with spaces\":{test}");
164+
}
165+
166+
@Test
167+
void testNestedQuotedFieldNames() {
168+
String vectorExpr = converter(RedisVectorStore.MetadataField.tag("value \"with\" quotes"))
169+
.convertExpression(new Expression(EQ, new Key("\"value \\\"with\\\" quotes\""), new Value("test")));
170+
171+
assertThat(vectorExpr).isEqualTo("@\"value \\\"with\\\" quotes\":{test}");
172+
}
173+
132174
}

0 commit comments

Comments
 (0)