Skip to content

Commit a15b495

Browse files
committed
test: Add edge case tests for MimeTypeDetector and CoherenceFilterExpressionConverter
Signed-off-by: Alex Klimenko <[email protected]>
1 parent e0ccc13 commit a15b495

File tree

2 files changed

+99
-1
lines changed

2 files changed

+99
-1
lines changed

models/spring-ai-google-genai/src/test/java/org/springframework/ai/google/genai/MimeTypeDetectorTests.java

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,13 @@
2626
import org.junit.jupiter.params.provider.Arguments;
2727
import org.junit.jupiter.params.provider.MethodSource;
2828

29-
import org.springframework.ai.google.genai.MimeTypeDetector;
29+
import org.junit.jupiter.params.provider.ValueSource;
3030
import org.springframework.core.io.PathResource;
3131
import org.springframework.util.MimeType;
3232

3333
import static org.assertj.core.api.Assertions.assertThat;
34+
import static org.assertj.core.api.AssertionsForClassTypes.assertThatCode;
35+
import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy;
3436
import static org.springframework.ai.google.genai.MimeTypeDetector.GEMINI_MIME_TYPES;
3537

3638
/**
@@ -90,4 +92,49 @@ void getMimeTypeByString(String extension, MimeType expectedMimeType) {
9092
assertThat(mimeType).isEqualTo(expectedMimeType);
9193
}
9294

95+
@ParameterizedTest
96+
@ValueSource(strings = { " ", "\t", "\n" })
97+
void getMimeTypeByStringWithInvalidInputShouldThrowException(String invalidPath) {
98+
assertThatThrownBy(() -> MimeTypeDetector.getMimeType(invalidPath)).isInstanceOf(IllegalArgumentException.class)
99+
.hasMessageContaining("Unable to detect the MIME type");
100+
}
101+
102+
@ParameterizedTest
103+
@ValueSource(strings = { "JPG", "PNG", "GIF" })
104+
void getMimeTypeByStringWithUppercaseExtensionsShouldWork(String uppercaseExt) {
105+
String upperFileName = "test." + uppercaseExt;
106+
String lowerFileName = "test." + uppercaseExt.toLowerCase();
107+
108+
// Should throw for uppercase (not in map) but work for lowercase
109+
assertThatThrownBy(() -> MimeTypeDetector.getMimeType(upperFileName))
110+
.isInstanceOf(IllegalArgumentException.class);
111+
112+
// Lowercase should work if it's a supported extension
113+
if (GEMINI_MIME_TYPES.containsKey(uppercaseExt.toLowerCase())) {
114+
assertThatCode(() -> MimeTypeDetector.getMimeType(lowerFileName)).doesNotThrowAnyException();
115+
}
116+
}
117+
118+
@ParameterizedTest
119+
@ValueSource(strings = { "test.jpg", "test.png", "test.gif" })
120+
void getMimeTypeSupportedFileAcrossDifferentMethodsShouldBeConsistent(String fileName) {
121+
MimeType stringResult = MimeTypeDetector.getMimeType(fileName);
122+
MimeType fileResult = MimeTypeDetector.getMimeType(new File(fileName));
123+
MimeType pathResult = MimeTypeDetector.getMimeType(Path.of(fileName));
124+
125+
// All methods should return the same result for supported extensions
126+
assertThat(stringResult).isEqualTo(fileResult);
127+
assertThat(stringResult).isEqualTo(pathResult);
128+
}
129+
130+
@ParameterizedTest
131+
@ValueSource(strings = { "https://example.com/documents/file.pdf", "https://example.com/data/file.json",
132+
"https://example.com/files/document.txt" })
133+
void getMimeTypeByURIWithUnsupportedExtensionsShouldThrowException(String url) {
134+
URI uri = URI.create(url);
135+
136+
assertThatThrownBy(() -> MimeTypeDetector.getMimeType(uri)).isInstanceOf(IllegalArgumentException.class)
137+
.hasMessageContaining("Unable to detect the MIME type");
138+
}
139+
93140
}

vector-stores/spring-ai-coherence-store/src/test/java/org/springframework/ai/vectorstore/coherence/CoherenceFilterExpressionConverterTests.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,55 @@ private ValueExtractor extractor(String property) {
9090
return new ChainedExtractor(new UniversalExtractor<>("metadata"), new UniversalExtractor<>(property));
9191
}
9292

93+
@Test
94+
void testBooleanValues() {
95+
final Expression e1 = new FilterExpressionTextParser().parse("active == true");
96+
final Expression e2 = new FilterExpressionTextParser().parse("deleted == false");
97+
98+
assertThat(CONVERTER.convert(e1)).isEqualTo(Filters.equal(extractor("active"), true));
99+
assertThat(CONVERTER.convert(e2)).isEqualTo(Filters.equal(extractor("deleted"), false));
100+
}
101+
102+
@Test
103+
void testNumericValues() {
104+
final Expression intExpr = new FilterExpressionTextParser().parse("count == 42");
105+
final Expression doubleExpr = new FilterExpressionTextParser().parse("rating == 4.5");
106+
final Expression negativeExpr = new FilterExpressionTextParser().parse("temperature == -10");
107+
108+
assertThat(CONVERTER.convert(intExpr)).isEqualTo(Filters.equal(extractor("count"), 42));
109+
assertThat(CONVERTER.convert(doubleExpr)).isEqualTo(Filters.equal(extractor("rating"), 4.5));
110+
assertThat(CONVERTER.convert(negativeExpr)).isEqualTo(Filters.equal(extractor("temperature"), -10));
111+
}
112+
113+
@Test
114+
void testStringWithSpecialCharacters() {
115+
final Expression e = new FilterExpressionTextParser().parse("description == 'This has \"quotes\" and spaces'");
116+
assertThat(CONVERTER.convert(e))
117+
.isEqualTo(Filters.equal(extractor("description"), "This has \"quotes\" and spaces"));
118+
}
119+
120+
@Test
121+
void testEmptyStringValue() {
122+
final Expression e = new FilterExpressionTextParser().parse("comment == ''");
123+
assertThat(CONVERTER.convert(e)).isEqualTo(Filters.equal(extractor("comment"), ""));
124+
}
125+
126+
@Test
127+
void testINWithMixedTypes() {
128+
final Expression e = new FilterExpressionTextParser().parse("status in [1, 'active', true]");
129+
assertThat(CONVERTER.convert(e)).isEqualTo(Filters.in(extractor("status"), 1, "active", true));
130+
}
131+
132+
@Test
133+
void testINWithSingleValue() {
134+
final Expression e = new FilterExpressionTextParser().parse("category in ['category1']");
135+
assertThat(CONVERTER.convert(e)).isEqualTo(Filters.in(extractor("category"), "category1"));
136+
}
137+
138+
@Test
139+
void testNINWithSingleValue() {
140+
final Expression e = new FilterExpressionTextParser().parse("category nin ['inactive']");
141+
assertThat(CONVERTER.convert(e)).isEqualTo(Filters.not(Filters.in(extractor("category"), "inactive")));
142+
}
143+
93144
}

0 commit comments

Comments
 (0)