Skip to content

Commit

Permalink
Renamed 'Randomkey' to 'RandomKey' to follow PascalCase convention fo…
Browse files Browse the repository at this point in the history
…r exported methods. Added more clear definition for the RANDOMKEY command.
  • Loading branch information
kelvinmwinuka committed Aug 17, 2024
1 parent 86630ce commit e5977ec
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 32 deletions.
7 changes: 3 additions & 4 deletions echovault/api_generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -553,13 +553,12 @@ func (server *EchoVault) Rename(oldKey string, newKey string) (string, error) {
return internal.ParseStringResponse(b)
}

//Randomkey returns a random key
//If no keys present in db returns an emtpy string
func (server *EchoVault) Randomkey() (string, error) {
// RandomKey returns a random key from the current active database.
// If no keys present in db returns an empty string.
func (server *EchoVault) RandomKey() (string, error) {
b, err := server.handleCommand(server.context, internal.EncodeCommand([]string{"RANDOMKEY"}), nil, false, true)
if err != nil {
return "", err
}
return internal.ParseStringResponse(b)
}

54 changes: 27 additions & 27 deletions echovault/api_generic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1312,33 +1312,33 @@ func TestEchoVault_Rename(t *testing.T) {
func TestEchoVault_RANDOMKEY(t *testing.T) {
server := createEchoVault()

// test without keys
got, err := server.Randomkey()
if err != nil {
t.Error(err)
return
}
if got != "" {
t.Errorf("RANDOMKEY error, expected emtpy string (%v), got (%v)", []byte(""), []byte(got))
}
// test with keys
testkeys := []string{"key1", "key2", "key3"}
for _, k := range testkeys {
err := presetValue(server, context.Background(), k, "")
if err != nil {
t.Error(err)
return
}
}
// test without keys
got, err := server.RandomKey()
if err != nil {
t.Error(err)
return
}
if got != "" {
t.Errorf("RANDOMKEY error, expected emtpy string (%v), got (%v)", []byte(""), []byte(got))
}

// test with keys
testkeys := []string{"key1", "key2", "key3"}
for _, k := range testkeys {
err := presetValue(server, context.Background(), k, "")
if err != nil {
t.Error(err)
return
}
}

actual, err := server.Randomkey()
if err != nil {
t.Error(err)
return
}
if !strings.Contains(actual, "key") {
t.Errorf("RANDOMKEY error, expected one of %v, got %s", testkeys, got)
}
actual, err := server.RandomKey()
if err != nil {
t.Error(err)
return
}
if !strings.Contains(actual, "key") {
t.Errorf("RANDOMKEY error, expected one of %v, got %s", testkeys, got)
}

}
2 changes: 1 addition & 1 deletion internal/modules/generic/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -968,7 +968,7 @@ Delete all the keys in the currently selected database. This command is always s
Command: "randomkey",
Module: constants.GenericModule,
Categories: []string{constants.KeyspaceCategory, constants.ReadCategory, constants.SlowCategory},
Description: "(RANDOMKEY) Returns a random key.",
Description: "(RANDOMKEY) Returns a random key from the current selected database.",
Sync: false,
KeyExtractionFunc: randomKeyFunc,
HandlerFunc: handleRandomkey,
Expand Down

0 comments on commit e5977ec

Please sign in to comment.