mirror of
https://github.com/EchoVault/SugarDB.git
synced 2025-10-06 00:16:53 +08:00
Implemented test for ZCOUNT command handler
This commit is contained in:
@@ -187,11 +187,12 @@ func handleZCARD(ctx context.Context, cmd []string, server utils.Server, conn *n
|
|||||||
}
|
}
|
||||||
|
|
||||||
func handleZCOUNT(ctx context.Context, cmd []string, server utils.Server, conn *net.Conn) ([]byte, error) {
|
func handleZCOUNT(ctx context.Context, cmd []string, server utils.Server, conn *net.Conn) ([]byte, error) {
|
||||||
if len(cmd) != 4 {
|
keys, err := zcountKeyFunc(cmd)
|
||||||
return nil, errors.New(utils.WRONG_ARGS_RESPONSE)
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
key := cmd[1]
|
key := keys[0]
|
||||||
|
|
||||||
minimum := Score(math.Inf(-1))
|
minimum := Score(math.Inf(-1))
|
||||||
switch utils.AdaptType(cmd[2]).(type) {
|
switch utils.AdaptType(cmd[2]).(type) {
|
||||||
@@ -230,11 +231,10 @@ func handleZCOUNT(ctx context.Context, cmd []string, server utils.Server, conn *
|
|||||||
}
|
}
|
||||||
|
|
||||||
if !server.KeyExists(key) {
|
if !server.KeyExists(key) {
|
||||||
return []byte("*0\r\n\r\n"), nil
|
return []byte(":0\r\n\r\n"), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err := server.KeyRLock(ctx, key)
|
if _, err = server.KeyRLock(ctx, key); err != nil {
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
defer server.KeyRUnlock(key)
|
defer server.KeyRUnlock(key)
|
||||||
@@ -1577,9 +1577,11 @@ Adds all the specified members with the specified scores to the sorted set at th
|
|||||||
HandlerFunc: handleZADD,
|
HandlerFunc: handleZADD,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Command: "zcard",
|
Command: "zcard",
|
||||||
Categories: []string{utils.SortedSetCategory, utils.ReadCategory, utils.SlowCategory},
|
Categories: []string{utils.SortedSetCategory, utils.ReadCategory, utils.SlowCategory},
|
||||||
Description: `(ZCARD key) Returns the set cardinality of the sorted set at key.`,
|
Description: `(ZCARD key) Returns the set cardinality of the sorted set at key.
|
||||||
|
If the key does not exist, 0 is returned, otherwise the cardinality of the sorted set is returned.
|
||||||
|
If the key holds a value that is not a sorted set, this command will return an error.`,
|
||||||
Sync: false,
|
Sync: false,
|
||||||
KeyExtractionFunc: zcardKeyFunc,
|
KeyExtractionFunc: zcardKeyFunc,
|
||||||
HandlerFunc: handleZCARD,
|
HandlerFunc: handleZCARD,
|
||||||
@@ -1588,7 +1590,9 @@ Adds all the specified members with the specified scores to the sorted set at th
|
|||||||
Command: "zcount",
|
Command: "zcount",
|
||||||
Categories: []string{utils.SortedSetCategory, utils.ReadCategory, utils.SlowCategory},
|
Categories: []string{utils.SortedSetCategory, utils.ReadCategory, utils.SlowCategory},
|
||||||
Description: `(ZCOUNT key min max)
|
Description: `(ZCOUNT key min max)
|
||||||
Returns the number of elements in the sorted set key with scores in the range of min and max.`,
|
Returns the number of elements in the sorted set key with scores in the range of min and max.
|
||||||
|
If the key does not exist, a count of 0 is returned, otherwise return the count.
|
||||||
|
If the key holds a value that is not a sorted set, an error is returned.`,
|
||||||
Sync: false,
|
Sync: false,
|
||||||
KeyExtractionFunc: zcountKeyFunc,
|
KeyExtractionFunc: zcountKeyFunc,
|
||||||
HandlerFunc: handleZCOUNT,
|
HandlerFunc: handleZCOUNT,
|
||||||
|
@@ -349,7 +349,135 @@ func Test_HandleZCARD(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test_HandleZCOUNT(t *testing.T) {}
|
func Test_HandleZCOUNT(t *testing.T) {
|
||||||
|
mockServer := server.NewServer(server.Opts{})
|
||||||
|
|
||||||
|
tests := []struct {
|
||||||
|
preset bool
|
||||||
|
presetValue *SortedSet
|
||||||
|
key string
|
||||||
|
command []string
|
||||||
|
expectedValue *SortedSet
|
||||||
|
expectedResponse int
|
||||||
|
expectedError error
|
||||||
|
}{
|
||||||
|
{ // 1. Get entire count using infinity boundaries
|
||||||
|
preset: true,
|
||||||
|
presetValue: NewSortedSet([]MemberParam{
|
||||||
|
{value: "member1", score: Score(5.5)},
|
||||||
|
{value: "member2", score: Score(67.77)},
|
||||||
|
{value: "member3", score: Score(10)},
|
||||||
|
{value: "member4", score: Score(1083.13)},
|
||||||
|
{value: "member5", score: Score(11)},
|
||||||
|
{value: "member6", score: Score(math.Inf(-1))},
|
||||||
|
{value: "member7", score: Score(math.Inf(1))},
|
||||||
|
}),
|
||||||
|
key: "key1",
|
||||||
|
command: []string{"ZCOUNT", "key1", "-inf", "+inf"},
|
||||||
|
expectedValue: nil,
|
||||||
|
expectedResponse: 7,
|
||||||
|
expectedError: nil,
|
||||||
|
},
|
||||||
|
{ // 2. Get count of sub-set from -inf to limit
|
||||||
|
preset: true,
|
||||||
|
presetValue: NewSortedSet([]MemberParam{
|
||||||
|
{value: "member1", score: Score(5.5)},
|
||||||
|
{value: "member2", score: Score(67.77)},
|
||||||
|
{value: "member3", score: Score(10)},
|
||||||
|
{value: "member4", score: Score(1083.13)},
|
||||||
|
{value: "member5", score: Score(11)},
|
||||||
|
{value: "member6", score: Score(math.Inf(-1))},
|
||||||
|
{value: "member7", score: Score(math.Inf(1))},
|
||||||
|
}),
|
||||||
|
key: "key2",
|
||||||
|
command: []string{"ZCOUNT", "key2", "-inf", "90"},
|
||||||
|
expectedValue: nil,
|
||||||
|
expectedResponse: 5,
|
||||||
|
expectedError: nil,
|
||||||
|
},
|
||||||
|
{ // 3. Get count of sub-set from bottom boundary to +inf limit
|
||||||
|
preset: true,
|
||||||
|
presetValue: NewSortedSet([]MemberParam{
|
||||||
|
{value: "member1", score: Score(5.5)},
|
||||||
|
{value: "member2", score: Score(67.77)},
|
||||||
|
{value: "member3", score: Score(10)},
|
||||||
|
{value: "member4", score: Score(1083.13)},
|
||||||
|
{value: "member5", score: Score(11)},
|
||||||
|
{value: "member6", score: Score(math.Inf(-1))},
|
||||||
|
{value: "member7", score: Score(math.Inf(1))},
|
||||||
|
}),
|
||||||
|
key: "key3",
|
||||||
|
command: []string{"ZCOUNT", "key3", "1000", "+inf"},
|
||||||
|
expectedValue: nil,
|
||||||
|
expectedResponse: 2,
|
||||||
|
expectedError: nil,
|
||||||
|
},
|
||||||
|
{ // 4. Return error when bottom boundary is not a valid double/float
|
||||||
|
preset: false,
|
||||||
|
presetValue: nil,
|
||||||
|
key: "key4",
|
||||||
|
command: []string{"ZCOUNT", "key4", "min", "10"},
|
||||||
|
expectedValue: nil,
|
||||||
|
expectedResponse: 0,
|
||||||
|
expectedError: errors.New("min constraint must be a double"),
|
||||||
|
},
|
||||||
|
{ // 5. Return error when top boundary is not a valid double/float
|
||||||
|
preset: false,
|
||||||
|
presetValue: nil,
|
||||||
|
key: "key5",
|
||||||
|
command: []string{"ZCOUNT", "key5", "-10", "max"},
|
||||||
|
expectedValue: nil,
|
||||||
|
expectedResponse: 0,
|
||||||
|
expectedError: errors.New("max constraint must be a double"),
|
||||||
|
},
|
||||||
|
{ // 6. Command is too short
|
||||||
|
preset: false,
|
||||||
|
presetValue: nil,
|
||||||
|
key: "key6",
|
||||||
|
command: []string{"ZCOUNT"},
|
||||||
|
expectedValue: nil,
|
||||||
|
expectedResponse: 0,
|
||||||
|
expectedError: errors.New(utils.WRONG_ARGS_RESPONSE),
|
||||||
|
},
|
||||||
|
{ // 7. Command too long
|
||||||
|
preset: false,
|
||||||
|
presetValue: nil,
|
||||||
|
key: "key7",
|
||||||
|
command: []string{"ZCOUNT", "key4", "min", "max", "count"},
|
||||||
|
expectedValue: nil,
|
||||||
|
expectedResponse: 0,
|
||||||
|
expectedError: errors.New(utils.WRONG_ARGS_RESPONSE),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range tests {
|
||||||
|
if test.preset {
|
||||||
|
if _, err := mockServer.CreateKeyAndLock(context.Background(), test.key); err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
mockServer.SetValue(context.Background(), test.key, test.presetValue)
|
||||||
|
mockServer.KeyUnlock(test.key)
|
||||||
|
}
|
||||||
|
res, err := handleZCOUNT(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.NewReader(res))
|
||||||
|
rv, _, err := rd.ReadValue()
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
if rv.Integer() != test.expectedResponse {
|
||||||
|
t.Errorf("expected response %d at key \"%s\", got %d", test.expectedResponse, test.key, rv.Integer())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func Test_HandleZLEXCOUNT(t *testing.T) {}
|
func Test_HandleZLEXCOUNT(t *testing.T) {}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user