Skip to content

Commit

Permalink
DiceDB#734: Http Integration tests for COMMAND and COMMAND/COUNT (Dic…
Browse files Browse the repository at this point in the history
  • Loading branch information
svkaizoku authored Oct 16, 2024
1 parent d78f8be commit d203b97
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 12 deletions.
53 changes: 46 additions & 7 deletions integration_tests/commands/http/command_count_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,63 @@ import (
func TestCommandCount(t *testing.T) {
exec := NewHTTPCommandExecutor()

testCases := []TestCase{
testCases := []struct {
name string
commands []HTTPCommand
expected []interface{}
errorExpected bool
assertType []string
}{
{
name: "Command count should be greather than zero",
commands: []HTTPCommand{
{Command: "COMMAND/COUNT"},
},
expected: []interface{}{float64(0)},
assertType: []string{"greater"},
},
{
name: "Command count should not support any argument",
commands: []HTTPCommand{
{Command: "COMMAND/COUNT", Body: map[string]interface{}{"key": ""}},
},
expected: []interface{}{"ERR wrong number of arguments for 'command|count' command"},
expected: []interface{}{"ERR wrong number of arguments for 'command|count' command"},
assertType: []string{"equal"},
},
}

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

switch tc.assertType[c] {
case "equal":
assert.DeepEqual(t, tc.expected[c], result)
case "greater":
assert.Assert(t, result.(float64) >= tc.expected[c].(float64))
}
}

})
}
}

func getCommandCount(exec *HTTPCommandExecutor) float64 {
cmd := HTTPCommand{Command: "COMMAND/COUNT", Body: map[string]interface{}{"key": ""}}
responseValue, _ := exec.FireCommand(cmd)
if responseValue == nil {
return -1
}
return responseValue.(float64)
}

func BenchmarkCountCommand(b *testing.B) {
exec := NewHTTPCommandExecutor()

b.ResetTimer()
for n := 0; n < b.N; n++ {
commandCount := getCommandCount(exec)
if commandCount <= 0 {
b.Fail()
}
}
}
46 changes: 46 additions & 0 deletions integration_tests/commands/http/command_default_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package http

import (
"fmt"
"testing"

"github.com/dicedb/dice/internal/eval"
"gotest.tools/v3/assert"
)

func TestCommandDefault(t *testing.T) {
exec := NewHTTPCommandExecutor()
commands := getCommandDefault(exec)
t.Run("Command should not be empty", func(t *testing.T) {
assert.Assert(t, len(commands) > 0,
fmt.Sprintf("Unexpected number of CLI commands found. expected greater than 0, %d found", len(commands)))
})

t.Run("Command count matches", func(t *testing.T) {
assert.Assert(t, len(commands) == len(eval.DiceCmds),
fmt.Sprintf("Unexpected number of CLI commands found. expected %d, %d found", len(eval.DiceCmds), len(commands)))
})
}

func getCommandDefault(exec *HTTPCommandExecutor) []interface{} {
cmd := HTTPCommand{Command: "COMMAND", Body: map[string]interface{}{"values": []interface{}{}}}
responseValue, _ := exec.FireCommand(cmd)
if responseValue == nil {
return nil
}
var cmds []interface{}
cmds = append(cmds, responseValue.([]interface{})...)
return cmds
}

func BenchmarkCommandDefault(b *testing.B) {
exec := NewHTTPCommandExecutor()

b.ResetTimer()
for n := 0; n < b.N; n++ {
commands := getCommandDefault(exec)
if len(commands) <= 0 {
b.Fail()
}
}
}
13 changes: 8 additions & 5 deletions integration_tests/commands/http/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,14 @@ type HTTPCommand struct {

func (e *HTTPCommandExecutor) FireCommand(cmd HTTPCommand) (interface{}, error) {
command := strings.ToUpper(cmd.Command)
body, err := json.Marshal(cmd.Body)

// Handle error during JSON marshaling
if err != nil {
return nil, err
var body []byte
if cmd.Body != nil {
var err error
body, err = json.Marshal(cmd.Body)
// Handle error during JSON marshaling
if err != nil {
return nil, err
}
}

ctx := context.Background()
Expand Down

0 comments on commit d203b97

Please sign in to comment.