Skip to content

Commit c18ca13

Browse files
author
Depravity-pig
authored
新增 set 操作方法(sadd、srem)调整测试 (#14)
* 新增 set 操作方法(sadd、srem)调整测试 * 注释查看覆盖率关闭的tag * 新增srem 部分忽略测试、新增对应key不存在测试
1 parent e4259a9 commit c18ca13

File tree

4 files changed

+328
-26
lines changed

4 files changed

+328
-26
lines changed

redis/cache.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,12 @@ func (c *Cache) LPop(ctx context.Context, key string) (result ecache.Value) {
6969
}
7070
return
7171
}
72+
73+
func (c *Cache) SAdd(ctx context.Context, key string, members ...any) (int64, error) {
74+
return c.client.SAdd(ctx, key, members...).Result()
75+
}
76+
77+
func (c *Cache) SRem(ctx context.Context, key string, members ...any) (result ecache.Value) {
78+
result.Val, result.Err = c.client.SRem(ctx, key, members...).Result()
79+
return
80+
}

redis/cache_e2e_test.go

Lines changed: 165 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func TestCache_e2e_Set(t *testing.T) {
4444
wantErr error
4545
}{
4646
{
47-
name: "set value",
47+
name: "set e2e value",
4848
after: func(ctx context.Context, t *testing.T) {
4949
result, err := rdb.Get(ctx, "name").Result()
5050
require.NoError(t, err)
@@ -89,7 +89,7 @@ func TestCache_e2e_Get(t *testing.T) {
8989
wantErr error
9090
}{
9191
{
92-
name: "get value",
92+
name: "get e2e value",
9393
before: func(ctx context.Context, t *testing.T) {
9494
require.NoError(t, rdb.Set(ctx, "name", "大明", time.Minute).Err())
9595
},
@@ -101,7 +101,7 @@ func TestCache_e2e_Get(t *testing.T) {
101101
wantVal: "大明",
102102
},
103103
{
104-
name: "get error",
104+
name: "get e2e error",
105105
key: "name",
106106
before: func(ctx context.Context, t *testing.T) {},
107107
after: func(ctx context.Context, t *testing.T) {},
@@ -144,7 +144,7 @@ func TestCache_e2e_SetNX(t *testing.T) {
144144
wantVal bool
145145
}{
146146
{
147-
name: "test setnx",
147+
name: "setnx e2e value",
148148
before: func(ctx context.Context, t *testing.T) {},
149149
after: func(ctx context.Context, t *testing.T) {
150150
assert.NoError(t, rdb.Del(context.Background(), "testnx").Err())
@@ -154,14 +154,14 @@ func TestCache_e2e_SetNX(t *testing.T) {
154154
wantVal: true,
155155
},
156156
{
157-
name: "test setnx fail",
157+
name: "setnx e2e fail",
158158
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())
160160
},
161161
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())
163163
},
164-
key: "testnxf",
164+
key: "testnx",
165165
val: "hello go",
166166
wantVal: false,
167167
},
@@ -199,11 +199,12 @@ func TestCache_e2e_GetSet(t *testing.T) {
199199
wantErr error
200200
}{
201201
{
202-
name: "test_get_set",
202+
name: "getset e2e value",
203203
before: func(ctx context.Context, t *testing.T) {
204204
require.NoError(t, rdb.Set(context.Background(), "test_get_set", "hello ecache", time.Second*10).Err())
205205
},
206206
after: func(ctx context.Context, t *testing.T) {
207+
assert.Equal(t, "hello go", rdb.Get(context.Background(), "test_get_set").Val())
207208
require.NoError(t, rdb.Del(context.Background(), "test_get_set").Err())
208209
},
209210
key: "test_get_set",
@@ -212,15 +213,15 @@ func TestCache_e2e_GetSet(t *testing.T) {
212213
wantVal: "hello ecache",
213214
},
214215
{
215-
name: "test_get_set",
216+
name: "getset e2e err",
216217
before: func(ctx context.Context, t *testing.T) {},
217218
after: func(ctx context.Context, t *testing.T) {
219+
assert.Equal(t, "hello key notfound", rdb.Get(context.Background(), "test_get_set").Val())
218220
require.NoError(t, rdb.Del(context.Background(), "test_get_set").Err())
219221
},
220222
key: "test_get_set",
221223
val: "hello key notfound",
222224
expire: time.Second * 10,
223-
wantVal: "",
224225
wantErr: errs.ErrKeyNotExist,
225226
},
226227
}
@@ -254,7 +255,7 @@ func TestCache_e2e_LPush(t *testing.T) {
254255
wantVal int64
255256
}{
256257
{
257-
name: "test_cache_lpush",
258+
name: "lpush e2e value",
258259
before: func(ctx context.Context, t *testing.T) {},
259260
after: func(ctx context.Context, t *testing.T) {
260261
assert.Equal(t, int64(2), rdb.LLen(context.Background(), "test_cache_lpush").Val())
@@ -265,7 +266,7 @@ func TestCache_e2e_LPush(t *testing.T) {
265266
wantVal: 2,
266267
},
267268
{
268-
name: "test_cache_lpush_want_val",
269+
name: "lpush e2e want value",
269270
before: func(ctx context.Context, t *testing.T) {
270271
require.NoError(t, rdb.LPush(context.Background(), "test_cache_lpush", "hello ecache", "hello go").Err())
271272
},
@@ -308,7 +309,7 @@ func TestCache_e2e_LPop(t *testing.T) {
308309
wantErr error
309310
}{
310311
{
311-
name: "test_cache_pop",
312+
name: "lpop e2e value",
312313
before: func(ctx context.Context, t *testing.T) {
313314
require.NoError(t, rdb.LPush(context.Background(), "test_cache_pop", "1", "2", "3", "4").Err())
314315
},
@@ -320,7 +321,7 @@ func TestCache_e2e_LPop(t *testing.T) {
320321
wantVal: "4",
321322
},
322323
{
323-
name: "test_cache_pop_one",
324+
name: "lpop e2e one value",
324325
before: func(ctx context.Context, t *testing.T) {
325326
require.NoError(t, rdb.LPush(context.Background(), "test_cache_pop", "1").Err())
326327
require.NoError(t, rdb.LPop(context.Background(), "test_cache_pop").Err())
@@ -333,7 +334,7 @@ func TestCache_e2e_LPop(t *testing.T) {
333334
wantErr: errs.ErrKeyNotExist,
334335
},
335336
{
336-
name: "test_cache_pop_err_nil",
337+
name: "lpop e2e err",
337338
before: func(ctx context.Context, t *testing.T) {},
338339
after: func(ctx context.Context, t *testing.T) {},
339340
key: "test_cache_pop",
@@ -355,3 +356,151 @@ func TestCache_e2e_LPop(t *testing.T) {
355356
})
356357
}
357358
}
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

Comments
 (0)