ledisdb/server/cmd_hash_test.go

312 lines
6.2 KiB
Go
Raw Permalink Normal View History

package server
2014-05-06 13:37:58 +04:00
import (
"fmt"
"strconv"
"testing"
2015-05-04 17:42:28 +03:00
"github.com/siddontang/goredis"
2014-05-06 13:37:58 +04:00
)
func TestHash(t *testing.T) {
c := getTestConn()
defer c.Close()
key := []byte("a")
2015-03-11 06:54:02 +03:00
if n, err := goredis.Int(c.Do("hkeyexists", key)); err != nil {
2015-02-01 12:19:46 +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("hset", key, 1, 0)); err != nil {
2014-05-06 13:37:58 +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("hkeyexists", key)); err != nil {
2015-02-01 12:19:46 +03:00
t.Fatal(err)
} else if n != 1 {
t.Fatal(n)
}
2014-05-06 13:37:58 +04:00
2015-03-11 06:54:02 +03:00
if n, err := goredis.Int(c.Do("hexists", key, 1)); err != nil {
2014-05-06 13:37:58 +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("hexists", key, -1)); err != nil {
2014-05-06 13:37:58 +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("hget", key, 1)); err != nil {
2014-05-06 13:37:58 +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("hset", key, 1, 1)); err != nil {
2014-05-06 13:37:58 +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("hget", key, 1)); err != nil {
2014-05-06 13:37:58 +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("hlen", key)); err != nil {
2014-05-06 13:37:58 +04:00
t.Fatal(err)
} else if n != 1 {
t.Fatal(n)
}
}
func testHashArray(ay []interface{}, checkValues ...int) 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++ {
if ay[i] == nil && checkValues[i] != 0 {
return fmt.Errorf("must nil")
} else if ay[i] != nil {
v, ok := ay[i].([]byte)
if !ok {
return fmt.Errorf("invalid return data %d %v :%T", i, ay[i], ay[i])
}
d, _ := strconv.Atoi(string(v))
if d != checkValues[i] {
return fmt.Errorf("invalid data %d %s != %d", i, v, checkValues[i])
}
}
}
return nil
}
func TestHashM(t *testing.T) {
c := getTestConn()
defer c.Close()
key := []byte("b")
2015-03-11 06:54:02 +03:00
if ok, err := goredis.String(c.Do("hmset", key, 1, 1, 2, 2, 3, 3)); err != nil {
2014-05-06 13:37:58 +04:00
t.Fatal(err)
} else if ok != OK {
t.Fatal(ok)
}
2015-03-11 06:54:02 +03:00
if n, err := goredis.Int(c.Do("hlen", key)); err != nil {
2014-05-06 13:37:58 +04:00
t.Fatal(err)
} else if n != 3 {
t.Fatal(n)
}
2015-03-11 06:54:02 +03:00
if v, err := goredis.MultiBulk(c.Do("hmget", key, 1, 2, 3, 4)); err != nil {
2014-05-06 13:37:58 +04:00
t.Fatal(err)
} else {
if err := testHashArray(v, 1, 2, 3, 0); err != nil {
t.Fatal(err)
}
}
2015-03-11 06:54:02 +03:00
if n, err := goredis.Int(c.Do("hdel", key, 1, 2, 3, 4)); err != nil {
2014-05-06 13:37:58 +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("hlen", key)); err != nil {
2014-05-06 13:37:58 +04:00
t.Fatal(err)
} else if n != 0 {
t.Fatal(n)
}
2015-03-11 06:54:02 +03:00
if v, err := goredis.MultiBulk(c.Do("hmget", key, 1, 2, 3, 4)); err != nil {
2014-05-06 13:37:58 +04:00
t.Fatal(err)
} else {
if err := testHashArray(v, 0, 0, 0, 0); err != nil {
t.Fatal(err)
}
}
2015-03-11 06:54:02 +03:00
if n, err := goredis.Int(c.Do("hlen", key)); err != nil {
2014-05-06 13:37:58 +04:00
t.Fatal(err)
} else if n != 0 {
t.Fatal(n)
}
}
func TestHashIncr(t *testing.T) {
c := getTestConn()
defer c.Close()
key := []byte("c")
2015-03-11 06:54:02 +03:00
if n, err := goredis.Int(c.Do("hincrby", key, 1, 1)); err != nil {
2014-05-06 13:37:58 +04:00
t.Fatal(err)
} else if n != 1 {
t.Fatal(err)
}
2015-03-11 06:54:02 +03:00
if n, err := goredis.Int(c.Do("hlen", key)); err != nil {
2014-05-06 13:37:58 +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("hincrby", key, 1, 10)); err != nil {
2014-05-06 13:37:58 +04:00
t.Fatal(err)
} else if n != 11 {
t.Fatal(err)
}
2015-03-11 06:54:02 +03:00
if n, err := goredis.Int(c.Do("hlen", key)); err != nil {
2014-05-06 13:37:58 +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("hincrby", key, 1, -11)); err != nil {
2014-05-06 13:37:58 +04:00
t.Fatal(err)
} else if n != 0 {
t.Fatal(err)
}
}
func TestHashGetAll(t *testing.T) {
c := getTestConn()
defer c.Close()
key := []byte("d")
2015-03-11 06:54:02 +03:00
if ok, err := goredis.String(c.Do("hmset", key, 1, 1, 2, 2, 3, 3)); err != nil {
2014-05-06 13:37:58 +04:00
t.Fatal(err)
} else if ok != OK {
t.Fatal(ok)
}
2015-03-11 06:54:02 +03:00
if v, err := goredis.MultiBulk(c.Do("hgetall", key)); err != nil {
2014-05-06 13:37:58 +04:00
t.Fatal(err)
} else {
if err := testHashArray(v, 1, 1, 2, 2, 3, 3); err != nil {
t.Fatal(err)
}
}
2015-03-11 06:54:02 +03:00
if v, err := goredis.MultiBulk(c.Do("hkeys", key)); err != nil {
2014-05-06 13:37:58 +04:00
t.Fatal(err)
} else {
if err := testHashArray(v, 1, 2, 3); err != nil {
t.Fatal(err)
}
}
2015-03-11 06:54:02 +03:00
if v, err := goredis.MultiBulk(c.Do("hvals", key)); err != nil {
2014-05-06 13:37:58 +04:00
t.Fatal(err)
} else {
if err := testHashArray(v, 1, 2, 3); err != nil {
t.Fatal(err)
}
}
2015-03-11 06:54:02 +03:00
if n, err := goredis.Int(c.Do("hclear", key)); err != nil {
2014-05-12 11:08:59 +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("hlen", key)); err != nil {
2014-05-12 11:08:59 +04:00
t.Fatal(err)
} else if n != 0 {
t.Fatal(n)
}
2014-05-06 13:37:58 +04:00
}
2014-07-27 21:58:55 +04:00
func TestHashErrorParams(t *testing.T) {
c := getTestConn()
defer c.Close()
2014-07-29 16:08:20 +04:00
if _, err := c.Do("hset", "test_hset"); err == nil {
2014-07-27 21:58:55 +04:00
t.Fatal("invalid err of %v", err)
}
2014-07-29 16:08:20 +04:00
if _, err := c.Do("hget", "test_hget"); err == nil {
2014-07-27 21:58:55 +04:00
t.Fatal("invalid err of %v", err)
}
2014-07-29 16:08:20 +04:00
if _, err := c.Do("hexists", "test_hexists"); err == nil {
2014-07-27 21:58:55 +04:00
t.Fatal("invalid err of %v", err)
}
2014-07-29 16:08:20 +04:00
if _, err := c.Do("hdel", "test_hdel"); err == nil {
2014-07-27 21:58:55 +04:00
t.Fatal("invalid err of %v", err)
}
2014-07-29 16:08:20 +04:00
if _, err := c.Do("hlen", "test_hlen", "a"); err == nil {
2014-07-27 21:58:55 +04:00
t.Fatal("invalid err of %v", err)
}
2014-07-29 16:08:20 +04:00
if _, err := c.Do("hincrby", "test_hincrby"); err == nil {
2014-07-27 21:58:55 +04:00
t.Fatal("invalid err of %v", err)
}
2014-07-29 16:08:20 +04:00
if _, err := c.Do("hmset", "test_hmset"); err == nil {
2014-07-27 21:58:55 +04:00
t.Fatal("invalid err of %v", err)
}
2014-07-29 16:08:20 +04:00
if _, err := c.Do("hmset", "test_hmset", "f1", "v1", "f2"); err == nil {
2014-07-27 21:58:55 +04:00
t.Fatal("invalid err of %v", err)
}
2014-07-29 16:08:20 +04:00
if _, err := c.Do("hmget", "test_hget"); err == nil {
2014-07-27 21:58:55 +04:00
t.Fatal("invalid err of %v", err)
}
2014-07-29 16:08:20 +04:00
if _, err := c.Do("hgetall"); err == nil {
2014-07-27 21:58:55 +04:00
t.Fatal("invalid err of %v", err)
}
2014-07-29 16:08:20 +04:00
if _, err := c.Do("hkeys"); err == nil {
2014-07-27 21:58:55 +04:00
t.Fatal("invalid err of %v", err)
}
2014-07-29 16:08:20 +04:00
if _, err := c.Do("hvals"); err == nil {
2014-07-27 21:58:55 +04:00
t.Fatal("invalid err of %v", err)
}
2014-07-29 16:08:20 +04:00
if _, err := c.Do("hclear"); err == nil {
2014-07-27 21:58:55 +04:00
t.Fatal("invalid err of %v", err)
}
2014-07-29 16:08:20 +04:00
if _, err := c.Do("hclear", "test_hclear", "a"); err == nil {
2014-07-27 21:58:55 +04:00
t.Fatal("invalid err of %v", err)
}
2014-07-29 16:08:20 +04:00
if _, err := c.Do("hmclear"); err == nil {
2014-07-27 21:58:55 +04:00
t.Fatal("invalid err of %v", err)
}
2014-07-29 16:08:20 +04:00
if _, err := c.Do("hexpire", "test_hexpire"); err == nil {
2014-07-27 21:58:55 +04:00
t.Fatal("invalid err of %v", err)
}
2014-07-29 16:08:20 +04:00
if _, err := c.Do("hexpireat", "test_hexpireat"); err == nil {
2014-07-27 21:58:55 +04:00
t.Fatal("invalid err of %v", err)
}
2014-07-29 16:08:20 +04:00
if _, err := c.Do("httl"); err == nil {
2014-07-27 21:58:55 +04:00
t.Fatal("invalid err of %v", err)
}
2014-07-29 16:08:20 +04:00
if _, err := c.Do("hpersist"); err == nil {
2014-07-27 21:58:55 +04:00
t.Fatal("invalid err of %v", err)
}
}