Skip to content

Commit

Permalink
#1020 Migrate SADD, SREM, SCARD, SMEMBERS command to store_eval (#1068)
Browse files Browse the repository at this point in the history
  • Loading branch information
sahoss authored Nov 6, 2024
1 parent 0323fae commit 3866213
Show file tree
Hide file tree
Showing 10 changed files with 681 additions and 261 deletions.
109 changes: 0 additions & 109 deletions integration_tests/commands/async/set_data_cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,115 +36,6 @@ func TestSetDataCommand(t *testing.T) {
assertType []string
delay []time.Duration
}{
// SADD
{
name: "SADD Simple Value",
cmd: []string{"SADD foo bar", "SMEMBERS foo"},
expected: []interface{}{int64(1), []any{"bar"}},
assertType: []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{"bar", "baz"}},
assertType: []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{"bar"}},
assertType: []string{"equal", "equal", "equal"},
delay: []time.Duration{0, 0, 0},
},
{
name: "SADD Wrong Key Value Type",
cmd: []string{"SET foo bar", "SADD foo baz"},
expected: []interface{}{"OK", "WRONGTYPE Operation against a key holding the wrong kind of value"},
assertType: []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{"bar", "baz", "1"}},
assertType: []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)},
assertType: []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)},
assertType: []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"},
assertType: []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{"bar", "baz"}},
assertType: []string{"equal", "equal", "equal"},
delay: []time.Duration{0, 0, 0},
},
{
name: "SADD & SMEMBERS with non existing key",
cmd: []string{"SMEMBERS foo"},
expected: []interface{}{[]any{}},
assertType: []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"},
assertType: []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{"baz"}},
assertType: []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)},
assertType: []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"},
assertType: []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{"bar", "baz", "bax"}, int64(0), []any{"bar", "baz", "bax"}},
assertType: []string{"equal", "equal", "equal", "equal"},
delay: []time.Duration{0, 0, 0, 0},
},
// SADD & SDIFF
{
name: "SADD & SDIFF",
Expand Down
164 changes: 164 additions & 0 deletions integration_tests/commands/resp/set_data_cmd_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
package resp

import (
"gotest.tools/v3/assert"
"sort"
"testing"
"time"
)

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{}
assertType []string
delay []time.Duration
}{
// SADD
{
name: "SADD Simple Value",
cmd: []string{"SADD foo bar", "SMEMBERS foo"},
expected: []interface{}{int64(1), []any{"bar"}},
assertType: []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{"bar", "baz"}},
assertType: []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{"bar"}},
assertType: []string{"equal", "equal", "equal"},
delay: []time.Duration{0, 0, 0},
},
{
name: "SADD Wrong Key Value Type",
cmd: []string{"SET foo bar", "SADD foo baz"},
expected: []interface{}{"OK", "WRONGTYPE Operation against a key holding the wrong kind of value"},
assertType: []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{"bar", "baz", "1"}},
assertType: []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)},
assertType: []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)},
assertType: []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"},
assertType: []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{"bar", "baz"}},
assertType: []string{"equal", "equal", "equal"},
delay: []time.Duration{0, 0, 0},
},
{
name: "SADD & SMEMBERS with non existing key",
cmd: []string{"SMEMBERS foo"},
expected: []interface{}{[]any{}},
assertType: []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"},
assertType: []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{"baz"}},
assertType: []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)},
assertType: []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"},
assertType: []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{"bar", "baz", "bax"}, int64(0), []any{"bar", "baz", "bax"}},
assertType: []string{"equal", "equal", "equal", "equal"},
delay: []time.Duration{0, 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)
CustomDeepEqual(t, result, tc.expected[i])
}
})
}

}
28 changes: 16 additions & 12 deletions internal/eval/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -922,36 +922,40 @@ var (
Specified members that are already a member of this set are ignored
Non existing keys are treated as empty sets.
An error is returned when the value stored at key is not a set.`,
Eval: evalSADD,
Arity: -3,
KeySpecs: KeySpecs{BeginIndex: 1},
NewEval: evalSADD,
Arity: -3,
KeySpecs: KeySpecs{BeginIndex: 1},
IsMigrated: true,
}
smembersCmdMeta = DiceCmdMeta{
Name: "SMEMBERS",
Info: `SMEMBERS key
Returns all the members of the set value stored at key.`,
Eval: evalSMEMBERS,
Arity: 2,
KeySpecs: KeySpecs{BeginIndex: 1},
Arity: 2,
KeySpecs: KeySpecs{BeginIndex: 1},
IsMigrated: true,
NewEval: evalSMEMBERS,
}
sremCmdMeta = DiceCmdMeta{
Name: "SREM",
Info: `SREM key member [member ...]
Removes the specified members from the set stored at key.
Non existing keys are treated as empty sets.
An error is returned when the value stored at key is not a set.`,
Eval: evalSREM,
Arity: -3,
KeySpecs: KeySpecs{BeginIndex: 1},
Arity: -3,
KeySpecs: KeySpecs{BeginIndex: 1},
IsMigrated: true,
NewEval: evalSREM,
}
scardCmdMeta = DiceCmdMeta{
Name: "SCARD",
Info: `SCARD key
Returns the number of elements of the set stored at key.
An error is returned when the value stored at key is not a set.`,
Eval: evalSCARD,
Arity: 2,
KeySpecs: KeySpecs{BeginIndex: 1},
Arity: 2,
KeySpecs: KeySpecs{BeginIndex: 1},
IsMigrated: true,
NewEval: evalSCARD,
}
sdiffCmdMeta = DiceCmdMeta{
Name: "SDIFF",
Expand Down
3 changes: 2 additions & 1 deletion internal/eval/deque_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,8 @@ func TestLRange(t *testing.T) {
tc.dq.LPush(i)
}
output, _ := tc.dq.LRange(tc.start, tc.stop)
assert.DeepEqual(t, output, tc.expectedOutput)
assert.ElementsMatch(t, output, tc.expectedOutput)

})
}
}
Expand Down
Loading

0 comments on commit 3866213

Please sign in to comment.