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

File diff suppressed because it is too large Load Diff

View File

@@ -1184,3 +1184,58 @@ func TestEchoVault_DECRBY(t *testing.T) {
}) })
} }
} }
func TestEchoVault_Rename(t *testing.T) {
server := createEchoVault()
tests := []struct {
name string
oldKey string
newKey string
presetValues map[string]internal.KeyData
want string
wantErr bool
}{
{
name: "1. Rename existing key",
oldKey: "oldKey1",
newKey: "newKey1",
presetValues: map[string]internal.KeyData{"oldKey1": {Value: "value1"}},
want: "OK",
wantErr: false,
},
{
name: "2. Rename non-existent key",
oldKey: "oldKey2",
newKey: "newKey2",
presetValues: nil,
want: "",
wantErr: true,
},
{
name: "3. Rename to existing key",
oldKey: "oldKey3",
newKey: "newKey4",
presetValues: map[string]internal.KeyData{"oldKey3": {Value: "value3"}, "newKey4": {Value: "value4"}},
want: "OK",
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.presetValues != nil {
for k, d := range tt.presetValues {
presetKeyData(server, context.Background(), k, d)
}
}
got, err := server.Rename(tt.oldKey, tt.newKey)
if (err != nil) != tt.wantErr {
t.Errorf("Rename() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("Rename() got = %v, want %v", got, tt.want)
}
})
}
}

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())
}
}
})
}
})
} }