Skip to content

Commit

Permalink
fixed few test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
AshwinKul28 committed Nov 12, 2024
1 parent 4ee43f2 commit 6ed7894
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 323 deletions.
40 changes: 0 additions & 40 deletions integration_tests/commands/async/discard_test.go

This file was deleted.

280 changes: 0 additions & 280 deletions integration_tests/commands/async/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -348,286 +348,6 @@ func TestJSONSetWithNXAndXX(t *testing.T) {
}
}

func TestJSONClearOperations(t *testing.T) {
conn := getLocalConnection()
defer conn.Close()

// deleteTestKeys([]string{"user"}, store)
FireCommand(conn, "DEL user")

testCases := []struct {
name string
commands []string
expected []interface{}
}{
{
name: "jsonclear root path",
commands: []string{
`JSON.SET user $ {"age":13,"high":1.60,"flag":true,"name":"jerry","pet":null,"language":["python","golang"],"partner":{"name":"tom","language":["rust"]}}`,
"JSON.CLEAR user $",
"JSON.GET user $",
},
expected: []interface{}{"OK", int64(1), "{}"},
},
{
name: "jsonclear string type",
commands: []string{
`JSON.SET user $ {"name":"Tom","age":30}`,
"JSON.CLEAR user $.name",
"JSON.GET user $.name",
},
expected: []interface{}{"OK", int64(0), `"Tom"`},
},
{
name: "jsonclear array type",
commands: []string{
`JSON.SET user $ {"names":["Rahul","Tom"],"ages":[25,30]}`,
"JSON.CLEAR user $.names",
"JSON.GET user $.names",
},
expected: []interface{}{"OK", int64(1), "[]"},
},
{
name: "jsonclear bool type",
commands: []string{
`JSON.SET user $ {"flag":true,"name":"Tom"}`,
"JSON.CLEAR user $.flag",
"JSON.GET user $.flag"},
expected: []interface{}{"OK", int64(0), "true"},
},
{
name: "jsonclear null type",
commands: []string{
`JSON.SET user $ {"name":null,"age":28}`,
"JSON.CLEAR user $.pet",
"JSON.GET user $.name"},
expected: []interface{}{"OK", int64(0), "null"},
},
{
name: "jsonclear integer type",
commands: []string{
`JSON.SET user $ {"age":28,"name":"Tom"}`,
"JSON.CLEAR user $.age",
"JSON.GET user $.age"},
expected: []interface{}{"OK", int64(1), "0"},
},
{
name: "jsonclear float type",
commands: []string{
`JSON.SET user $ {"price":3.14,"name":"sugar"}`,
"JSON.CLEAR user $.price",
"JSON.GET user $.price"},
expected: []interface{}{"OK", int64(1), "0"},
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
for i, cmd := range tc.commands {
result := FireCommand(conn, cmd)
assert.Equal(t, tc.expected[i], result)
}
})
}
}

