ledisdb/server/cmd_kv_test.go

140 lines
2.5 KiB
Go
Raw Normal View History

package server
2014-05-05 07:37:44 +04:00
import (
2014-06-22 06:39:23 +04:00
"github.com/siddontang/ledisdb/client/go/ledis"
2014-05-05 07:37:44 +04:00
"testing"
)
func TestKV(t *testing.T) {
c := getTestConn()
defer c.Close()
2014-06-22 06:39:23 +04:00
if ok, err := ledis.String(c.Do("set", "a", "1234")); err != nil {
2014-05-05 07:37:44 +04:00
t.Fatal(err)
} else if ok != OK {
t.Fatal(ok)
}
2014-06-22 06:39:23 +04:00
if n, err := ledis.Int(c.Do("setnx", "a", "123")); err != nil {
2014-05-05 07:37:44 +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("setnx", "b", "123")); err != nil {
2014-05-05 07:37:44 +04:00
t.Fatal(err)
} else if n != 1 {
t.Fatal(n)
}
2014-06-22 06:39:23 +04:00
if v, err := ledis.String(c.Do("get", "a")); err != nil {
2014-05-05 07:37:44 +04:00
t.Fatal(err)
} else if v != "1234" {
t.Fatal(v)
}
2014-06-22 06:39:23 +04:00
if v, err := ledis.String(c.Do("getset", "a", "123")); err != nil {
2014-05-05 07:37:44 +04:00
t.Fatal(err)
} else if v != "1234" {
t.Fatal(v)
}
2014-06-22 06:39:23 +04:00
if v, err := ledis.String(c.Do("get", "a")); err != nil {
2014-05-05 07:37:44 +04:00
t.Fatal(err)
} else if v != "123" {
t.Fatal(v)
}
2014-06-22 06:39:23 +04:00
if n, err := ledis.Int(c.Do("exists", "a")); err != nil {
2014-05-05 07:37:44 +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("exists", "empty_key_test")); err != nil {
2014-05-05 07:37:44 +04:00
t.Fatal(err)
} else if n != 0 {
t.Fatal(n)
}
2014-06-22 06:39:23 +04:00
if _, err := ledis.Int(c.Do("del", "a", "b")); err != nil {
2014-05-05 07:37:44 +04:00
t.Fatal(err)
}
2014-06-22 06:39:23 +04:00
if n, err := ledis.Int(c.Do("exists", "a")); err != nil {
2014-05-05 07:37:44 +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("exists", "b")); err != nil {
2014-05-05 07:37:44 +04:00
t.Fatal(err)
} else if n != 0 {
t.Fatal(n)
}
}
func TestKVM(t *testing.T) {
c := getTestConn()
defer c.Close()
2014-06-22 06:39:23 +04:00
if ok, err := ledis.String(c.Do("mset", "a", "1", "b", "2")); err != nil {
2014-05-05 07:37:44 +04:00
t.Fatal(err)
} else if ok != OK {
t.Fatal(ok)
}
2014-06-22 06:39:23 +04:00
if v, err := ledis.MultiBulk(c.Do("mget", "a", "b", "c")); err != nil {
2014-05-05 07:37:44 +04:00
t.Fatal(err)
} else if len(v) != 3 {
t.Fatal(len(v))
} else {
if vv, ok := v[0].([]byte); !ok || string(vv) != "1" {
t.Fatal("not 1")
}
if vv, ok := v[1].([]byte); !ok || string(vv) != "2" {
t.Fatal("not 2")
}
if v[2] != nil {
t.Fatal("must nil")
}
}
}
func TestKVIncrDecr(t *testing.T) {
c := getTestConn()
defer c.Close()
2014-06-22 06:39:23 +04:00
if n, err := ledis.Int64(c.Do("incr", "n")); err != nil {
2014-05-05 07:37:44 +04:00
t.Fatal(err)
} else if n != 1 {
t.Fatal(n)
}
2014-06-22 06:39:23 +04:00
if n, err := ledis.Int64(c.Do("incr", "n")); err != nil {
2014-05-05 07:37:44 +04:00
t.Fatal(err)
} else if n != 2 {
t.Fatal(n)
}
2014-06-22 06:39:23 +04:00
if n, err := ledis.Int64(c.Do("decr", "n")); err != nil {
2014-05-05 07:37:44 +04:00
t.Fatal(err)
} else if n != 1 {
t.Fatal(n)
}
2014-06-22 06:39:23 +04:00
if n, err := ledis.Int64(c.Do("incrby", "n", 10)); err != nil {
2014-05-05 07:37:44 +04:00
t.Fatal(err)
} else if n != 11 {
t.Fatal(n)
}
2014-06-22 06:39:23 +04:00
if n, err := ledis.Int64(c.Do("decrby", "n", 10)); err != nil {
2014-05-05 07:37:44 +04:00
t.Fatal(err)
} else if n != 1 {
t.Fatal(n)
}
}