Skip to content

Commit

Permalink
DiceDB#903 Add support for SINTER single key parameter (DiceDB#906)
Browse files Browse the repository at this point in the history
  • Loading branch information
arushi-08 authored Oct 5, 2024
1 parent e4258b3 commit ab639d6
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 1 deletion.
10 changes: 10 additions & 0 deletions integration_tests/commands/http/set_data_cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,16 @@ func TestSetDataCmd(t *testing.T) {
assert_type: []string{"equal", "equal", "equal", "equal"},
expected: []interface{}{float64(1), float64(1), "OK", "WRONGTYPE Operation against a key holding the wrong kind of value"},
},
{
name: "SADD & SINTER with single key",
commands: []HTTPCommand{
{Command: "SADD", Body: map[string]interface{}{"key": "foo", "value": "bar"}},
{Command: "SADD", Body: map[string]interface{}{"key": "foo", "value": "baz"}},
{Command: "SINTER", Body: map[string]interface{}{"values": []interface{}{"foo"}}},
},
assert_type: []string{"equal", "equal", "unordered_equal"},
expected: []interface{}{float64(1), float64(1), []any{string("bar"), string("baz")}},
},
}

defer exec.FireCommand(HTTPCommand{
Expand Down
2 changes: 1 addition & 1 deletion internal/eval/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -3765,7 +3765,7 @@ func evalSDIFF(args []string, store *dstore.Store) []byte {
}

func evalSINTER(args []string, store *dstore.Store) []byte {
if len(args) < 2 {
if len(args) < 1 {
return diceerrors.NewErrArity("SINTER")
}

Expand Down
51 changes: 51 additions & 0 deletions internal/eval/eval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ func TestEval(t *testing.T) {
testEvalHINCRBYFLOAT(t, store)
testEvalGEOADD(t, store)
testEvalGEODIST(t, store)
testEvalSINTER(t, store)
}

func testEvalPING(t *testing.T, store *dstore.Store) {
Expand Down Expand Up @@ -5456,3 +5457,53 @@ func testEvalGEODIST(t *testing.T, store *dstore.Store) {

runEvalTests(t, tests, evalGEODIST, store)
}

func testEvalSINTER(t *testing.T, store *dstore.Store) {
tests := map[string]evalTestCase{
"intersection of two sets": {
setup: func() {
evalSADD([]string{"set1", "a", "b", "c"}, store)
evalSADD([]string{"set2", "c", "d", "e"}, store)
},
input: []string{"set1", "set2"},
output: clientio.Encode([]string{"c"}, false),
},
"intersection of three sets": {
setup: func() {
evalSADD([]string{"set1", "a", "b", "c"}, store)
evalSADD([]string{"set2", "b", "c", "d"}, store)
evalSADD([]string{"set3", "c", "d", "e"}, store)
},
input: []string{"set1", "set2", "set3"},
output: clientio.Encode([]string{"c"}, false),
},
"intersection with single set": {
setup: func() {
evalSADD([]string{"set1", "a"}, store)
},
input: []string{"set1"},
output: clientio.Encode([]string{"a"}, false),
},
"intersection with a non-existent key": {
setup: func() {
evalSADD([]string{"set1", "a", "b", "c"}, store)
},
input: []string{"set1", "nonexistent"},
output: clientio.Encode([]string{}, false),
},
"intersection with wrong type": {
setup: func() {
evalSADD([]string{"set1", "a", "b", "c"}, store)
store.Put("string", &object.Obj{Value: "string", TypeEncoding: object.ObjTypeString})
},
input: []string{"set1", "string"},
output: []byte("-WRONGTYPE Operation against a key holding the wrong kind of value\r\n"),
},
"no arguments": {
input: []string{},
output: diceerrors.NewErrArity("SINTER"),
},
}

runEvalTests(t, tests, evalSINTER, store)
}

0 comments on commit ab639d6

Please sign in to comment.