diff --git a/integration_tests/commands/http/set_data_cmd_test.go b/integration_tests/commands/http/set_data_cmd_test.go index f16511972..0dac37c02 100644 --- a/integration_tests/commands/http/set_data_cmd_test.go +++ b/integration_tests/commands/http/set_data_cmd_test.go @@ -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{ diff --git a/internal/eval/eval.go b/internal/eval/eval.go index aeee31dfc..e71b2d59e 100644 --- a/internal/eval/eval.go +++ b/internal/eval/eval.go @@ -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") } diff --git a/internal/eval/eval_test.go b/internal/eval/eval_test.go index ae2093c29..c99455823 100644 --- a/internal/eval/eval_test.go +++ b/internal/eval/eval_test.go @@ -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) { @@ -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) +}