|
| 1 | +package com.microsoft.semantickernel.tests.connectors.memory.redis; |
| 2 | + |
| 3 | +import com.microsoft.semantickernel.connectors.data.redis.RedisHashSetVectorStoreRecordCollection; |
| 4 | +import com.microsoft.semantickernel.connectors.data.redis.RedisHashSetVectorStoreRecordCollectionOptions; |
| 5 | +import com.microsoft.semantickernel.data.recorddefinition.VectorStoreRecordDataField; |
| 6 | +import com.microsoft.semantickernel.data.recorddefinition.VectorStoreRecordDefinition; |
| 7 | +import com.microsoft.semantickernel.data.recorddefinition.VectorStoreRecordField; |
| 8 | +import com.microsoft.semantickernel.data.recorddefinition.VectorStoreRecordKeyField; |
| 9 | +import com.microsoft.semantickernel.data.recorddefinition.VectorStoreRecordVectorField; |
| 10 | +import com.microsoft.semantickernel.data.recordoptions.GetRecordOptions; |
| 11 | +import com.microsoft.semantickernel.tests.connectors.memory.Hotel; |
| 12 | +import com.redis.testcontainers.RedisContainer; |
| 13 | +import org.junit.jupiter.api.BeforeAll; |
| 14 | +import org.junit.jupiter.api.MethodOrderer; |
| 15 | +import org.junit.jupiter.api.Order; |
| 16 | +import org.junit.jupiter.api.Test; |
| 17 | +import org.junit.jupiter.api.TestMethodOrder; |
| 18 | +import org.junit.jupiter.params.ParameterizedTest; |
| 19 | +import org.junit.jupiter.params.provider.EnumSource; |
| 20 | +import org.testcontainers.junit.jupiter.Container; |
| 21 | +import org.testcontainers.junit.jupiter.Testcontainers; |
| 22 | +import redis.clients.jedis.JedisPooled; |
| 23 | + |
| 24 | +import javax.annotation.Nonnull; |
| 25 | +import java.util.ArrayList; |
| 26 | +import java.util.Arrays; |
| 27 | +import java.util.HashMap; |
| 28 | +import java.util.List; |
| 29 | +import java.util.Map; |
| 30 | + |
| 31 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 32 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 33 | +import static org.junit.jupiter.api.Assertions.assertNull; |
| 34 | + |
| 35 | +@Testcontainers |
| 36 | +@TestMethodOrder(MethodOrderer.OrderAnnotation.class) |
| 37 | +public class RedisHashSetVectorStoreRecordCollectionTest { |
| 38 | + |
| 39 | + @Container private static final RedisContainer redisContainer = new RedisContainer("redis/redis-stack:latest"); |
| 40 | + |
| 41 | + private static final Map<RecordCollectionOptions, RedisHashSetVectorStoreRecordCollectionOptions<Hotel>> optionsMap = new HashMap<>(); |
| 42 | + |
| 43 | + public enum RecordCollectionOptions { |
| 44 | + DEFAULT, WITH_CUSTOM_DEFINITION |
| 45 | + } |
| 46 | + |
| 47 | + @BeforeAll |
| 48 | + static void setup() { |
| 49 | + optionsMap.put(RecordCollectionOptions.DEFAULT, RedisHashSetVectorStoreRecordCollectionOptions.<Hotel>builder() |
| 50 | + .withRecordClass(Hotel.class) |
| 51 | + .build()); |
| 52 | + |
| 53 | + List<VectorStoreRecordField> fields = new ArrayList<>(); |
| 54 | + fields.add(VectorStoreRecordKeyField.builder() |
| 55 | + .withName("id") |
| 56 | + .withFieldType(String.class) |
| 57 | + .build()); |
| 58 | + fields.add(VectorStoreRecordDataField.builder() |
| 59 | + .withName("name") |
| 60 | + .withFieldType(String.class) |
| 61 | + .build()); |
| 62 | + fields.add(VectorStoreRecordDataField.builder() |
| 63 | + .withName("code") |
| 64 | + .withFieldType(Integer.class) |
| 65 | + .build()); |
| 66 | + fields.add(VectorStoreRecordDataField.builder() |
| 67 | + .withName("description") |
| 68 | + .withStorageName("summary") |
| 69 | + .withFieldType(String.class) |
| 70 | + .withHasEmbedding(true) |
| 71 | + .withEmbeddingFieldName("descriptionEmbedding") |
| 72 | + .build()); |
| 73 | + fields.add(VectorStoreRecordVectorField.builder() |
| 74 | + .withName("descriptionEmbedding") |
| 75 | + .withStorageName("summaryEmbedding") |
| 76 | + .withFieldType(List.class) |
| 77 | + .withDimensions(768) |
| 78 | + .build()); |
| 79 | + fields.add(VectorStoreRecordDataField.builder() |
| 80 | + .withName("rating") |
| 81 | + .withFieldType(Double.class) |
| 82 | + .build()); |
| 83 | + VectorStoreRecordDefinition recordDefinition = VectorStoreRecordDefinition.fromFields(fields); |
| 84 | + |
| 85 | + optionsMap.put(RecordCollectionOptions.WITH_CUSTOM_DEFINITION, RedisHashSetVectorStoreRecordCollectionOptions.<Hotel>builder() |
| 86 | + .withRecordClass(Hotel.class) |
| 87 | + .withRecordDefinition(recordDefinition) |
| 88 | + .build()); |
| 89 | + } |
| 90 | + |
| 91 | + private RedisHashSetVectorStoreRecordCollection<Hotel> buildrecordCollection(@Nonnull RedisHashSetVectorStoreRecordCollectionOptions<Hotel> options, @Nonnull String collectionName) { |
| 92 | + return new RedisHashSetVectorStoreRecordCollection<>(new JedisPooled(redisContainer.getRedisURI()), collectionName, RedisHashSetVectorStoreRecordCollectionOptions.<Hotel>builder() |
| 93 | + .withRecordClass(options.getRecordClass()) |
| 94 | + .withVectorStoreRecordMapper(options.getVectorStoreRecordMapper()) |
| 95 | + .withRecordDefinition(options.getRecordDefinition()) |
| 96 | + .withPrefixCollectionName(options.isPrefixCollectionName()) |
| 97 | + .build()); |
| 98 | + } |
| 99 | + |
| 100 | + private List<Hotel> getHotels() { |
| 101 | + return List.of( |
| 102 | + new Hotel("id_1", "Hotel 1", 1, "Hotel 1 description", Arrays.asList(1.0f, 2.0f, 3.0f), 4.0), |
| 103 | + new Hotel("id_2", "Hotel 2", 2, "Hotel 2 description", Arrays.asList(1.0f, 2.0f, 3.0f), 3.0), |
| 104 | + new Hotel("id_3", "Hotel 3", 3, "Hotel 3 description", Arrays.asList(1.0f, 2.0f, 3.0f), 5.0), |
| 105 | + new Hotel("id_4", "Hotel 4", 4, "Hotel 4 description", Arrays.asList(1.0f, 2.0f, 3.0f), 4.0), |
| 106 | + new Hotel("id_5", "Hotel 5", 5, "Hotel 5 description", Arrays.asList(1.0f, 2.0f, 3.0f), 5.0) |
| 107 | + ); |
| 108 | + } |
| 109 | + |
| 110 | + @Order(1) |
| 111 | + @ParameterizedTest |
| 112 | + @EnumSource(RecordCollectionOptions.class) |
| 113 | + public void buildrecordCollection(RecordCollectionOptions options) { |
| 114 | + assertNotNull(buildrecordCollection(optionsMap.get(options), options.name())); |
| 115 | + } |
| 116 | + |
| 117 | + @Order(2) |
| 118 | + @ParameterizedTest |
| 119 | + @EnumSource(RecordCollectionOptions.class) |
| 120 | + public void createCollectionAsync(RecordCollectionOptions options) { |
| 121 | + RedisHashSetVectorStoreRecordCollection<Hotel> recordCollection = buildrecordCollection(optionsMap.get(options), options.name()); |
| 122 | + |
| 123 | + assertEquals(false, recordCollection.collectionExistsAsync().block()); |
| 124 | + recordCollection.createCollectionAsync().block(); |
| 125 | + assertEquals(true, recordCollection.collectionExistsAsync().block()); |
| 126 | + } |
| 127 | + |
| 128 | + @Test |
| 129 | + public void deleteCollectionAsync() { |
| 130 | + RedisHashSetVectorStoreRecordCollection<Hotel> recordCollection = buildrecordCollection(optionsMap.get(RecordCollectionOptions.DEFAULT), "deleteCollectionAsync"); |
| 131 | + |
| 132 | + assertEquals(false, recordCollection.collectionExistsAsync().block()); |
| 133 | + recordCollection.createCollectionAsync().block(); |
| 134 | + recordCollection.deleteCollectionAsync().block(); |
| 135 | + assertEquals(false, recordCollection.collectionExistsAsync().block()); |
| 136 | + } |
| 137 | + |
| 138 | + @ParameterizedTest |
| 139 | + @EnumSource(RecordCollectionOptions.class) |
| 140 | + public void upsertAndGetRecordAsync(RecordCollectionOptions options) { |
| 141 | + RedisHashSetVectorStoreRecordCollection<Hotel> recordCollection = buildrecordCollection(optionsMap.get(options), options.name()); |
| 142 | + |
| 143 | + List<Hotel> hotels = getHotels(); |
| 144 | + for (Hotel hotel : hotels) { |
| 145 | + recordCollection.upsertAsync(hotel, null).block(); |
| 146 | + } |
| 147 | + |
| 148 | + for (Hotel hotel : hotels) { |
| 149 | + Hotel retrievedHotel = recordCollection.getAsync(hotel.getId(), null).block(); |
| 150 | + assertNotNull(retrievedHotel); |
| 151 | + assertEquals(hotel.getId(), retrievedHotel.getId()); |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + @ParameterizedTest |
| 156 | + @EnumSource(RecordCollectionOptions.class) |
| 157 | + public void getBatchAsync(RecordCollectionOptions options) { |
| 158 | + RedisHashSetVectorStoreRecordCollection<Hotel> recordCollection = buildrecordCollection(optionsMap.get(options), options.name()); |
| 159 | + |
| 160 | + List<Hotel> hotels = getHotels(); |
| 161 | + for (Hotel hotel : hotels) { |
| 162 | + recordCollection.upsertAsync(hotel, null).block(); |
| 163 | + } |
| 164 | + |
| 165 | + List<String> ids = new ArrayList<>(); |
| 166 | + hotels.forEach(hotel -> ids.add(hotel.getId())); |
| 167 | + |
| 168 | + List<Hotel> retrievedHotels = recordCollection.getBatchAsync(ids, null).block(); |
| 169 | + |
| 170 | + assertNotNull(retrievedHotels); |
| 171 | + assertEquals(hotels.size(), retrievedHotels.size()); |
| 172 | + for (int i = 0; i < hotels.size(); i++) { |
| 173 | + assertEquals(hotels.get(i).getId(), retrievedHotels.get(i).getId()); |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + @ParameterizedTest |
| 178 | + @EnumSource(RecordCollectionOptions.class) |
| 179 | + public void upsertBatchAsync(RecordCollectionOptions options) { |
| 180 | + RedisHashSetVectorStoreRecordCollection<Hotel> recordCollection = buildrecordCollection(optionsMap.get(options), options.name()); |
| 181 | + |
| 182 | + List<Hotel> hotels = getHotels(); |
| 183 | + List<String> keys = recordCollection.upsertBatchAsync(hotels, null).block(); |
| 184 | + assertNotNull(keys); |
| 185 | + |
| 186 | + List<Hotel> retrievedHotels = (List<Hotel>) recordCollection.getBatchAsync(keys, null).block(); |
| 187 | + |
| 188 | + assertNotNull(retrievedHotels); |
| 189 | + assertEquals(hotels.size(), retrievedHotels.size()); |
| 190 | + for (int i = 0; i < hotels.size(); i++) { |
| 191 | + assertEquals(hotels.get(i).getId(), retrievedHotels.get(i).getId()); |
| 192 | + } |
| 193 | + } |
| 194 | + |
| 195 | + @ParameterizedTest |
| 196 | + @EnumSource(RecordCollectionOptions.class) |
| 197 | + public void deleteAsync(RecordCollectionOptions options) { |
| 198 | + RedisHashSetVectorStoreRecordCollection<Hotel> recordCollection = buildrecordCollection(optionsMap.get(options), options.name()); |
| 199 | + |
| 200 | + List<Hotel> hotels = getHotels(); |
| 201 | + recordCollection.upsertBatchAsync(hotels, null).block(); |
| 202 | + |
| 203 | + for (Hotel hotel : hotels) { |
| 204 | + recordCollection.deleteAsync(hotel.getId(), null).block(); |
| 205 | + Hotel retrievedHotel = recordCollection.getAsync(hotel.getId(), null).block(); |
| 206 | + assertNull(retrievedHotel); |
| 207 | + } |
| 208 | + } |
| 209 | + |
| 210 | + @ParameterizedTest |
| 211 | + @EnumSource(RecordCollectionOptions.class) |
| 212 | + public void deleteBatchAsync(RecordCollectionOptions options) { |
| 213 | + RedisHashSetVectorStoreRecordCollection<Hotel> recordCollection = buildrecordCollection(optionsMap.get(options), options.name()); |
| 214 | + |
| 215 | + List<Hotel> hotels = getHotels(); |
| 216 | + recordCollection.upsertBatchAsync(hotels, null).block(); |
| 217 | + |
| 218 | + List<String> ids = new ArrayList<>(); |
| 219 | + hotels.forEach(hotel -> ids.add(hotel.getId())); |
| 220 | + |
| 221 | + recordCollection.deleteBatchAsync(ids, null).block(); |
| 222 | + |
| 223 | + for (String id : ids) { |
| 224 | + Hotel retrievedHotel = recordCollection.getAsync(id, null).block(); |
| 225 | + assertNull(retrievedHotel); |
| 226 | + } |
| 227 | + } |
| 228 | + |
| 229 | + @ParameterizedTest |
| 230 | + @EnumSource(RecordCollectionOptions.class) |
| 231 | + public void getAsyncWithVectors(RecordCollectionOptions options) { |
| 232 | + RedisHashSetVectorStoreRecordCollection<Hotel> recordCollection = buildrecordCollection(optionsMap.get(options), options.name()); |
| 233 | + |
| 234 | + List<Hotel> hotels = getHotels(); |
| 235 | + recordCollection.upsertBatchAsync(hotels, null).block(); |
| 236 | + |
| 237 | + for (Hotel hotel : hotels) { |
| 238 | + Hotel retrievedHotel = recordCollection.getAsync(hotel.getId(), null).block(); |
| 239 | + assertNotNull(retrievedHotel); |
| 240 | + assertNotNull(retrievedHotel.getDescriptionEmbedding()); |
| 241 | + assertEquals(hotel.getId(), retrievedHotel.getId()); |
| 242 | + assertEquals(hotel.getDescription(), retrievedHotel.getDescription()); |
| 243 | + } |
| 244 | + } |
| 245 | + |
| 246 | + @ParameterizedTest |
| 247 | + @EnumSource(RecordCollectionOptions.class) |
| 248 | + public void getBatchAsyncWithVectors(RecordCollectionOptions options) { |
| 249 | + RedisHashSetVectorStoreRecordCollection<Hotel> recordCollection = buildrecordCollection(optionsMap.get(options), options.name()); |
| 250 | + |
| 251 | + List<Hotel> hotels = getHotels(); |
| 252 | + recordCollection.upsertBatchAsync(hotels, null).block(); |
| 253 | + |
| 254 | + List<String> ids = new ArrayList<>(); |
| 255 | + hotels.forEach(hotel -> ids.add(hotel.getId())); |
| 256 | + |
| 257 | + List<Hotel> retrievedHotels = recordCollection.getBatchAsync(ids, null).block(); |
| 258 | + |
| 259 | + assertNotNull(retrievedHotels); |
| 260 | + assertEquals(hotels.size(), retrievedHotels.size()); |
| 261 | + for (int i = 0; i < hotels.size(); i++) { |
| 262 | + assertEquals(hotels.get(i).getId(), retrievedHotels.get(i).getId()); |
| 263 | + assertEquals(hotels.get(i).getDescription(), retrievedHotels.get(i).getDescription()); |
| 264 | + assertNotNull(retrievedHotels.get(i).getDescriptionEmbedding()); |
| 265 | + } |
| 266 | + } |
| 267 | + |
| 268 | + @ParameterizedTest |
| 269 | + @EnumSource(RecordCollectionOptions.class) |
| 270 | + public void getAsyncWithNoVectors(RecordCollectionOptions options) { |
| 271 | + RedisHashSetVectorStoreRecordCollection<Hotel> recordCollection = buildrecordCollection(optionsMap.get(options), options.name()); |
| 272 | + |
| 273 | + List<Hotel> hotels = getHotels(); |
| 274 | + recordCollection.upsertBatchAsync(hotels, null).block(); |
| 275 | + |
| 276 | + GetRecordOptions getRecordOptions = GetRecordOptions.builder().includeVectors(false).build(); |
| 277 | + for (Hotel hotel : hotels) { |
| 278 | + Hotel retrievedHotel = recordCollection.getAsync(hotel.getId(), getRecordOptions).block(); |
| 279 | + assertNotNull(retrievedHotel); |
| 280 | + assertNull(retrievedHotel.getDescriptionEmbedding()); |
| 281 | + assertEquals(hotel.getId(), retrievedHotel.getId()); |
| 282 | + assertEquals(hotel.getDescription(), retrievedHotel.getDescription()); |
| 283 | + } |
| 284 | + } |
| 285 | + |
| 286 | + @ParameterizedTest |
| 287 | + @EnumSource(RecordCollectionOptions.class) |
| 288 | + public void getBatchAsyncWithNoVectors(RecordCollectionOptions options) { |
| 289 | + RedisHashSetVectorStoreRecordCollection<Hotel> recordCollection = buildrecordCollection(optionsMap.get(options), options.name()); |
| 290 | + |
| 291 | + List<Hotel> hotels = getHotels(); |
| 292 | + recordCollection.upsertBatchAsync(hotels, null).block(); |
| 293 | + |
| 294 | + GetRecordOptions getRecordOptions = GetRecordOptions.builder().includeVectors(false).build(); |
| 295 | + List<String> ids = new ArrayList<>(); |
| 296 | + hotels.forEach(hotel -> ids.add(hotel.getId())); |
| 297 | + |
| 298 | + List<Hotel> retrievedHotels = recordCollection.getBatchAsync(ids, getRecordOptions).block(); |
| 299 | + |
| 300 | + assertNotNull(retrievedHotels); |
| 301 | + assertEquals(hotels.size(), retrievedHotels.size()); |
| 302 | + for (int i = 0; i < hotels.size(); i++) { |
| 303 | + assertEquals(hotels.get(i).getId(), retrievedHotels.get(i).getId()); |
| 304 | + assertEquals(hotels.get(i).getDescription(), retrievedHotels.get(i).getDescription()); |
| 305 | + assertNull(retrievedHotels.get(i).getDescriptionEmbedding()); |
| 306 | + } |
| 307 | + } |
| 308 | +} |
0 commit comments