From d203b97160c15082cdb44d34a01462a20bcc18f6 Mon Sep 17 00:00:00 2001 From: svkaizoku Date: Wed, 16 Oct 2024 06:42:06 +0200 Subject: [PATCH] #734: Http Integration tests for COMMAND and COMMAND/COUNT (#899) --- .../commands/http/command_count_test.go | 53 ++++++++++++++++--- .../commands/http/command_default_test.go | 46 ++++++++++++++++ integration_tests/commands/http/setup.go | 13 +++-- 3 files changed, 100 insertions(+), 12 deletions(-) create mode 100644 integration_tests/commands/http/command_default_test.go diff --git a/integration_tests/commands/http/command_count_test.go b/integration_tests/commands/http/command_count_test.go index 07a24c9c8..265d3a7be 100644 --- a/integration_tests/commands/http/command_count_test.go +++ b/integration_tests/commands/http/command_count_test.go @@ -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() + } + } +} diff --git a/integration_tests/commands/http/command_default_test.go b/integration_tests/commands/http/command_default_test.go new file mode 100644 index 000000000..e3d2c8dfe --- /dev/null +++ b/integration_tests/commands/http/command_default_test.go @@ -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() + } + } +} diff --git a/integration_tests/commands/http/setup.go b/integration_tests/commands/http/setup.go index 86637eae2..b09e22d8e 100644 --- a/integration_tests/commands/http/setup.go +++ b/integration_tests/commands/http/setup.go @@ -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()