Skip to content

Commit

Permalink
Added command length tests for INCRBY command. Renamed INCRBY key fun…
Browse files Browse the repository at this point in the history
…c. Return WRONG_ARG_RESPONSE from key funcs when commands are not the correct length.
  • Loading branch information
kelvinmwinuka committed Jun 25, 2024
1 parent fe9344d commit db04465
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 35 deletions.
10 changes: 5 additions & 5 deletions echovault/api_generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -467,12 +467,12 @@ func (server *EchoVault) Decr(key string) (int, error) {
// If the value stored at the key is not an integer, an error is returned.
//
// Parameters:
// - `key` (string): The key whose value is to be incremented.
// - `increment` (int): The amount by which to increment the key's value. This can be a positive or negative integer.
//
// Returns:
// - (int): The new value of the key after the increment operation.

// `key` - string - The key whose value is to be incremented.
//
// `increment` - int - The amount by which to increment the key's value. This can be a positive or negative integer.
//
// Returns: The new value of the key after the increment operation as an integer.
func (server *EchoVault) IncrBy(key string, value string) (int, error) {
// Construct the command
cmd := []string{"INCRBY", key, value}
Expand Down
19 changes: 8 additions & 11 deletions internal/modules/generic/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -478,13 +478,8 @@ func handleDecr(params internal.HandlerFuncParams) ([]byte, error) {
}

func handleIncrBy(params internal.HandlerFuncParams) ([]byte, error) {
// Ensure command has the correct number of arguments
if len(params.Command) != 3 {
return nil, errors.New("wrong number of arguments for INCRBY")
}

// Extract key from command
keys, err := incrKeyByFunc(params.Command)
keys, err := incrByKeyFunc(params.Command)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -728,12 +723,14 @@ This operation is limited to 64 bit signed integers.`,
HandlerFunc: handleDecr,
},
{
Command: "incrby",
Module: constants.GenericModule,
Categories: []string{constants.KeyspaceCategory, constants.WriteCategory, constants.FastCategory},
Description: `(INCRBY key increment) Increments the number stored at key by increment. If the key does not exist, it is set to 0 before performing the operation. An error is returned if the key contains a value of the wrong type or contains a string that can not be represented as integer.`,
Command: "incrby",
Module: constants.GenericModule,
Categories: []string{constants.KeyspaceCategory, constants.WriteCategory, constants.FastCategory},
Description: `(INCRBY key increment)
Increments the number stored at key by increment. If the key does not exist, it is set to 0 before performing the operation.
An error is returned if the key contains a value of the wrong type or contains a string that can not be represented as integer.`,
Sync: true,
KeyExtractionFunc: incrKeyByFunc,
KeyExtractionFunc: incrByKeyFunc,
HandlerFunc: handleIncrBy,
},
}
Expand Down
37 changes: 23 additions & 14 deletions internal/modules/generic/commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2195,20 +2195,29 @@ func Test_Generic(t *testing.T) {
expectedResponse: 17,
expectedError: nil,
},
// {
// name: "6. Command too long",
// key: "IncrByKey6",
// increment: "5",
// presetValue: nil,
// command: []resp.Value{
// resp.StringValue("INCRBY"),
// resp.StringValue("IncrByKey6"),
// resp.StringValue("5"),
// resp.StringValue("extra_arg"),
// },
// expectedResponse: 0,
// expectedError: errors.New("ERR wrong number of arguments for 'incrby' command"),
// },
{
name: "5. Command too short",
key: "IncrByKey5",
increment: "5",
presetValue: nil,
command: []resp.Value{resp.StringValue("INCRBY"), resp.StringValue("IncrByKey5")},
expectedResponse: 0,
expectedError: errors.New(constants.WrongArgsResponse),
},
{
name: "6. Command too long",
key: "IncrByKey6",
increment: "5",
presetValue: nil,
command: []resp.Value{
resp.StringValue("INCRBY"),
resp.StringValue("IncrByKey6"),
resp.StringValue("5"),
resp.StringValue("extra_arg"),
},
expectedResponse: 0,
expectedError: errors.New(constants.WrongArgsResponse),
},
}

for _, test := range tests {
Expand Down
10 changes: 5 additions & 5 deletions internal/modules/generic/key_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func expireAtKeyFunc(cmd []string) (internal.KeyExtractionFuncResult, error) {

func incrKeyFunc(cmd []string) (internal.KeyExtractionFuncResult, error) {
if len(cmd) != 2 {
return internal.KeyExtractionFuncResult{}, errors.New("wrong number of arguments for INCR")
return internal.KeyExtractionFuncResult{}, errors.New(constants.WrongArgsResponse)
}
return internal.KeyExtractionFuncResult{
WriteKeys: cmd[1:2],
Expand All @@ -148,16 +148,16 @@ func incrKeyFunc(cmd []string) (internal.KeyExtractionFuncResult, error) {

func decrKeyFunc(cmd []string) (internal.KeyExtractionFuncResult, error) {
if len(cmd) != 2 {
return internal.KeyExtractionFuncResult{}, errors.New("wrong number of arguments for INCR")
return internal.KeyExtractionFuncResult{}, errors.New(constants.WrongArgsResponse)
}
return internal.KeyExtractionFuncResult{
WriteKeys: cmd[1:2],
}, nil
}

func incrKeyByFunc(cmd []string) (internal.KeyExtractionFuncResult, error) {
if len(cmd) < 3 {
return internal.KeyExtractionFuncResult{}, errors.New("wrong number of arguments for INCRBY")
func incrByKeyFunc(cmd []string) (internal.KeyExtractionFuncResult, error) {
if len(cmd) != 3 {
return internal.KeyExtractionFuncResult{}, errors.New(constants.WrongArgsResponse)
}
return internal.KeyExtractionFuncResult{
WriteKeys: []string{cmd[1]},
Expand Down

0 comments on commit db04465

Please sign in to comment.