Implemented unit test for ZREM command handler

This commit is contained in:
Kelvin Clement Mwinuka
2024-02-22 04:53:25 +08:00
parent 464f8cd1b6
commit ba61e103ee
2 changed files with 102 additions and 15 deletions

View File

@@ -899,18 +899,18 @@ func handleZRANK(ctx context.Context, cmd []string, server utils.Server, conn *n
}
func handleZREM(ctx context.Context, cmd []string, server utils.Server, conn *net.Conn) ([]byte, error) {
if len(cmd) < 3 {
return nil, errors.New(utils.WRONG_ARGS_RESPONSE)
keys, err := zremKeyFunc(cmd)
if err != nil {
return nil, err
}
key := cmd[1]
key := keys[0]
if !server.KeyExists(key) {
return []byte(":0\r\n\r\n"), nil
}
_, err := server.KeyLock(ctx, key)
if err != nil {
if _, err = server.KeyLock(ctx, key); err != nil {
return nil, err
}
defer server.KeyUnlock(key)
@@ -1663,23 +1663,25 @@ Returns the rank of the specified member in the sorted set. WITHSCORE modifies t
KeyExtractionFunc: zrankKeyFunc,
HandlerFunc: handleZRANK,
},
{
Command: "zrem",
Categories: []string{utils.SortedSetCategory, utils.WriteCategory, utils.FastCategory},
Description: `(ZREM key member [member ...]) Removes the listed members from the sorted set.`,
Sync: true,
KeyExtractionFunc: zremKeyFunc,
HandlerFunc: handleZREM,
},
{
Command: "zrevrank",
Categories: []string{utils.SortedSetCategory, utils.ReadCategory, utils.SlowCategory},
Description: `(ZREVRANK key member [WITHSCORE])
Returns the rank of the member in the sorted set. WITHSCORE modifies the result to include the score.`,
Returns the rank of the member in the sorted set in reverse order.
WITHSCORE modifies the result to include the score.`,
Sync: false,
KeyExtractionFunc: zrevrankKeyFunc,
HandlerFunc: handleZRANK,
},
{
Command: "zrem",
Categories: []string{utils.SortedSetCategory, utils.WriteCategory, utils.FastCategory},
Description: `(ZREM key member [member ...]) Removes the listed members from the sorted set.
Returns the number of elements removed.`,
Sync: true,
KeyExtractionFunc: zremKeyFunc,
HandlerFunc: handleZREM,
},
{
Command: "zscore",
Categories: []string{utils.SortedSetCategory, utils.ReadCategory, utils.FastCategory},

View File

@@ -2022,7 +2022,92 @@ func Test_HandleZRANK(t *testing.T) {
}
}
func Test_HandleZREM(t *testing.T) {}
func Test_HandleZREM(t *testing.T) {
mockServer := server.NewServer(server.Opts{})
tests := []struct {
preset bool
presetValues map[string]interface{}
command []string
expectedValues map[string]*SortedSet
expectedResponse int
expectedError error
}{
{ // 1. Successfully remove multiple elements from sorted set, skipping non-existent members.
// Return deleted count.
preset: true,
presetValues: map[string]interface{}{
"key1": NewSortedSet([]MemberParam{
{value: "one", score: 1}, {value: "two", score: 2},
{value: "three", score: 3}, {value: "four", score: 4},
{value: "five", score: 5}, {value: "six", score: 6},
{value: "seven", score: 7}, {value: "eight", score: 8},
{value: "nine", score: 9}, {value: "ten", score: 10},
}),
},
command: []string{"ZREM", "key1", "three", "four", "five", "none", "six", "none", "seven"},
expectedValues: map[string]*SortedSet{
"key1": NewSortedSet([]MemberParam{
{value: "one", score: 1}, {value: "two", score: 2}, {value: "eight", score: 8},
{value: "nine", score: 9}, {value: "ten", score: 10},
}),
},
expectedResponse: 5,
expectedError: nil,
},
{ // 2. If key does not exist, return 0
preset: false,
presetValues: nil,
command: []string{"ZREM", "key2", "member"},
expectedValues: nil,
expectedResponse: 0,
expectedError: nil,
},
{ // 3. Return error key is not a sorted set
preset: true,
presetValues: map[string]interface{}{
"key3": "Default value",
},
command: []string{"ZREM", "key3", "member"},
expectedError: errors.New("value at key3 is not a sorted set"),
},
{ // 9. Command too short
preset: false,
command: []string{"ZREM"},
expectedError: errors.New(utils.WRONG_ARGS_RESPONSE),
},
}
for _, test := range tests {
if test.preset {
for key, value := range test.presetValues {
if _, err := mockServer.CreateKeyAndLock(context.Background(), key); err != nil {
t.Error(err)
}
mockServer.SetValue(context.Background(), key, value)
mockServer.KeyUnlock(key)
}
}
res, err := handleZREM(context.Background(), test.command, mockServer, nil)
if test.expectedError != nil {
if err.Error() != test.expectedError.Error() {
t.Errorf("expected error \"%s\", got \"%s\"", test.expectedError.Error(), err.Error())
}
continue
}
if err != nil {
t.Error(err)
}
rd := resp.NewReader(bytes.NewBuffer(res))
rv, _, err := rd.ReadValue()
if err != nil {
t.Error(err)
}
if rv.Integer() != test.expectedResponse {
t.Errorf("expected response %d, got %d", test.expectedResponse, rv.Integer())
}
}
}
func Test_HandleZREMRANGEBYSCORE(t *testing.T) {}