add errer input params test for kv; fix typo for t_kv.go

This commit is contained in:
holys 2014-07-25 17:26:29 +08:00
parent 251b7eae5c
commit fe881ba03c
8 changed files with 103 additions and 22 deletions

View File

@ -197,7 +197,7 @@ func (db *DB) Incr(key []byte) (int64, error) {
return db.incr(key, 1)
}
func (db *DB) IncryBy(key []byte, increment int64) (int64, error) {
func (db *DB) IncrBy(key []byte, increment int64) (int64, error) {
return db.incr(key, increment)
}

View File

@ -196,7 +196,7 @@ func bexpireCommand(c *client) error {
duration, err := ledis.StrInt64(args[1], nil)
if err != nil {
return err
return ErrValue
}
if v, err := c.db.BExpire(args[0], duration); err != nil {
@ -216,7 +216,7 @@ func bexpireatCommand(c *client) error {
when, err := ledis.StrInt64(args[1], nil)
if err != nil {
return err
return ErrValue
}
if v, err := c.db.BExpireAt(args[0], when); err != nil {

View File

@ -91,7 +91,7 @@ func hincrbyCommand(c *client) error {
delta, err := ledis.StrInt64(args[2], nil)
if err != nil {
return err
return ErrValue
}
var n int64
@ -230,7 +230,7 @@ func hexpireCommand(c *client) error {
duration, err := ledis.StrInt64(args[1], nil)
if err != nil {
return err
return ErrValue
}
if v, err := c.db.HExpire(args[0], duration); err != nil {
@ -250,7 +250,7 @@ func hexpireAtCommand(c *client) error {
when, err := ledis.StrInt64(args[1], nil)
if err != nil {
return err
return ErrValue
}
if v, err := c.db.HExpireAt(args[0], when); err != nil {

View File

@ -116,10 +116,10 @@ func incrbyCommand(c *client) error {
delta, err := ledis.StrInt64(args[1], nil)
if err != nil {
return err
return ErrValue
}
if n, err := c.db.IncryBy(c.args[0], delta); err != nil {
if n, err := c.db.IncrBy(c.args[0], delta); err != nil {
return err
} else {
c.writeInteger(n)
@ -136,7 +136,7 @@ func decrbyCommand(c *client) error {
delta, err := ledis.StrInt64(args[1], nil)
if err != nil {
return err
return ErrValue
}
if n, err := c.db.DecrBy(c.args[0], delta); err != nil {
@ -211,7 +211,7 @@ func expireCommand(c *client) error {
duration, err := ledis.StrInt64(args[1], nil)
if err != nil {
return err
return ErrValue
}
if v, err := c.db.Expire(args[0], duration); err != nil {
@ -231,7 +231,7 @@ func expireAtCommand(c *client) error {
when, err := ledis.StrInt64(args[1], nil)
if err != nil {
return err
return ErrValue
}
if v, err := c.db.ExpireAt(args[0], when); err != nil {

View File

@ -137,3 +137,81 @@ func TestKVIncrDecr(t *testing.T) {
t.Fatal(n)
}
}
func TestErrorParams(t *testing.T) {
c := getTestConn()
defer c.Close()
if _, err := c.Do("get", "a", "b", "c"); err == nil || err.Error() != SErrCmdParams {
t.Fatalf("invalid err %v", err)
}
if _, err := c.Do("set", "a", "b", "c"); err == nil || err.Error() != SErrCmdParams {
t.Fatalf("invalid err %v", err)
}
if _, err := c.Do("getset", "a", "b", "c"); err == nil || err.Error() != SErrCmdParams {
t.Fatalf("invalid err %v", err)
}
if _, err := c.Do("setnx", "a", "b", "c"); err == nil || err.Error() != SErrCmdParams {
t.Fatalf("invalid err %v", err)
}
if _, err := c.Do("exists", "a", "b"); err == nil || err.Error() != SErrCmdParams {
t.Fatalf("invalid err %v", err)
}
if _, err := c.Do("incr", "a", "b"); err == nil || err.Error() != SErrCmdParams {
t.Fatalf("invalid err %v", err)
}
if _, err := c.Do("incrby", "a"); err == nil || err.Error() != SErrCmdParams {
t.Fatalf("invalid err %v", err)
}
if _, err := c.Do("decrby", "a"); err == nil || err.Error() != SErrCmdParams {
t.Fatalf("invalid err %v", err)
}
if _, err := c.Do("del"); err == nil || err.Error() != SErrCmdParams {
t.Fatal("invalid err of %v", err)
}
if _, err := c.Do("mset"); err == nil || err.Error() != SErrCmdParams {
t.Fatal("invalid err of %v", err)
}
if _, err := c.Do("mset", "a", "b", "c"); err == nil || err.Error() != SErrCmdParams {
t.Fatal("invalid err of %v", err)
}
if _, err := c.Do("mget"); err == nil || err.Error() != SErrCmdParams {
t.Fatal("invalid err of %v", err)
}
if _, err := c.Do("expire"); err == nil || err.Error() != SErrCmdParams {
t.Fatal("invalid err of %v", err)
}
if _, err := c.Do("expire", "a", "b"); err == nil || err.Error() != SErrValue {
t.Fatal("invalid err of %v", err)
}
if _, err := c.Do("expireat"); err == nil || err.Error() != SErrCmdParams {
t.Fatal("invalid err of %v", err)
}
if _, err := c.Do("expireat", "a", "b"); err == nil || err.Error() != SErrValue {
t.Fatal("invalid err of %v", err)
}
if _, err := c.Do("ttl"); err == nil || err.Error() != SErrCmdParams {
t.Fatal("invalid err of %v", err)
}
if _, err := c.Do("persist"); err == nil || err.Error() != SErrCmdParams {
t.Fatal("invalid err of %v", err)
}
}

View File

@ -87,7 +87,7 @@ func lindexCommand(c *client) error {
index, err := ledis.StrInt64(args[1], nil)
if err != nil {
return err
return ErrValue
}
if v, err := c.db.LIndex(args[0], int32(index)); err != nil {
@ -111,12 +111,12 @@ func lrangeCommand(c *client) error {
start, err = ledis.StrInt64(args[1], nil)
if err != nil {
return err
return ErrValue
}
stop, err = ledis.StrInt64(args[2], nil)
if err != nil {
return err
return ErrValue
}
if v, err := c.db.LRange(args[0], int32(start), int32(stop)); err != nil {
@ -166,7 +166,7 @@ func lexpireCommand(c *client) error {
duration, err := ledis.StrInt64(args[1], nil)
if err != nil {
return err
return ErrValue
}
if v, err := c.db.LExpire(args[0], duration); err != nil {
@ -186,7 +186,7 @@ func lexpireAtCommand(c *client) error {
when, err := ledis.StrInt64(args[1], nil)
if err != nil {
return err
return ErrValue
}
if v, err := c.db.LExpireAt(args[0], when); err != nil {

View File

@ -29,7 +29,7 @@ func zaddCommand(c *client) error {
for i := 0; i < len(params); i++ {
score, err := ledis.StrInt64(args[2*i], nil)
if err != nil {
return err
return ErrValue
}
params[i].Score = score
@ -104,7 +104,7 @@ func zincrbyCommand(c *client) error {
delta, err := ledis.StrInt64(args[1], nil)
if err != nil {
return err
return ErrValue
}
if v, err := c.db.ZIncrBy(key, delta, args[2]); err != nil {
@ -444,7 +444,7 @@ func zexpireCommand(c *client) error {
duration, err := ledis.StrInt64(args[1], nil)
if err != nil {
return err
return ErrValue
}
if v, err := c.db.ZExpire(args[0], duration); err != nil {
@ -464,7 +464,7 @@ func zexpireAtCommand(c *client) error {
when, err := ledis.StrInt64(args[1], nil)
if err != nil {
return err
return ErrValue
}
if v, err := c.db.ZExpireAt(args[0], when); err != nil {

View File

@ -8,6 +8,7 @@ var (
ErrEmptyCommand = errors.New("empty command")
ErrNotFound = errors.New("command not found")
ErrCmdParams = errors.New("invalid command param")
ErrValue = errors.New("value is not an integer or out of range")
)
var (
@ -16,6 +17,8 @@ var (
NullBulk = []byte("-1")
NullArray = []byte("-1")
PONG = "PONG"
OK = "OK"
PONG = "PONG"
OK = "OK"
SErrCmdParams = "ERR invalid command param"
SErrValue = "ERR value is not an integer or out of range"
)