Skip to content

Commit

Permalink
del command test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
mohit-nagaraj committed Oct 21, 2024
1 parent 20245d3 commit eef7c3a
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 0 deletions.
74 changes: 74 additions & 0 deletions integration_tests/commands/http/del_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package http

import (
"testing"
"time"

"gotest.tools/v3/assert"
)

func TestDel(t *testing.T) {
exec := NewHTTPCommandExecutor()

testCases := []struct {
name string
commands []HTTPCommand
expected []interface{}
delays []time.Duration
}{
{
name: "DEL with set key",
commands: []HTTPCommand{
{Command: "SET", Body: map[string]interface{}{"key": "k1", "value": "v1"}},
{Command: "DEL", Body: map[string]interface{}{"key": "k1"}},
{Command: "GET", Body: map[string]interface{}{"key": "k1"}},
},
expected: []interface{}{"OK", int64(1), "(nil)"},
delays: []time.Duration{0, 0},
},
{
name: "DEL with multiple keys",
commands: []HTTPCommand{
{Command: "SET", Body: map[string]interface{}{"key": "k1", "value": "v1"}},
{Command: "SET", Body: map[string]interface{}{"key": "k2", "value": "v2"}},
{Command: "DEL", Body: map[string]interface{}{"key": "k1"}},
{Command: "DEL", Body: map[string]interface{}{"key": "k2"}},
{Command: "GET", Body: map[string]interface{}{"key": "k1"}},
{Command: "GET", Body: map[string]interface{}{"key": "k2"}},
},
expected: []interface{}{"OK", "OK", int64(1), int64(1), "(nil)", "(nil)"},
},
{
name: "DEL with key not set",
commands: []HTTPCommand{
{Command: "GET", Body: map[string]interface{}{"key": "k3"}},
{Command: "DEL", Body: map[string]interface{}{"key": "k3"}},
},
expected: []interface{}{"(nil)", int64(0)},
},
{
name: "DEL with no keys or arguments",
commands: []HTTPCommand{
{Command: "DEL", Body: map[string]interface{}{}},
},
expected: []interface{}{"ERR wrong number of arguments for 'del' command"},
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
for i, cmd := range tc.commands {
if tc.delays[i] > 0 {
time.Sleep(tc.delays[i])
}
result, err := exec.FireCommand(cmd)
if err != nil {
// Check if the error message matches the expected result
assert.Equal(t, tc.expected[i], err.Error(), "Error message mismatch for cmd %s", cmd)
} else {
assert.Equal(t, tc.expected[i], result, "Value mismatch for cmd %s, expected %v, got %v", cmd, tc.expected[i], result)
}
}
})
}
}
46 changes: 46 additions & 0 deletions integration_tests/commands/websocket/del_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package websocket

import (
"testing"

"gotest.tools/v3/assert"
)

func TestDel(t *testing.T) {
exec := NewWebsocketCommandExecutor()

testCases := []TestCase{
{
name: "DEL with set key",
commands: []string{"SET k1 v1", "DEL k1", "GET k1"},
expected: []interface{}{"OK", int64(1), "(nil)"},
},
{
name: "DEL with multiple keys",
commands: []string{"SET k1 v1", "SET k2 v2", "DEL k1 k2", "GET k1", "GET k2"},
expected: []interface{}{"OK", "OK", int64(1), int64(1), "(nil)", "(nil)"},
},
{
name: "DEL with key not set",
commands: []string{"GET k3", "DEL k3"},
expected: []interface{}{"(nil)", int64(0)},
},
{
name: "DEL with no keys or arguments",
commands: []string{"DEL"},
expected: []interface{}{"ERR wrong number of arguments for 'del' command"},
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
conn := exec.ConnectToServer()
exec.FireCommand(conn, "del k")

for i, cmd := range tc.commands {
result := exec.FireCommand(conn, cmd)
assert.DeepEqual(t, tc.expected[i], result)
}
})
}
}

0 comments on commit eef7c3a

Please sign in to comment.