-
Notifications
You must be signed in to change notification settings - Fork 206
/
test_redis_client_test.go
176 lines (152 loc) · 3.5 KB
/
test_redis_client_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package rmq
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestTestRedisClient_Set(t *testing.T) {
type args struct {
key string
value string
expiration time.Duration
}
tests := []struct {
name string
client *TestRedisClient
args args
}{
{
"successful add",
NewTestRedisClient(),
args{
"somekey",
"somevalue",
time.Duration(0),
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// add
err := tt.client.Set(tt.args.key, tt.args.value, tt.args.expiration)
assert.NoError(t, err)
// get
v, err := tt.client.Get(tt.args.key)
assert.Equal(t, tt.args.value, v)
assert.NoError(t, err)
// delete
affected, err := tt.client.Del(tt.args.key)
assert.Equal(t, int64(1), affected)
assert.NoError(t, err)
// delete it again
affected, err = tt.client.Del(tt.args.key)
assert.Equal(t, int64(0), affected)
assert.NoError(t, err)
})
}
}
func TestTestRedisClient_SAdd(t *testing.T) {
type args struct {
key string
value string
}
tests := []struct {
name string
client *TestRedisClient
args args
}{
{
"adding member",
NewTestRedisClient(),
args{
"somekey",
"somevalue",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
total, err := tt.client.SAdd(tt.args.key, tt.args.value)
assert.NoError(t, err)
assert.Equal(t, int64(1), total)
total, err = tt.client.SAdd(tt.args.key, tt.args.value)
assert.NoError(t, err)
assert.Equal(t, int64(1), total)
members, err := tt.client.SMembers(tt.args.key)
assert.Equal(t, []string{tt.args.value}, members)
assert.NoError(t, err)
count, err := tt.client.SRem(tt.args.key, tt.args.value)
assert.Equal(t, int64(1), count)
assert.NoError(t, err)
})
}
}
func TestTestRedisClient_LPush(t *testing.T) {
type args struct {
key string
value string
}
tests := []struct {
name string
client *TestRedisClient
args args
total int64
}{
{
"adding to list",
NewTestRedisClient(),
args{
"somekey",
"somevalue",
},
1,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Push
total, err := tt.client.LPush(tt.args.key, tt.args.value)
assert.NoError(t, err)
assert.Equal(t, tt.total, total)
// Len
count, err := tt.client.LLen(tt.args.key)
assert.Equal(t, int64(1), count)
assert.NoError(t, err)
// Len of non-existing
count, err = tt.client.LLen(tt.args.key + "nonsense")
assert.Equal(t, int64(0), count)
assert.NoError(t, err)
// Lrem
count, err = tt.client.LRem(tt.args.key, 100, tt.args.value)
assert.Equal(t, int64(1), count)
assert.NoError(t, err)
// Len again
count, err = tt.client.LLen(tt.args.key)
assert.Equal(t, int64(0), count)
assert.NoError(t, err)
})
}
}
func TestTestRedisClient_LPush_Len(t *testing.T) {
client := NewTestRedisClient()
key := "list-key"
total, err := client.LPush(key, "1", "2", "3")
assert.NoError(t, err)
assert.Equal(t, int64(3), total)
total, err = client.LPush(key, "4", "5", "6")
assert.NoError(t, err)
assert.Equal(t, int64(6), total)
}
func TestTestRedisClient_RPop(t *testing.T) {
client := NewTestRedisClient()
key := "list-key"
total, err := client.LPush(key, "1", "2", "3")
assert.NoError(t, err)
assert.Equal(t, int64(3), total)
value, err := client.RPop(key)
assert.NoError(t, err)
assert.Equal(t, "3", value)
total, err = client.LLen(key)
assert.NoError(t, err)
assert.Equal(t, int64(2), total)
}