func TestJSONDelOperations(t *testing.T) {
conn := getLocalConnection()
defer conn.Close()

FireCommand(conn, "DEL user")

testCases := []struct {
name string
commands []string
expected []interface{}
}{
{
name: "Delete root path",
commands: []string{
`JSON.SET user $ {"age":13,"high":1.60,"flag":true,"name":"jerry","pet":null,"language":["python","golang"],"partner":{"name":"tom","language":["rust"]}}`,
"JSON.DEL user $",
"JSON.GET user $",
},
expected: []interface{}{"OK", int64(1), "(nil)"},
},
{
name: "Delete nested field",
commands: []string{
`JSON.SET user $ {"name":"Tom","address":{"city":"New York","zip":"10001"}}`,
"JSON.DEL user $.address.city",
"JSON.GET user $",
},
expected: []interface{}{"OK", int64(1), `{"name":"Tom","address":{"zip":"10001"}}`},
},
{
name: "del string type",
commands: []string{
`JSON.SET user $ {"flag":true,"name":"Tom"}`,
"JSON.DEL user $.name",
"JSON.GET user $"},
expected: []interface{}{"OK", int64(1), `{"flag":true}`},
},
{
name: "del bool type",
commands: []string{
`JSON.SET user $ {"flag":true,"name":"Tom"}`,
"JSON.DEL user $.flag",
"JSON.GET user $"},
expected: []interface{}{"OK", int64(1), `{"name":"Tom"}`},
},
{
name: "del null type",
commands: []string{
`JSON.SET user $ {"name":null,"age":28}`,
"JSON.DEL user $.name",
"JSON.GET user $"},
expected: []interface{}{"OK", int64(1), `{"age":28}`},
},
{
name: "del array type",
commands: []string{
`JSON.SET user $ {"names":["Rahul","Tom"],"bosses":{"names":["Jerry","Rocky"],"hobby":"swim"}}`,
"JSON.DEL user $..names",
"JSON.GET user $"},
expected: []interface{}{"OK", int64(2), `{"bosses":{"hobby":"swim"}}`},
},
{
name: "del integer type",
commands: []string{
`JSON.SET user $ {"age":28,"name":"Tom"}`,
"JSON.DEL user $.age",
"JSON.GET user $"},
expected: []interface{}{"OK", int64(1), `{"name":"Tom"}`},
},
{
name: "del float type",
commands: []string{
`JSON.SET user $ {"price":3.14,"name":"sugar"}`,
"JSON.DEL user $.price",
"JSON.GET user $"},
expected: []interface{}{"OK", int64(1), `{"name":"sugar"}`},
},
{
name: "delete key with []",
commands: []string{
`JSON.SET data $ {"key[0]":"value","array":["a","b"]}`,
`JSON.DEL data ["key[0]"]`,
"JSON.GET data $"},
expected: []interface{}{"OK", int64(1), `{"array": ["a","b"]}`},
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
for i, cmd := range tc.commands {
result := FireCommand(conn, cmd)
stringResult, ok := result.(string)
if ok && testutils.IsJSONResponse(stringResult) {
assert.JSONEq(t, tc.expected[i].(string), stringResult)
} else {
assert.Equal(t, tc.expected[i], result)
}
}
})
}
}

func TestJSONForgetOperations(t *testing.T) {
conn := getLocalConnection()
defer conn.Close()

FireCommand(conn, "FORGET user")

testCases := []struct {
name string
commands []string
expected []interface{}
}{
{
name: "Forget root path",
commands: []string{
`JSON.SET user $ {"age":13,"high":1.60,"flag":true,"name":"jerry","pet":null,"language":["python","golang"],"partner":{"name":"tom","language":["rust"]}}`,
"JSON.FORGET user $",
"JSON.GET user $",
},
expected: []interface{}{"OK", int64(1), "(nil)"},
},
{
name: "Forget nested field",
commands: []string{
`JSON.SET user $ {"name":"Tom","address":{"city":"New York","zip":"10001"}}`,
"JSON.FORGET user $.address.city",
"JSON.GET user $",
},
expected: []interface{}{"OK", int64(1), `{"name":"Tom","address":{"zip":"10001"}}`},
},
{
name: "forget string type",
commands: []string{`JSON.SET user $ {"flag":true,"name":"Tom"}`,
"JSON.FORGET user $.name",
"JSON.GET user $"},
expected: []interface{}{"OK", int64(1), `{"flag":true}`},
},
{
name: "forget bool type",
commands: []string{`JSON.SET user $ {"flag":true,"name":"Tom"}`,
"JSON.FORGET user $.flag",
"JSON.GET user $"},
expected: []interface{}{"OK", int64(1), `{"name":"Tom"}`},
},
{
name: "forget null type",
commands: []string{`JSON.SET user $ {"name":null,"age":28}`,
"JSON.FORGET user $.name",
"JSON.GET user $"},
expected: []interface{}{"OK", int64(1), `{"age":28}`},
},
{
name: "forget array type",
commands: []string{`JSON.SET user $ {"names":["Rahul","Tom"],"bosses":{"names":["Jerry","Rocky"],"hobby":"swim"}}`,
"JSON.FORGET user $..names",
"JSON.GET user $"},
expected: []interface{}{"OK", int64(2), `{"bosses":{"hobby":"swim"}}`},
},
{
name: "forget integer type",
commands: []string{`JSON.SET user $ {"age":28,"name":"Tom"}`,
"JSON.FORGET user $.age",
"JSON.GET user $"},
expected: []interface{}{"OK", int64(1), `{"name":"Tom"}`},
},
{
name: "forget float type",
commands: []string{`JSON.SET user $ {"price":3.14,"name":"sugar"}`,
"JSON.FORGET user $.price",
"JSON.GET user $"},
expected: []interface{}{"OK", int64(1), `{"name":"sugar"}`},
},
{
name: "forget array element",
commands: []string{`JSON.SET user $ {"names":["Rahul","Tom"],"bosses":{"names":["Jerry","Rocky"],"hobby":"swim"}}`,
"JSON.FORGET user $.names[0]",
"JSON.GET user $"},
expected: []interface{}{"OK", int64(1), `{"names":["Tom"],"bosses":{"names":["Jerry","Rocky"],"hobby":"swim"}}`},
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
for i, cmd := range tc.commands {
result := FireCommand(conn, cmd)
stringResult, ok := result.(string)
if ok && testutils.IsJSONResponse(stringResult) {
assert.JSONEq(t, tc.expected[i].(string), stringResult)
} else {
assert.Equal(t, tc.expected[i], result)
}
}
})
}
}

