-
Notifications
You must be signed in to change notification settings - Fork 145
/
Copy pathreply_test.go
298 lines (254 loc) · 7.44 KB
/
reply_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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
// Copyright 2015 Joel Wu
//
// Licensed under the Apache License, Version 2.0 (the "License"): you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations
// under the License.
package redis
import (
"testing"
"strconv"
)
func TestRedisInt(t *testing.T) {
node := newRedisNode()
_, err := node.do("SET", "count", 10)
if err != nil {
t.Errorf("SET error: %s\n", err.Error())
}
count, err := Int(node.do("INCR", "count"))
if err != nil {
t.Errorf("SET error: %s\n", err.Error())
}
if count != 11 {
t.Errorf("unexpected count %d", count)
}
count, err = Int(node.do("INCRBY", "count", 10))
if err != nil {
t.Errorf("SET error: %s\n", err.Error())
}
if count != 21 {
t.Errorf("unexpected count %d", count)
}
_, err = node.do("SET", "count", "string string")
if err != nil {
t.Errorf("SET error: %s\n", err.Error())
}
count, err = Int(node.do("GET", "count"))
if err == nil {
t.Errorf("expected an error")
}
}
func TestRedisInt64(t *testing.T) {
node := newRedisNode()
count, err := Int64(node.do("LPUSH", "mylist", 10))
if err != nil {
t.Errorf("SET error: %s\n", err.Error())
}
if count != 1 {
t.Errorf("unexpected count %d", count)
}
count, err = Int64(node.do("LPUSH", "mylist", -20))
if err != nil {
t.Errorf("SET error: %s\n", err.Error())
}
if count != 2 {
t.Errorf("unexpected count %d", count)
}
value, err := Int64(node.do("RPOP", "mylist"))
if err != nil {
t.Errorf("SET error: %s\n", err.Error())
}
if value != 10 {
t.Errorf("unexpected count %d", count)
}
value, err = Int64(node.do("RPOP", "mylist"))
if err != nil {
t.Errorf("SET error: %s\n", err.Error())
}
if value != -20 {
t.Errorf("unexpected count %d", count)
}
}
func TestRedisFloat64(t *testing.T) {
node := newRedisNode()
_, err := node.do("SET", "pi", 3.14)
if err != nil {
t.Errorf("SET error: %s\n", err.Error())
}
pi, err := Float64(node.do("GET", "pi"))
if err != nil {
t.Errorf("SET error: %s\n", err.Error())
}
if strconv.FormatFloat(pi, 'f', -1, 64) != "3.14" {
t.Errorf("unexpected value: %f\n", pi)
}
}
func TestRedisString(t *testing.T) {
node := newRedisNode()
_, err := node.do("SET", "hello", "hello world")
if err != nil {
t.Errorf("SET error: %s\n", err.Error())
}
value, err := String(node.do("GET", "hello"))
if err != nil {
t.Errorf("SET error: %s\n", err.Error())
}
if value != "hello world" {
t.Errorf("unexpected value: %f\n", value)
}
}
func TestRedisBytes(t *testing.T) {
node := newRedisNode()
_, err := node.do("SET", "mykey1", "hello world")
if err != nil {
t.Errorf("SET error: %s\n", err.Error())
}
value, err := Bytes(node.do("GET", "mykey1"))
if err != nil {
t.Errorf("SET error: %s\n", err.Error())
}
if string(value) != "hello world" {
t.Errorf("unexpected value: %f\n", value)
}
}
func TestRedisBool(t *testing.T) {
node := newRedisNode()
_, err := node.do("SET", "mykey2", "true")
if err != nil {
t.Errorf("SET error: %s\n", err.Error())
}
value, err := Bool(node.do("GET", "mykey2"))
if err != nil {
t.Errorf("SET error: %s\n", err.Error())
}
if value != true {
t.Errorf("unexpected value: %t\n", value)
}
_, err = node.do("SET", "mykey2", 0)
if err != nil {
t.Errorf("SET error: %s\n", err.Error())
}
value, err = Bool(node.do("GET", "mykey2"))
if err != nil {
t.Errorf("SET error: %s\n", err.Error())
}
if value != false {
t.Errorf("unexpected value: %t\n", value)
}
}
func TestRedisInts(t *testing.T) {
node := newRedisNode()
_, err := node.do("MSET", "key1", 0, "key2", 1, "key3", 2)
if err != nil {
t.Errorf("SET error: %s\n", err.Error())
}
value, err := Ints(node.do("MGET", "key1", "key2", "key3"))
if err != nil {
t.Errorf("SET error: %s\n", err.Error())
}
if len(value) != 3 || value[0] != 0 || value[1] != 1 || value[2] != 2 {
t.Errorf("unexpected value: %v\n", value)
}
}
func TestRedisStrings(t *testing.T) {
node := newRedisNode()
_, err := node.do("LPUSH", "mylist2", "aaa")
if err != nil {
t.Errorf("SET error: %s\n", err.Error())
}
_, err = node.do("LPUSH", "mylist2", "bbb")
if err != nil {
t.Errorf("SET error: %s\n", err.Error())
}
_, err = node.do("LPUSH", "mylist2", "ccc")
if err != nil {
t.Errorf("SET error: %s\n", err.Error())
}
value, err := Strings(node.do("LRANGE", "mylist2", 0, -1))
if err != nil {
t.Errorf("SET error: %s\n", err.Error())
}
if len(value) != 3 || string(value[0]) != "ccc" || string(value[1]) != "bbb" || string(value[2]) != "aaa" {
t.Errorf("unexpected value: %v\n", value)
}
}
func TestRedisStringMap(t *testing.T) {
node := newRedisNode()
_, err := node.do("HMSET", "hashkey1", "name", "joel", "age", "26", "gender", "male")
if err != nil {
t.Errorf("HMSET error: %s\n", err.Error())
}
value, err := StringMap(node.do("HGETALL", "hashkey1"))
if err != nil {
t.Errorf("HGETALL error: %s\n", err.Error())
}
if v, ok := value["name"]; !ok || v != "joel" {
t.Errorf("unexpected value: %v\n", value)
}
if v, ok := value["age"]; !ok || v != "26" {
t.Errorf("unexpected value: %v\n", value)
}
if v, ok := value["gender"]; !ok || v != "male" {
t.Errorf("unexpected value: %v\n", value)
}
}
func TestRedisScan(t *testing.T) {
node := newRedisNode()
kv := []string{"key1", "hello", "key2", "-1024", "key3", "false", "key4", "-3.14"}
ikv := make([]interface{}, len(kv))
for i, _ := range ikv {
ikv[i] = kv[i]
}
_, err := node.do("MSET", ikv...)
if err != nil {
t.Errorf("MSET error: %s\n", err.Error())
}
keys := []string{"key1", "key2", "key3", "key4"}
ikeys := make([]interface{}, len(keys))
for i, _ := range ikeys {
ikeys[i] = keys[i]
}
reply, err := Values(node.do("MGET", ikeys...))
if err != nil {
t.Errorf("MSET error: %s\n", err.Error())
}
var stringVal string
var intVal int
var boolVal bool
var floatVal float64
if reply, err = Scan(reply, &stringVal); err != nil{
t.Errorf("Scan error: %s\n", err.Error())
}
if stringVal != "hello" {
t.Errorf("unexpected value: %s\n", stringVal)
}
if reply, err = Scan(reply, &intVal); err != nil{
t.Errorf("Scan error: %s\n", err.Error())
}
if intVal != -1024 {
t.Errorf("unexpected value: %s\n", stringVal)
}
if reply, err = Scan(reply, &intVal); err == nil{
t.Errorf("expected an error")
}
if reply, err = Scan(reply, &floatVal); err != nil{
t.Errorf("Scan error: %s\n", err.Error())
}
reply, err = Values(node.do("MGET", ikeys...))
if err != nil {
t.Errorf("MSET error: %s\n", err.Error())
}
if reply, err = Scan(reply, &stringVal, &intVal, &boolVal, &floatVal); err != nil{
t.Errorf("Scan error: %s\n", err.Error())
}
if stringVal != "hello" && intVal != -1024 && boolVal != false {
t.Errorf("unexpected value: %s %d %t %f\n", stringVal, intVal, boolVal, floatVal)
}
}