implementing appended for embedded api and started to write tests

This commit is contained in:
DMcP89
2024-07-01 20:50:48 -04:00
committed by Kelvin Mwinuka
parent 8e4b3e8e3d
commit 19dc8150eb
2 changed files with 45 additions and 1 deletions

View File

@@ -87,5 +87,9 @@ func (server *EchoVault) GetRange(key string, start, end int) (string, error) {
//
// - "value at key <key> is not a string" - when the value at the keys is not a string.
func (server *EchoVault) Append(key string, value string) (int, error) {
b, err := server.handleCommand(server.context, internal.EncodeCommand([]string{"APPEND", key, value}), nil, false, true)
if err != nil {
return 0, err
}
return internal.ParseIntegerResponse(b)
}

View File

@@ -311,3 +311,43 @@ func TestEchoVault_STRLEN(t *testing.T) {
})
}
}
func TestEchoVault_APPEND(t *testing.T) {
server := createEchoVault()
tests := []struct {
name string
presetValue interface{}
key string
value string
want int
wantErr bool
}{
{
name: "Return the correct string length for appended value",
presetValue: "Hello ",
key: "key1",
value: "World",
want: 11,
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.presetValue != nil {
err := presetValue(server, context.Background(), tt.key, tt.presetValue)
if err != nil {
t.Error(err)
return
}
}
got, err := server.Append(tt.key, tt.value)
if (err != nil) != tt.wantErr {
t.Errorf("APPEND() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("APPEND() got = %v, want %v", got, tt.want)
}
})
}
}