@@ -257,6 +257,89 @@ func Test_GroupGeneric_Keys(t *testing.T) {
257
257
})
258
258
}
259
259
260
+ func Test_GroupGeneric_Scan (t * testing.T ) {
261
+ gtest .C (t , func (t * gtest.T ) {
262
+ defer redis .FlushDB (ctx )
263
+
264
+ err := redis .GroupString ().MSet (ctx , map [string ]interface {}{
265
+ "firstname" : "Jack" ,
266
+ "lastname" : "Stuntman" ,
267
+ "age" : 35 ,
268
+ "nickname" : "Jumper" ,
269
+ })
270
+ t .AssertNil (err )
271
+
272
+ performScan := func (cursor uint64 , option ... gredis.ScanOption ) ([]string , error ) {
273
+ var allKeys = []string {}
274
+ for {
275
+ var nextCursor uint64
276
+ var keys []string
277
+ var err error
278
+
279
+ if option != nil {
280
+ nextCursor , keys , err = redis .Scan (ctx , cursor , option [0 ])
281
+ } else {
282
+ nextCursor , keys , err = redis .Scan (ctx , cursor )
283
+ }
284
+ if err != nil {
285
+ return nil , err
286
+ }
287
+
288
+ allKeys = append (allKeys , keys ... )
289
+ if nextCursor == 0 {
290
+ break
291
+ }
292
+ cursor = nextCursor
293
+ }
294
+ return allKeys , nil
295
+ }
296
+
297
+ // Test scanning for keys with `*name*` pattern
298
+ optWithName := gredis.ScanOption {Match : "*name*" , Count : 10 }
299
+ keysWithName , err := performScan (0 , optWithName )
300
+ t .AssertNil (err )
301
+ t .AssertGE (len (keysWithName ), 3 )
302
+ t .AssertIN (keysWithName , []string {"lastname" , "firstname" , "nickname" })
303
+
304
+ // Test scanning with a pattern that matches exactly one key
305
+ optWithAge := gredis.ScanOption {Match : "a??" , Count : 10 }
306
+ keysWithAge , err := performScan (0 , optWithAge )
307
+ t .AssertNil (err )
308
+ t .AssertEQ (len (keysWithAge ), 1 )
309
+ t .AssertEQ (keysWithAge , []string {"age" })
310
+
311
+ // Test scanning for all keys
312
+ optWithAll := gredis.ScanOption {Match : "*" , Count : 10 }
313
+ all , err := performScan (0 , optWithAll )
314
+ t .AssertNil (err )
315
+ t .AssertGE (len (all ), 4 )
316
+ t .AssertIN (all , []string {"lastname" , "firstname" , "age" , "nickname" })
317
+
318
+ // Test empty pattern
319
+ optWithEmptyPattern := gredis.ScanOption {Match : "" }
320
+ emptyPatternKeys , err := performScan (0 , optWithEmptyPattern )
321
+ t .AssertNil (err )
322
+ t .AssertEQ (len (emptyPatternKeys ), 4 )
323
+
324
+ // Test pattern with no matches
325
+ optWithNoMatch := gredis.ScanOption {Match : "xyz*" , Count : 10 }
326
+ noMatchKeys , err := performScan (0 , optWithNoMatch )
327
+ t .AssertNil (err )
328
+ t .AssertEQ (len (noMatchKeys ), 0 )
329
+
330
+ // Test scanning for keys with invalid count value
331
+ optWithInvalidCount := gredis.ScanOption {Count : - 1 }
332
+ _ , err = performScan (0 , optWithInvalidCount )
333
+ t .AssertNQ (err , nil )
334
+
335
+ // Test scanning for all keys without options
336
+ allWithoutOpt , err := performScan (0 )
337
+ t .AssertNil (err )
338
+ t .AssertGE (len (allWithoutOpt ), 4 )
339
+ t .AssertIN (all , []string {"lastname" , "firstname" , "age" , "nickname" })
340
+ })
341
+ }
342
+
260
343
func Test_GroupGeneric_FlushDB (t * testing.T ) {
261
344
gtest .C (t , func (t * gtest.T ) {
262
345
defer redis .FlushDB (ctx )
0 commit comments