|
25 | 25 | import org.mockito.junit.jupiter.MockitoExtension; |
26 | 26 | import org.mockito.junit.jupiter.MockitoSettings; |
27 | 27 | import org.mockito.quality.Strictness; |
| 28 | +import org.springframework.ai.document.Document; |
| 29 | +import org.springframework.ai.embedding.EmbeddingModel; |
| 30 | +import org.springframework.ai.vectorstore.SearchRequest; |
28 | 31 | import org.springframework.ai.vectorstore.filter.Filter; |
29 | 32 |
|
30 | 33 | import java.util.ArrayList; |
|
33 | 36 | import java.util.Map; |
34 | 37 |
|
35 | 38 | import static org.junit.jupiter.api.Assertions.*; |
| 39 | +import static org.mockito.ArgumentMatchers.any; |
36 | 40 | import static org.mockito.ArgumentMatchers.anyInt; |
37 | 41 | import static org.mockito.ArgumentMatchers.anyLong; |
| 42 | +import static org.mockito.Mockito.mock; |
38 | 43 | import static org.mockito.Mockito.when; |
39 | 44 |
|
40 | 45 | @ExtendWith(MockitoExtension.class) |
@@ -84,6 +89,25 @@ void buildDynamicFilter_businessTerm_withValidIds_returnsFilterExpression() { |
84 | 89 | assertNotNull(result); |
85 | 90 | } |
86 | 91 |
|
| 92 | + @Test |
| 93 | + void buildDynamicFilter_businessTerm_matchesStringMetadataInSimpleVectorStore() { |
| 94 | + when(businessKnowledgeMapper.selectRecalledKnowledgeIds(anyLong())).thenReturn(List.of(10L)); |
| 95 | + EmbeddingModel embeddingModel = mock(EmbeddingModel.class); |
| 96 | + when(embeddingModel.dimensions()).thenReturn(2); |
| 97 | + when(embeddingModel.embed(any(Document.class))).thenReturn(new float[] { 1.0f, 0.0f }); |
| 98 | + when(embeddingModel.embed(any(String.class))).thenReturn(new float[] { 1.0f, 0.0f }); |
| 99 | + MetadataAwareSimpleVectorStore store = new MetadataAwareSimpleVectorStore(embeddingModel); |
| 100 | + store.add(List.of(new Document("term", Map.of("agentId", "1", DocumentMetadataConstant.VECTOR_TYPE, |
| 101 | + DocumentMetadataConstant.BUSINESS_TERM, DocumentMetadataConstant.DB_BUSINESS_TERM_ID, "10")))); |
| 102 | + |
| 103 | + Filter.Expression filter = dynamicFilterService.buildDynamicFilter("1", |
| 104 | + DocumentMetadataConstant.BUSINESS_TERM); |
| 105 | + List<Document> result = store.similaritySearch( |
| 106 | + SearchRequest.builder().query("default").filterExpression(filter).topK(10).similarityThreshold(0).build()); |
| 107 | + |
| 108 | + assertEquals(1, result.size()); |
| 109 | + } |
| 110 | + |
87 | 111 | @Test |
88 | 112 | void buildDynamicFilter_businessTerm_noValidIds_returnsNull() { |
89 | 113 | when(businessKnowledgeMapper.selectRecalledKnowledgeIds(anyLong())).thenReturn(new ArrayList<>()); |
|
0 commit comments