@@ -44,7 +44,7 @@ func TestCache_e2e_Set(t *testing.T) {
44
44
wantErr error
45
45
}{
46
46
{
47
- name : "set value" ,
47
+ name : "set e2e value" ,
48
48
after : func (ctx context.Context , t * testing.T ) {
49
49
result , err := rdb .Get (ctx , "name" ).Result ()
50
50
require .NoError (t , err )
@@ -89,7 +89,7 @@ func TestCache_e2e_Get(t *testing.T) {
89
89
wantErr error
90
90
}{
91
91
{
92
- name : "get value" ,
92
+ name : "get e2e value" ,
93
93
before : func (ctx context.Context , t * testing.T ) {
94
94
require .NoError (t , rdb .Set (ctx , "name" , "大明" , time .Minute ).Err ())
95
95
},
@@ -101,7 +101,7 @@ func TestCache_e2e_Get(t *testing.T) {
101
101
wantVal : "大明" ,
102
102
},
103
103
{
104
- name : "get error" ,
104
+ name : "get e2e error" ,
105
105
key : "name" ,
106
106
before : func (ctx context.Context , t * testing.T ) {},
107
107
after : func (ctx context.Context , t * testing.T ) {},
@@ -144,7 +144,7 @@ func TestCache_e2e_SetNX(t *testing.T) {
144
144
wantVal bool
145
145
}{
146
146
{
147
- name : "test setnx" ,
147
+ name : "setnx e2e value " ,
148
148
before : func (ctx context.Context , t * testing.T ) {},
149
149
after : func (ctx context.Context , t * testing.T ) {
150
150
assert .NoError (t , rdb .Del (context .Background (), "testnx" ).Err ())
@@ -154,14 +154,14 @@ func TestCache_e2e_SetNX(t *testing.T) {
154
154
wantVal : true ,
155
155
},
156
156
{
157
- name : "test setnx fail" ,
157
+ name : "setnx e2e fail" ,
158
158
before : func (ctx context.Context , t * testing.T ) {
159
- require .NoError (t , rdb .SetNX (ctx , "testnxf " , "hello ecache" , time .Minute ).Err ())
159
+ require .NoError (t , rdb .SetNX (ctx , "testnx " , "hello ecache" , time .Minute ).Err ())
160
160
},
161
161
after : func (ctx context.Context , t * testing.T ) {
162
- require .NoError (t , rdb .Del (ctx , "testnxf " ).Err ())
162
+ require .NoError (t , rdb .Del (ctx , "testnx " ).Err ())
163
163
},
164
- key : "testnxf " ,
164
+ key : "testnx " ,
165
165
val : "hello go" ,
166
166
wantVal : false ,
167
167
},
@@ -199,11 +199,12 @@ func TestCache_e2e_GetSet(t *testing.T) {
199
199
wantErr error
200
200
}{
201
201
{
202
- name : "test_get_set " ,
202
+ name : "getset e2e value " ,
203
203
before : func (ctx context.Context , t * testing.T ) {
204
204
require .NoError (t , rdb .Set (context .Background (), "test_get_set" , "hello ecache" , time .Second * 10 ).Err ())
205
205
},
206
206
after : func (ctx context.Context , t * testing.T ) {
207
+ assert .Equal (t , "hello go" , rdb .Get (context .Background (), "test_get_set" ).Val ())
207
208
require .NoError (t , rdb .Del (context .Background (), "test_get_set" ).Err ())
208
209
},
209
210
key : "test_get_set" ,
@@ -212,15 +213,15 @@ func TestCache_e2e_GetSet(t *testing.T) {
212
213
wantVal : "hello ecache" ,
213
214
},
214
215
{
215
- name : "test_get_set " ,
216
+ name : "getset e2e err " ,
216
217
before : func (ctx context.Context , t * testing.T ) {},
217
218
after : func (ctx context.Context , t * testing.T ) {
219
+ assert .Equal (t , "hello key notfound" , rdb .Get (context .Background (), "test_get_set" ).Val ())
218
220
require .NoError (t , rdb .Del (context .Background (), "test_get_set" ).Err ())
219
221
},
220
222
key : "test_get_set" ,
221
223
val : "hello key notfound" ,
222
224
expire : time .Second * 10 ,
223
- wantVal : "" ,
224
225
wantErr : errs .ErrKeyNotExist ,
225
226
},
226
227
}
@@ -254,7 +255,7 @@ func TestCache_e2e_LPush(t *testing.T) {
254
255
wantVal int64
255
256
}{
256
257
{
257
- name : "test_cache_lpush " ,
258
+ name : "lpush e2e value " ,
258
259
before : func (ctx context.Context , t * testing.T ) {},
259
260
after : func (ctx context.Context , t * testing.T ) {
260
261
assert .Equal (t , int64 (2 ), rdb .LLen (context .Background (), "test_cache_lpush" ).Val ())
@@ -265,7 +266,7 @@ func TestCache_e2e_LPush(t *testing.T) {
265
266
wantVal : 2 ,
266
267
},
267
268
{
268
- name : "test_cache_lpush_want_val " ,
269
+ name : "lpush e2e want value " ,
269
270
before : func (ctx context.Context , t * testing.T ) {
270
271
require .NoError (t , rdb .LPush (context .Background (), "test_cache_lpush" , "hello ecache" , "hello go" ).Err ())
271
272
},
@@ -308,7 +309,7 @@ func TestCache_e2e_LPop(t *testing.T) {
308
309
wantErr error
309
310
}{
310
311
{
311
- name : "test_cache_pop " ,
312
+ name : "lpop e2e value " ,
312
313
before : func (ctx context.Context , t * testing.T ) {
313
314
require .NoError (t , rdb .LPush (context .Background (), "test_cache_pop" , "1" , "2" , "3" , "4" ).Err ())
314
315
},
@@ -320,7 +321,7 @@ func TestCache_e2e_LPop(t *testing.T) {
320
321
wantVal : "4" ,
321
322
},
322
323
{
323
- name : "test_cache_pop_one " ,
324
+ name : "lpop e2e one value " ,
324
325
before : func (ctx context.Context , t * testing.T ) {
325
326
require .NoError (t , rdb .LPush (context .Background (), "test_cache_pop" , "1" ).Err ())
326
327
require .NoError (t , rdb .LPop (context .Background (), "test_cache_pop" ).Err ())
@@ -333,7 +334,7 @@ func TestCache_e2e_LPop(t *testing.T) {
333
334
wantErr : errs .ErrKeyNotExist ,
334
335
},
335
336
{
336
- name : "test_cache_pop_err_nil " ,
337
+ name : "lpop e2e err " ,
337
338
before : func (ctx context.Context , t * testing.T ) {},
338
339
after : func (ctx context.Context , t * testing.T ) {},
339
340
key : "test_cache_pop" ,
@@ -355,3 +356,151 @@ func TestCache_e2e_LPop(t *testing.T) {
355
356
})
356
357
}
357
358
}
359
+
360
+ func TestCache_e2e_SAdd (t * testing.T ) {
361
+ rdb := redis .NewClient (& redis.Options {
362
+ Addr : "localhost:6379" ,
363
+ })
364
+ require .NoError (t , rdb .Ping (context .Background ()).Err ())
365
+
366
+ testCase := []struct {
367
+ name string
368
+ before func (ctx context.Context , t * testing.T )
369
+ after func (ctx context.Context , t * testing.T )
370
+ key string
371
+ val []any
372
+ wantVal int64
373
+ wantErr error
374
+ }{
375
+ {
376
+ name : "sadd e2e value" ,
377
+ before : func (ctx context.Context , t * testing.T ) {},
378
+ after : func (ctx context.Context , t * testing.T ) {
379
+ assert .Equal (t , int64 (2 ), rdb .SCard (context .Background (), "test_e2e_sadd" ).Val ())
380
+ require .NoError (t , rdb .Del (context .Background (), "test_e2e_sadd" ).Err ())
381
+ },
382
+ key : "test_e2e_sadd" ,
383
+ val : []any {"hello ecache" , "hello go" },
384
+ wantVal : 2 ,
385
+ },
386
+ {
387
+ name : "sadd e2e ignore" ,
388
+ before : func (ctx context.Context , t * testing.T ) {
389
+ require .NoError (t , rdb .SAdd (context .Background (), "test_e2e_sadd" , "hello" ).Err ())
390
+ },
391
+ after : func (ctx context.Context , t * testing.T ) {
392
+ assert .Equal (t , int64 (1 ), rdb .SCard (context .Background (), "test_e2e_sadd" ).Val ())
393
+ require .NoError (t , rdb .Del (context .Background (), "test_e2e_sadd" ).Err ())
394
+ },
395
+ key : "test_e2e_sadd" ,
396
+ val : []any {"hello" },
397
+ wantVal : 0 ,
398
+ },
399
+ }
400
+
401
+ for _ , tc := range testCase {
402
+ t .Run (tc .name , func (t * testing.T ) {
403
+ ctx , cancelFunc := context .WithTimeout (context .Background (), time .Second * 5 )
404
+ defer cancelFunc ()
405
+ c := NewCache (rdb )
406
+ tc .before (ctx , t )
407
+ val , err := c .SAdd (ctx , tc .key , tc .val ... )
408
+ assert .Equal (t , val , tc .wantVal )
409
+ assert .Equal (t , err , tc .wantErr )
410
+ tc .after (ctx , t )
411
+ })
412
+ }
413
+ }
414
+
415
+ func TestCache_e2e_SRem (t * testing.T ) {
416
+ rdb := redis .NewClient (& redis.Options {
417
+ Addr : "localhost:6379" ,
418
+ })
419
+ require .NoError (t , rdb .Ping (context .Background ()).Err ())
420
+
421
+ testCase := []struct {
422
+ name string
423
+ before func (ctx context.Context , t * testing.T )
424
+ after func (ctx context.Context , t * testing.T )
425
+ key string
426
+ val []any
427
+ wantVal int64
428
+ wantErr error
429
+ }{
430
+ {
431
+ name : "srem e2e value" ,
432
+ before : func (ctx context.Context , t * testing.T ) {
433
+ require .NoError (t , rdb .SAdd (context .Background (), "test_e2e_srem" , "hello" , "ecache" ).Err ())
434
+ },
435
+ after : func (ctx context.Context , t * testing.T ) {
436
+ assert .Equal (t , int64 (1 ), rdb .SCard (context .Background (), "test_e2e_srem" ).Val ())
437
+ require .NoError (t , rdb .Del (context .Background (), "test_e2e_srem" ).Err ())
438
+ },
439
+ key : "test_e2e_srem" ,
440
+ val : []any {"hello" },
441
+ wantVal : 1 ,
442
+ },
443
+ {
444
+ name : "srem e2e nil" ,
445
+ before : func (ctx context.Context , t * testing.T ) {
446
+ require .NoError (t , rdb .SAdd (context .Background (), "test_e2e_srem" , "hello" , "ecache" ).Err ())
447
+ require .NoError (t , rdb .SRem (context .Background (), "test_e2e_srem" , "hello" , "ecache" ).Err ())
448
+ },
449
+ after : func (ctx context.Context , t * testing.T ) {
450
+ assert .Equal (t , int64 (0 ), rdb .SCard (context .Background (), "test_e2e_srem" ).Val ())
451
+ require .NoError (t , rdb .Del (context .Background (), "test_e2e_srem" ).Err ())
452
+ },
453
+ key : "test_e2e_srem" ,
454
+ val : []any {"hello" },
455
+ },
456
+ {
457
+ name : "srem e2e ignore" ,
458
+ before : func (ctx context.Context , t * testing.T ) {
459
+ require .NoError (t , rdb .SAdd (context .Background (), "test_e2e_srem" , "hello" , "ecache" ).Err ())
460
+ },
461
+ after : func (ctx context.Context , t * testing.T ) {
462
+ assert .Equal (t , int64 (2 ), rdb .SCard (context .Background (), "test_e2e_srem" ).Val ())
463
+ require .NoError (t , rdb .Del (context .Background (), "test_e2e_srem" ).Err ())
464
+ },
465
+ key : "test_e2e_srem" ,
466
+ val : []any {"go" },
467
+ wantVal : 0 ,
468
+ wantErr : nil ,
469
+ },
470
+ {
471
+ name : "srem e2e key nil" ,
472
+ before : func (ctx context.Context , t * testing.T ) {},
473
+ after : func (ctx context.Context , t * testing.T ) {},
474
+ key : "test_e2e_srem" ,
475
+ val : []any {"ecache" },
476
+ wantVal : 0 ,
477
+ wantErr : nil ,
478
+ },
479
+ {
480
+ name : "srem e2e section ignore" ,
481
+ before : func (ctx context.Context , t * testing.T ) {
482
+ require .NoError (t , rdb .SAdd (context .Background (), "test_e2e_srem" , "hello" , "ecache" ).Err ())
483
+ },
484
+ after : func (ctx context.Context , t * testing.T ) {
485
+ assert .Equal (t , int64 (1 ), rdb .SCard (context .Background (), "test_e2e_srem" ).Val ())
486
+ require .NoError (t , rdb .Del (context .Background (), "test_e2e_srem" ).Err ())
487
+ },
488
+ key : "test_e2e_srem" ,
489
+ val : []any {"hello" , "go" },
490
+ wantVal : 1 ,
491
+ },
492
+ }
493
+
494
+ for _ , tc := range testCase {
495
+ t .Run (tc .name , func (t * testing.T ) {
496
+ ctx , cancelFunc := context .WithTimeout (context .Background (), time .Second * 5 )
497
+ defer cancelFunc ()
498
+ c := NewCache (rdb )
499
+ tc .before (ctx , t )
500
+ val := c .SRem (ctx , tc .key , tc .val ... )
501
+ assert .Equal (t , val .Val , tc .wantVal )
502
+ assert .Equal (t , val .Err , tc .wantErr )
503
+ tc .after (ctx , t )
504
+ })
505
+ }
506
+ }
0 commit comments