diff --git a/echovault/api_string.go b/echovault/api_string.go index 2122f51..2aaa398 100644 --- a/echovault/api_string.go +++ b/echovault/api_string.go @@ -87,5 +87,9 @@ func (server *EchoVault) GetRange(key string, start, end int) (string, error) { // // - "value at 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) } diff --git a/echovault/api_string_test.go b/echovault/api_string_test.go index 2d13c79..41e810a 100644 --- a/echovault/api_string_test.go +++ b/echovault/api_string_test.go @@ -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) + } + }) + } +}