forked from adjust/rmq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_redis_client_test.go
155 lines (138 loc) · 3.81 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
package rmq
import (
"strings"
"testing"
"time"
)
func TestTestRedisClient_Set(t *testing.T) {
type args struct {
key string
value string
expiration time.Duration
}
tests := []struct {
name string
client *TestRedisClient
args args
want bool
}{
{
"successfull add",
NewTestRedisClient(),
args{
"somekey",
"somevalue",
time.Duration(0),
},
true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
//add
if got := tt.client.Set(tt.args.key, tt.args.value, tt.args.expiration); got != tt.want {
t.Errorf("TestRedisClient.Set() = %v, want %v", got, tt.want)
}
//get
if strings.Compare(tt.client.Get(tt.args.key), tt.args.value) != 0 {
t.Errorf("TestRedisClient.Get(%v) =, want %v", tt.args.key, tt.args.value)
}
//delete
if affected, found := tt.client.Del(tt.args.key); affected != 1 || found != true {
t.Errorf("TestRedisClient.Del(%v) =, want %v, %v", tt.args.key, 1, true)
}
//delete it again
if affected, found := tt.client.Del(tt.args.key); affected != 0 || found != false {
t.Errorf("TestRedisClient.Del(%v) =, want %v, %v", tt.args.key, 0, false)
}
})
}
}
func TestTestRedisClient_SAdd(t *testing.T) {
type args struct {
key string
value string
}
tests := []struct {
name string
client *TestRedisClient
args args
want bool
}{
{
"adding member",
NewTestRedisClient(),
args{
"somekey",
"somevalue",
},
true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.client.SAdd(tt.args.key, tt.args.value); got != tt.want {
t.Errorf("TestRedisClient.SAdd() = %v, want %v", got, tt.want)
}
if got := tt.client.SAdd(tt.args.key, tt.args.value); got != tt.want {
t.Errorf("TestRedisClient.SAdd() = %v, want %v", got, tt.want)
}
if got := tt.client.SMembers(tt.args.key); len(got) != 1 || strings.Compare(got[0], tt.args.value) != 0 {
t.Errorf("TestRedisClient.SMembers(%v) = %v, want %v", tt.args.key, got, []string{tt.args.value})
}
if got, ok := tt.client.SRem(tt.args.key, tt.args.value); got != 1 || ok != true {
t.Errorf("TestRedisClient.SRem(%v, %v) = %v, %v, want %v, %v", tt.args.key, tt.args.value, got, ok, 1, true)
}
})
}
}
func TestTestRedisClient_LPush(t *testing.T) {
type args struct {
key string
value string
}
tests := []struct {
name string
client *TestRedisClient
args args
want bool
}{
{
"adding to list",
NewTestRedisClient(),
args{
"somekey",
"somevalue",
},
true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
//Push
if got := tt.client.LPush(tt.args.key, tt.args.value); got != tt.want {
t.Errorf("TestRedisClient.LPush() = %v, want %v", got, tt.want)
}
//Len
if got, ok := tt.client.LLen(tt.args.key); got != 1 || ok != true {
t.Errorf("TestRedisClient.LLen(%v) = %v, %v want %v, %v", tt.args.key, got, ok, 1, true)
}
//Len of non-existing
if got, ok := tt.client.LLen(tt.args.key + "nonsense"); got != 0 || ok != true {
t.Errorf("TestRedisClient.LLen(%vnonsense) = %v, %v want %v, %v", tt.args.key, got, ok, 0, true)
}
//Range
if got := tt.client.LRange(tt.args.key, 0, 100); len(got) != 1 || strings.Compare(got[0], tt.args.value) != 0 {
t.Errorf("TestRedisClient.LRange(%v, 0, 100) = %v want %v", tt.args.key, got, []string{tt.args.value})
}
//Lrem
if got, ok := tt.client.LRem(tt.args.key, 100, tt.args.value); got != 1 || ok != true {
t.Errorf("TestRedisClient.LRem(%v, 100, %v) = %v, %v want %v, %v", tt.args.key, tt.args.value, got, ok, 1, true)
}
//Len again
if got, ok := tt.client.LLen(tt.args.key); got != 0 || ok != true {
t.Errorf("TestRedisClient.LLen(%v) = %v, %v want %v, %v", tt.args.key, got, ok, 0, true)
}
})
}
}