ledisdb/server/cmd_zset_test.go

803 lines
18 KiB
Go
Raw Normal View History

package server
2014-05-08 06:54:33 +04:00
import (
"fmt"
2014-10-01 19:26:46 +04:00
"reflect"
2014-05-08 06:54:33 +04:00
"strconv"
"testing"
2015-05-04 17:42:28 +03:00
"github.com/siddontang/goredis"
2014-05-08 06:54:33 +04:00
)
func TestZSet(t *testing.T) {
c := getTestConn()
defer c.Close()
key := []byte("myzset")
2015-02-01 12:54:52 +03:00
2015-03-11 06:54:02 +03:00
if n, err := goredis.Int(c.Do("zkeyexists", key)); err != nil {
2015-02-01 12:54:52 +03:00
t.Fatal(err)
} else if n != 0 {
t.Fatal(n)
}
2015-03-11 06:54:02 +03:00
if n, err := goredis.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)
}
2015-03-11 06:54:02 +03:00
if n, err := goredis.Int(c.Do("zkeyexists", key)); err != nil {
2015-02-01 12:54:52 +03:00
t.Fatal(err)
} else if n != 1 {
t.Fatal(n)
}
2015-03-11 06:54:02 +03:00
if n, err := goredis.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)
}
2015-03-11 06:54:02 +03:00
if n, err := goredis.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)
}
2015-03-11 06:54:02 +03:00
if n, err := goredis.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)
}
2015-03-11 06:54:02 +03:00
if n, err := goredis.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)
}
2015-03-11 06:54:02 +03:00
if n, err := goredis.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)
}
2015-03-11 06:54:02 +03:00
if s, err := goredis.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)
}
2015-03-11 06:54:02 +03:00
if n, err := goredis.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)
}
2015-03-11 06:54:02 +03:00
if n, err := goredis.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)
}
2015-03-11 06:54:02 +03:00
if n, err := goredis.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)
}
2015-03-11 06:54:02 +03:00
if n, err := goredis.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)
}
2015-03-11 06:54:02 +03:00
if n, err := goredis.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)
}
2015-03-11 06:54:02 +03:00
if n, err := goredis.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)
}
2015-03-11 06:54:02 +03:00
if n, err := goredis.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)
}
2015-03-11 06:54:02 +03:00
if n, err := goredis.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")
2015-03-11 06:54:02 +03:00
if _, err := goredis.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)
}
2015-03-11 06:54:02 +03:00
if n, err := goredis.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)
}
2015-03-11 06:54:02 +03:00
if n, err := goredis.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)
}
2015-03-11 06:54:02 +03:00
if n, err := goredis.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)
}
2015-03-11 06:54:02 +03:00
if n, err := goredis.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)
}
2015-03-11 06:54:02 +03:00
if n, err := goredis.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)
}
2015-03-11 06:54:02 +03:00
if n, err := goredis.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)
}
2015-03-11 06:54:02 +03:00
if n, err := goredis.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")
2015-03-11 06:54:02 +03:00
if n, err := goredis.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")
2015-03-11 06:54:02 +03:00
if _, err := goredis.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)
}
2015-03-11 06:54:02 +03:00
if n, err := goredis.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)
}
2015-03-11 06:54:02 +03:00
if _, err := goredis.Int(c.Do("zrank", key, "e")); err != goredis.ErrNil {
2014-05-08 06:54:33 +04:00
t.Fatal(err)
}
2015-03-11 06:54:02 +03:00
if n, err := goredis.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)
}
2015-03-11 06:54:02 +03:00
if _, err := goredis.Int(c.Do("zrevrank", key, "e")); err != goredis.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")
2015-03-11 06:54:02 +03:00
if _, err := goredis.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)
}
2015-03-11 06:54:02 +03:00
if v, err := goredis.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)
}
}
2015-03-11 06:54:02 +03:00
if v, err := goredis.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)
}
}
2015-03-11 06:54:02 +03:00
if v, err := goredis.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)
}
}
2015-03-11 06:54:02 +03:00
if v, err := goredis.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)
}
}
2015-03-11 06:54:02 +03:00
if v, err := goredis.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)
}
}
2015-03-11 06:54:02 +03:00
if v, err := goredis.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)
}
}
2015-03-11 06:54:02 +03:00
if v, err := goredis.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)
}
}
2015-03-11 06:54:02 +03:00
if v, err := goredis.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)
}
}
2015-03-11 06:54:02 +03:00
if n, err := goredis.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)
}
2015-03-11 06:54:02 +03:00
if n, err := goredis.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)
}
2015-03-11 06:54:02 +03:00
if v, err := goredis.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")
2015-03-11 06:54:02 +03:00
if _, err := goredis.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)
}
2015-03-11 06:54:02 +03:00
if v, err := goredis.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)
}
}
2015-03-11 06:54:02 +03:00
if v, err := goredis.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)
}
}
2015-03-11 06:54:02 +03:00
if v, err := goredis.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)
}
}
2015-03-11 06:54:02 +03:00
if v, err := goredis.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)
}
}
2015-03-11 06:54:02 +03:00
if v, err := goredis.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))
}
2015-03-11 06:54:02 +03:00
if v, err := goredis.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)
}
}
2015-03-11 06:54:02 +03:00
if v, err := goredis.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)
}
}
2015-03-11 06:54:02 +03:00
if v, err := goredis.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)
}
}
2015-03-11 06:54:02 +03:00
if v, err := goredis.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)
}
}
2015-03-11 06:54:02 +03:00
if n, err := goredis.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)
}
2015-03-11 06:54:02 +03:00
if n, err := goredis.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)
}
2015-03-11 06:54:02 +03:00
if v, err := goredis.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
2015-03-11 06:54:02 +03:00
if n, err := goredis.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)
}
2015-03-11 06:54:02 +03:00
if n, err := goredis.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 {
2018-03-29 15:33:36 +03:00
t.Fatalf("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 {
2018-03-29 15:33:36 +03:00
t.Fatalf("invalid err of %v", err)
}
2014-07-29 16:08:20 +04:00
if _, err := c.Do("zadd", "test_zadd", "-a", "a"); err == nil {
2018-03-29 15:33:36 +03:00
t.Fatalf("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 {
2018-03-29 15:33:36 +03:00
t.Fatalf("invalid err of %v", err)
}
//zcard
2014-07-29 16:08:20 +04:00
if _, err := c.Do("zcard"); err == nil {
2018-03-29 15:33:36 +03:00
t.Fatalf("invalid err of %v", err)
}
//zscore
2014-07-29 16:08:20 +04:00
if _, err := c.Do("zscore", "test_zscore"); err == nil {
2018-03-29 15:33:36 +03:00
t.Fatalf("invalid err of %v", err)
}
//zrem
2014-07-29 16:08:20 +04:00
if _, err := c.Do("zrem", "test_zrem"); err == nil {
2018-03-29 15:33:36 +03:00
t.Fatalf("invalid err of %v", err)
}
//zincrby
2014-07-29 16:08:20 +04:00
if _, err := c.Do("zincrby", "test_zincrby"); err == nil {
2018-03-29 15:33:36 +03:00
t.Fatalf("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 {
2018-03-29 15:33:36 +03:00
t.Fatalf("invalid err of %v", err)
}
//zcount
2014-07-29 16:08:20 +04:00
if _, err := c.Do("zcount", "test_zcount"); err == nil {
2018-03-29 15:33:36 +03:00
t.Fatalf("invalid err of %v", err)
}
2014-07-29 16:08:20 +04:00
if _, err := c.Do("zcount", "test_zcount", "-inf", "=inf"); err == nil {
2018-03-29 15:33:36 +03:00
t.Fatalf("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 {
2018-03-29 15:33:36 +03:00
t.Fatalf("invalid err of %v", err)
}
//zrank
2014-07-29 16:08:20 +04:00
if _, err := c.Do("zrank", "test_zrank"); err == nil {
2018-03-29 15:33:36 +03:00
t.Fatalf("invalid err of %v", err)
}
//zrevzrank
2014-07-29 16:08:20 +04:00
if _, err := c.Do("zrevrank", "test_zrevrank"); err == nil {
2018-03-29 15:33:36 +03:00
t.Fatalf("invalid err of %v", err)
}
//zremrangebyrank
2014-07-29 16:08:20 +04:00
if _, err := c.Do("zremrangebyrank", "test_zremrangebyrank"); err == nil {
2018-03-29 15:33:36 +03:00
t.Fatalf("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 {
2018-03-29 15:33:36 +03:00
t.Fatalf("invalid err of %v", err)
}
//zremrangebyscore
2014-07-29 16:08:20 +04:00
if _, err := c.Do("zremrangebyscore", "test_zremrangebyscore"); err == nil {
2018-03-29 15:33:36 +03:00
t.Fatalf("invalid err of %v", err)
}
2014-07-29 16:08:20 +04:00
if _, err := c.Do("zremrangebyscore", "test_zremrangebyscore", "-inf", "a"); err == nil {
2018-03-29 15:33:36 +03:00
t.Fatalf("invalid err of %v", err)
}
2014-07-29 16:08:20 +04:00
if _, err := c.Do("zremrangebyscore", "test_zremrangebyscore", 0, "a"); err == nil {
2018-03-29 15:33:36 +03:00
t.Fatalf("invalid err of %v", err)
}
//zrange
2014-07-29 16:08:20 +04:00
if _, err := c.Do("zrange", "test_zrange"); err == nil {
2018-03-29 15:33:36 +03:00
t.Fatalf("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 {
2018-03-29 15:33:36 +03:00
t.Fatalf("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 {
2018-03-29 15:33:36 +03:00
t.Fatalf("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 {
2018-03-29 15:33:36 +03:00
t.Fatalf("invalid err of %v", err)
}
//zrangebyscore
2014-07-29 16:08:20 +04:00
if _, err := c.Do("zrangebyscore", "test_zrangebyscore"); err == nil {
2018-03-29 15:33:36 +03:00
t.Fatalf("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 {
2018-03-29 15:33:36 +03:00
t.Fatalf("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 {
2018-03-29 15:33:36 +03:00
t.Fatalf("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 {
2018-03-29 15:33:36 +03:00
t.Fatalf("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 {
2018-03-29 15:33:36 +03:00
t.Fatalf("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 {
2018-03-29 15:33:36 +03:00
t.Fatalf("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 {
2018-03-29 15:33:36 +03:00
t.Fatalf("invalid err of %v", err)
}
//zclear
2014-07-29 16:08:20 +04:00
if _, err := c.Do("zclear"); err == nil {
2018-03-29 15:33:36 +03:00
t.Fatalf("invalid err of %v", err)
}
//zmclear
2014-07-29 16:08:20 +04:00
if _, err := c.Do("zmclear"); err == nil {
2018-03-29 15:33:36 +03:00
t.Fatalf("invalid err of %v", err)
}
//zexpire
2014-07-29 16:08:20 +04:00
if _, err := c.Do("zexpire", "test_zexpire"); err == nil {
2018-03-29 15:33:36 +03:00
t.Fatalf("invalid err of %v", err)
}
//zexpireat
2014-07-29 16:08:20 +04:00
if _, err := c.Do("zexpireat", "test_zexpireat"); err == nil {
2018-03-29 15:33:36 +03:00
t.Fatalf("invalid err of %v", err)
}
//zttl
2014-07-29 16:08:20 +04:00
if _, err := c.Do("zttl"); err == nil {
2018-03-29 15:33:36 +03:00
t.Fatalf("invalid err of %v", err)
}
//zpersist
2014-07-29 16:08:20 +04:00
if _, err := c.Do("zpersist"); err == nil {
2018-03-29 15:33:36 +03:00
t.Fatalf("invalid err of %v", err)
}
}
func TestZUnionStore(t *testing.T) {
c := getTestConn()
defer c.Close()
if _, err := c.Do("zadd", "k1", "1", "one"); err != nil {
t.Fatal(err.Error())
}
if _, err := c.Do("zadd", "k1", "2", "two"); err != nil {
t.Fatal(err.Error())
}
if _, err := c.Do("zadd", "k2", "1", "two"); err != nil {
t.Fatal(err.Error())
}
if _, err := c.Do("zadd", "k2", "2", "three"); err != nil {
t.Fatal(err.Error())
}
2015-03-11 06:54:02 +03:00
if n, err := goredis.Int64(c.Do("zunionstore", "out", "2", "k1", "k2", "weights", "1", "2")); err != nil {
t.Fatal(err.Error())
} else {
if n != 3 {
t.Fatal("invalid value ", n)
}
}
2015-03-11 06:54:02 +03:00
if n, err := goredis.Int64(c.Do("zunionstore", "out", "2", "k1", "k2", "weights", "1", "2", "aggregate", "min")); err != nil {
t.Fatal(err.Error())
} else {
if n != 3 {
t.Fatal("invalid value ", n)
}
}
2015-03-11 06:54:02 +03:00
if n, err := goredis.Int64(c.Do("zunionstore", "out", "2", "k1", "k2", "aggregate", "max")); err != nil {
t.Fatal(err.Error())
} else {
if n != 3 {
t.Fatal("invalid value ", n)
}
}
2014-08-13 10:23:27 +04:00
2015-03-11 06:54:02 +03:00
if n, err := goredis.Int64(c.Do("zscore", "out", "two")); err != nil {
2014-08-13 10:23:27 +04:00
t.Fatal(err.Error())
} else {
if n != 2 {
t.Fatal("invalid value ", n)
}
}
}
func TestZInterStore(t *testing.T) {
c := getTestConn()
defer c.Close()
if _, err := c.Do("zadd", "k1", "1", "one"); err != nil {
t.Fatal(err.Error())
}
if _, err := c.Do("zadd", "k1", "2", "two"); err != nil {
t.Fatal(err.Error())
}
if _, err := c.Do("zadd", "k2", "1", "two"); err != nil {
t.Fatal(err.Error())
}
if _, err := c.Do("zadd", "k2", "2", "three"); err != nil {
t.Fatal(err.Error())
}
2015-03-11 06:54:02 +03:00
if n, err := goredis.Int64(c.Do("zinterstore", "out", "2", "k1", "k2", "weights", "1", "2")); err != nil {
2014-08-13 10:23:27 +04:00
t.Fatal(err.Error())
} else {
if n != 1 {
t.Fatal("invalid value ", n)
}
}
2015-03-11 06:54:02 +03:00
if n, err := goredis.Int64(c.Do("zinterstore", "out", "2", "k1", "k2", "aggregate", "min", "weights", "1", "2")); err != nil {
2014-08-13 10:23:27 +04:00
t.Fatal(err.Error())
} else {
if n != 1 {
t.Fatal("invalid value ", n)
}
}
2015-03-11 06:54:02 +03:00
if n, err := goredis.Int64(c.Do("zinterstore", "out", "2", "k1", "k2", "aggregate", "sum")); err != nil {
2014-08-13 10:23:27 +04:00
t.Fatal(err.Error())
} else {
if n != 1 {
t.Fatal("invalid value ", n)
}
}
2015-03-11 06:54:02 +03:00
if n, err := goredis.Int64(c.Do("zscore", "out", "two")); err != nil {
2014-08-13 10:23:27 +04:00
t.Fatal(err.Error())
} else {
if n != 3 {
t.Fatal("invalid value ", n)
}
}
2014-08-14 12:10:07 +04:00
if _, err := c.Do("zadd", "k3", "3", "three"); err != nil {
t.Fatal(err.Error())
}
2015-03-11 06:54:02 +03:00
if n, err := goredis.Int64(c.Do("zinterstore", "out", "3", "k1", "k2", "k3", "aggregate", "sum")); err != nil {
2014-08-14 12:10:07 +04:00
t.Fatal(err.Error())
} else {
if n != 0 {
t.Fatal("invalid value ", n)
}
}
if _, err := c.Do("zadd", "k3", "3", "two"); err != nil {
t.Fatal(err.Error())
}
2015-03-11 06:54:02 +03:00
if n, err := goredis.Int64(c.Do("zinterstore", "out", "3", "k1", "k2", "k3", "aggregate", "sum", "weights", "3", "2", "2")); err != nil {
2014-08-14 12:10:07 +04:00
t.Fatal(err.Error())
} else {
if n != 1 {
t.Fatal("invalid value ", n)
}
}
2015-03-11 06:54:02 +03:00
if n, err := goredis.Int64(c.Do("zscore", "out", "two")); err != nil {
2014-08-14 12:10:07 +04:00
t.Fatal(err.Error())
} else {
if n != 14 {
t.Fatal("invalid value ", n)
}
}
}
2014-10-01 19:26:46 +04:00
func TestZSetLex(t *testing.T) {
c := getTestConn()
defer c.Close()
key := []byte("myzlexset")
if _, err := c.Do("zadd", key,
0, "a", 0, "b", 0, "c", 0, "d", 0, "e", 0, "f", 0, "g"); err != nil {
t.Fatal(err)
}
2015-03-11 06:54:02 +03:00
if ay, err := goredis.Strings(c.Do("zrangebylex", key, "-", "[c")); err != nil {
2014-10-01 19:26:46 +04:00
t.Fatal(err)
} else if !reflect.DeepEqual(ay, []string{"a", "b", "c"}) {
t.Fatal("must equal")
}
2015-03-11 06:54:02 +03:00
if ay, err := goredis.Strings(c.Do("zrangebylex", key, "-", "(c")); err != nil {
2014-10-01 19:26:46 +04:00
t.Fatal(err)
} else if !reflect.DeepEqual(ay, []string{"a", "b"}) {
t.Fatal("must equal")
}
2015-03-11 06:54:02 +03:00
if ay, err := goredis.Strings(c.Do("zrangebylex", key, "[aaa", "(g")); err != nil {
2014-10-01 19:26:46 +04:00
t.Fatal(err)
} else if !reflect.DeepEqual(ay, []string{"b", "c", "d", "e", "f"}) {
t.Fatal("must equal")
}
2015-03-11 06:54:02 +03:00
if n, err := goredis.Int64(c.Do("zlexcount", key, "-", "(c")); err != nil {
2014-10-01 19:26:46 +04:00
t.Fatal(err)
} else if n != 2 {
t.Fatal(n)
}
2015-03-11 06:54:02 +03:00
if n, err := goredis.Int64(c.Do("zremrangebylex", key, "[aaa", "(g")); err != nil {
2014-10-01 19:26:46 +04:00
t.Fatal(err)
} else if n != 5 {
t.Fatal(n)
}
2015-03-11 06:54:02 +03:00
if n, err := goredis.Int64(c.Do("zlexcount", key, "-", "+")); err != nil {
2014-10-01 19:26:46 +04:00
t.Fatal(err)
} else if n != 2 {
t.Fatal(n)
}
}