Skip to content

Commit 6189d79

Browse files
ngocnhan-tran1996ericbottard
authored andcommitted
Use assertThatExceptionOfType
Signed-off-by: Tran Ngoc Nhan <[email protected]>
1 parent 610ae5f commit 6189d79

File tree

4 files changed

+26
-43
lines changed

4 files changed

+26
-43
lines changed

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

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
import com.oracle.bedrock.testsupport.junit.TestLogsExtension;
4040
import com.tangosol.net.Coherence;
4141
import com.tangosol.net.Session;
42-
import org.junit.Assert;
4342
import org.junit.jupiter.api.Disabled;
4443
import org.junit.jupiter.api.Test;
4544
import org.junit.jupiter.api.extension.RegisterExtension;
@@ -64,6 +63,7 @@
6463
import org.springframework.util.CollectionUtils;
6564

6665
import static org.assertj.core.api.Assertions.assertThat;
66+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
6767

6868
@Disabled("Crashes on github actions run")
6969
public class CoherenceVectorStoreIT {
@@ -132,7 +132,7 @@ public void addAndSearch(CoherenceVectorStore.DistanceType distanceType, Coheren
132132
assertThat(resultDoc.getMetadata()).containsKeys("meta2", DocumentMetadata.DISTANCE.value());
133133

134134
// Remove all documents from the store
135-
vectorStore.delete(this.documents.stream().map(doc -> doc.getId()).toList());
135+
vectorStore.delete(this.documents.stream().map(Document::getId).toList());
136136

137137
List<Document> results2 = vectorStore
138138
.similaritySearch(SearchRequest.builder().query("Great Depression").topK(1).build());
@@ -204,15 +204,10 @@ public void searchWithFilters(CoherenceVectorStore.DistanceType distanceType,
204204

205205
assertThat(results).hasSize(1);
206206
assertThat(results.get(0).getId()).isEqualTo(bgDocument2.getId());
207-
208-
try {
209-
vectorStore
210-
.similaritySearch(SearchRequest.from(searchRequest).filterExpression("country == NL").build());
211-
Assert.fail("Invalid filter expression should have been cached!");
212-
}
213-
catch (FilterExpressionTextParser.FilterExpressionParseException e) {
214-
assertThat(e.getMessage()).contains("Line: 1:17, Error: no viable alternative at input 'NL'");
215-
}
207+
assertThatExceptionOfType(FilterExpressionTextParser.FilterExpressionParseException.class)
208+
.isThrownBy(() -> vectorStore
209+
.similaritySearch(SearchRequest.from(searchRequest).filterExpression("country == NL").build()))
210+
.withMessageContaining("Line: 1:17, Error: no viable alternative at input 'NL'");
216211

217212
// Remove all documents from the store
218213
truncateMap(context, ((CoherenceVectorStore) vectorStore).getMapName());

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

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
import javax.sql.DataSource;
3232

3333
import com.zaxxer.hikari.HikariDataSource;
34-
import org.junit.Assert;
3534
import org.junit.jupiter.api.Test;
3635
import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;
3736
import org.junit.jupiter.params.ParameterizedTest;
@@ -50,7 +49,7 @@
5049
import org.springframework.ai.vectorstore.SearchRequest;
5150
import org.springframework.ai.vectorstore.VectorStore;
5251
import org.springframework.ai.vectorstore.filter.Filter;
53-
import org.springframework.ai.vectorstore.filter.FilterExpressionTextParser.FilterExpressionParseException;
52+
import org.springframework.ai.vectorstore.filter.FilterExpressionTextParser;
5453
import org.springframework.beans.factory.annotation.Value;
5554
import org.springframework.boot.SpringBootConfiguration;
5655
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
@@ -66,6 +65,7 @@
6665
import org.springframework.util.CollectionUtils;
6766

6867
import static org.assertj.core.api.Assertions.assertThat;
68+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
6969

7070
/**
7171
* @author Diego Dupin
@@ -170,7 +170,7 @@ public void addAndSearch(String distanceType) {
170170
assertThat(resultDoc.getScore()).isBetween(0.0, 1.0);
171171

172172
// Remove all documents from the store
173-
vectorStore.delete(this.documents.stream().map(doc -> doc.getId()).toList());
173+
vectorStore.delete(this.documents.stream().map(Document::getId).toList());
174174

175175
List<Document> results2 = vectorStore
176176
.similaritySearch(SearchRequest.builder().query("Great Depression").topK(1).build());
@@ -282,14 +282,10 @@ public void searchWithFilters(String distanceType) {
282282
assertThat(results).hasSize(1);
283283
assertThat(results.get(0).getId()).isEqualTo(bgDocument.getId());
284284

285-
try {
286-
vectorStore
287-
.similaritySearch(SearchRequest.from(searchRequest).filterExpression("country == NL").build());
288-
Assert.fail("Invalid filter expression should have been cached!");
289-
}
290-
catch (FilterExpressionParseException e) {
291-
assertThat(e.getMessage()).contains("Line: 1:17, Error: no viable alternative at input 'NL'");
292-
}
285+
assertThatExceptionOfType(FilterExpressionTextParser.FilterExpressionParseException.class)
286+
.isThrownBy(() -> vectorStore
287+
.similaritySearch(SearchRequest.from(searchRequest).filterExpression("country == NL").build()))
288+
.withMessageContaining("Line: 1:17, Error: no viable alternative at input 'NL'");
293289

294290
// Remove all documents from the store
295291
dropTable(context);

vector-stores/spring-ai-oracle-store/src/test/java/org/springframework/ai/vectorstore/oracle/OracleVectorStoreIT.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import javax.sql.DataSource;
3131

3232
import oracle.jdbc.pool.OracleDataSource;
33-
import org.junit.Assert;
3433
import org.junit.jupiter.api.Disabled;
3534
import org.junit.jupiter.api.Test;
3635
import org.junit.jupiter.params.ParameterizedTest;
@@ -65,6 +64,7 @@
6564
import org.springframework.util.CollectionUtils;
6665

6766
import static org.assertj.core.api.Assertions.assertThat;
67+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
6868

6969
@Testcontainers
7070
@Disabled("Oracle image is 2GB")
@@ -155,7 +155,7 @@ public void addAndSearch(String distanceType) {
155155
assertThat(resultDoc.getMetadata()).containsKeys("meta2", DocumentMetadata.DISTANCE.value());
156156

157157
// Remove all documents from the store
158-
vectorStore.delete(this.documents.stream().map(doc -> doc.getId()).toList());
158+
vectorStore.delete(this.documents.stream().map(Document::getId).toList());
159159

160160
List<Document> results2 = vectorStore
161161
.similaritySearch(SearchRequest.builder().query("Great Depression").topK(1).build());
@@ -237,14 +237,10 @@ public void searchWithFilters(String distanceType, int searchAccuracy) {
237237
assertThat(results).hasSize(1);
238238
assertThat(results.get(0).getId()).isEqualTo(bgDocument.getId());
239239

240-
try {
241-
vectorStore
242-
.similaritySearch(SearchRequest.from(searchRequest).filterExpression("country == NL").build());
243-
Assert.fail("Invalid filter expression should have been cached!");
244-
}
245-
catch (FilterExpressionTextParser.FilterExpressionParseException e) {
246-
assertThat(e.getMessage()).contains("Line: 1:17, Error: no viable alternative at input 'NL'");
247-
}
240+
assertThatExceptionOfType(FilterExpressionTextParser.FilterExpressionParseException.class)
241+
.isThrownBy(() -> vectorStore
242+
.similaritySearch(SearchRequest.from(searchRequest).filterExpression("country == NL").build()))
243+
.withMessageContaining("Line: 1:17, Error: no viable alternative at input 'NL'");
248244

249245
// Remove all documents from the store
250246
dropTable(context, ((OracleVectorStore) vectorStore).getTableName());

vector-stores/spring-ai-pgvector-store/src/test/java/org/springframework/ai/vectorstore/pgvector/PgVectorStoreIT.java

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
import javax.sql.DataSource;
3232

3333
import com.zaxxer.hikari.HikariDataSource;
34-
import org.junit.Assert;
3534
import org.junit.jupiter.api.Test;
3635
import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;
3736
import org.junit.jupiter.params.ParameterizedTest;
@@ -51,7 +50,7 @@
5150
import org.springframework.ai.test.vectorstore.BaseVectorStoreTests;
5251
import org.springframework.ai.vectorstore.SearchRequest;
5352
import org.springframework.ai.vectorstore.VectorStore;
54-
import org.springframework.ai.vectorstore.filter.FilterExpressionTextParser.FilterExpressionParseException;
53+
import org.springframework.ai.vectorstore.filter.FilterExpressionTextParser;
5554
import org.springframework.ai.vectorstore.pgvector.PgVectorStore.PgIdType;
5655
import org.springframework.ai.vectorstore.pgvector.PgVectorStore.PgIndexType;
5756
import org.springframework.beans.factory.annotation.Value;
@@ -69,6 +68,7 @@
6968
import org.springframework.util.CollectionUtils;
7069

7170
import static org.assertj.core.api.Assertions.assertThat;
71+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
7272

7373
/**
7474
* @author Muthukumaran Navaneethakrishnan
@@ -173,7 +173,7 @@ public void addAndSearch(String distanceType) {
173173
assertThat(resultDoc.getMetadata()).containsKeys("meta2", DocumentMetadata.DISTANCE.value());
174174

175175
// Remove all documents from the store
176-
vectorStore.delete(this.documents.stream().map(doc -> doc.getId()).toList());
176+
vectorStore.delete(this.documents.stream().map(Document::getId).toList());
177177

178178
List<Document> results2 = vectorStore
179179
.similaritySearch(SearchRequest.builder().query("Great Depression").topK(1).build());
@@ -382,14 +382,10 @@ public void searchWithFilters(String distanceType) {
382382
assertThat(results).hasSize(1);
383383
assertThat(results.get(0).getId()).isEqualTo(bgDocument.getId());
384384

385-
try {
386-
vectorStore
387-
.similaritySearch(SearchRequest.from(searchRequest).filterExpression("country == NL").build());
388-
Assert.fail("Invalid filter expression should have been cached!");
389-
}
390-
catch (FilterExpressionParseException e) {
391-
assertThat(e.getMessage()).contains("Line: 1:17, Error: no viable alternative at input 'NL'");
392-
}
385+
assertThatExceptionOfType(FilterExpressionTextParser.FilterExpressionParseException.class)
386+
.isThrownBy(() -> vectorStore
387+
.similaritySearch(SearchRequest.from(searchRequest).filterExpression("country == NL").build()))
388+
.withMessageContaining("Line: 1:17, Error: no viable alternative at input 'NL'");
393389

394390
// Remove all documents from the store
395391
dropTable(context);

0 commit comments

Comments
 (0)