forked from DiceDB/dice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhsetnx_test.go
73 lines (68 loc) · 2.38 KB
/
hsetnx_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
package http
import (
"testing"
"time"
"gotest.tools/v3/assert"
)
func TestHSetNX(t *testing.T) {
exec := NewHTTPCommandExecutor()
testCases := []struct {
name string
commands []HTTPCommand
expected []interface{}
delays []time.Duration
}{
{
name: "HSetNX returns 0 when field is already set",
commands: []HTTPCommand{
{Command: "HSETNX", Body: map[string]interface{}{"key": "key_nx_t1", "field": "field", "value": "value"}},
{Command: "HSETNX", Body: map[string]interface{}{"key": "key_nx_t1", "field": "field", "value": "value_new"}},
},
expected: []interface{}{float64(1), float64(0)},
delays: []time.Duration{0, 0},
},
{
name: "HSetNX with new field",
commands: []HTTPCommand{
{Command: "HSETNX", Body: map[string]interface{}{"key": "key_nx_t2", "field": "field", "value": "value"}},
},
expected: []interface{}{float64(1)},
delays: []time.Duration{0},
},
{
name: "HSetNX with wrong number of arguments",
commands: []HTTPCommand{
{Command: "HSETNX", Body: map[string]interface{}{"key": "key_nx_t3", "field": "field", "value": "value"}},
{Command: "HSETNX", Body: map[string]interface{}{"key": "key_nx_t3", "field": "field", "value": "value_new"}},
{Command: "HSETNX", Body: map[string]interface{}{"key": "key_nx_t3"}},
},
expected: []interface{}{float64(1), float64(0), "ERR wrong number of arguments for 'hsetnx' command"},
delays: []time.Duration{0, 0, 0},
},
{
name: "HSetNX with wrong type",
commands: []HTTPCommand{
{Command: "SET", Body: map[string]interface{}{"key": "key_nx_t4", "value": "v"}},
{Command: "HSETNX", Body: map[string]interface{}{"key": "key_nx_t4", "field": "f", "value": "v_new"}},
},
expected: []interface{}{"OK", "WRONGTYPE Operation against a key holding the wrong kind of value"},
delays: []time.Duration{0, 0},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
for i, cmd := range tc.commands {
if tc.delays[i] > 0 {
time.Sleep(tc.delays[i])
}
result, err := exec.FireCommand(cmd)
if err != nil {
// Check if the error message matches the expected result
assert.Equal(t, tc.expected[i], err.Error(), "Error message mismatch for cmd %s", cmd)
} else {
assert.Equal(t, tc.expected[i], result, "Value mismatch for cmd %s, expected %v, got %v", cmd, tc.expected[i], result)
}
}
})
}
}