forked from HDT3213/godis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhash_test.go
286 lines (258 loc) · 9.02 KB
/
hash_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
package godis
import (
"fmt"
"github.com/hdt3213/godis/lib/utils"
"github.com/hdt3213/godis/redis/reply"
"github.com/hdt3213/godis/redis/reply/asserts"
"strconv"
"testing"
)
func TestHSet(t *testing.T) {
testDB.Flush()
size := 100
// test hset
key := utils.RandString(10)
values := make(map[string][]byte, size)
for i := 0; i < size; i++ {
value := utils.RandString(10)
field := strconv.Itoa(i)
values[field] = []byte(value)
result := testDB.Exec(nil, utils.ToCmdLine("hset", key, field, value))
if intResult, _ := result.(*reply.IntReply); intResult.Code != int64(1) {
t.Error(fmt.Sprintf("expected %d, actually %d", 1, intResult.Code))
}
}
// test hget and hexists
for field, v := range values {
actual := testDB.Exec(nil, utils.ToCmdLine("hget", key, field))
expected := reply.MakeBulkReply(v)
if !utils.BytesEquals(actual.ToBytes(), expected.ToBytes()) {
t.Error(fmt.Sprintf("expected %s, actually %s", string(expected.ToBytes()), string(actual.ToBytes())))
}
actual = testDB.Exec(nil, utils.ToCmdLine("hexists", key, field))
if intResult, _ := actual.(*reply.IntReply); intResult.Code != int64(1) {
t.Error(fmt.Sprintf("expected %d, actually %d", 1, intResult.Code))
}
}
// test hlen
actual := testDB.Exec(nil, utils.ToCmdLine("hlen", key))
if intResult, _ := actual.(*reply.IntReply); intResult.Code != int64(len(values)) {
t.Error(fmt.Sprintf("expected %d, actually %d", len(values), intResult.Code))
}
}
func TestHDel(t *testing.T) {
testDB.Flush()
size := 100
// set values
key := utils.RandString(10)
fields := make([]string, size)
for i := 0; i < size; i++ {
value := utils.RandString(10)
field := strconv.Itoa(i)
fields[i] = field
testDB.Exec(nil, utils.ToCmdLine("hset", key, field, value))
}
// test HDel
args := []string{key}
args = append(args, fields...)
actual := testDB.Exec(nil, utils.ToCmdLine2("hdel", args...))
if intResult, _ := actual.(*reply.IntReply); intResult.Code != int64(len(fields)) {
t.Error(fmt.Sprintf("expected %d, actually %d", len(fields), intResult.Code))
}
actual = testDB.Exec(nil, utils.ToCmdLine("hlen", key))
if intResult, _ := actual.(*reply.IntReply); intResult.Code != int64(0) {
t.Error(fmt.Sprintf("expected %d, actually %d", 0, intResult.Code))
}
}
func TestHMSet(t *testing.T) {
testDB.Flush()
size := 100
// test hset
key := utils.RandString(10)
fields := make([]string, size)
values := make([]string, size)
setArgs := []string{key}
for i := 0; i < size; i++ {
fields[i] = utils.RandString(10)
values[i] = utils.RandString(10)
setArgs = append(setArgs, fields[i], values[i])
}
result := testDB.Exec(nil, utils.ToCmdLine2("hmset", setArgs...))
if _, ok := result.(*reply.OkReply); !ok {
t.Error(fmt.Sprintf("expected ok, actually %s", string(result.ToBytes())))
}
// test HMGet
getArgs := []string{key}
getArgs = append(getArgs, fields...)
actual := testDB.Exec(nil, utils.ToCmdLine2("hmget", getArgs...))
expected := reply.MakeMultiBulkReply(utils.ToCmdLine(values...))
if !utils.BytesEquals(actual.ToBytes(), expected.ToBytes()) {
t.Error(fmt.Sprintf("expected %s, actually %s", string(expected.ToBytes()), string(actual.ToBytes())))
}
}
func TestHGetAll(t *testing.T) {
testDB.Flush()
size := 100
key := utils.RandString(10)
fields := make([]string, size)
valueSet := make(map[string]bool, size)
valueMap := make(map[string]string)
all := make([]string, 0)
for i := 0; i < size; i++ {
fields[i] = utils.RandString(10)
value := utils.RandString(10)
all = append(all, fields[i], value)
valueMap[fields[i]] = value
valueSet[value] = true
execHSet(testDB, utils.ToCmdLine(key, fields[i], value))
}
// test HGetAll
result := testDB.Exec(nil, utils.ToCmdLine("hgetall", key))
multiBulk, ok := result.(*reply.MultiBulkReply)
if !ok {
t.Error(fmt.Sprintf("expected MultiBulkReply, actually %s", string(result.ToBytes())))
}
if 2*len(fields) != len(multiBulk.Args) {
t.Error(fmt.Sprintf("expected %d items , actually %d ", 2*len(fields), len(multiBulk.Args)))
}
for i := range fields {
field := string(multiBulk.Args[2*i])
actual := string(multiBulk.Args[2*i+1])
expected, ok := valueMap[field]
if !ok {
t.Error(fmt.Sprintf("unexpected field %s", field))
continue
}
if actual != expected {
t.Error(fmt.Sprintf("expected %s, actually %s", expected, actual))
}
}
// test HKeys
result = testDB.Exec(nil, utils.ToCmdLine("hkeys", key))
multiBulk, ok = result.(*reply.MultiBulkReply)
if !ok {
t.Error(fmt.Sprintf("expected MultiBulkReply, actually %s", string(result.ToBytes())))
}
if len(fields) != len(multiBulk.Args) {
t.Error(fmt.Sprintf("expected %d items , actually %d ", len(fields), len(multiBulk.Args)))
}
for _, v := range multiBulk.Args {
field := string(v)
if _, ok := valueMap[field]; !ok {
t.Error(fmt.Sprintf("unexpected field %s", field))
}
}
// test HVals
result = testDB.Exec(nil, utils.ToCmdLine("hvals", key))
multiBulk, ok = result.(*reply.MultiBulkReply)
if !ok {
t.Error(fmt.Sprintf("expected MultiBulkReply, actually %s", string(result.ToBytes())))
}
if len(fields) != len(multiBulk.Args) {
t.Error(fmt.Sprintf("expected %d items , actually %d ", len(fields), len(multiBulk.Args)))
}
for _, v := range multiBulk.Args {
value := string(v)
_, ok := valueSet[value]
if !ok {
t.Error(fmt.Sprintf("unexpected value %s", value))
}
}
}
func TestHIncrBy(t *testing.T) {
testDB.Flush()
key := utils.RandString(10)
result := testDB.Exec(nil, utils.ToCmdLine("hincrby", key, "a", "1"))
if bulkResult, _ := result.(*reply.BulkReply); string(bulkResult.Arg) != "1" {
t.Error(fmt.Sprintf("expected %s, actually %s", "1", string(bulkResult.Arg)))
}
result = testDB.Exec(nil, utils.ToCmdLine("hincrby", key, "a", "1"))
if bulkResult, _ := result.(*reply.BulkReply); string(bulkResult.Arg) != "2" {
t.Error(fmt.Sprintf("expected %s, actually %s", "2", string(bulkResult.Arg)))
}
result = testDB.Exec(nil, utils.ToCmdLine("hincrbyfloat", key, "b", "1.2"))
if bulkResult, _ := result.(*reply.BulkReply); string(bulkResult.Arg) != "1.2" {
t.Error(fmt.Sprintf("expected %s, actually %s", "1.2", string(bulkResult.Arg)))
}
result = testDB.Exec(nil, utils.ToCmdLine("hincrbyfloat", key, "b", "1.2"))
if bulkResult, _ := result.(*reply.BulkReply); string(bulkResult.Arg) != "2.4" {
t.Error(fmt.Sprintf("expected %s, actually %s", "2.4", string(bulkResult.Arg)))
}
}
func TestHSetNX(t *testing.T) {
testDB.Flush()
key := utils.RandString(10)
field := utils.RandString(10)
value := utils.RandString(10)
result := testDB.Exec(nil, utils.ToCmdLine("hsetnx", key, field, value))
asserts.AssertIntReply(t, result, 1)
value2 := utils.RandString(10)
result = testDB.Exec(nil, utils.ToCmdLine("hsetnx", key, field, value2))
asserts.AssertIntReply(t, result, 0)
result = testDB.Exec(nil, utils.ToCmdLine("hget", key, field))
asserts.AssertBulkReply(t, result, value)
}
func TestUndoHDel(t *testing.T) {
testDB.Flush()
key := utils.RandString(10)
field := utils.RandString(10)
value := utils.RandString(10)
testDB.Exec(nil, utils.ToCmdLine("hset", key, field, value))
cmdLine := utils.ToCmdLine("hdel", key, field)
undoCmdLines := undoHDel(testDB, cmdLine[1:])
testDB.Exec(nil, cmdLine)
for _, cmdLine := range undoCmdLines {
testDB.Exec(nil, cmdLine)
}
result := testDB.Exec(nil, utils.ToCmdLine("hget", key, field))
asserts.AssertBulkReply(t, result, value)
}
func TestUndoHSet(t *testing.T) {
testDB.Flush()
key := utils.RandString(10)
field := utils.RandString(10)
value := utils.RandString(10)
value2 := utils.RandString(10)
testDB.Exec(nil, utils.ToCmdLine("hset", key, field, value))
cmdLine := utils.ToCmdLine("hset", key, field, value2)
undoCmdLines := undoHSet(testDB, cmdLine[1:])
testDB.Exec(nil, cmdLine)
for _, cmdLine := range undoCmdLines {
testDB.Exec(nil, cmdLine)
}
result := testDB.Exec(nil, utils.ToCmdLine("hget", key, field))
asserts.AssertBulkReply(t, result, value)
}
func TestUndoHMSet(t *testing.T) {
testDB.Flush()
key := utils.RandString(10)
field1 := utils.RandString(10)
field2 := utils.RandString(10)
value := utils.RandString(10)
value2 := utils.RandString(10)
testDB.Exec(nil, utils.ToCmdLine("hmset", key, field1, value, field2, value))
cmdLine := utils.ToCmdLine("hmset", key, field1, value2, field2, value2)
undoCmdLines := undoHMSet(testDB, cmdLine[1:])
testDB.Exec(nil, cmdLine)
for _, cmdLine := range undoCmdLines {
testDB.Exec(nil, cmdLine)
}
result := testDB.Exec(nil, utils.ToCmdLine("hget", key, field1))
asserts.AssertBulkReply(t, result, value)
result = testDB.Exec(nil, utils.ToCmdLine("hget", key, field2))
asserts.AssertBulkReply(t, result, value)
}
func TestUndoHIncr(t *testing.T) {
testDB.Flush()
key := utils.RandString(10)
field := utils.RandString(10)
testDB.Exec(nil, utils.ToCmdLine("hset", key, field, "1"))
cmdLine := utils.ToCmdLine("hinctby", key, field, "2")
undoCmdLines := undoHIncr(testDB, cmdLine[1:])
testDB.Exec(nil, cmdLine)
for _, cmdLine := range undoCmdLines {
testDB.Exec(nil, cmdLine)
}
result := testDB.Exec(nil, utils.ToCmdLine("hget", key, field))
asserts.AssertBulkReply(t, result, "1")
}