From 1dd7d22198133994536baa40d40a948708c56484 Mon Sep 17 00:00:00 2001 From: wenyekui Date: Wed, 23 Jul 2014 10:59:37 +0800 Subject: [PATCH] add unit test --- server/http/cmd_bit.go | 2 +- server/http/cmd_bit_test.go | 93 +++++++++++++++++++++++++++++++++++++ server/http/handler.go | 5 +- 3 files changed, 98 insertions(+), 2 deletions(-) create mode 100644 server/http/cmd_bit_test.go diff --git a/server/http/cmd_bit.go b/server/http/cmd_bit.go index e5d9965..538b3a9 100644 --- a/server/http/cmd_bit.go +++ b/server/http/cmd_bit.go @@ -14,7 +14,7 @@ func bgetCommand(db *ledis.DB, args ...string) (interface{}, error) { if v, err := db.BGet([]byte(args[0])); err != nil { return nil, err } else { - return v, nil + return ledis.String(v), nil } } diff --git a/server/http/cmd_bit_test.go b/server/http/cmd_bit_test.go new file mode 100644 index 0000000..21844db --- /dev/null +++ b/server/http/cmd_bit_test.go @@ -0,0 +1,93 @@ +package http + +import ( + // "github.com/siddontang/ledisdb/ledis" + "testing" +) + +func TestBgetCommand(t *testing.T) { + db := getTestDB() + db.BSetBit([]byte("test_bget"), 0, 1) + db.BSetBit([]byte("test_bget"), 1, 1) + db.BSetBit([]byte("test_bget"), 2, 1) + + _, err := bgetCommand(db, "test_bget", "a", "b", "c") + if err == nil || err.Error() != "ERR wrong number of arguments for 'bget' command" { + t.Fatal("invalid err %v", err) + } + + r, err := bgetCommand(db, "test_bget") + if err != nil { + t.Fatal(err.Error()) + } + str := r.(string) + if str != "\x07" { + t.Fatal("wrong result of 'bget': %v", []byte(str)) + } +} + +func TestBDeleteCommand(t *testing.T) { + db := getTestDB() + + _, err := bdeleteCommand(db, "test_bdelete", "a", "b", "c") + if err == nil || err.Error() != "ERR wrong number of arguments for 'bdelete' command" { + t.Fatalf("invalid err %v", err) + } + + db.BSetBit([]byte("test_bdelete"), 0, 1) + db.BSetBit([]byte("test_bdelete"), 1, 1) + db.BSetBit([]byte("test_bdelete"), 2, 1) + n, err := bdeleteCommand(db, "test_bdelete") + if err != nil { + t.Fatal(err.Error()) + } + if n.(int64) != 1 { + t.Fatalf("wrong result: %v", n) + } + + n, err = bdeleteCommand(db, "test_bdelete_not_exit") + if err != nil { + t.Fatal(err.Error()) + } + if n.(int64) != 0 { + t.Fatalf("wrong result: %v", n) + } +} + +func TestBSetbitCommand(t *testing.T) { + db := getTestDB() + _, err := bsetbitCommand(db, "test_bsetbit", "a", "b", "c") + if err == nil || err.Error() != "ERR wrong number of arguments for 'bsetbit' command" { + t.Fatalf("invalid err %v", err) + } + n, err := bsetbitCommand(db, "test_bsetbit", "1", "1") + if err != nil { + t.Fatal(err.Error()) + } + if n.(uint8) != 0 { + t.Fatal("wrong result: %v", n) + } + n, err = db.BGetBit([]byte("test_bsetbit"), 1) + if err != nil { + t.Fatal(err.Error()) + } + if n.(uint8) != 1 { + t.Fatalf("wrong result: %v", n) + } +} + +func TestBMsetbitCommand(t *testing.T) { + db := getTestDB() + _, err := bmsetbitCommand(db, "test_bmsetbit", "a", "b", "c") + + if err == nil || err.Error() != "ERR wrong number of arguments for 'bmsetbit' command" { + t.Fatalf("invalid err %v", err) + } + n, err := bmsetbitCommand(db, "test_bmsetbit", "1", "1", "3", "1") + if err != nil { + t.Fatal(err.Error()) + } + if n.(int64) != 2 { + t.Fatalf("wrong result: %v", n) + } +} diff --git a/server/http/handler.go b/server/http/handler.go index 34aa24f..d395534 100644 --- a/server/http/handler.go +++ b/server/http/handler.go @@ -41,13 +41,16 @@ func (h *CmdHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { } func (h *CmdHandler) parseReqPath(path string) (db int, cmd string, args []string) { + /* + the proper format of `path` is /cmd/arg1/arg2/../argN or /db/cmd/arg1/arg2/../argN + if `path` is the first kind, `db` will be 0 + */ substrings := strings.Split(strings.TrimLeft(path, "/"), "/") if len(substrings) == 1 { return 0, substrings[0], substrings[1:] } db, err := strconv.Atoi(substrings[0]) if err != nil { - // db = 0 cmd = substrings[0] args = substrings[1:] } else {