mirror of
https://github.com/EchoVault/SugarDB.git
synced 2025-10-04 15:42:40 +08:00
added test for incrementHandler
This commit is contained in:
@@ -1897,98 +1897,97 @@ func Test_Generic(t *testing.T) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
t.Run("Test_HandlerINCR", func(t *testing.T) {
|
t.Run("Test_HandlerINCR", func(t *testing.T) {
|
||||||
t.Run("Test_HandleIncr", func(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
conn, err := internal.GetConnection("localhost", port)
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
defer func() {
|
|
||||||
_ = conn.Close()
|
|
||||||
}()
|
|
||||||
client := resp.NewConn(conn)
|
|
||||||
|
|
||||||
tests := []struct {
|
t.Parallel()
|
||||||
name string
|
conn, err := internal.GetConnection("localhost", port)
|
||||||
key string
|
if err != nil {
|
||||||
presetValue interface{}
|
t.Error(err)
|
||||||
expectedResponse int64
|
return
|
||||||
expectedError error
|
}
|
||||||
}{
|
defer func() {
|
||||||
{
|
_ = conn.Close()
|
||||||
name: "1. Increment non-existent key",
|
}()
|
||||||
key: "IncrKey1",
|
client := resp.NewConn(conn)
|
||||||
presetValue: nil,
|
|
||||||
expectedResponse: 1,
|
|
||||||
expectedError: nil,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "2. Increment existing key with integer value",
|
|
||||||
key: "IncrKey2",
|
|
||||||
presetValue: "5",
|
|
||||||
expectedResponse: 6,
|
|
||||||
expectedError: nil,
|
|
||||||
},
|
|
||||||
// {
|
|
||||||
// name: "3. Increment existing key with non-integer value",
|
|
||||||
// key: "IncrKey3",
|
|
||||||
// presetValue: "not_an_int",
|
|
||||||
// expectedResponse: 0,
|
|
||||||
// expectedError: errors.New("value is not an integer or out of range"),
|
|
||||||
// },
|
|
||||||
{
|
|
||||||
name: "4. Increment existing key with int64 value",
|
|
||||||
key: "IncrKey4",
|
|
||||||
presetValue: int64(10),
|
|
||||||
expectedResponse: 11,
|
|
||||||
expectedError: nil,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, test := range tests {
|
tests := []struct {
|
||||||
t.Run(test.name, func(t *testing.T) {
|
name string
|
||||||
if test.presetValue != nil {
|
key string
|
||||||
command := []resp.Value{resp.StringValue("SET"), resp.StringValue(test.key), resp.StringValue(fmt.Sprintf("%v", test.presetValue))}
|
presetValue interface{}
|
||||||
if err = client.WriteArray(command); err != nil {
|
expectedResponse int64
|
||||||
t.Error(err)
|
expectedError error
|
||||||
}
|
}{
|
||||||
res, _, err := client.ReadValue()
|
{
|
||||||
if err != nil {
|
name: "1. Increment non-existent key",
|
||||||
t.Error(err)
|
key: "IncrKey1",
|
||||||
}
|
presetValue: nil,
|
||||||
if !strings.EqualFold(res.String(), "ok") {
|
expectedResponse: 1,
|
||||||
t.Errorf("expected preset response to be OK, got %s", res.String())
|
expectedError: nil,
|
||||||
}
|
},
|
||||||
}
|
{
|
||||||
|
name: "2. Increment existing key with integer value",
|
||||||
command := []resp.Value{resp.StringValue("INCR"), resp.StringValue(test.key)}
|
key: "IncrKey2",
|
||||||
|
presetValue: "5",
|
||||||
|
expectedResponse: 6,
|
||||||
|
expectedError: nil,
|
||||||
|
},
|
||||||
|
// {
|
||||||
|
// name: "3. Increment existing key with non-integer value",
|
||||||
|
// key: "IncrKey3",
|
||||||
|
// presetValue: "not_an_int",
|
||||||
|
// expectedResponse: 0,
|
||||||
|
// expectedError: errors.New("value is not an integer or out of range"),
|
||||||
|
// },
|
||||||
|
{
|
||||||
|
name: "4. Increment existing key with int64 value",
|
||||||
|
key: "IncrKey4",
|
||||||
|
presetValue: int64(10),
|
||||||
|
expectedResponse: 11,
|
||||||
|
expectedError: nil,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range tests {
|
||||||
|
t.Run(test.name, func(t *testing.T) {
|
||||||
|
if test.presetValue != nil {
|
||||||
|
command := []resp.Value{resp.StringValue("SET"), resp.StringValue(test.key), resp.StringValue(fmt.Sprintf("%v", test.presetValue))}
|
||||||
if err = client.WriteArray(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 test.expectedError != nil {
|
|
||||||
if err == nil || !strings.Contains(err.Error(), test.expectedError.Error()) {
|
|
||||||
t.Errorf("expected error \"%s\", got \"%s\"", test.expectedError.Error(), err.Error())
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
} else {
|
|
||||||
responseInt, err := strconv.ParseInt(res.String(), 10, 64)
|
|
||||||
if err != nil {
|
|
||||||
t.Errorf("error parsing response to int64: %s", err)
|
|
||||||
}
|
|
||||||
if responseInt != test.expectedResponse {
|
|
||||||
t.Errorf("expected response %d, got %d", test.expectedResponse, responseInt)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
})
|
if !strings.EqualFold(res.String(), "ok") {
|
||||||
}
|
t.Errorf("expected preset response to be OK, got %s", res.String())
|
||||||
})
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
command := []resp.Value{resp.StringValue("INCR"), resp.StringValue(test.key)}
|
||||||
|
|
||||||
|
if err = client.WriteArray(command); err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
res, _, err := client.ReadValue()
|
||||||
|
if test.expectedError != nil {
|
||||||
|
if err == nil || !strings.Contains(err.Error(), test.expectedError.Error()) {
|
||||||
|
t.Errorf("expected error \"%s\", got \"%s\"", test.expectedError.Error(), err.Error())
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
} else {
|
||||||
|
responseInt, err := strconv.ParseInt(res.String(), 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("error parsing response to int64: %s", err)
|
||||||
|
}
|
||||||
|
if responseInt != test.expectedResponse {
|
||||||
|
t.Errorf("expected response %d, got %d", test.expectedResponse, responseInt)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user