mirror of
https://github.com/EchoVault/SugarDB.git
synced 2025-10-08 17:30:47 +08:00
Implemented test for INCR embedded API.
Added test cases for incorrect commands length for INCR command. Fixed error checking on INCR commands test. RESP errors will be contained in the response object of the ReadValue method. The error object only contains an error when ReadValue failes.
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -919,3 +919,67 @@ func TestEchoVault_TTL(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestEchoVault_INCR(t *testing.T) {
|
||||||
|
server := createEchoVault()
|
||||||
|
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
key string
|
||||||
|
presetValues map[string]internal.KeyData
|
||||||
|
want int
|
||||||
|
wantErr bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "1. Increment non-existent key",
|
||||||
|
key: "IncrKey1",
|
||||||
|
presetValues: nil,
|
||||||
|
want: 1,
|
||||||
|
wantErr: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "2. Increment existing key with integer value",
|
||||||
|
key: "IncrKey2",
|
||||||
|
presetValues: map[string]internal.KeyData{
|
||||||
|
"IncrKey2": {Value: "5"},
|
||||||
|
},
|
||||||
|
want: 6,
|
||||||
|
wantErr: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "3. Increment existing key with non-integer value",
|
||||||
|
key: "IncrKey3",
|
||||||
|
presetValues: map[string]internal.KeyData{
|
||||||
|
"IncrKey3": {Value: "not_an_int"},
|
||||||
|
},
|
||||||
|
want: 0,
|
||||||
|
wantErr: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "4. Increment existing key with int64 value",
|
||||||
|
key: "IncrKey4",
|
||||||
|
presetValues: map[string]internal.KeyData{
|
||||||
|
"IncrKey4": {Value: int64(10)},
|
||||||
|
},
|
||||||
|
want: 11,
|
||||||
|
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.Incr(tt.key)
|
||||||
|
if (err != nil) != tt.wantErr {
|
||||||
|
t.Errorf("TTL() error = %v, wantErr %v", err, tt.wantErr)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if got != tt.want {
|
||||||
|
t.Errorf("TTL() got = %v, want %v", got, tt.want)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@@ -1897,7 +1897,6 @@ func Test_Generic(t *testing.T) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
t.Run("Test_HandlerINCR", func(t *testing.T) {
|
t.Run("Test_HandlerINCR", 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 {
|
||||||
@@ -1913,6 +1912,7 @@ func Test_Generic(t *testing.T) {
|
|||||||
name string
|
name string
|
||||||
key string
|
key string
|
||||||
presetValue interface{}
|
presetValue interface{}
|
||||||
|
command []resp.Value
|
||||||
expectedResponse int64
|
expectedResponse int64
|
||||||
expectedError error
|
expectedError error
|
||||||
}{
|
}{
|
||||||
@@ -1920,6 +1920,7 @@ func Test_Generic(t *testing.T) {
|
|||||||
name: "1. Increment non-existent key",
|
name: "1. Increment non-existent key",
|
||||||
key: "IncrKey1",
|
key: "IncrKey1",
|
||||||
presetValue: nil,
|
presetValue: nil,
|
||||||
|
command: []resp.Value{resp.StringValue("INCR"), resp.StringValue("IncrKey1")},
|
||||||
expectedResponse: 1,
|
expectedResponse: 1,
|
||||||
expectedError: nil,
|
expectedError: nil,
|
||||||
},
|
},
|
||||||
@@ -1927,23 +1928,46 @@ func Test_Generic(t *testing.T) {
|
|||||||
name: "2. Increment existing key with integer value",
|
name: "2. Increment existing key with integer value",
|
||||||
key: "IncrKey2",
|
key: "IncrKey2",
|
||||||
presetValue: "5",
|
presetValue: "5",
|
||||||
|
command: []resp.Value{resp.StringValue("INCR"), resp.StringValue("IncrKey2")},
|
||||||
expectedResponse: 6,
|
expectedResponse: 6,
|
||||||
expectedError: nil,
|
expectedError: nil,
|
||||||
},
|
},
|
||||||
// {
|
{
|
||||||
// name: "3. Increment existing key with non-integer value",
|
name: "3. Increment existing key with non-integer value",
|
||||||
// key: "IncrKey3",
|
key: "IncrKey3",
|
||||||
// presetValue: "not_an_int",
|
presetValue: "not_an_int",
|
||||||
// expectedResponse: 0,
|
command: []resp.Value{resp.StringValue("INCR"), resp.StringValue("IncrKey3")},
|
||||||
// expectedError: errors.New("value is not an integer or out of range"),
|
expectedResponse: 0,
|
||||||
// },
|
expectedError: errors.New("value is not an integer or out of range"),
|
||||||
|
},
|
||||||
{
|
{
|
||||||
name: "4. Increment existing key with int64 value",
|
name: "4. Increment existing key with int64 value",
|
||||||
key: "IncrKey4",
|
key: "IncrKey4",
|
||||||
presetValue: int64(10),
|
presetValue: int64(10),
|
||||||
|
command: []resp.Value{resp.StringValue("INCR"), resp.StringValue("IncrKey4")},
|
||||||
expectedResponse: 11,
|
expectedResponse: 11,
|
||||||
expectedError: nil,
|
expectedError: nil,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "5. Command too short",
|
||||||
|
key: "IncrKey5",
|
||||||
|
presetValue: nil,
|
||||||
|
command: []resp.Value{resp.StringValue("INCR")},
|
||||||
|
expectedResponse: 0,
|
||||||
|
expectedError: errors.New(constants.WrongArgsResponse),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "6. Command too long",
|
||||||
|
key: "IncrKey6",
|
||||||
|
presetValue: nil,
|
||||||
|
command: []resp.Value{
|
||||||
|
resp.StringValue("INCR"),
|
||||||
|
resp.StringValue("IncrKey6"),
|
||||||
|
resp.StringValue("IncrKey6"),
|
||||||
|
},
|
||||||
|
expectedResponse: 0,
|
||||||
|
expectedError: errors.New(constants.WrongArgsResponse),
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
@@ -1962,15 +1986,17 @@ func Test_Generic(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
command := []resp.Value{resp.StringValue("INCR"), resp.StringValue(test.key)}
|
if err = client.WriteArray(test.command); err != nil {
|
||||||
|
|
||||||
if err = client.WriteArray(command); err != nil {
|
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
res, _, err := client.ReadValue()
|
res, _, err := client.ReadValue()
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
|
||||||
if test.expectedError != nil {
|
if test.expectedError != nil {
|
||||||
if err == nil || !strings.Contains(err.Error(), test.expectedError.Error()) {
|
if !strings.Contains(res.Error().Error(), test.expectedError.Error()) {
|
||||||
t.Errorf("expected error \"%s\", got \"%s\"", test.expectedError.Error(), err.Error())
|
t.Errorf("expected error \"%s\", got \"%s\"", test.expectedError.Error(), err.Error())
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
|
Reference in New Issue
Block a user