chore: use t.Errorf(...) instead of t.Error(fmt.Sprintf(...))

This commit is contained in:
guoguangwu
2023-07-13 13:19:43 +08:00
committed by finley
parent 8873975901
commit 2a0d96b8c3
6 changed files with 66 additions and 71 deletions

View File

@@ -1,7 +1,6 @@
package database package database
import ( import (
"fmt"
"github.com/hdt3213/godis/lib/utils" "github.com/hdt3213/godis/lib/utils"
"github.com/hdt3213/godis/redis/protocol" "github.com/hdt3213/godis/redis/protocol"
"github.com/hdt3213/godis/redis/protocol/asserts" "github.com/hdt3213/godis/redis/protocol/asserts"
@@ -48,7 +47,7 @@ func TestDumpKeyAndRenameTo(t *testing.T) {
result = testDB.Exec(nil, utils.ToCmdLine("ttl", newKey)) result = testDB.Exec(nil, utils.ToCmdLine("ttl", newKey))
intResult, ok := result.(*protocol.IntReply) intResult, ok := result.(*protocol.IntReply)
if !ok { if !ok {
t.Error(fmt.Sprintf("expected int protocol, actually %s", result.ToBytes())) t.Errorf("expected int protocol, actually %s", result.ToBytes())
return return
} }
if intResult.Code <= 0 { if intResult.Code <= 0 {

View File

@@ -1,7 +1,6 @@
package database package database
import ( import (
"fmt"
"github.com/hdt3213/godis/lib/utils" "github.com/hdt3213/godis/lib/utils"
"github.com/hdt3213/godis/redis/protocol" "github.com/hdt3213/godis/redis/protocol"
"github.com/hdt3213/godis/redis/protocol/asserts" "github.com/hdt3213/godis/redis/protocol/asserts"
@@ -74,7 +73,7 @@ func TestGeoDist(t *testing.T) {
result := execGeoDist(testDB, utils.ToCmdLine(key, pos1, pos2, "km")) result := execGeoDist(testDB, utils.ToCmdLine(key, pos1, pos2, "km"))
bulkReply, ok := result.(*protocol.BulkReply) bulkReply, ok := result.(*protocol.BulkReply)
if !ok { if !ok {
t.Error(fmt.Sprintf("expected bulk protocol, actually %s", result.ToBytes())) t.Errorf("expected bulk protocol, actually %s", result.ToBytes())
return return
} }
dist, err := strconv.ParseFloat(string(bulkReply.Arg), 10) dist, err := strconv.ParseFloat(string(bulkReply.Arg), 10)
@@ -89,7 +88,7 @@ func TestGeoDist(t *testing.T) {
result = execGeoDist(testDB, utils.ToCmdLine(key, pos1, pos2, "m")) result = execGeoDist(testDB, utils.ToCmdLine(key, pos1, pos2, "m"))
bulkReply, ok = result.(*protocol.BulkReply) bulkReply, ok = result.(*protocol.BulkReply)
if !ok { if !ok {
t.Error(fmt.Sprintf("expected bulk protocol, actually %s", result.ToBytes())) t.Errorf("expected bulk protocol, actually %s", result.ToBytes())
return return
} }
dist, err = strconv.ParseFloat(string(bulkReply.Arg), 10) dist, err = strconv.ParseFloat(string(bulkReply.Arg), 10)

View File

@@ -1,7 +1,6 @@
package database package database
import ( import (
"fmt"
"github.com/hdt3213/godis/lib/utils" "github.com/hdt3213/godis/lib/utils"
"github.com/hdt3213/godis/redis/protocol" "github.com/hdt3213/godis/redis/protocol"
"github.com/hdt3213/godis/redis/protocol/asserts" "github.com/hdt3213/godis/redis/protocol/asserts"
@@ -23,7 +22,7 @@ func TestHSet(t *testing.T) {
values[field] = []byte(value) values[field] = []byte(value)
result := testDB.Exec(nil, utils.ToCmdLine("hset", key, field, value)) result := testDB.Exec(nil, utils.ToCmdLine("hset", key, field, value))
if intResult, _ := result.(*protocol.IntReply); intResult.Code != int64(1) { if intResult, _ := result.(*protocol.IntReply); intResult.Code != int64(1) {
t.Error(fmt.Sprintf("expected %d, actually %d", 1, intResult.Code)) t.Errorf("expected %d, actually %d", 1, intResult.Code)
} }
} }
@@ -32,23 +31,23 @@ func TestHSet(t *testing.T) {
actual := testDB.Exec(nil, utils.ToCmdLine("hget", key, field)) actual := testDB.Exec(nil, utils.ToCmdLine("hget", key, field))
expected := protocol.MakeBulkReply(v) expected := protocol.MakeBulkReply(v)
if !utils.BytesEquals(actual.ToBytes(), expected.ToBytes()) { if !utils.BytesEquals(actual.ToBytes(), expected.ToBytes()) {
t.Error(fmt.Sprintf("expected %s, actually %s", string(expected.ToBytes()), string(actual.ToBytes()))) t.Errorf("expected %s, actually %s", string(expected.ToBytes()), string(actual.ToBytes()))
} }
actual = testDB.Exec(nil, utils.ToCmdLine("hexists", key, field)) actual = testDB.Exec(nil, utils.ToCmdLine("hexists", key, field))
if intResult, _ := actual.(*protocol.IntReply); intResult.Code != int64(1) { if intResult, _ := actual.(*protocol.IntReply); intResult.Code != int64(1) {
t.Error(fmt.Sprintf("expected %d, actually %d", 1, intResult.Code)) t.Errorf("expected %d, actually %d", 1, intResult.Code)
} }
actual = testDB.Exec(nil, utils.ToCmdLine("hstrlen", key, field)) actual = testDB.Exec(nil, utils.ToCmdLine("hstrlen", key, field))
if intResult, _ := actual.(*protocol.IntReply); intResult.Code != int64(len(v)) { if intResult, _ := actual.(*protocol.IntReply); intResult.Code != int64(len(v)) {
t.Error(fmt.Sprintf("expected %d, actually %d", int64(len(v)), intResult.Code)) t.Errorf("expected %d, actually %d", int64(len(v)), intResult.Code)
} }
} }
// test hlen // test hlen
actual := testDB.Exec(nil, utils.ToCmdLine("hlen", key)) actual := testDB.Exec(nil, utils.ToCmdLine("hlen", key))
if intResult, _ := actual.(*protocol.IntReply); intResult.Code != int64(len(values)) { if intResult, _ := actual.(*protocol.IntReply); intResult.Code != int64(len(values)) {
t.Error(fmt.Sprintf("expected %d, actually %d", len(values), intResult.Code)) t.Errorf("expected %d, actually %d", len(values), intResult.Code)
} }
} }
@@ -71,12 +70,12 @@ func TestHDel(t *testing.T) {
args = append(args, fields...) args = append(args, fields...)
actual := testDB.Exec(nil, utils.ToCmdLine2("hdel", args...)) actual := testDB.Exec(nil, utils.ToCmdLine2("hdel", args...))
if intResult, _ := actual.(*protocol.IntReply); intResult.Code != int64(len(fields)) { if intResult, _ := actual.(*protocol.IntReply); intResult.Code != int64(len(fields)) {
t.Error(fmt.Sprintf("expected %d, actually %d", len(fields), intResult.Code)) t.Errorf("expected %d, actually %d", len(fields), intResult.Code)
} }
actual = testDB.Exec(nil, utils.ToCmdLine("hlen", key)) actual = testDB.Exec(nil, utils.ToCmdLine("hlen", key))
if intResult, _ := actual.(*protocol.IntReply); intResult.Code != int64(0) { if intResult, _ := actual.(*protocol.IntReply); intResult.Code != int64(0) {
t.Error(fmt.Sprintf("expected %d, actually %d", 0, intResult.Code)) t.Errorf("expected %d, actually %d", 0, intResult.Code)
} }
} }
@@ -96,7 +95,7 @@ func TestHMSet(t *testing.T) {
} }
result := testDB.Exec(nil, utils.ToCmdLine2("hmset", setArgs...)) result := testDB.Exec(nil, utils.ToCmdLine2("hmset", setArgs...))
if _, ok := result.(*protocol.OkReply); !ok { if _, ok := result.(*protocol.OkReply); !ok {
t.Error(fmt.Sprintf("expected ok, actually %s", string(result.ToBytes()))) t.Errorf("expected ok, actually %s", string(result.ToBytes()))
} }
// test HMGet // test HMGet
@@ -105,7 +104,7 @@ func TestHMSet(t *testing.T) {
actual := testDB.Exec(nil, utils.ToCmdLine2("hmget", getArgs...)) actual := testDB.Exec(nil, utils.ToCmdLine2("hmget", getArgs...))
expected := protocol.MakeMultiBulkReply(utils.ToCmdLine(values...)) expected := protocol.MakeMultiBulkReply(utils.ToCmdLine(values...))
if !utils.BytesEquals(actual.ToBytes(), expected.ToBytes()) { if !utils.BytesEquals(actual.ToBytes(), expected.ToBytes()) {
t.Error(fmt.Sprintf("expected %s, actually %s", string(expected.ToBytes()), string(actual.ToBytes()))) t.Errorf("expected %s, actually %s", string(expected.ToBytes()), string(actual.ToBytes()))
} }
} }
@@ -130,21 +129,21 @@ func TestHGetAll(t *testing.T) {
result := testDB.Exec(nil, utils.ToCmdLine("hgetall", key)) result := testDB.Exec(nil, utils.ToCmdLine("hgetall", key))
multiBulk, ok := result.(*protocol.MultiBulkReply) multiBulk, ok := result.(*protocol.MultiBulkReply)
if !ok { if !ok {
t.Error(fmt.Sprintf("expected MultiBulkReply, actually %s", string(result.ToBytes()))) t.Errorf("expected MultiBulkReply, actually %s", string(result.ToBytes()))
} }
if 2*len(fields) != len(multiBulk.Args) { if 2*len(fields) != len(multiBulk.Args) {
t.Error(fmt.Sprintf("expected %d items , actually %d ", 2*len(fields), len(multiBulk.Args))) t.Errorf("expected %d items , actually %d ", 2*len(fields), len(multiBulk.Args))
} }
for i := range fields { for i := range fields {
field := string(multiBulk.Args[2*i]) field := string(multiBulk.Args[2*i])
actual := string(multiBulk.Args[2*i+1]) actual := string(multiBulk.Args[2*i+1])
expected, ok := valueMap[field] expected, ok := valueMap[field]
if !ok { if !ok {
t.Error(fmt.Sprintf("unexpected field %s", field)) t.Errorf("unexpected field %s", field)
continue continue
} }
if actual != expected { if actual != expected {
t.Error(fmt.Sprintf("expected %s, actually %s", expected, actual)) t.Errorf("expected %s, actually %s", expected, actual)
} }
} }
@@ -152,15 +151,15 @@ func TestHGetAll(t *testing.T) {
result = testDB.Exec(nil, utils.ToCmdLine("hkeys", key)) result = testDB.Exec(nil, utils.ToCmdLine("hkeys", key))
multiBulk, ok = result.(*protocol.MultiBulkReply) multiBulk, ok = result.(*protocol.MultiBulkReply)
if !ok { if !ok {
t.Error(fmt.Sprintf("expected MultiBulkReply, actually %s", string(result.ToBytes()))) t.Errorf("expected MultiBulkReply, actually %s", string(result.ToBytes()))
} }
if len(fields) != len(multiBulk.Args) { if len(fields) != len(multiBulk.Args) {
t.Error(fmt.Sprintf("expected %d items , actually %d ", len(fields), len(multiBulk.Args))) t.Errorf("expected %d items , actually %d ", len(fields), len(multiBulk.Args))
} }
for _, v := range multiBulk.Args { for _, v := range multiBulk.Args {
field := string(v) field := string(v)
if _, ok := valueMap[field]; !ok { if _, ok := valueMap[field]; !ok {
t.Error(fmt.Sprintf("unexpected field %s", field)) t.Errorf("unexpected field %s", field)
} }
} }
@@ -168,16 +167,16 @@ func TestHGetAll(t *testing.T) {
result = testDB.Exec(nil, utils.ToCmdLine("hvals", key)) result = testDB.Exec(nil, utils.ToCmdLine("hvals", key))
multiBulk, ok = result.(*protocol.MultiBulkReply) multiBulk, ok = result.(*protocol.MultiBulkReply)
if !ok { if !ok {
t.Error(fmt.Sprintf("expected MultiBulkReply, actually %s", string(result.ToBytes()))) t.Errorf("expected MultiBulkReply, actually %s", string(result.ToBytes()))
} }
if len(fields) != len(multiBulk.Args) { if len(fields) != len(multiBulk.Args) {
t.Error(fmt.Sprintf("expected %d items , actually %d ", len(fields), len(multiBulk.Args))) t.Errorf("expected %d items , actually %d ", len(fields), len(multiBulk.Args))
} }
for _, v := range multiBulk.Args { for _, v := range multiBulk.Args {
value := string(v) value := string(v)
_, ok := valueSet[value] _, ok := valueSet[value]
if !ok { if !ok {
t.Error(fmt.Sprintf("unexpected value %s", value)) t.Errorf("unexpected value %s", value)
} }
} }
@@ -188,7 +187,7 @@ func TestHGetAll(t *testing.T) {
if !ok { if !ok {
resultBytes := string(result.ToBytes()) resultBytes := string(result.ToBytes())
if resultBytes != "*0\r\n" { if resultBytes != "*0\r\n" {
t.Error(fmt.Sprintf("expected MultiBulkReply, actually %s", resultBytes)) t.Errorf("expected MultiBulkReply, actually %s", resultBytes)
} }
} }
@@ -196,40 +195,40 @@ func TestHGetAll(t *testing.T) {
result = testDB.Exec(nil, utils.ToCmdLine("hrandfield", key, strconv.Itoa(size+100))) result = testDB.Exec(nil, utils.ToCmdLine("hrandfield", key, strconv.Itoa(size+100)))
multiBulk, ok = result.(*protocol.MultiBulkReply) multiBulk, ok = result.(*protocol.MultiBulkReply)
if !ok { if !ok {
t.Error(fmt.Sprintf("expected MultiBulkReply, actually %s", string(result.ToBytes()))) t.Errorf("expected MultiBulkReply, actually %s", string(result.ToBytes()))
} }
if len(fields) != len(multiBulk.Args) { if len(fields) != len(multiBulk.Args) {
t.Error(fmt.Sprintf("expected %d items , actually %d ", len(fields), len(multiBulk.Args))) t.Errorf("expected %d items , actually %d ", len(fields), len(multiBulk.Args))
} }
// test HRandField count > size withvalues // test HRandField count > size withvalues
result = testDB.Exec(nil, utils.ToCmdLine("hrandfield", key, strconv.Itoa(size+100), "withvalues")) result = testDB.Exec(nil, utils.ToCmdLine("hrandfield", key, strconv.Itoa(size+100), "withvalues"))
multiBulk, ok = result.(*protocol.MultiBulkReply) multiBulk, ok = result.(*protocol.MultiBulkReply)
if !ok { if !ok {
t.Error(fmt.Sprintf("expected MultiBulkReply, actually %s", string(result.ToBytes()))) t.Errorf("expected MultiBulkReply, actually %s", string(result.ToBytes()))
} }
if 2*len(fields) != len(multiBulk.Args) { if 2*len(fields) != len(multiBulk.Args) {
t.Error(fmt.Sprintf("expected %d items , actually %d ", 2*len(fields), len(multiBulk.Args))) t.Errorf("expected %d items , actually %d ", 2*len(fields), len(multiBulk.Args))
} }
// test HRandField count < size // test HRandField count < size
result = testDB.Exec(nil, utils.ToCmdLine("hrandfield", key, strconv.Itoa(-size-10))) result = testDB.Exec(nil, utils.ToCmdLine("hrandfield", key, strconv.Itoa(-size-10)))
multiBulk, ok = result.(*protocol.MultiBulkReply) multiBulk, ok = result.(*protocol.MultiBulkReply)
if !ok { if !ok {
t.Error(fmt.Sprintf("expected MultiBulkReply, actually %s", string(result.ToBytes()))) t.Errorf("expected MultiBulkReply, actually %s", string(result.ToBytes()))
} }
if len(fields)+10 != len(multiBulk.Args) { if len(fields)+10 != len(multiBulk.Args) {
t.Error(fmt.Sprintf("expected %d items , actually %d ", len(fields)+10, len(multiBulk.Args))) t.Errorf("expected %d items , actually %d ", len(fields)+10, len(multiBulk.Args))
} }
// test HRandField count < size withvalues // test HRandField count < size withvalues
result = testDB.Exec(nil, utils.ToCmdLine("hrandfield", key, strconv.Itoa(-size-10), "withvalues")) result = testDB.Exec(nil, utils.ToCmdLine("hrandfield", key, strconv.Itoa(-size-10), "withvalues"))
multiBulk, ok = result.(*protocol.MultiBulkReply) multiBulk, ok = result.(*protocol.MultiBulkReply)
if !ok { if !ok {
t.Error(fmt.Sprintf("expected MultiBulkReply, actually %s", string(result.ToBytes()))) t.Errorf("expected MultiBulkReply, actually %s", string(result.ToBytes()))
} }
if 2*(len(fields)+10) != len(multiBulk.Args) { if 2*(len(fields)+10) != len(multiBulk.Args) {
t.Error(fmt.Sprintf("expected %d items , actually %d ", 2*(len(fields)+10), len(multiBulk.Args))) t.Errorf("expected %d items , actually %d ", 2*(len(fields)+10), len(multiBulk.Args))
} }
} }
@@ -239,25 +238,25 @@ func TestHIncrBy(t *testing.T) {
key := utils.RandString(10) key := utils.RandString(10)
result := testDB.Exec(nil, utils.ToCmdLine("hincrby", key, "a", "1")) result := testDB.Exec(nil, utils.ToCmdLine("hincrby", key, "a", "1"))
if bulkResult, _ := result.(*protocol.BulkReply); string(bulkResult.Arg) != "1" { if bulkResult, _ := result.(*protocol.BulkReply); string(bulkResult.Arg) != "1" {
t.Error(fmt.Sprintf("expected %s, actually %s", "1", string(bulkResult.Arg))) t.Errorf("expected %s, actually %s", "1", string(bulkResult.Arg))
} }
result = testDB.Exec(nil, utils.ToCmdLine("hincrby", key, "a", "1")) result = testDB.Exec(nil, utils.ToCmdLine("hincrby", key, "a", "1"))
if bulkResult, _ := result.(*protocol.BulkReply); string(bulkResult.Arg) != "2" { if bulkResult, _ := result.(*protocol.BulkReply); string(bulkResult.Arg) != "2" {
t.Error(fmt.Sprintf("expected %s, actually %s", "2", string(bulkResult.Arg))) t.Errorf("expected %s, actually %s", "2", string(bulkResult.Arg))
} }
result = testDB.Exec(nil, utils.ToCmdLine("hincrbyfloat", key, "b", "1.2")) result = testDB.Exec(nil, utils.ToCmdLine("hincrbyfloat", key, "b", "1.2"))
if bulkResult, ok := result.(*protocol.BulkReply); ok { if bulkResult, ok := result.(*protocol.BulkReply); ok {
val, _ := strconv.ParseFloat(string(bulkResult.Arg), 10) val, _ := strconv.ParseFloat(string(bulkResult.Arg), 10)
if math.Abs(val-1.2) > 1e-4 { if math.Abs(val-1.2) > 1e-4 {
t.Error(fmt.Sprintf("expected %s, actually %s", "1.2", string(bulkResult.Arg))) t.Errorf("expected %s, actually %s", "1.2", string(bulkResult.Arg))
} }
} }
result = testDB.Exec(nil, utils.ToCmdLine("hincrbyfloat", key, "b", "1.2")) result = testDB.Exec(nil, utils.ToCmdLine("hincrbyfloat", key, "b", "1.2"))
if bulkResult, ok := result.(*protocol.BulkReply); ok { if bulkResult, ok := result.(*protocol.BulkReply); ok {
val, _ := strconv.ParseFloat(string(bulkResult.Arg), 10) val, _ := strconv.ParseFloat(string(bulkResult.Arg), 10)
if math.Abs(val-2.4) > 1e-4 { if math.Abs(val-2.4) > 1e-4 {
t.Error(fmt.Sprintf("expected %s, actually %s", "1.2", string(bulkResult.Arg))) t.Errorf("expected %s, actually %s", "1.2", string(bulkResult.Arg))
} }
} }
} }

View File

@@ -1,7 +1,6 @@
package database package database
import ( import (
"fmt"
"github.com/hdt3213/godis/lib/utils" "github.com/hdt3213/godis/lib/utils"
"github.com/hdt3213/godis/redis/connection" "github.com/hdt3213/godis/redis/connection"
"github.com/hdt3213/godis/redis/protocol" "github.com/hdt3213/godis/redis/protocol"
@@ -73,7 +72,7 @@ func TestRename(t *testing.T) {
result = testDB.Exec(nil, utils.ToCmdLine("ttl", newKey)) result = testDB.Exec(nil, utils.ToCmdLine("ttl", newKey))
intResult, ok := result.(*protocol.IntReply) intResult, ok := result.(*protocol.IntReply)
if !ok { if !ok {
t.Error(fmt.Sprintf("expected int protocol, actually %s", result.ToBytes())) t.Errorf("expected int protocol, actually %s", result.ToBytes())
return return
} }
if intResult.Code <= 0 { if intResult.Code <= 0 {
@@ -97,7 +96,7 @@ func TestRenameNx(t *testing.T) {
result = testDB.Exec(nil, utils.ToCmdLine("ttl", newKey)) result = testDB.Exec(nil, utils.ToCmdLine("ttl", newKey))
intResult, ok := result.(*protocol.IntReply) intResult, ok := result.(*protocol.IntReply)
if !ok { if !ok {
t.Error(fmt.Sprintf("expected int protocol, actually %s", result.ToBytes())) t.Errorf("expected int protocol, actually %s", result.ToBytes())
return return
} }
if intResult.Code <= 0 { if intResult.Code <= 0 {
@@ -117,7 +116,7 @@ func TestTTL(t *testing.T) {
result = testDB.Exec(nil, utils.ToCmdLine("ttl", key)) result = testDB.Exec(nil, utils.ToCmdLine("ttl", key))
intResult, ok := result.(*protocol.IntReply) intResult, ok := result.(*protocol.IntReply)
if !ok { if !ok {
t.Error(fmt.Sprintf("expected int protocol, actually %s", result.ToBytes())) t.Errorf("expected int protocol, actually %s", result.ToBytes())
return return
} }
if intResult.Code <= 0 { if intResult.Code <= 0 {
@@ -135,7 +134,7 @@ func TestTTL(t *testing.T) {
result = testDB.Exec(nil, utils.ToCmdLine("PTTL", key)) result = testDB.Exec(nil, utils.ToCmdLine("PTTL", key))
intResult, ok = result.(*protocol.IntReply) intResult, ok = result.(*protocol.IntReply)
if !ok { if !ok {
t.Error(fmt.Sprintf("expected int protocol, actually %s", result.ToBytes())) t.Errorf("expected int protocol, actually %s", result.ToBytes())
return return
} }
if intResult.Code <= 0 { if intResult.Code <= 0 {
@@ -168,7 +167,7 @@ func TestExpireAt(t *testing.T) {
result = testDB.Exec(nil, utils.ToCmdLine("ttl", key)) result = testDB.Exec(nil, utils.ToCmdLine("ttl", key))
intResult, ok := result.(*protocol.IntReply) intResult, ok := result.(*protocol.IntReply)
if !ok { if !ok {
t.Error(fmt.Sprintf("expected int protocol, actually %s", result.ToBytes())) t.Errorf("expected int protocol, actually %s", result.ToBytes())
return return
} }
if intResult.Code <= 0 { if intResult.Code <= 0 {
@@ -182,7 +181,7 @@ func TestExpireAt(t *testing.T) {
result = testDB.Exec(nil, utils.ToCmdLine("ttl", key)) result = testDB.Exec(nil, utils.ToCmdLine("ttl", key))
intResult, ok = result.(*protocol.IntReply) intResult, ok = result.(*protocol.IntReply)
if !ok { if !ok {
t.Error(fmt.Sprintf("expected int protocol, actually %s", result.ToBytes())) t.Errorf("expected int protocol, actually %s", result.ToBytes())
return return
} }
if intResult.Code <= 0 { if intResult.Code <= 0 {
@@ -210,7 +209,7 @@ func TestExpiredTime(t *testing.T) {
result = testDB.Exec(nil, utils.ToCmdLine("ttl", key)) result = testDB.Exec(nil, utils.ToCmdLine("ttl", key))
intResult, ok := result.(*protocol.IntReply) intResult, ok := result.(*protocol.IntReply)
if !ok { if !ok {
t.Error(fmt.Sprintf("expected int protocol, actually %s", result.ToBytes())) t.Errorf("expected int protocol, actually %s", result.ToBytes())
return return
} }
if intResult.Code < 0 || intResult.Code > 2 { if intResult.Code < 0 || intResult.Code > 2 {
@@ -221,7 +220,7 @@ func TestExpiredTime(t *testing.T) {
asserts.AssertIntReply(t, result, int(time.Now().Add(2*time.Second).Unix())) asserts.AssertIntReply(t, result, int(time.Now().Add(2*time.Second).Unix()))
intResult, ok = result.(*protocol.IntReply) intResult, ok = result.(*protocol.IntReply)
if !ok { if !ok {
t.Error(fmt.Sprintf("expected int protocol, actually %s", result.ToBytes())) t.Errorf("expected int protocol, actually %s", result.ToBytes())
return return
} }
if intResult.Code <= 0 { if intResult.Code <= 0 {
@@ -233,7 +232,7 @@ func TestExpiredTime(t *testing.T) {
asserts.AssertIntReply(t, result, int(time.Now().Add(2*time.Second).UnixMilli())) asserts.AssertIntReply(t, result, int(time.Now().Add(2*time.Second).UnixMilli()))
intResult, ok = result.(*protocol.IntReply) intResult, ok = result.(*protocol.IntReply)
if !ok { if !ok {
t.Error(fmt.Sprintf("expected int protocol, actually %s", result.ToBytes())) t.Errorf("expected int protocol, actually %s", result.ToBytes())
return return
} }
if intResult.Code <= 0 { if intResult.Code <= 0 {

View File

@@ -1,7 +1,6 @@
package database package database
import ( import (
"fmt"
"github.com/hdt3213/godis/lib/utils" "github.com/hdt3213/godis/lib/utils"
"github.com/hdt3213/godis/redis/protocol" "github.com/hdt3213/godis/redis/protocol"
"github.com/hdt3213/godis/redis/protocol/asserts" "github.com/hdt3213/godis/redis/protocol/asserts"
@@ -37,11 +36,11 @@ func TestSAdd(t *testing.T) {
result = testDB.Exec(nil, utils.ToCmdLine("SMembers", key)) result = testDB.Exec(nil, utils.ToCmdLine("SMembers", key))
multiBulk, ok := result.(*protocol.MultiBulkReply) multiBulk, ok := result.(*protocol.MultiBulkReply)
if !ok { if !ok {
t.Error(fmt.Sprintf("expected bulk protocol, actually %s", result.ToBytes())) t.Errorf("expected bulk protocol, actually %s", result.ToBytes())
return return
} }
if len(multiBulk.Args) != size { if len(multiBulk.Args) != size {
t.Error(fmt.Sprintf("expected %d elements, actually %d", size, len(multiBulk.Args))) t.Errorf("expected %d elements, actually %d", size, len(multiBulk.Args))
return return
} }
} }
@@ -84,7 +83,7 @@ func TestSPop(t *testing.T) {
resultSpop := testDB.Exec(nil, utils.ToCmdLine("spop", key, strconv.FormatInt(int64(count), 10))) resultSpop := testDB.Exec(nil, utils.ToCmdLine("spop", key, strconv.FormatInt(int64(count), 10)))
multiBulk, ok := resultSpop.(*protocol.MultiBulkReply) multiBulk, ok := resultSpop.(*protocol.MultiBulkReply)
if !ok { if !ok {
t.Error(fmt.Sprintf("expected bulk protocol, actually %s", resultSpop.ToBytes())) t.Errorf("expected bulk protocol, actually %s", resultSpop.ToBytes())
return return
} }
removedSize := len(multiBulk.Args) removedSize := len(multiBulk.Args)
@@ -220,7 +219,7 @@ func TestSRandMember(t *testing.T) {
result := testDB.Exec(nil, utils.ToCmdLine("SRandMember", key)) result := testDB.Exec(nil, utils.ToCmdLine("SRandMember", key))
br, ok := result.(*protocol.BulkReply) br, ok := result.(*protocol.BulkReply)
if !ok && len(br.Arg) > 0 { if !ok && len(br.Arg) > 0 {
t.Error(fmt.Sprintf("expected bulk protocol, actually %s", result.ToBytes())) t.Errorf("expected bulk protocol, actually %s", result.ToBytes())
return return
} }
@@ -228,7 +227,7 @@ func TestSRandMember(t *testing.T) {
asserts.AssertMultiBulkReplySize(t, result, 10) asserts.AssertMultiBulkReplySize(t, result, 10)
multiBulk, ok := result.(*protocol.MultiBulkReply) multiBulk, ok := result.(*protocol.MultiBulkReply)
if !ok { if !ok {
t.Error(fmt.Sprintf("expected bulk protocol, actually %s", result.ToBytes())) t.Errorf("expected bulk protocol, actually %s", result.ToBytes())
return return
} }
m := make(map[string]struct{}) m := make(map[string]struct{})
@@ -236,7 +235,7 @@ func TestSRandMember(t *testing.T) {
m[string(arg)] = struct{}{} m[string(arg)] = struct{}{}
} }
if len(m) != 10 { if len(m) != 10 {
t.Error(fmt.Sprintf("expected 10 members, actually %d", len(m))) t.Errorf("expected 10 members, actually %d", len(m))
return return
} }

View File

@@ -94,11 +94,11 @@ func TestSet(t *testing.T) {
actual = testDB.Exec(nil, utils.ToCmdLine("TTL", key)) actual = testDB.Exec(nil, utils.ToCmdLine("TTL", key))
intResult, ok := actual.(*protocol.IntReply) intResult, ok := actual.(*protocol.IntReply)
if !ok { if !ok {
t.Error(fmt.Sprintf("expected int protocol, actually %s", actual.ToBytes())) t.Errorf("expected int protocol, actually %s", actual.ToBytes())
return return
} }
if intResult.Code <= 0 || intResult.Code > 1000 { if intResult.Code <= 0 || intResult.Code > 1000 {
t.Error(fmt.Sprintf("expected int between [0, 1000], actually %d", intResult.Code)) t.Errorf("expected int between [0, 1000], actually %d", intResult.Code)
return return
} }
@@ -111,11 +111,11 @@ func TestSet(t *testing.T) {
actual = testDB.Exec(nil, utils.ToCmdLine("TTL", key)) actual = testDB.Exec(nil, utils.ToCmdLine("TTL", key))
intResult, ok = actual.(*protocol.IntReply) intResult, ok = actual.(*protocol.IntReply)
if !ok { if !ok {
t.Error(fmt.Sprintf("expected int protocol, actually %s", actual.ToBytes())) t.Errorf("expected int protocol, actually %s", actual.ToBytes())
return return
} }
if intResult.Code <= 0 || intResult.Code > 1000 { if intResult.Code <= 0 || intResult.Code > 1000 {
t.Error(fmt.Sprintf("expected int between [0, 1000], actually %d", intResult.Code)) t.Errorf("expected int between [0, 1000], actually %d", intResult.Code)
return return
} }
} }
@@ -150,11 +150,11 @@ func TestSetEX(t *testing.T) {
actual = testDB.Exec(nil, utils.ToCmdLine("TTL", key)) actual = testDB.Exec(nil, utils.ToCmdLine("TTL", key))
intResult, ok := actual.(*protocol.IntReply) intResult, ok := actual.(*protocol.IntReply)
if !ok { if !ok {
t.Error(fmt.Sprintf("expected int protocol, actually %s", actual.ToBytes())) t.Errorf("expected int protocol, actually %s", actual.ToBytes())
return return
} }
if intResult.Code <= 0 || intResult.Code > 1000 { if intResult.Code <= 0 || intResult.Code > 1000 {
t.Error(fmt.Sprintf("expected int between [0, 1000], actually %d", intResult.Code)) t.Errorf("expected int between [0, 1000], actually %d", intResult.Code)
return return
} }
} }
@@ -171,11 +171,11 @@ func TestPSetEX(t *testing.T) {
actual = testDB.Exec(nil, utils.ToCmdLine("PTTL", key)) actual = testDB.Exec(nil, utils.ToCmdLine("PTTL", key))
intResult, ok := actual.(*protocol.IntReply) intResult, ok := actual.(*protocol.IntReply)
if !ok { if !ok {
t.Error(fmt.Sprintf("expected int protocol, actually %s", actual.ToBytes())) t.Errorf("expected int protocol, actually %s", actual.ToBytes())
return return
} }
if intResult.Code <= 0 || intResult.Code > 1000000 { if intResult.Code <= 0 || intResult.Code > 1000000 {
t.Error(fmt.Sprintf("expected int between [0, 1000], actually %d", intResult.Code)) t.Errorf("expected int between [0, 1000], actually %d", intResult.Code)
return return
} }
} }
@@ -252,7 +252,7 @@ func TestIncr(t *testing.T) {
expected := -i - 1 expected := -i - 1
bulk, ok := actual.(*protocol.BulkReply) bulk, ok := actual.(*protocol.BulkReply)
if !ok { if !ok {
t.Error(fmt.Sprintf("expected bulk protocol, actually %s", actual.ToBytes())) t.Errorf("expected bulk protocol, actually %s", actual.ToBytes())
return return
} }
val, err := strconv.ParseFloat(string(bulk.Arg), 10) val, err := strconv.ParseFloat(string(bulk.Arg), 10)
@@ -283,7 +283,7 @@ func TestDecr(t *testing.T) {
expected := -i - 1 expected := -i - 1
bulk, ok := actual.(*protocol.BulkReply) bulk, ok := actual.(*protocol.BulkReply)
if !ok { if !ok {
t.Error(fmt.Sprintf("expected bulk protocol, actually %s", actual.ToBytes())) t.Errorf("expected bulk protocol, actually %s", actual.ToBytes())
return return
} }
val, err := strconv.ParseFloat(string(bulk.Arg), 10) val, err := strconv.ParseFloat(string(bulk.Arg), 10)
@@ -316,11 +316,11 @@ func TestGetEX(t *testing.T) {
actual = testDB.Exec(nil, utils.ToCmdLine("TTL", key)) actual = testDB.Exec(nil, utils.ToCmdLine("TTL", key))
intResult, ok := actual.(*protocol.IntReply) intResult, ok := actual.(*protocol.IntReply)
if !ok { if !ok {
t.Error(fmt.Sprintf("expected int protocol, actually %s", actual.ToBytes())) t.Errorf("expected int protocol, actually %s", actual.ToBytes())
return return
} }
if intResult.Code <= 0 || intResult.Code > 1000 { if intResult.Code <= 0 || intResult.Code > 1000 {
t.Error(fmt.Sprintf("expected int between [0, 1000], actually %d", intResult.Code)) t.Errorf("expected int between [0, 1000], actually %d", intResult.Code)
return return
} }
@@ -330,11 +330,11 @@ func TestGetEX(t *testing.T) {
actual = testDB.Exec(nil, utils.ToCmdLine("TTL", key)) actual = testDB.Exec(nil, utils.ToCmdLine("TTL", key))
intResult, ok = actual.(*protocol.IntReply) intResult, ok = actual.(*protocol.IntReply)
if !ok { if !ok {
t.Error(fmt.Sprintf("expected int protocol, actually %s", actual.ToBytes())) t.Errorf("expected int protocol, actually %s", actual.ToBytes())
return return
} }
if intResult.Code != -1 { if intResult.Code != -1 {
t.Error(fmt.Sprintf("expected int equals -1, actually %d", intResult.Code)) t.Errorf("expected int equals -1, actually %d", intResult.Code)
return return
} }
@@ -345,11 +345,11 @@ func TestGetEX(t *testing.T) {
actual = testDB.Exec(nil, utils.ToCmdLine("TTL", key)) actual = testDB.Exec(nil, utils.ToCmdLine("TTL", key))
intResult, ok = actual.(*protocol.IntReply) intResult, ok = actual.(*protocol.IntReply)
if !ok { if !ok {
t.Error(fmt.Sprintf("expected int protocol, actually %s", actual.ToBytes())) t.Errorf("expected int protocol, actually %s", actual.ToBytes())
return return
} }
if intResult.Code <= 0 || intResult.Code > 1000000 { if intResult.Code <= 0 || intResult.Code > 1000000 {
t.Error(fmt.Sprintf("expected int between [0, 1000000], actually %d", intResult.Code)) t.Errorf("expected int between [0, 1000000], actually %d", intResult.Code)
return return
} }
} }