From 9b897b472f111165f98ea77b19000b72712c25ff Mon Sep 17 00:00:00 2001 From: Kelvin Mwinuka Date: Tue, 25 Jun 2024 22:39:46 +0800 Subject: [PATCH] Added wrong args tests for RENAME command. --- internal/modules/generic/commands.go | 11 ++++++----- internal/modules/generic/commands_test.go | 15 +++++++++++++++ 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/internal/modules/generic/commands.go b/internal/modules/generic/commands.go index e8e3c368..b340577c 100644 --- a/internal/modules/generic/commands.go +++ b/internal/modules/generic/commands.go @@ -583,7 +583,7 @@ func handleDecrBy(params internal.HandlerFuncParams) ([]byte, error) { func handleRename(params internal.HandlerFuncParams) ([]byte, error) { if len(params.Command) != 3 { - return nil, errors.New("wrong number of arguments for RENAME") + return nil, errors.New(constants.WrongArgsResponse) } oldKey := params.Command[1] @@ -827,10 +827,11 @@ If the key's value is not of the correct type or cannot be represented as an int HandlerFunc: handleDecrBy, }, { - Command: "rename", - Module: constants.GenericModule, - Categories: []string{constants.KeyspaceCategory, constants.WriteCategory, constants.FastCategory}, - Description: `(RENAME key newkey) Renames key to newkey. If newkey already exists, it is overwritten. If key does not exist, an error is returned.`, + Command: "rename", + Module: constants.GenericModule, + Categories: []string{constants.KeyspaceCategory, constants.WriteCategory, constants.FastCategory}, + Description: `(RENAME key newkey) +Renames key to newkey. If newkey already exists, it is overwritten. If key does not exist, an error is returned.`, Sync: true, KeyExtractionFunc: renameKeyFunc, HandlerFunc: handleRename, diff --git a/internal/modules/generic/commands_test.go b/internal/modules/generic/commands_test.go index 0af92d12..17190ae3 100644 --- a/internal/modules/generic/commands_test.go +++ b/internal/modules/generic/commands_test.go @@ -2442,6 +2442,21 @@ func Test_Generic(t *testing.T) { expectedResponse: "OK", expectedError: nil, }, + { + name: "4. Command too short", + command: []resp.Value{resp.StringValue("RENAME"), resp.StringValue("key")}, + expectedError: errors.New(constants.WrongArgsResponse), + }, + { + name: "5. Command too long", + command: []resp.Value{ + resp.StringValue("RENAME"), + resp.StringValue("key"), + resp.StringValue("newkey"), + resp.StringValue("extra_arg"), + }, + expectedError: errors.New(constants.WrongArgsResponse), + }, } for _, test := range tests {