func arraysArePermutations[T comparable](a, b []T) bool {
// If lengths are different, they cannot be permutations
if len(a) != len(b) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package async
package resp

import (
"encoding/base64"
Expand Down Expand Up @@ -81,7 +81,7 @@ func TestDumpRestore(t *testing.T) {
"DUMP nonexistentkey",
},
expected: []interface{}{
"ERR nil",
"(nil)",
},
},
}
Expand Down
36 changes: 35 additions & 1 deletion internal/server/cmd_meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,34 @@ var (
Cmd: "JSON.NUMMULTBY",
CmdType: SingleShard,
}

jsonSetCmdMeta = CmdsMeta{
Cmd: "JSON.SET",
CmdType: SingleShard,
}
jsonGetCmdMeta = CmdsMeta{
Cmd: "JSON.GET",
CmdType: SingleShard,
}
jsonTypeCmdMeta = CmdsMeta{
Cmd: "JSON.TYPE",
CmdType: SingleShard,
}
jsonIngestCmdMeta = CmdsMeta{
Cmd: "JSON.INGEST",
CmdType: SingleShard,
}
jsonHGetAllCmdMeta = CmdsMeta{
Cmd: "JSON.HGETALL",
CmdType: SingleShard,
}
dumpCmdMeta = CmdsMeta{
Cmd: "DUMP",
CmdType: SingleShard,
}
restoreCmdMeta = CmdsMeta{
Cmd: "RESTORE",
CmdType: SingleShard,
}
// Metadata for multishard commands would go here.
// These commands require both breakup and gather logic.

Expand Down Expand Up @@ -501,5 +528,12 @@ func init() {
WorkerCmdsMeta["JSON.TOGGLE"] = jsonToggleCmdMeta
WorkerCmdsMeta["JSON.NUMINCRBY"] = jsonNumIncrByCmdMeta
WorkerCmdsMeta["JSON.NUMMULTBY"] = jsonNumMultByCmdMeta
WorkerCmdsMeta["JSON.SET"] = jsonSetCmdMeta
WorkerCmdsMeta["JSON.GET"] = jsonGetCmdMeta
WorkerCmdsMeta["JSON.TYPE"] = jsonTypeCmdMeta
WorkerCmdsMeta["JSON.INGEST"] = jsonIngestCmdMeta
WorkerCmdsMeta["JSON.HGETALL"] = jsonHGetAllCmdMeta
WorkerCmdsMeta["DUMP"] = dumpCmdMeta
WorkerCmdsMeta["RESTORE"] = restoreCmdMeta
// Additional commands (multishard, custom) can be added here as needed.
}

0 comments on commit 6ed7894

Please sign in to comment.