Skip to content

Commit

Permalink
GETDEL embedded spec command and test added
Browse files Browse the repository at this point in the history
  • Loading branch information
osteensco committed Aug 21, 2024
1 parent 0291e0f commit a7ea002
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
16 changes: 16 additions & 0 deletions echovault/api_generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -562,3 +562,19 @@ func (server *EchoVault) RandomKey() (string, error) {
}
return internal.ParseStringResponse(b)
}

// GetDel retrieves the value at the provided key and deletes that key.
//
// Parameters:
//
// `key` - string - the key whose value should be retrieved and then deleted.
//
// Returns: A string representing the value at the specified key. If the value does not exist, an empty
// string is returned.
func (server *EchoVault) GetDel(key string) (string, error) {
b, err := server.handleCommand(server.context, internal.EncodeCommand([]string{"GETDEL", key}), nil, false, true)
if err != nil {
return "", err
}
return internal.ParseStringResponse(b)
}
58 changes: 58 additions & 0 deletions echovault/api_generic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1342,3 +1342,61 @@ func TestEchoVault_RANDOMKEY(t *testing.T) {
}

}

func TestEchoVault_GETDEL(t *testing.T) {
server := createEchoVault()

tests := []struct {
name string
presetValue interface{}
key string
want string
wantErr bool
}{
{
name: "Return string from existing key",
presetValue: "value1",
key: "key1",
want: "value1",
wantErr: false,
},
{
name: "Return empty string if the key does not exist",
presetValue: nil,
key: "key2",
want: "",
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
}
}
//Check value received
got, err := server.GetDel(tt.key)
if (err != nil) != tt.wantErr {
t.Errorf("GETDEL() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("GETDEL() got = %v, want %v", got, tt.want)
}
//Check key was deleted
if tt.presetValue != nil {
got, err := server.Get(tt.key)
if (err != nil) != tt.wantErr {
t.Errorf("GETDEL() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != "" {
t.Errorf("GETDEL() got = %v, want empty string", got)
}
}
})
}
}

0 comments on commit a7ea002

Please sign in to comment.