mirror of
https://github.com/EchoVault/SugarDB.git
synced 2025-10-05 16:06:57 +08:00
Added command length tests for DECRBY command. Updated godoc comment for DecrBy embedded API method.
This commit is contained in:
@@ -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.
|
// If the value stored at the key is not an integer, an error is returned.
|
||||||
//
|
//
|
||||||
// Parameters:
|
// Parameters:
|
||||||
// - `key` (string): The key whose value is to be decremented.
|
|
||||||
// - `increment` (int): The amount by which to decrement the key's value. This can be a positive or negative integer.
|
|
||||||
//
|
//
|
||||||
// Returns:
|
// `key` - string - The key whose value is to be decremented.
|
||||||
// - (int): The new value of the key after the decrement operation.
|
//
|
||||||
|
// `increment` - int - The amount by which to decrement the key's value. This can be a positive or negative integer.
|
||||||
|
//
|
||||||
|
// Returns: The new value of the key after the decrement operation as an integer.
|
||||||
func (server *EchoVault) DecrBy(key string, value string) (int, error) {
|
func (server *EchoVault) DecrBy(key string, value string) (int, error) {
|
||||||
// Construct the command
|
// Construct the command
|
||||||
cmd := []string{"DECRBY", key, value}
|
cmd := []string{"DECRBY", key, value}
|
||||||
|
@@ -974,11 +974,11 @@ func TestEchoVault_INCR(t *testing.T) {
|
|||||||
}
|
}
|
||||||
got, err := server.Incr(tt.key)
|
got, err := server.Incr(tt.key)
|
||||||
if (err != nil) != tt.wantErr {
|
if (err != nil) != tt.wantErr {
|
||||||
t.Errorf("TTL() error = %v, wantErr %v", err, tt.wantErr)
|
t.Errorf("INCR() error = %v, wantErr %v", err, tt.wantErr)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if got != tt.want {
|
if got != tt.want {
|
||||||
t.Errorf("TTL() got = %v, want %v", got, tt.want)
|
t.Errorf("INCR() got = %v, want %v", got, tt.want)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -1038,11 +1038,11 @@ func TestEchoVault_DECR(t *testing.T) {
|
|||||||
}
|
}
|
||||||
got, err := server.Decr(tt.key)
|
got, err := server.Decr(tt.key)
|
||||||
if (err != nil) != tt.wantErr {
|
if (err != nil) != tt.wantErr {
|
||||||
t.Errorf("TTL() error = %v, wantErr %v", err, tt.wantErr)
|
t.Errorf("DECR() error = %v, wantErr %v", err, tt.wantErr)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if got != tt.want {
|
if got != tt.want {
|
||||||
t.Errorf("TTL() got = %v, want %v", got, tt.want)
|
t.Errorf("DECR() got = %v, want %v", got, tt.want)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -1107,11 +1107,11 @@ func TestEchoVault_DECRBY(t *testing.T) {
|
|||||||
}
|
}
|
||||||
got, err := server.DecrBy(tt.key, tt.decrement)
|
got, err := server.DecrBy(tt.key, tt.decrement)
|
||||||
if (err != nil) != tt.wantErr {
|
if (err != nil) != tt.wantErr {
|
||||||
t.Errorf("IncrBy() error = %v, wantErr %v", err, tt.wantErr)
|
t.Errorf("DecrBy() error = %v, wantErr %v", err, tt.wantErr)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if got != tt.want {
|
if got != tt.want {
|
||||||
t.Errorf("IncrBy() got = %v, want %v", got, tt.want)
|
t.Errorf("DecrBy() got = %v, want %v", got, tt.want)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@@ -478,13 +478,8 @@ func handleDecr(params internal.HandlerFuncParams) ([]byte, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func handleDecrBy(params internal.HandlerFuncParams) ([]byte, error) {
|
func handleDecrBy(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 DECRBY")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Extract key from command
|
// Extract key from command
|
||||||
keys, err := decrKeyByFunc(params.Command)
|
keys, err := decrByKeyFunc(params.Command)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -731,9 +726,12 @@ This operation is limited to 64 bit signed integers.`,
|
|||||||
Command: "decrby",
|
Command: "decrby",
|
||||||
Module: constants.GenericModule,
|
Module: constants.GenericModule,
|
||||||
Categories: []string{constants.KeyspaceCategory, constants.WriteCategory, constants.FastCategory},
|
Categories: []string{constants.KeyspaceCategory, constants.WriteCategory, constants.FastCategory},
|
||||||
Description: `(DECRBY key decrement) The DECRBY command reduces the value stored at the specified key by the specified decrement. If the key does not exist, it is initialized with a value of 0 before performing the operation. If the key's value is not of the correct type or cannot be represented as an integer, an error is returned.`,
|
Description: `(DECRBY key decrement)
|
||||||
|
The DECRBY command reduces the value stored at the specified key by the specified decrement.
|
||||||
|
If the key does not exist, it is initialized with a value of 0 before performing the operation.
|
||||||
|
If the key's value is not of the correct type or cannot be represented as an integer, an error is returned.`,
|
||||||
Sync: true,
|
Sync: true,
|
||||||
KeyExtractionFunc: decrKeyByFunc,
|
KeyExtractionFunc: decrByKeyFunc,
|
||||||
HandlerFunc: handleDecrBy,
|
HandlerFunc: handleDecrBy,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
@@ -2195,6 +2195,27 @@ func Test_Generic(t *testing.T) {
|
|||||||
expectedResponse: 3,
|
expectedResponse: 3,
|
||||||
expectedError: nil,
|
expectedError: nil,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "5. Command too short",
|
||||||
|
key: "DecrByKey5",
|
||||||
|
presetValue: nil,
|
||||||
|
command: []resp.Value{resp.StringValue("DECRBY"), resp.StringValue("DecrByKey5")},
|
||||||
|
expectedResponse: 0,
|
||||||
|
expectedError: errors.New(constants.WrongArgsResponse),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "6. Command too long",
|
||||||
|
key: "DecrKey6",
|
||||||
|
presetValue: nil,
|
||||||
|
command: []resp.Value{
|
||||||
|
resp.StringValue("DECRBY"),
|
||||||
|
resp.StringValue("DecrKey6"),
|
||||||
|
resp.StringValue("3"),
|
||||||
|
resp.StringValue("extra_arg"),
|
||||||
|
},
|
||||||
|
expectedResponse: 0,
|
||||||
|
expectedError: errors.New(constants.WrongArgsResponse),
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
|
@@ -155,9 +155,9 @@ func decrKeyFunc(cmd []string) (internal.KeyExtractionFuncResult, error) {
|
|||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func decrKeyByFunc(cmd []string) (internal.KeyExtractionFuncResult, error) {
|
func decrByKeyFunc(cmd []string) (internal.KeyExtractionFuncResult, error) {
|
||||||
if len(cmd) < 3 {
|
if len(cmd) != 3 {
|
||||||
return internal.KeyExtractionFuncResult{}, errors.New("wrong number of arguments for DECRBY")
|
return internal.KeyExtractionFuncResult{}, errors.New(constants.WrongArgsResponse)
|
||||||
}
|
}
|
||||||
return internal.KeyExtractionFuncResult{
|
return internal.KeyExtractionFuncResult{
|
||||||
WriteKeys: []string{cmd[1]},
|
WriteKeys: []string{cmd[1]},
|
||||||
|
Reference in New Issue
Block a user