forked from DiceDB/dice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecr_test.go
145 lines (132 loc) · 3.27 KB
/
decr_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
package tests
import (
"fmt"
"math"
"testing"
"github.com/dicedb/dice/internal/server/utils"
"gotest.tools/v3/assert"
)
func TestDECR(t *testing.T) {
conn := getLocalConnection()
defer conn.Close()
testCases := []struct {
name string
commands []struct {
op string
key string
val int64
expectedErr string
}
}{
{
name: "Decrement multiple keys",
commands: []struct {
op string
key string
val int64
expectedErr string
}{
{"s", "key1", 3, utils.EmptyStr},
{"d", "key1", 2, utils.EmptyStr},
{"d", "key1", 1, utils.EmptyStr},
{"d", "key2", -1, utils.EmptyStr},
{"g", "key1", 1, utils.EmptyStr},
{"g", "key2", -1, utils.EmptyStr},
{"s", "key3", math.MinInt64 + 1, utils.EmptyStr},
{"d", "key3", math.MinInt64, utils.EmptyStr},
{"d", "key3", math.MinInt64, "ERR value is out of range"},
},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
for _, cmd := range tc.commands {
switch cmd.op {
case "s":
fireCommand(conn, fmt.Sprintf("SET %s %d", cmd.key, cmd.val))
case "d":
result := fireCommand(conn, fmt.Sprintf("DECR %s", cmd.key))
switch v := result.(type) {
case string:
assert.Equal(t, cmd.expectedErr, v)
case int64:
assert.Equal(t, cmd.val, v)
}
case "g":
result := fireCommand(conn, fmt.Sprintf("GET %s", cmd.key))
assert.Equal(t, cmd.val, result)
}
}
})
}
}
func TestDECRBY(t *testing.T) {
conn := getLocalConnection()
defer conn.Close()
type SetCommand struct {
key string
val int64
}
type DecrByCommand struct {
key string
decrValue any
expectedVal int64
expectedErr string
}
type GetCommand struct {
key string
expectedVal int64
}
testCases := []struct {
name string
setCommands []SetCommand
decrByCommands []DecrByCommand
getCommands []GetCommand
}{
{
name: "Decrement multiple keys",
setCommands: []SetCommand{
{"key1", 3},
{"key3", math.MinInt64 + 1},
},
decrByCommands: []DecrByCommand{
{"key1", int64(2), 1, utils.EmptyStr},
{"key1", int64(1), 0, utils.EmptyStr},
{"key4", int64(1), -1, utils.EmptyStr},
{"key3", int64(1), math.MinInt64, utils.EmptyStr},
{"key3", int64(math.MinInt64), 0, "ERR value is out of range"},
{"key5", "abc", 0, "ERR value is not an integer or out of range"},
},
getCommands: []GetCommand{
{"key1", 0},
{"key4", -1},
},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
for _, cmd := range tc.setCommands {
fireCommand(conn, fmt.Sprintf("SET %s %d", cmd.key, cmd.val))
}
for _, cmd := range tc.decrByCommands {
var result any
switch v := cmd.decrValue.(type) {
case int64:
result = fireCommand(conn, fmt.Sprintf("DECRBY %s %d", cmd.key, v))
case string:
result = fireCommand(conn, fmt.Sprintf("DECRBY %s %s", cmd.key, v))
}
switch v := result.(type) {
case string:
assert.Equal(t, cmd.expectedErr, v)
case int64:
assert.Equal(t, cmd.expectedVal, v)
}
}
for _, cmd := range tc.getCommands {
result := fireCommand(conn, fmt.Sprintf("GET %s", cmd.key))
assert.Equal(t, cmd.expectedVal, result)
}
})
}
}