add unit test

This commit is contained in:
wenyekui 2014-07-25 16:17:45 +08:00
parent ee690fd35c
commit deb30383c6
3 changed files with 148 additions and 3 deletions

View File

@ -14,7 +14,7 @@ func TestBgetCommand(t *testing.T) {
_, err := bgetCommand(db, "test_bget", "a", "b", "c") _, err := bgetCommand(db, "test_bget", "a", "b", "c")
if err == nil || err.Error() != "wrong number of arguments for 'bget' command" { if err == nil || err.Error() != "wrong number of arguments for 'bget' command" {
t.Fatal("invalid err %v", err) t.Fatalf("invalid err %v", err)
} }
r, err := bgetCommand(db, "test_bget") r, err := bgetCommand(db, "test_bget")
@ -23,7 +23,7 @@ func TestBgetCommand(t *testing.T) {
} }
str := r.(string) str := r.(string)
if str != "\x07" { if str != "\x07" {
t.Fatal("wrong result of 'bget': %v", []byte(str)) t.Fatalf("wrong result of 'bget': %v", []byte(str))
} }
} }

View File

@ -73,7 +73,7 @@ func TestHExistsCommand(t *testing.T) {
} }
func TestDelCommand(t *testing.T) { func TestHDelCommand(t *testing.T) {
db := getTestDB() db := getTestDB()
_, err := hdelCommand(db, "test_hdel") _, err := hdelCommand(db, "test_hdel")
if err == nil || err.Error() != fmt.Sprintf(ERR_ARGUMENT_FORMAT, "hdel") { if err == nil || err.Error() != fmt.Sprintf(ERR_ARGUMENT_FORMAT, "hdel") {

View File

@ -3,6 +3,7 @@ package http
import ( import (
"fmt" "fmt"
"testing" "testing"
"time"
) )
func TestGetCommand(t *testing.T) { func TestGetCommand(t *testing.T) {
@ -114,3 +115,147 @@ func TestIncrCommand(t *testing.T) {
} }
} }
func TestDecrCommand(t *testing.T) {
db := getTestDB()
_, err := decrCommand(db, "test_decr", "a")
if err == nil || err.Error() != fmt.Sprintf(ERR_ARGUMENT_FORMAT, "decr") {
t.Fatal("invalid err ", err)
}
v, err := decrCommand(db, "test_decr")
if err != nil {
t.Fatal(err.Error())
}
if v.(int64) != -1 {
t.Fatal("invalid result ", v)
}
}
func TestDelCommand(t *testing.T) {
db := getTestDB()
_, err := delCommand(db)
if err == nil || err.Error() != fmt.Sprintf(ERR_ARGUMENT_FORMAT, "del") {
t.Fatal("invalid err ", err)
}
v, err := delCommand(db, "test_del")
if err != nil {
t.Fatal(err.Error())
}
if v.(int64) != 1 {
t.Fatal("invalid result ", v)
}
}
func TestMsetCommand(t *testing.T) {
db := getTestDB()
_, err := msetCommand(db, "a", "b", "c")
if err == nil || err.Error() != fmt.Sprintf(ERR_ARGUMENT_FORMAT, "mset") {
t.Fatal("invalid err ", err)
}
v, err := msetCommand(db, "test_mset", "v")
if err != nil {
t.Fatal(err.Error())
}
r := v.([]interface{})
if len(r) != 2 {
t.Fatal("invalid result ", v)
}
if r[0].(bool) != true {
t.Fatal("invalid result ", r[0])
}
if r[1].(string) != "OK" {
t.Fatal("invalid result ", r[1])
}
}
func TestMgetCommand(t *testing.T) {
db := getTestDB()
_, err := mgetCommand(db)
if err == nil || err.Error() != fmt.Sprintf(ERR_ARGUMENT_FORMAT, "mget") {
t.Fatal("invalid err ", err)
}
v, err := mgetCommand(db, "test_mget")
if err != nil {
t.Fatal(err.Error())
}
arr := v.([]interface{})
if arr[0] != nil {
t.Fatal("invalid result ", arr)
}
}
func TestExpireCommand(t *testing.T) {
db := getTestDB()
_, err := expireCommand(db)
if err == nil || err.Error() != fmt.Sprintf(ERR_ARGUMENT_FORMAT, "expire") {
t.Fatal("invalid err ", err)
}
v, err := expireCommand(db, "test_expire", "10")
if err != nil {
t.Fatal(err.Error())
}
if v.(int64) != 0 {
t.Fatal("invalid result ", v)
}
}
func TestExpireAtCommand(t *testing.T) {
db := getTestDB()
_, err := expireAtCommand(db)
if err == nil || err.Error() != fmt.Sprintf(ERR_ARGUMENT_FORMAT, "expireat") {
t.Fatal("invalid err ", err)
}
expireAt := fmt.Sprintf("%d", time.Now().Unix()+10)
v, err := expireAtCommand(db, "test_expireat", expireAt)
if err != nil {
t.Fatal(err.Error())
}
if v.(int64) != 0 {
t.Fatal("invalid result ", v)
}
}
func TestTTLCommand(t *testing.T) {
db := getTestDB()
_, err := ttlCommand(db)
if err == nil || err.Error() != fmt.Sprintf(ERR_ARGUMENT_FORMAT, "ttl") {
t.Fatal("invalid err ", err)
}
v, err := ttlCommand(db, "test_ttl")
if err != nil {
t.Fatal(err.Error())
}
if v.(int64) != -1 {
t.Fatal("invalid result ", v)
}
}
func TestPersistCommand(t *testing.T) {
db := getTestDB()
_, err := persistCommand(db)
if err == nil || err.Error() != fmt.Sprintf(ERR_ARGUMENT_FORMAT, "persist") {
t.Fatal("invalid err ", err)
}
v, err := persistCommand(db, "test_persist")
if err != nil {
t.Fatal(err.Error())
}
if v.(int64) != 0 {
t.Fatal("invalid result ", v)
}
}