forked from DiceDB/dice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathset_data_cmd_test.go
239 lines (232 loc) · 8.69 KB
/
set_data_cmd_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
package tests
import (
"sort"
"testing"
"time"
"gotest.tools/v3/assert"
)
func CustomDeepEqual(t *testing.T, a, b interface{}) {
if a == nil || b == nil {
assert.DeepEqual(t, a, b)
}
switch a.(type) {
case []any:
sort.Slice(a.([]any), func(i, j int) bool {
return a.([]any)[i].(string) < a.([]any)[j].(string)
})
sort.Slice(b.([]any), func(i, j int) bool {
return b.([]any)[i].(string) < b.([]any)[j].(string)
})
}
assert.DeepEqual(t, a, b)
}
func TestSetDataCommand(t *testing.T) {
conn := getLocalConnection()
defer conn.Close()
testCases := []struct {
name string
cmd []string
expected []interface{}
assert_type []string
delay []time.Duration
}{
// SADD
{
name: "SADD Simple Value",
cmd: []string{"SADD foo bar", "SMEMBERS foo"},
expected: []interface{}{int64(1), []any{string("bar")}},
assert_type: []string{"equal", "equal"},
delay: []time.Duration{0, 0},
},
{
name: "SADD Multiple Values",
cmd: []string{"SADD foo bar", "SADD foo baz", "SMEMBERS foo"},
expected: []interface{}{int64(1), int64(1), []any{string("bar"), string("baz")}},
assert_type: []string{"equal", "equal", "equal"},
delay: []time.Duration{0, 0, 0},
},
{
name: "SADD Duplicate Values",
cmd: []string{"SADD foo bar", "SADD foo bar", "SMEMBERS foo"},
expected: []interface{}{int64(1), int64(0), []any{string("bar")}},
assert_type: []string{"equal", "equal", "equal"},
delay: []time.Duration{0, 0, 0},
},
{
name: "SADD Wrong Key Valye Type",
cmd: []string{"SET foo bar", "SADD foo baz"},
expected: []interface{}{"OK", "WRONGTYPE Operation against a key holding the wrong kind of value"},
assert_type: []string{"equal", "equal"},
delay: []time.Duration{0, 0},
},
{
name: "SADD Multiple add and multiple kind of values",
cmd: []string{"SADD foo bar", "SADD foo baz", "SADD foo 1", "SMEMBERS foo"},
expected: []interface{}{int64(1), int64(1), int64(1), []any{string("bar"), string("baz"), string("1")}},
assert_type: []string{"equal", "equal", "equal", "equal"},
delay: []time.Duration{0, 0, 0, 0},
},
// SCARD
{
name: "SADD & SCARD",
cmd: []string{"SADD foo bar", "SADD foo baz", "SCARD foo"},
expected: []interface{}{int64(1), int64(1), int64(2)},
assert_type: []string{"equal", "equal", "equal"},
delay: []time.Duration{0, 0, 0},
},
{
name: "SADD & CARD with non existing key",
cmd: []string{"SADD foo bar", "SADD foo baz", "SCARD bar"},
expected: []interface{}{int64(1), int64(1), int64(0)},
assert_type: []string{"equal", "equal", "equal"},
delay: []time.Duration{0, 0, 0},
},
{
name: "SADD & SCARD with wrong key type",
cmd: []string{"SET foo bar", "SCARD foo"},
expected: []interface{}{"OK", "WRONGTYPE Operation against a key holding the wrong kind of value"},
assert_type: []string{"equal", "equal"},
delay: []time.Duration{0, 0},
},
// SMEMBERS
{
name: "SADD & SMEMBERS",
cmd: []string{"SADD foo bar", "SADD foo baz", "SMEMBERS foo"},
expected: []interface{}{int64(1), int64(1), []any{string("bar"), string("baz")}},
assert_type: []string{"equal", "equal", "equal"},
delay: []time.Duration{0, 0, 0},
},
{
name: "SADD & SMEMBERS with non existing key",
cmd: []string{"SMEMBERS foo"},
expected: []interface{}{[]any{}},
assert_type: []string{"equal"},
delay: []time.Duration{0},
},
{
name: "SADD & SMEMBERS with wrong key type",
cmd: []string{"SET foo bar", "SMEMBERS foo"},
expected: []interface{}{"OK", "WRONGTYPE Operation against a key holding the wrong kind of value"},
assert_type: []string{"equal", "equal"},
delay: []time.Duration{0, 0},
},
// SREM
{
name: "SADD & SREM",
cmd: []string{"SADD foo bar", "SADD foo baz", "SREM foo bar", "SMEMBERS foo"},
expected: []interface{}{int64(1), int64(1), int64(1), []any{string("baz")}},
assert_type: []string{"equal", "equal", "equal", "equal"},
delay: []time.Duration{0, 0, 0, 0},
},
{
name: "SADD & SREM with non existing key",
cmd: []string{"SREM foo bar"},
expected: []interface{}{int64(0)},
assert_type: []string{"equal"},
delay: []time.Duration{0},
},
{
name: "SADD & SREM with wrong key type",
cmd: []string{"SET foo bar", "SREM foo bar"},
expected: []interface{}{"OK", "WRONGTYPE Operation against a key holding the wrong kind of value"},
assert_type: []string{"equal", "equal"},
delay: []time.Duration{0, 0},
},
{
name: "SADD & SREM with non existing value",
cmd: []string{"SADD foo bar baz bax", "SMEMBERS foo", "SREM foo bat", "SMEMBERS foo"},
expected: []interface{}{int64(3), []any{string("bar"), string("baz"), string("bax")}, int64(0), []any{string("bar"), string("baz"), string("bax")}},
assert_type: []string{"equal", "equal", "equal", "equal"},
delay: []time.Duration{0, 0, 0, 0},
},
// SADD & SDIFF
{
name: "SADD & SDIFF",
cmd: []string{"SADD foo bar baz", "SADD foo2 baz bax", "SDIFF foo foo2"},
expected: []interface{}{int64(2), int64(2), []any{string("bar")}},
assert_type: []string{"equal", "equal", "equal"},
delay: []time.Duration{0, 0, 0},
},
{
name: "SADD & SDIFF with non existing subsequent key",
cmd: []string{"SADD foo bar baz", "SDIFF foo foo2"},
expected: []interface{}{int64(2), []any{string("bar"), string("baz")}},
assert_type: []string{"equal", "equal"},
delay: []time.Duration{0, 0},
},
{
name: "SADD & SDIFF with wrong key type",
cmd: []string{"SET foo bar", "SDIFF foo foo2"},
expected: []interface{}{"OK", "WRONGTYPE Operation against a key holding the wrong kind of value"},
assert_type: []string{"equal", "equal"},
delay: []time.Duration{0, 0},
},
{
name: "SADD & SDIFF with subsequent key of wrong type",
cmd: []string{"SADD foo bar baz", "SET foo2 bar", "SDIFF foo foo2"},
expected: []interface{}{int64(2), "OK", "WRONGTYPE Operation against a key holding the wrong kind of value"},
assert_type: []string{"equal", "equal", "equal"},
delay: []time.Duration{0, 0, 0},
},
{
name: "SADD & SDIFF with non existing first key",
cmd: []string{"SADD foo bar baz", "SDIFF foo2 foo"},
expected: []interface{}{int64(2), []any{}},
assert_type: []string{"equal", "equal"},
delay: []time.Duration{0, 0},
},
{
name: "SADD & SDIFF with one key",
cmd: []string{"SADD foo bar baz", "SDIFF foo"},
expected: []interface{}{int64(2), []any{string("bar"), string("baz")}},
assert_type: []string{"equal", "equal"},
delay: []time.Duration{0, 0},
},
// SADD & SINTER
{
name: "SADD & SINTER",
cmd: []string{"SADD foo bar baz", "SADD foo2 baz bax", "SINTER foo foo2"},
expected: []interface{}{int64(2), int64(2), []any{string("baz")}},
assert_type: []string{"equal", "equal", "equal"},
delay: []time.Duration{0, 0, 0},
},
{
name: "SADD & SINTER with non existing subsequent key",
cmd: []string{"SADD foo bar baz", "SINTER foo foo2"},
expected: []interface{}{int64(2), []any{}},
assert_type: []string{"equal", "equal"},
delay: []time.Duration{0, 0},
},
{
name: "SADD & SINTER with wrong key type",
cmd: []string{"SET foo bar", "SINTER foo foo2"},
expected: []interface{}{"OK", "WRONGTYPE Operation against a key holding the wrong kind of value"},
assert_type: []string{"equal", "equal"},
delay: []time.Duration{0, 0},
},
{
name: "SADD & SINTER with subsequent key of wrong type",
cmd: []string{"SADD foo bar baz", "SET foo2 bar", "SINTER foo foo2"},
expected: []interface{}{int64(2), "OK", "WRONGTYPE Operation against a key holding the wrong kind of value"},
assert_type: []string{"equal", "equal", "equal"},
delay: []time.Duration{0, 0, 0},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
fireCommand(conn, "DEL foo")
fireCommand(conn, "DEL foo2")
for i, cmd := range tc.cmd {
if tc.delay[i] > 0 {
time.Sleep(tc.delay[i])
}
result := fireCommand(conn, cmd)
if tc.assert_type[i] == "equal" {
CustomDeepEqual(t, result, tc.expected[i])
} else if tc.assert_type[i] == "assert" {
assert.Assert(t, result.(int64) <= tc.expected[i].(int64), "Expected %v to be less than or equal to %v", result, tc.expected[i])
}
}
})
}
}