Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix getex command returning value for json based object #674

24 changes: 24 additions & 0 deletions integration_tests/commands/getex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,30 @@ func TestGetEx(t *testing.T) {
assert_type: []string{"equal", "equal", "assert", "equal", "assert"},
delay: []time.Duration{0, 0, 0, 0, 0},
},
{
name: "GetEx with key holding JSON type",
commands: []string{"JSON.SET KEY $ \"1\"", "GETEX KEY"},
expected: []interface{}{"OK", "WRONGTYPE Operation against a key holding the wrong kind of value"},
assert_type: []string{"equal", "equal"},
delay: []time.Duration{0, 0},
},
{
name: "GetEx with key holding JSON type with multiple set commands",
commands: []string{"JSON.SET KEY $ \"{\"a\":2}\"", "GETEX KEY", "JSON.SET KEY $.a \"3\"", "GETEX KEY"},
expected: []interface{}{"OK",
"WRONGTYPE Operation against a key holding the wrong kind of value",
"OK",
"WRONGTYPE Operation against a key holding the wrong kind of value"},
assert_type: []string{"equal", "equal", "equal", "equal"},
delay: []time.Duration{0, 0, 0, 0},
},
{
name: "GetEx with key holding SET type",
commands: []string{"SADD KEY 1 2 3", "GETEX KEY"},
expected: []interface{}{"OK", "WRONGTYPE Operation against a key holding the wrong kind of value"},
assert_type: []string{"equal", "equal"},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, the test is failing as you are not deleting key with name KEY before executing an SADD command. Please add a delete command for KEY. please see line 151 for reference.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

incorporated. I have changed key name. Ideally, tests should be independent.

delay: []time.Duration{0, 0},
},
}

for _, tc := range testCases {
Expand Down
5 changes: 3 additions & 2 deletions internal/eval/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -2480,8 +2480,9 @@ func evalGETEX(args []string, store *dstore.Store) []byte {
return clientio.RespNIL
}

// check if the object is set type if yes then return error
if object.AssertType(obj.TypeEncoding, object.ObjTypeSet) == nil {
// check if the object is set type or json type if yes then return error
if object.AssertType(obj.TypeEncoding, object.ObjTypeSet) == nil ||
object.AssertType(obj.TypeEncoding, object.ObjTypeJSON) == nil {
return diceerrors.NewErrWithFormattedMessage(diceerrors.WrongTypeErr)
}

Expand Down
17 changes: 17 additions & 0 deletions internal/eval/eval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func TestEval(t *testing.T) {
testEvalHELLO(t, store)
testEvalSET(t, store)
testEvalGET(t, store)
testEvalGETEX(t, store)
testEvalDebug(t, store)
testEvalJSONARRPOP(t, store)
testEvalJSONARRLEN(t, store)
Expand Down Expand Up @@ -159,6 +160,22 @@ func testEvalGETEX(t *testing.T, store *dstore.Store) {
},
input: []string{"foo", Ex, "10000000000000000"},
output: []byte("-ERR invalid expire time in 'getex' command\r\n")},
"key holding json type": {
setup: func() {
evalJSONSET([]string{"JSONKEY", "$", "1"}, store)

},
input: []string{"JSONKEY"},
output: []byte("-WRONGTYPE Operation against a key holding the wrong kind of value\r\n"),
},
"key holding set type": {
setup: func() {
evalSADD([]string{"SETKEY", "FRUITS", "APPLE", "MANGO", "BANANA"}, store)

},
input: []string{"SETKEY"},
output: []byte("-WRONGTYPE Operation against a key holding the wrong kind of value\r\n"),
},
}

runEvalTests(t, tests, evalGETEX, store)
Expand Down
Loading