add unit test

This commit is contained in:
wenyekui 2014-07-25 15:18:39 +08:00
parent 42174ed78a
commit ee690fd35c
3 changed files with 123 additions and 7 deletions

View File

@ -17,18 +17,18 @@ var (
ErrSyntax = errors.New("syntax error") ErrSyntax = errors.New("syntax error")
) )
type commondFunc func(*ledis.DB, ...string) (interface{}, error) type commandFunc func(*ledis.DB, ...string) (interface{}, error)
var regCmds = map[string]commondFunc{} var regCmds = map[string]commandFunc{}
func register(name string, f commondFunc) { func register(name string, f commandFunc) {
if _, ok := regCmds[strings.ToLower(name)]; ok { if _, ok := regCmds[strings.ToLower(name)]; ok {
panic(fmt.Sprintf("%s has been registered", name)) panic(fmt.Sprintf("%s has been registered", name))
} }
regCmds[name] = f regCmds[name] = f
} }
func lookup(name string) commondFunc { func lookup(name string) commandFunc {
return regCmds[strings.ToLower(name)] return regCmds[strings.ToLower(name)]
} }

View File

@ -12,11 +12,11 @@ var ldb *ledis.Ledis
func getTestDB() *ledis.DB { func getTestDB() *ledis.DB {
f := func() { f := func() {
var err error var err error
if _, err := os.Stat("/tmp/test_http_api_db"); err == nil { if _, err = os.Stat("/tmp/test_http_api_db"); err == nil {
if err := os.RemoveAll("/tmp/test_http_api_db"); err != nil { if err = os.RemoveAll("/tmp/test_http_api_db"); err != nil {
panic(err) panic(err)
} }
} else if err != os.ErrNotExist { } else if !os.IsNotExist(err) {
panic(err) panic(err)
} }
var cfg ledis.Config var cfg ledis.Config

116
server/http/cmd_kv_test.go Normal file
View File

@ -0,0 +1,116 @@
package http
import (
"fmt"
"testing"
)
func TestGetCommand(t *testing.T) {
db := getTestDB()
_, err := getCommand(db, "test_get", "a")
if err == nil || err.Error() != fmt.Sprintf(ERR_ARGUMENT_FORMAT, "get") {
t.Fatal("invalid err ", err)
}
err = db.Set([]byte("test_get"), []byte("v"))
if err != nil {
t.Fatal(err.Error())
}
v, err := getCommand(db, "test_get")
if err != nil {
t.Fatal(err.Error())
}
if v.(string) != "v" {
t.Fatalf("invalid result %v", v)
}
}
func TestSetCommand(t *testing.T) {
db := getTestDB()
_, err := setCommand(db, "test_set")
if err == nil || err.Error() != fmt.Sprintf(ERR_ARGUMENT_FORMAT, "set") {
t.Fatal("invalid err ", err)
}
v, err := setCommand(db, "test_set", "v")
if err != nil {
t.Fatal(err.Error())
}
r := v.([]interface{})
if len(r) != 2 {
t.Fatalf("invalid result %v", v)
}
if r[0].(bool) != true {
t.Fatalf("invalid result %v", r[0])
}
if r[1].(string) != "OK" {
t.Fatalf("invalid result %v", r[1])
}
}
func TestGetsetCommand(t *testing.T) {
db := getTestDB()
_, err := getsetCommand(db, "test_getset")
if err == nil || err.Error() != fmt.Sprintf(ERR_ARGUMENT_FORMAT, "getset") {
t.Fatal("invalid err ", err)
}
v, err := getsetCommand(db, "test_getset", "v")
if err != nil {
t.Fatal(err.Error())
}
if v != nil {
t.Fatal("invalid result ", v)
}
}
func TestSetnxCommand(t *testing.T) {
db := getTestDB()
_, err := setnxCommand(db, "test_setnx")
if err == nil || err.Error() != fmt.Sprintf(ERR_ARGUMENT_FORMAT, "setnx") {
t.Fatal("invalid err ", err)
}
v, err := setnxCommand(db, "test_setnx", "v")
if err != nil {
t.Fatal(err.Error())
}
if v.(int64) != 1 {
t.Fatal("invalid result ", v)
}
}
func TestExistsCommand(t *testing.T) {
db := getTestDB()
_, err := existsCommand(db, "test_exists", "a")
if err == nil || err.Error() != fmt.Sprintf(ERR_ARGUMENT_FORMAT, "exists") {
t.Fatal("invalid err ", err)
}
v, err := existsCommand(db, "test_exists")
if err != nil {
t.Fatal(err.Error())
}
if v.(int64) != 0 {
t.Fatal("invalid result ", v)
}
}
func TestIncrCommand(t *testing.T) {
db := getTestDB()
_, err := incrCommand(db, "test_incr", "a")
if err == nil || err.Error() != fmt.Sprintf(ERR_ARGUMENT_FORMAT, "incr") {
t.Fatal("invalid err ", err)
}
v, err := incrCommand(db, "test_incr")
if err != nil {
t.Fatal(err.Error())
}
if v.(int64) != 1 {
t.Fatal("invalid result ", v)
}
}