diff --git a/integration_tests/commands/resp/json_test.go b/integration_tests/commands/resp/json_test.go index 216918ac2..56abed50a 100644 --- a/integration_tests/commands/resp/json_test.go +++ b/integration_tests/commands/resp/json_test.go @@ -1706,7 +1706,7 @@ func TestJSONARRINDEX(t *testing.T) { assertType: []string{"equal", "equal"}, }, { - name: "should return empty list if invalid stop index provided", + name: "should return error if invalid stop index provided", commands: []string{"json.set key $ " + normalArray, "json.arrindex key $ 3 4 abc"}, expected: []interface{}{"OK", "ERR Couldn't parse as integer"}, assertType: []string{"equal", "equal"}, diff --git a/integration_tests/commands/websocket/json_test.go b/integration_tests/commands/websocket/json_test.go index 007732dcf..9ad3483b7 100644 --- a/integration_tests/commands/websocket/json_test.go +++ b/integration_tests/commands/websocket/json_test.go @@ -1266,7 +1266,7 @@ func TestJSONARRINDEX(t *testing.T) { cleanUp: []string{"DEL key"}, }, { - name: "should return empty list if invalid stop index provided", + name: "should return error if invalid stop index provided", commands: []string{"json.set key $ " + normalArray, "json.arrindex key $ 3 4 abc"}, expected: []interface{}{"OK", "ERR Couldn't parse as integer"}, assertType: []string{"equal", "equal"}, diff --git a/internal/eval/eval_test.go b/internal/eval/eval_test.go index f1a629a6e..cf958b349 100644 --- a/internal/eval/eval_test.go +++ b/internal/eval/eval_test.go @@ -142,6 +142,7 @@ func TestEval(t *testing.T) { testEvalBFINFO(t, store) testEvalBFEXISTS(t, store) testEvalBFADD(t, store) + testEvalJSONARRINDEX(t, store) } func testEvalPING(t *testing.T, store *dstore.Store) { @@ -9222,3 +9223,90 @@ func testEvalLRANGE(t *testing.T, store *dstore.Store) { } runMigratedEvalTests(t, tests, evalLRANGE, store) } + +func testEvalJSONARRINDEX(t *testing.T, store *dstore.Store) { + normalArray := `[0,1,2,3,4,3]` + tests := []evalTestCase{ + { + name: "nil value", + setup: func() {}, + input: nil, + migratedOutput: EvalResponse{ + Result: nil, + Error: diceerrors.ErrWrongArgumentCount("JSON.ARRINDEX"), + }, + }, + { + name: "empty args", + setup: func() {}, + input: []string{}, + migratedOutput: EvalResponse{ + Result: nil, + Error: diceerrors.ErrWrongArgumentCount("JSON.ARRINDEX"), + }, + }, + { + name: "start index is invalid", + setup: func() { + key := "EXISTING_KEY" + var rootData interface{} + _ = sonic.Unmarshal([]byte(normalArray), &rootData) + obj := store.NewObj(rootData, -1, object.ObjTypeJSON) + store.Put(key, obj) + }, + input: []string{"EXISTING_KEY", "$", "3", "abc"}, + migratedOutput: EvalResponse{ + Result: nil, + Error: errors.New("ERR Couldn't parse as integer"), + }, + }, + { + name: "stop index is invalid", + setup: func() { + key := "EXISTING_KEY" + var rootData interface{} + _ = sonic.Unmarshal([]byte(normalArray), &rootData) + obj := store.NewObj(rootData, -1, object.ObjTypeJSON) + store.Put(key, obj) + }, + input: []string{"EXISTING_KEY", "$", "3", "4", "abc"}, + migratedOutput: EvalResponse{ + Result: nil, + Error: errors.New("ERR Couldn't parse as integer"), + }, + }, + { + name: "start and stop optional param valid", + setup: func() { + key := "EXISTING_KEY" + var rootData interface{} + _ = sonic.Unmarshal([]byte(normalArray), &rootData) + obj := store.NewObj(rootData, -1, object.ObjTypeJSON) + store.Put(key, obj) + }, + input: []string{"EXISTING_KEY", "$", "4", "4", "5"}, + migratedOutput: EvalResponse{ + Result: []interface{}{4}, + Error: nil, + }, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + store = setupTest(store) + + if tt.setup != nil { + tt.setup() + } + + response := evalJSONARRINDEX(tt.input, store) + assert.Equal(t, tt.migratedOutput.Result, response.Result) + if tt.migratedOutput.Error != nil { + assert.EqualError(t, response.Error, tt.migratedOutput.Error.Error()) + } else { + assert.NoError(t, response.Error) + } + }) + } +} \ No newline at end of file