-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Co-authored-by: Jyotinder Singh <jyotindrsingh@gmail.com>
- Loading branch information
1 parent
8d02493
commit 75c0af9
Showing
4 changed files
with
286 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package commands | ||
|
||
import ( | ||
"encoding/json" | ||
|
||
"testing" | ||
|
||
"github.com/dicedb/dice/testutils" | ||
"gotest.tools/v3/assert" | ||
) | ||
|
||
func compareJSON(t *testing.T, expected, actual string) { | ||
var expectedMap map[string]interface{} | ||
var actualMap map[string]interface{} | ||
|
||
err1 := json.Unmarshal([]byte(expected), &expectedMap) | ||
err2 := json.Unmarshal([]byte(actual), &actualMap) | ||
|
||
assert.NilError(t, err1) | ||
assert.NilError(t, err2) | ||
|
||
assert.DeepEqual(t, expectedMap, actualMap) | ||
} | ||
|
||
func TestEvalJSONTOGGLE(t *testing.T) { | ||
conn := getLocalConnection() | ||
defer conn.Close() | ||
|
||
simpleJSON := `{"name":true,"age":false}` | ||
complexJson := `{"field":true,"nested":{"field":false,"nested":{"field":true}}}` | ||
|
||
testCases := []struct { | ||
name string | ||
commands []string | ||
expected []interface{} | ||
}{ | ||
{ | ||
name: "JSON.TOGGLE with existing key", | ||
commands: []string{`JSON.SET user $ ` + simpleJSON, "JSON.TOGGLE user $.name"}, | ||
expected: []interface{}{"OK", []any{int64(0)}}, | ||
}, | ||
{ | ||
name: "JSON.TOGGLE with non-existing key", | ||
commands: []string{"JSON.TOGGLE user $.flag"}, | ||
expected: []interface{}{"ERR could not perform this operation on a key that doesn't exist"}, | ||
}, | ||
{ | ||
name: "JSON.TOGGLE with invalid path", | ||
commands: []string{`JSON.SET testkey $ ` + simpleJSON, "JSON.TOGGLE user $.invalidPath"}, | ||
expected: []interface{}{"WRONGTYPE Operation against a key holding the wrong kind of value", "ERR could not perform this operation on a key that doesn't exist"}, | ||
}, | ||
{ | ||
name: "JSON.TOGGLE with invalid command format", | ||
commands: []string{"JSON.TOGGLE testKey"}, | ||
expected: []interface{}{"ERR wrong number of arguments for 'json.toggle' command"}, | ||
}, | ||
{ | ||
name: "deeply nested JSON structure with multiple matching fields", | ||
commands: []string{ | ||
`JSON.SET user $ ` + complexJson, | ||
"JSON.GET user", | ||
"JSON.TOGGLE user $..field", | ||
"JSON.GET user", | ||
}, | ||
expected: []interface{}{ | ||
"OK", | ||
`{"field":true,"nested":{"field":false,"nested":{"field":true}}}`, | ||
[]any{int64(0), int64(1), int64(0)}, // Toggle: true -> false, false -> true, true -> false | ||
`{"field":false,"nested":{"field":true,"nested":{"field":false}}}`, | ||
}, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
FireCommand(conn, "DEL user") | ||
for i, cmd := range tc.commands { | ||
result := FireCommand(conn, cmd) | ||
switch expected := tc.expected[i].(type) { | ||
case string: | ||
if isJSONString(expected) { | ||
compareJSON(t, expected, result.(string)) | ||
} else { | ||
assert.Equal(t, expected, result) | ||
} | ||
case []interface{}: | ||
assert.Assert(t, testutils.UnorderedEqual(expected, result)) | ||
default: | ||
assert.DeepEqual(t, expected, result) | ||
} | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func isJSONString(s string) bool { | ||
var js json.RawMessage | ||
return json.Unmarshal([]byte(s), &js) == nil | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters