ledisdb/server/cmd_zset_test.go

602 lines
13 KiB
Go
Raw Normal View History

package server
2014-05-08 06:54:33 +04:00
import (
"fmt"
2014-06-22 06:39:23 +04:00
"github.com/siddontang/ledisdb/client/go/ledis"
2014-05-08 06:54:33 +04:00
"strconv"
"testing"
)
func TestZSet(t *testing.T) {
c := getTestConn()
defer c.Close()
key := []byte("myzset")
2014-06-22 06:39:23 +04:00
if n, err := ledis.Int(c.Do("zadd", key, 3, "a", 4, "b")); err != nil {
2014-05-08 06:54:33 +04:00
t.Fatal(err)
} else if n != 2 {
t.Fatal(n)
}
2014-06-22 06:39:23 +04:00
if n, err := ledis.Int(c.Do("zcard", key)); err != nil {
2014-05-08 06:54:33 +04:00
t.Fatal(n)
} else if n != 2 {
t.Fatal(n)
}
2014-06-22 06:39:23 +04:00
if n, err := ledis.Int(c.Do("zadd", key, 1, "a", 2, "b")); err != nil {
2014-05-08 06:54:33 +04:00
t.Fatal(err)
} else if n != 0 {
t.Fatal(n)
}
2014-06-22 06:39:23 +04:00
if n, err := ledis.Int(c.Do("zcard", key)); err != nil {
2014-05-08 06:54:33 +04:00
t.Fatal(n)
} else if n != 2 {
t.Fatal(n)
}
2014-06-22 06:39:23 +04:00
if n, err := ledis.Int(c.Do("zadd", key, 3, "c", 4, "d")); err != nil {
2014-05-08 06:54:33 +04:00
t.Fatal(err)
} else if n != 2 {
t.Fatal(n)
}
2014-06-22 06:39:23 +04:00
if n, err := ledis.Int(c.Do("zcard", key)); err != nil {
2014-05-08 06:54:33 +04:00
t.Fatal(err)
} else if n != 4 {
t.Fatal(n)
}
2014-06-22 06:39:23 +04:00
if s, err := ledis.Int(c.Do("zscore", key, "c")); err != nil {
2014-05-08 06:54:33 +04:00
t.Fatal(err)
} else if s != 3 {
t.Fatal(s)
}
2014-06-22 06:39:23 +04:00
if n, err := ledis.Int(c.Do("zrem", key, "d", "e")); err != nil {
2014-05-08 06:54:33 +04:00
t.Fatal(err)
} else if n != 1 {
t.Fatal(n)
}
2014-06-22 06:39:23 +04:00
if n, err := ledis.Int(c.Do("zcard", key)); err != nil {
2014-05-08 06:54:33 +04:00
t.Fatal(err)
} else if n != 3 {
t.Fatal(n)
}
2014-06-22 06:39:23 +04:00
if n, err := ledis.Int(c.Do("zincrby", key, 4, "c")); err != nil {
2014-05-08 06:54:33 +04:00
t.Fatal(err)
} else if n != 7 {
t.Fatal(n)
}
2014-06-22 06:39:23 +04:00
if n, err := ledis.Int(c.Do("zincrby", key, -4, "c")); err != nil {
2014-05-08 06:54:33 +04:00
t.Fatal(err)
} else if n != 3 {
t.Fatal(n)
}
2014-06-22 06:39:23 +04:00
if n, err := ledis.Int(c.Do("zincrby", key, 4, "d")); err != nil {
2014-05-08 06:54:33 +04:00
t.Fatal(err)
} else if n != 4 {
t.Fatal(n)
}
2014-06-22 06:39:23 +04:00
if n, err := ledis.Int(c.Do("zcard", key)); err != nil {
2014-05-08 06:54:33 +04:00
t.Fatal(err)
} else if n != 4 {
t.Fatal(n)
}
2014-06-22 06:39:23 +04:00
if n, err := ledis.Int(c.Do("zrem", key, "a", "b", "c", "d")); err != nil {
2014-05-08 06:54:33 +04:00
t.Fatal(err)
} else if n != 4 {
t.Fatal(n)
}
2014-06-22 06:39:23 +04:00
if n, err := ledis.Int(c.Do("zcard", key)); err != nil {
2014-05-08 06:54:33 +04:00
t.Fatal(err)
} else if n != 0 {
t.Fatal(n)
}
}
func TestZSetCount(t *testing.T) {
c := getTestConn()
defer c.Close()
key := []byte("myzset")
2014-06-22 06:39:23 +04:00
if _, err := ledis.Int(c.Do("zadd", key, 1, "a", 2, "b", 3, "c", 4, "d")); err != nil {
2014-05-08 06:54:33 +04:00
t.Fatal(err)
}
2014-06-22 06:39:23 +04:00
if n, err := ledis.Int(c.Do("zcount", key, 2, 4)); err != nil {
2014-05-08 06:54:33 +04:00
t.Fatal(err)
} else if n != 3 {
t.Fatal(n)
}
2014-06-22 06:39:23 +04:00
if n, err := ledis.Int(c.Do("zcount", key, 4, 4)); err != nil {
2014-05-08 06:54:33 +04:00
t.Fatal(err)
} else if n != 1 {
t.Fatal(n)
}
2014-06-22 06:39:23 +04:00
if n, err := ledis.Int(c.Do("zcount", key, 4, 3)); err != nil {
2014-05-08 06:54:33 +04:00
t.Fatal(err)
} else if n != 0 {
t.Fatal(n)
}
2014-06-22 06:39:23 +04:00
if n, err := ledis.Int(c.Do("zcount", key, "(2", 4)); err != nil {
2014-05-08 06:54:33 +04:00
t.Fatal(err)
} else if n != 2 {
t.Fatal(n)
}
2014-06-22 06:39:23 +04:00
if n, err := ledis.Int(c.Do("zcount", key, "2", "(4")); err != nil {
2014-05-08 06:54:33 +04:00
t.Fatal(err)
} else if n != 2 {
t.Fatal(n)
}
2014-06-22 06:39:23 +04:00
if n, err := ledis.Int(c.Do("zcount", key, "(2", "(4")); err != nil {
2014-05-08 06:54:33 +04:00
t.Fatal(err)
} else if n != 1 {
t.Fatal(n)
}
2014-06-22 06:39:23 +04:00
if n, err := ledis.Int(c.Do("zcount", key, "-inf", "+inf")); err != nil {
2014-05-08 06:54:33 +04:00
t.Fatal(err)
} else if n != 4 {
t.Fatal(n)
}
c.Do("zadd", key, 3, "e")
2014-06-22 06:39:23 +04:00
if n, err := ledis.Int(c.Do("zcount", key, "(2", "(4")); err != nil {
2014-05-08 06:54:33 +04:00
t.Fatal(err)
} else if n != 2 {
t.Fatal(n)
}
c.Do("zrem", key, "a", "b", "c", "d", "e")
}
func TestZSetRank(t *testing.T) {
c := getTestConn()
defer c.Close()
key := []byte("myzset")
2014-06-22 06:39:23 +04:00
if _, err := ledis.Int(c.Do("zadd", key, 1, "a", 2, "b", 3, "c", 4, "d")); err != nil {
2014-05-08 06:54:33 +04:00
t.Fatal(err)
}
2014-06-22 06:39:23 +04:00
if n, err := ledis.Int(c.Do("zrank", key, "c")); err != nil {
2014-05-08 06:54:33 +04:00
t.Fatal(err)
} else if n != 2 {
t.Fatal(n)
}
2014-06-22 06:39:23 +04:00
if _, err := ledis.Int(c.Do("zrank", key, "e")); err != ledis.ErrNil {
2014-05-08 06:54:33 +04:00
t.Fatal(err)
}
2014-06-22 06:39:23 +04:00
if n, err := ledis.Int(c.Do("zrevrank", key, "c")); err != nil {
2014-05-08 06:54:33 +04:00
t.Fatal(err)
} else if n != 1 {
t.Fatal(n)
}
2014-06-22 06:39:23 +04:00
if _, err := ledis.Int(c.Do("zrevrank", key, "e")); err != ledis.ErrNil {
2014-05-08 06:54:33 +04:00
t.Fatal(err)
}
}
func testZSetRange(ay []interface{}, checkValues ...interface{}) error {
if len(ay) != len(checkValues) {
return fmt.Errorf("invalid return number %d != %d", len(ay), len(checkValues))
}
for i := 0; i < len(ay); i++ {
v, ok := ay[i].([]byte)
if !ok {
return fmt.Errorf("invalid data %d %v %T", i, ay[i], ay[i])
}
switch cv := checkValues[i].(type) {
case string:
if string(v) != cv {
return fmt.Errorf("not equal %s != %s", v, checkValues[i])
}
default:
if s, _ := strconv.Atoi(string(v)); s != checkValues[i] {
return fmt.Errorf("not equal %s != %v", v, checkValues[i])
}
}
}
return nil
}
func TestZSetRangeScore(t *testing.T) {
c := getTestConn()
defer c.Close()
key := []byte("myzset_range")
2014-06-22 06:39:23 +04:00
if _, err := ledis.Int(c.Do("zadd", key, 1, "a", 2, "b", 3, "c", 4, "d")); err != nil {
2014-05-08 06:54:33 +04:00
t.Fatal(err)
}
2014-06-22 06:39:23 +04:00
if v, err := ledis.MultiBulk(c.Do("zrangebyscore", key, 1, 4, "withscores")); err != nil {
2014-05-08 06:54:33 +04:00
t.Fatal(err)
} else {
if err := testZSetRange(v, "a", 1, "b", 2, "c", 3, "d", 4); err != nil {
t.Fatal(err)
}
}
2014-06-22 06:39:23 +04:00
if v, err := ledis.MultiBulk(c.Do("zrangebyscore", key, 1, 4, "withscores", "limit", 1, 2)); err != nil {
2014-05-08 06:54:33 +04:00
t.Fatal(err)
} else {
if err := testZSetRange(v, "b", 2, "c", 3); err != nil {
t.Fatal(err)
}
}
2014-06-22 06:39:23 +04:00
if v, err := ledis.MultiBulk(c.Do("zrangebyscore", key, "-inf", "+inf", "withscores")); err != nil {
2014-05-08 06:54:33 +04:00
t.Fatal(err)
} else {
if err := testZSetRange(v, "a", 1, "b", 2, "c", 3, "d", 4); err != nil {
t.Fatal(err)
}
}
2014-06-22 06:39:23 +04:00
if v, err := ledis.MultiBulk(c.Do("zrangebyscore", key, "(1", "(4")); err != nil {
2014-05-08 06:54:33 +04:00
t.Fatal(err)
} else {
if err := testZSetRange(v, "b", "c"); err != nil {
t.Fatal(err)
}
}
if v, err := ledis.MultiBulk(c.Do("zrevrangebyscore", key, 4, 1, "withscores")); err != nil {
2014-05-08 06:54:33 +04:00
t.Fatal(err)
} else {
if err := testZSetRange(v, "d", 4, "c", 3, "b", 2, "a", 1); err != nil {
t.Fatal(err)
}
}
if v, err := ledis.MultiBulk(c.Do("zrevrangebyscore", key, 4, 1, "withscores", "limit", 1, 2)); err != nil {
2014-05-08 06:54:33 +04:00
t.Fatal(err)
} else {
if err := testZSetRange(v, "c", 3, "b", 2); err != nil {
t.Fatal(err)
}
}
if v, err := ledis.MultiBulk(c.Do("zrevrangebyscore", key, "+inf", "-inf", "withscores")); err != nil {
2014-05-08 06:54:33 +04:00
t.Fatal(err)
} else {
if err := testZSetRange(v, "d", 4, "c", 3, "b", 2, "a", 1); err != nil {
t.Fatal(err)
}
}
if v, err := ledis.MultiBulk(c.Do("zrevrangebyscore", key, "(4", "(1")); err != nil {
2014-05-08 06:54:33 +04:00
t.Fatal(err)
} else {
if err := testZSetRange(v, "c", "b"); err != nil {
t.Fatal(err)
}
}
2014-06-22 06:39:23 +04:00
if n, err := ledis.Int(c.Do("zremrangebyscore", key, 2, 3)); err != nil {
2014-05-08 06:54:33 +04:00
t.Fatal(err)
} else if n != 2 {
t.Fatal(n)
}
2014-06-22 06:39:23 +04:00
if n, err := ledis.Int(c.Do("zcard", key)); err != nil {
2014-05-08 06:54:33 +04:00
t.Fatal(err)
} else if n != 2 {
t.Fatal(n)
}
2014-06-22 06:39:23 +04:00
if v, err := ledis.MultiBulk(c.Do("zrangebyscore", key, 1, 4)); err != nil {
2014-05-08 06:54:33 +04:00
t.Fatal(err)
} else {
if err := testZSetRange(v, "a", "d"); err != nil {
t.Fatal(err)
}
}
}
func TestZSetRange(t *testing.T) {
c := getTestConn()
defer c.Close()
key := []byte("myzset_range_rank")
2014-06-22 06:39:23 +04:00
if _, err := ledis.Int(c.Do("zadd", key, 1, "a", 2, "b", 3, "c", 4, "d")); err != nil {
2014-05-08 06:54:33 +04:00
t.Fatal(err)
}
2014-06-22 06:39:23 +04:00
if v, err := ledis.MultiBulk(c.Do("zrange", key, 0, 3, "withscores")); err != nil {
2014-05-08 06:54:33 +04:00
t.Fatal(err)
} else {
if err := testZSetRange(v, "a", 1, "b", 2, "c", 3, "d", 4); err != nil {
t.Fatal(err)
}
}
2014-06-22 06:39:23 +04:00
if v, err := ledis.MultiBulk(c.Do("zrange", key, 1, 4, "withscores")); err != nil {
2014-05-08 06:54:33 +04:00
t.Fatal(err)
} else {
if err := testZSetRange(v, "b", 2, "c", 3, "d", 4); err != nil {
t.Fatal(err)
}
}
2014-06-22 06:39:23 +04:00
if v, err := ledis.MultiBulk(c.Do("zrange", key, -2, -1, "withscores")); err != nil {
2014-05-08 06:54:33 +04:00
t.Fatal(err)
} else {
if err := testZSetRange(v, "c", 3, "d", 4); err != nil {
t.Fatal(err)
}
}
2014-06-22 06:39:23 +04:00
if v, err := ledis.MultiBulk(c.Do("zrange", key, 0, -1, "withscores")); err != nil {
2014-05-08 06:54:33 +04:00
t.Fatal(err)
} else {
if err := testZSetRange(v, "a", 1, "b", 2, "c", 3, "d", 4); err != nil {
t.Fatal(err)
}
}
2014-06-22 06:39:23 +04:00
if v, err := ledis.MultiBulk(c.Do("zrange", key, -1, -2, "withscores")); err != nil {
2014-05-08 06:54:33 +04:00
t.Fatal(err)
} else if len(v) != 0 {
t.Fatal(len(v))
}
2014-06-22 06:39:23 +04:00
if v, err := ledis.MultiBulk(c.Do("zrevrange", key, 0, 4, "withscores")); err != nil {
2014-05-08 06:54:33 +04:00
t.Fatal(err)
} else {
if err := testZSetRange(v, "d", 4, "c", 3, "b", 2, "a", 1); err != nil {
t.Fatal(err)
}
}
2014-06-22 06:39:23 +04:00
if v, err := ledis.MultiBulk(c.Do("zrevrange", key, 0, -1, "withscores")); err != nil {
2014-05-08 06:54:33 +04:00
t.Fatal(err)
} else {
if err := testZSetRange(v, "d", 4, "c", 3, "b", 2, "a", 1); err != nil {
t.Fatal(err)
}
}
2014-06-22 06:39:23 +04:00
if v, err := ledis.MultiBulk(c.Do("zrevrange", key, 2, 3, "withscores")); err != nil {
2014-05-08 06:54:33 +04:00
t.Fatal(err)
} else {
if err := testZSetRange(v, "b", 2, "a", 1); err != nil {
t.Fatal(err)
}
}
2014-06-22 06:39:23 +04:00
if v, err := ledis.MultiBulk(c.Do("zrevrange", key, -2, -1, "withscores")); err != nil {
2014-05-08 06:54:33 +04:00
t.Fatal(err)
} else {
if err := testZSetRange(v, "b", 2, "a", 1); err != nil {
t.Fatal(err)
}
}
2014-06-22 06:39:23 +04:00
if n, err := ledis.Int(c.Do("zremrangebyrank", key, 2, 3)); err != nil {
2014-05-08 06:54:33 +04:00
t.Fatal(err)
} else if n != 2 {
t.Fatal(n)
}
2014-06-22 06:39:23 +04:00
if n, err := ledis.Int(c.Do("zcard", key)); err != nil {
2014-05-08 06:54:33 +04:00
t.Fatal(err)
} else if n != 2 {
t.Fatal(n)
}
2014-06-22 06:39:23 +04:00
if v, err := ledis.MultiBulk(c.Do("zrange", key, 0, 4)); err != nil {
2014-05-08 06:54:33 +04:00
t.Fatal(err)
} else {
if err := testZSetRange(v, "a", "b"); err != nil {
t.Fatal(err)
}
}
2014-05-12 11:08:59 +04:00
2014-06-22 06:39:23 +04:00
if n, err := ledis.Int(c.Do("zclear", key)); err != nil {
2014-05-12 11:08:59 +04:00
t.Fatal(err)
} else if n != 2 {
t.Fatal(n)
}
2014-06-22 06:39:23 +04:00
if n, err := ledis.Int(c.Do("zcard", key)); err != nil {
2014-05-12 11:08:59 +04:00
t.Fatal(err)
} else if n != 0 {
t.Fatal(n)
}
2014-05-08 06:54:33 +04:00
}
func TestZsetErrorParams(t *testing.T) {
c := getTestConn()
defer c.Close()
//zadd
2014-07-29 16:08:20 +04:00
if _, err := c.Do("zadd", "test_zadd"); err == nil {
t.Fatal("invalid err of %v", err)
}
2014-07-29 16:08:20 +04:00
if _, err := c.Do("zadd", "test_zadd", "a", "b", "c"); err == nil {
t.Fatal("invalid err of %v", err)
}
2014-07-29 16:08:20 +04:00
if _, err := c.Do("zadd", "test_zadd", "-a", "a"); err == nil {
t.Fatal("invalid err of %v", err)
}
2014-07-29 16:08:20 +04:00
if _, err := c.Do("zadd", "test_zad", "0.1", "a"); err == nil {
t.Fatal("invalid err of %v", err)
}
//zcard
2014-07-29 16:08:20 +04:00
if _, err := c.Do("zcard"); err == nil {
t.Fatal("invalid err of %v", err)
}
//zscore
2014-07-29 16:08:20 +04:00
if _, err := c.Do("zscore", "test_zscore"); err == nil {
t.Fatal("invalid err of %v", err)
}
//zrem
2014-07-29 16:08:20 +04:00
if _, err := c.Do("zrem", "test_zrem"); err == nil {
t.Fatal("invalid err of %v", err)
}
//zincrby
2014-07-29 16:08:20 +04:00
if _, err := c.Do("zincrby", "test_zincrby"); err == nil {
t.Fatal("invalid err of %v", err)
}
2014-07-29 16:08:20 +04:00
if _, err := c.Do("zincrby", "test_zincrby", 0.1, "a"); err == nil {
t.Fatal("invalid err of %v", err)
}
//zcount
2014-07-29 16:08:20 +04:00
if _, err := c.Do("zcount", "test_zcount"); err == nil {
t.Fatal("invalid err of %v", err)
}
2014-07-29 16:08:20 +04:00
if _, err := c.Do("zcount", "test_zcount", "-inf", "=inf"); err == nil {
t.Fatal("invalid err of %v", err)
}
2014-07-29 16:08:20 +04:00
if _, err := c.Do("zcount", "test_zcount", 0.1, 0.1); err == nil {
t.Fatal("invalid err of %v", err)
}
//zrank
2014-07-29 16:08:20 +04:00
if _, err := c.Do("zrank", "test_zrank"); err == nil {
t.Fatal("invalid err of %v", err)
}
//zrevzrank
2014-07-29 16:08:20 +04:00
if _, err := c.Do("zrevrank", "test_zrevrank"); err == nil {
t.Fatal("invalid err of %v", err)
}
//zremrangebyrank
2014-07-29 16:08:20 +04:00
if _, err := c.Do("zremrangebyrank", "test_zremrangebyrank"); err == nil {
t.Fatal("invalid err of %v", err)
}
2014-07-29 16:08:20 +04:00
if _, err := c.Do("zremrangebyrank", "test_zremrangebyrank", 0.1, 0.1); err == nil {
t.Fatal("invalid err of %v", err)
}
//zremrangebyscore
2014-07-29 16:08:20 +04:00
if _, err := c.Do("zremrangebyscore", "test_zremrangebyscore"); err == nil {
t.Fatal("invalid err of %v", err)
}
2014-07-29 16:08:20 +04:00
if _, err := c.Do("zremrangebyscore", "test_zremrangebyscore", "-inf", "a"); err == nil {
t.Fatal("invalid err of %v", err)
}
2014-07-29 16:08:20 +04:00
if _, err := c.Do("zremrangebyscore", "test_zremrangebyscore", 0, "a"); err == nil {
t.Fatal("invalid err of %v", err)
}
//zrange
2014-07-29 16:08:20 +04:00
if _, err := c.Do("zrange", "test_zrange"); err == nil {
t.Fatal("invalid err of %v", err)
}
2014-07-29 16:08:20 +04:00
if _, err := c.Do("zrange", "test_zrange", 0, 1, "withscore"); err == nil {
t.Fatal("invalid err of %v", err)
}
2014-07-29 16:08:20 +04:00
if _, err := c.Do("zrange", "test_zrange", 0, 1, "withscores", "a"); err == nil {
t.Fatal("invalid err of %v", err)
}
//zrevrange, almost same as zrange
2014-07-29 16:08:20 +04:00
if _, err := c.Do("zrevrange", "test_zrevrange"); err == nil {
t.Fatal("invalid err of %v", err)
}
//zrangebyscore
2014-07-29 16:08:20 +04:00
if _, err := c.Do("zrangebyscore", "test_zrangebyscore"); err == nil {
t.Fatal("invalid err of %v", err)
}
2014-07-29 16:08:20 +04:00
if _, err := c.Do("zrangebyscore", "test_zrangebyscore", 0, 1, "withscore"); err == nil {
t.Fatal("invalid err of %v", err)
}
2014-07-29 16:08:20 +04:00
if _, err := c.Do("zrangebyscore", "test_zrangebyscore", 0, 1, "withscores", "limit"); err == nil {
t.Fatal("invalid err of %v", err)
}
2014-07-29 16:08:20 +04:00
if _, err := c.Do("zrangebyscore", "test_zrangebyscore", 0, 1, "withscores", "limi", 1, 1); err == nil {
t.Fatal("invalid err of %v", err)
}
2014-07-29 16:08:20 +04:00
if _, err := c.Do("zrangebyscore", "test_zrangebyscore", 0, 1, "withscores", "limit", "a", 1); err == nil {
t.Fatal("invalid err of %v", err)
}
2014-07-29 16:08:20 +04:00
if _, err := c.Do("zrangebyscore", "test_zrangebyscore", 0, 1, "withscores", "limit", 1, "a"); err == nil {
t.Fatal("invalid err of %v", err)
}
//zrevrangebyscore, almost same as zrangebyscore
2014-07-29 16:08:20 +04:00
if _, err := c.Do("zrevrangebyscore", "test_zrevrangebyscore"); err == nil {
t.Fatal("invalid err of %v", err)
}
//zclear
2014-07-29 16:08:20 +04:00
if _, err := c.Do("zclear"); err == nil {
t.Fatal("invalid err of %v", err)
}
//zmclear
2014-07-29 16:08:20 +04:00
if _, err := c.Do("zmclear"); err == nil {
t.Fatal("invalid err of %v", err)
}
//zexpire
2014-07-29 16:08:20 +04:00
if _, err := c.Do("zexpire", "test_zexpire"); err == nil {
t.Fatal("invalid err of %v", err)
}
//zexpireat
2014-07-29 16:08:20 +04:00
if _, err := c.Do("zexpireat", "test_zexpireat"); err == nil {
t.Fatal("invalid err of %v", err)
}
//zttl
2014-07-29 16:08:20 +04:00
if _, err := c.Do("zttl"); err == nil {
t.Fatal("invalid err of %v", err)
}
//zpersist
2014-07-29 16:08:20 +04:00
if _, err := c.Do("zpersist"); err == nil {
t.Fatal("invalid err of %v", err)
}
}