From ee690fd35c5d9f94745d2e0e2972006496b86ae7 Mon Sep 17 00:00:00 2001 From: wenyekui Date: Fri, 25 Jul 2014 15:18:39 +0800 Subject: [PATCH] add unit test --- server/http/base.go | 8 +-- server/http/base_test.go | 6 +- server/http/cmd_kv_test.go | 116 +++++++++++++++++++++++++++++++++++++ 3 files changed, 123 insertions(+), 7 deletions(-) create mode 100644 server/http/cmd_kv_test.go diff --git a/server/http/base.go b/server/http/base.go index 38e592e..2d81907 100644 --- a/server/http/base.go +++ b/server/http/base.go @@ -17,18 +17,18 @@ var ( 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 { panic(fmt.Sprintf("%s has been registered", name)) } regCmds[name] = f } -func lookup(name string) commondFunc { +func lookup(name string) commandFunc { return regCmds[strings.ToLower(name)] } diff --git a/server/http/base_test.go b/server/http/base_test.go index 1f07b31..fee37f2 100644 --- a/server/http/base_test.go +++ b/server/http/base_test.go @@ -12,11 +12,11 @@ var ldb *ledis.Ledis func getTestDB() *ledis.DB { f := func() { var err error - 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.Stat("/tmp/test_http_api_db"); err == nil { + if err = os.RemoveAll("/tmp/test_http_api_db"); err != nil { panic(err) } - } else if err != os.ErrNotExist { + } else if !os.IsNotExist(err) { panic(err) } var cfg ledis.Config diff --git a/server/http/cmd_kv_test.go b/server/http/cmd_kv_test.go new file mode 100644 index 0000000..0ff661e --- /dev/null +++ b/server/http/cmd_kv_test.go @@ -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) + } + +}