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