-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
20245d3
commit eef7c3a
Showing
2 changed files
with
120 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,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) | ||
} | ||
} | ||
}) | ||
} | ||
} |
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,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) | ||
} | ||
}) | ||
} | ||
} |