added test for RENAME command

This commit is contained in:
Sahil
2024-06-25 18:56:00 +05:30
parent 38ca643d05
commit 26959200ed
3 changed files with 7231 additions and 1345 deletions

View File

@@ -2393,4 +2393,97 @@ func Test_Generic(t *testing.T) {
})
}
})
t.Run("Test_HandlerRENAME", func(t *testing.T) {
t.Parallel()
conn, err := internal.GetConnection("localhost", port)
if err != nil {
t.Error(err)
return
}
defer func() {
_ = conn.Close()
}()
client := resp.NewConn(conn)
tests := []struct {
name string
oldKey string
newKey string
presetValue interface{}
command []resp.Value
expectedResponse string
expectedError error
}{
{
name: "1. Rename existing key",
oldKey: "oldKey1",
newKey: "newKey1",
presetValue: "value1",
command: []resp.Value{resp.StringValue("RENAME"), resp.StringValue("oldKey1"), resp.StringValue("newKey1")},
expectedResponse: "OK",
expectedError: nil,
},
{
name: "2. Rename non-existent key",
oldKey: "oldKey2",
newKey: "newKey2",
presetValue: nil,
command: []resp.Value{resp.StringValue("RENAME"), resp.StringValue("oldKey2"), resp.StringValue("newKey2")},
expectedResponse: "",
expectedError: errors.New("no such key"),
},
{
name: "3. Rename to existing key",
oldKey: "oldKey3",
newKey: "newKey3",
presetValue: "value3",
command: []resp.Value{resp.StringValue("RENAME"), resp.StringValue("oldKey3"), resp.StringValue("newKey3")},
expectedResponse: "OK",
expectedError: nil,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
if test.presetValue != nil {
command := []resp.Value{resp.StringValue("SET"), resp.StringValue(test.oldKey), resp.StringValue(fmt.Sprintf("%v", test.presetValue))}
if err = client.WriteArray(command); err != nil {
t.Error(err)
}
res, _, err := client.ReadValue()
if err != nil {
t.Error(err)
}
if !strings.EqualFold(res.String(), "OK") {
t.Errorf("expected preset response to be OK, got %s", res.String())
}
}
if err = client.WriteArray(test.command); err != nil {
t.Error(err)
}
res, _, err := client.ReadValue()
if err != nil {
t.Error(err)
}
if test.expectedError != nil {
if !strings.Contains(res.Error().Error(), test.expectedError.Error()) {
t.Errorf("expected error \"%s\", got \"%s\"", test.expectedError.Error(), res.Error().Error())
}
return
}
if err != nil {
t.Error(err)
} else {
if res.String() != test.expectedResponse {
t.Errorf("expected response \"%s\", got \"%s\"", test.expectedResponse, res.String())
}
}
})
}
})
}