Skip to content

Commit

Permalink
add unit test, fix typos
Browse files Browse the repository at this point in the history
  • Loading branch information
iamskp11 committed Dec 6, 2024
1 parent 6abdcbe commit d4fc39e
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 2 deletions.
2 changes: 1 addition & 1 deletion integration_tests/commands/resp/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
Expand Down
2 changes: 1 addition & 1 deletion integration_tests/commands/websocket/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
Expand Down
88 changes: 88 additions & 0 deletions internal/eval/eval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
}
})
}
}

0 comments on commit d4fc39e

Please sign in to comment.