mirror of
https://github.com/EchoVault/SugarDB.git
synced 2025-10-06 16:36:54 +08:00
added tests
This commit is contained in:
8635
coverage/coverage.out
Normal file
8635
coverage/coverage.out
Normal file
File diff suppressed because it is too large
Load Diff
@@ -1116,3 +1116,72 @@ func TestEchoVault_DECRBY(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestEchoVault_INCRBY(t *testing.T) {
|
||||||
|
server := createEchoVault()
|
||||||
|
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
key string
|
||||||
|
increment string
|
||||||
|
presetValues map[string]internal.KeyData
|
||||||
|
want int
|
||||||
|
wantErr bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "1. Increment non-existent key by 4",
|
||||||
|
key: "IncrByKey1",
|
||||||
|
increment: "4",
|
||||||
|
presetValues: nil,
|
||||||
|
want: 4,
|
||||||
|
wantErr: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "2. Increment existing key with integer value by 3",
|
||||||
|
key: "IncrByKey2",
|
||||||
|
increment: "3",
|
||||||
|
presetValues: map[string]internal.KeyData{
|
||||||
|
"IncrByKey2": {Value: "5"},
|
||||||
|
},
|
||||||
|
want: 8,
|
||||||
|
wantErr: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "3. Increment existing key with non-integer value by 2",
|
||||||
|
key: "IncrByKey3",
|
||||||
|
increment: "2",
|
||||||
|
presetValues: map[string]internal.KeyData{
|
||||||
|
"IncrByKey3": {Value: "not_an_int"},
|
||||||
|
},
|
||||||
|
want: 0,
|
||||||
|
wantErr: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "4. Increment existing key with int64 value by 7",
|
||||||
|
key: "IncrByKey4",
|
||||||
|
increment: "7",
|
||||||
|
presetValues: map[string]internal.KeyData{
|
||||||
|
"IncrByKey4": {Value: int64(10)},
|
||||||
|
},
|
||||||
|
want: 17,
|
||||||
|
wantErr: false,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
if tt.presetValues != nil {
|
||||||
|
for k, d := range tt.presetValues {
|
||||||
|
presetKeyData(server, context.Background(), k, d)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
got, err := server.IncrBy(tt.key, tt.increment)
|
||||||
|
if (err != nil) != tt.wantErr {
|
||||||
|
t.Errorf("IncrBy() error = %v, wantErr %v", err, tt.wantErr)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if got != tt.want {
|
||||||
|
t.Errorf("IncrBy() got = %v, want %v", got, tt.want)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@@ -2138,7 +2138,7 @@ func Test_Generic(t *testing.T) {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("Test_HandlerDECRBY", func(t *testing.T) {
|
t.Run("Test_HandlerINCRBY", func(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
conn, err := internal.GetConnection("localhost", port)
|
conn, err := internal.GetConnection("localhost", port)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -2153,69 +2153,62 @@ func Test_Generic(t *testing.T) {
|
|||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
key string
|
key string
|
||||||
decrement string
|
increment string
|
||||||
presetValue interface{}
|
presetValue interface{}
|
||||||
command []resp.Value
|
command []resp.Value
|
||||||
expectedResponse int64
|
expectedResponse int64
|
||||||
expectedError error
|
expectedError error
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "1. Decrement non-existent key by 4",
|
name: "1. Increment non-existent key by 4",
|
||||||
key: "DecrByKey1",
|
key: "IncrByKey1",
|
||||||
decrement: "4",
|
increment: "4",
|
||||||
presetValue: nil,
|
presetValue: nil,
|
||||||
command: []resp.Value{resp.StringValue("DECRBY"), resp.StringValue("DecrByKey1"), resp.StringValue("4")},
|
command: []resp.Value{resp.StringValue("INCRBY"), resp.StringValue("IncrByKey1"), resp.StringValue("4")},
|
||||||
expectedResponse: -4,
|
expectedResponse: 4,
|
||||||
expectedError: nil,
|
expectedError: nil,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "2. Decrement existing key with integer value by 3",
|
name: "2. Increment existing key with integer value by 3",
|
||||||
key: "DecrByKey2",
|
key: "IncrByKey2",
|
||||||
decrement: "3",
|
increment: "3",
|
||||||
presetValue: "5",
|
presetValue: "5",
|
||||||
command: []resp.Value{resp.StringValue("DECRBY"), resp.StringValue("DecrByKey2"), resp.StringValue("3")},
|
command: []resp.Value{resp.StringValue("INCRBY"), resp.StringValue("IncrByKey2"), resp.StringValue("3")},
|
||||||
expectedResponse: 2,
|
expectedResponse: 8,
|
||||||
expectedError: nil,
|
expectedError: nil,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "3. Decrement existing key with non-integer value by 2",
|
name: "3. Increment existing key with non-integer value by 2",
|
||||||
key: "DecrByKey3",
|
key: "IncrByKey3",
|
||||||
decrement: "2",
|
increment: "2",
|
||||||
presetValue: "not_an_int",
|
presetValue: "not_an_int",
|
||||||
command: []resp.Value{resp.StringValue("DECRBY"), resp.StringValue("DecrByKey3"), resp.StringValue("2")},
|
command: []resp.Value{resp.StringValue("INCRBY"), resp.StringValue("IncrByKey3"), resp.StringValue("2")},
|
||||||
expectedResponse: 0,
|
expectedResponse: 0,
|
||||||
expectedError: errors.New("value is not an integer or out of range"),
|
expectedError: errors.New("value is not an integer or out of range"),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "4. Decrement existing key with int64 value by 7",
|
name: "4. Increment existing key with int64 value by 7",
|
||||||
key: "DecrByKey4",
|
key: "IncrByKey4",
|
||||||
decrement: "7",
|
increment: "7",
|
||||||
presetValue: int64(10),
|
presetValue: int64(10),
|
||||||
command: []resp.Value{resp.StringValue("DECRBY"), resp.StringValue("DecrByKey4"), resp.StringValue("7")},
|
command: []resp.Value{resp.StringValue("INCRBY"), resp.StringValue("IncrByKey4"), resp.StringValue("7")},
|
||||||
expectedResponse: 3,
|
expectedResponse: 17,
|
||||||
expectedError: nil,
|
expectedError: nil,
|
||||||
},
|
},
|
||||||
{
|
// {
|
||||||
name: "5. Command too short",
|
// name: "6. Command too long",
|
||||||
key: "DecrByKey5",
|
// key: "IncrByKey6",
|
||||||
presetValue: nil,
|
// increment: "5",
|
||||||
command: []resp.Value{resp.StringValue("DECRBY"), resp.StringValue("DecrByKey5")},
|
// presetValue: nil,
|
||||||
expectedResponse: 0,
|
// command: []resp.Value{
|
||||||
expectedError: errors.New(constants.WrongArgsResponse),
|
// resp.StringValue("INCRBY"),
|
||||||
},
|
// resp.StringValue("IncrByKey6"),
|
||||||
{
|
// resp.StringValue("5"),
|
||||||
name: "6. Command too long",
|
// resp.StringValue("extra_arg"),
|
||||||
key: "DecrKey6",
|
// },
|
||||||
presetValue: nil,
|
// expectedResponse: 0,
|
||||||
command: []resp.Value{
|
// expectedError: errors.New("ERR wrong number of arguments for 'incrby' command"),
|
||||||
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 {
|
||||||
|
Reference in New Issue
Block a user