ledisdb/ledis/t_kv_test.go

68 lines
1.1 KiB
Go
Raw Normal View History

2014-05-16 06:36:18 +04:00
package ledis
import (
"testing"
)
2014-05-20 04:41:24 +04:00
func TestKVCodec(t *testing.T) {
db := getTestDB()
ek := db.encodeKVKey([]byte("key"))
if k, err := db.decodeKVKey(ek); err != nil {
t.Fatal(err)
} else if string(k) != "key" {
t.Fatal(string(k))
}
}
2014-05-16 06:36:18 +04:00
func TestDBKV(t *testing.T) {
db := getTestDB()
2014-06-19 13:19:40 +04:00
key1 := []byte("testdb_kv_a")
2014-05-16 06:36:18 +04:00
2014-06-19 13:19:40 +04:00
if err := db.Set(key1, []byte("hello world 1")); err != nil {
2014-05-16 06:36:18 +04:00
t.Fatal(err)
}
2014-06-19 13:19:40 +04:00
key2 := []byte("testdb_kv_b")
if err := db.Set(key2, []byte("hello world 2")); err != nil {
t.Fatal(err)
}
ay, _ := db.MGet(key1, key2)
if v1 := ay[0]; string(v1) != "hello world 1" {
2014-06-19 13:19:40 +04:00
t.Fatal(string(v1))
}
if v2 := ay[1]; string(v2) != "hello world 2" {
2014-06-19 13:19:40 +04:00
t.Fatal(string(v2))
}
2014-05-16 06:36:18 +04:00
}
2014-05-26 05:21:17 +04:00
2014-06-23 07:12:20 +04:00
func TestKVPersist(t *testing.T) {
db := getTestDB()
key := []byte("persist")
db.Set(key, []byte{})
if n, err := db.Persist(key); err != nil {
t.Fatal(err)
} else if n != 0 {
t.Fatal(n)
}
if _, err := db.Expire(key, 10); err != nil {
t.Fatal(err)
}
if n, err := db.Persist(key); err != nil {
t.Fatal(err)
} else if n != 1 {
t.Fatal(n)
}
}