From e5977ec5ef49e288a1795a65e195b10b1e4c35df Mon Sep 17 00:00:00 2001 From: Kelvin Clement Mwinuka Date: Sun, 18 Aug 2024 01:50:07 +0800 Subject: [PATCH] Renamed 'Randomkey' to 'RandomKey' to follow PascalCase convention for exported methods. Added more clear definition for the RANDOMKEY command. --- echovault/api_generic.go | 7 ++-- echovault/api_generic_test.go | 54 ++++++++++++++-------------- internal/modules/generic/commands.go | 2 +- 3 files changed, 31 insertions(+), 32 deletions(-) diff --git a/echovault/api_generic.go b/echovault/api_generic.go index b9e3cf23..efdeb5fb 100644 --- a/echovault/api_generic.go +++ b/echovault/api_generic.go @@ -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) } - diff --git a/echovault/api_generic_test.go b/echovault/api_generic_test.go index b57ac933..0565791a 100644 --- a/echovault/api_generic_test.go +++ b/echovault/api_generic_test.go @@ -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) + } } diff --git a/internal/modules/generic/commands.go b/internal/modules/generic/commands.go index 48569821..08c56663 100644 --- a/internal/modules/generic/commands.go +++ b/internal/modules/generic/commands.go @@ -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,