mirror of
https://github.com/EchoVault/SugarDB.git
synced 2025-10-24 00:14:08 +08:00
implementing appended for embedded api and started to write tests
This commit is contained in:
@@ -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.
|
// - "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) {
|
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)
|
||||||
}
|
}
|
||||||
|
@@ -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)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user