mirror of
https://github.com/HDT3213/godis.git
synced 2025-10-06 09:17:10 +08:00
refactor command table
This commit is contained in:
106
keys_test.go
106
keys_test.go
@@ -11,65 +11,65 @@ import (
|
||||
)
|
||||
|
||||
func TestExists(t *testing.T) {
|
||||
FlushAll(testDB, [][]byte{})
|
||||
execFlushAll(testDB, [][]byte{})
|
||||
key := utils.RandString(10)
|
||||
value := utils.RandString(10)
|
||||
Set(testDB, utils.ToBytesList(key, value))
|
||||
result := Exists(testDB, utils.ToBytesList(key))
|
||||
execSet(testDB, utils.ToBytesList(key, value))
|
||||
result := execExists(testDB, utils.ToBytesList(key))
|
||||
asserts.AssertIntReply(t, result, 1)
|
||||
key = utils.RandString(10)
|
||||
result = Exists(testDB, utils.ToBytesList(key))
|
||||
result = execExists(testDB, utils.ToBytesList(key))
|
||||
asserts.AssertIntReply(t, result, 0)
|
||||
}
|
||||
|
||||
func TestType(t *testing.T) {
|
||||
FlushAll(testDB, [][]byte{})
|
||||
execFlushAll(testDB, [][]byte{})
|
||||
key := utils.RandString(10)
|
||||
value := utils.RandString(10)
|
||||
Set(testDB, utils.ToBytesList(key, value))
|
||||
result := Type(testDB, utils.ToBytesList(key))
|
||||
execSet(testDB, utils.ToBytesList(key, value))
|
||||
result := execType(testDB, utils.ToBytesList(key))
|
||||
asserts.AssertStatusReply(t, result, "string")
|
||||
|
||||
Del(testDB, utils.ToBytesList(key))
|
||||
result = Type(testDB, utils.ToBytesList(key))
|
||||
testDB.Remove(key)
|
||||
result = execType(testDB, utils.ToBytesList(key))
|
||||
asserts.AssertStatusReply(t, result, "none")
|
||||
RPush(testDB, utils.ToBytesList(key, value))
|
||||
result = Type(testDB, utils.ToBytesList(key))
|
||||
execRPush(testDB, utils.ToBytesList(key, value))
|
||||
result = execType(testDB, utils.ToBytesList(key))
|
||||
asserts.AssertStatusReply(t, result, "list")
|
||||
|
||||
Del(testDB, utils.ToBytesList(key))
|
||||
HSet(testDB, utils.ToBytesList(key, key, value))
|
||||
result = Type(testDB, utils.ToBytesList(key))
|
||||
testDB.Remove(key)
|
||||
execHSet(testDB, utils.ToBytesList(key, key, value))
|
||||
result = execType(testDB, utils.ToBytesList(key))
|
||||
asserts.AssertStatusReply(t, result, "hash")
|
||||
|
||||
Del(testDB, utils.ToBytesList(key))
|
||||
SAdd(testDB, utils.ToBytesList(key, value))
|
||||
result = Type(testDB, utils.ToBytesList(key))
|
||||
testDB.Remove(key)
|
||||
execSAdd(testDB, utils.ToBytesList(key, value))
|
||||
result = execType(testDB, utils.ToBytesList(key))
|
||||
asserts.AssertStatusReply(t, result, "set")
|
||||
|
||||
Del(testDB, utils.ToBytesList(key))
|
||||
ZAdd(testDB, utils.ToBytesList(key, "1", value))
|
||||
result = Type(testDB, utils.ToBytesList(key))
|
||||
testDB.Remove(key)
|
||||
execZAdd(testDB, utils.ToBytesList(key, "1", value))
|
||||
result = execType(testDB, utils.ToBytesList(key))
|
||||
asserts.AssertStatusReply(t, result, "zset")
|
||||
}
|
||||
|
||||
func TestRename(t *testing.T) {
|
||||
FlushAll(testDB, [][]byte{})
|
||||
execFlushAll(testDB, [][]byte{})
|
||||
key := utils.RandString(10)
|
||||
value := utils.RandString(10)
|
||||
newKey := key + utils.RandString(2)
|
||||
Set(testDB, utils.ToBytesList(key, value, "ex", "1000"))
|
||||
result := Rename(testDB, utils.ToBytesList(key, newKey))
|
||||
execSet(testDB, utils.ToBytesList(key, value, "ex", "1000"))
|
||||
result := execRename(testDB, utils.ToBytesList(key, newKey))
|
||||
if _, ok := result.(*reply.OkReply); !ok {
|
||||
t.Error("expect ok")
|
||||
return
|
||||
}
|
||||
result = Exists(testDB, utils.ToBytesList(key))
|
||||
result = execExists(testDB, utils.ToBytesList(key))
|
||||
asserts.AssertIntReply(t, result, 0)
|
||||
result = Exists(testDB, utils.ToBytesList(newKey))
|
||||
result = execExists(testDB, utils.ToBytesList(newKey))
|
||||
asserts.AssertIntReply(t, result, 1)
|
||||
// check ttl
|
||||
result = TTL(testDB, utils.ToBytesList(newKey))
|
||||
result = execTTL(testDB, utils.ToBytesList(newKey))
|
||||
intResult, ok := result.(*reply.IntReply)
|
||||
if !ok {
|
||||
t.Error(fmt.Sprintf("expected int reply, actually %s", result.ToBytes()))
|
||||
@@ -82,18 +82,18 @@ func TestRename(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestRenameNx(t *testing.T) {
|
||||
FlushAll(testDB, [][]byte{})
|
||||
execFlushAll(testDB, [][]byte{})
|
||||
key := utils.RandString(10)
|
||||
value := utils.RandString(10)
|
||||
newKey := key + utils.RandString(2)
|
||||
Set(testDB, utils.ToBytesList(key, value, "ex", "1000"))
|
||||
result := RenameNx(testDB, utils.ToBytesList(key, newKey))
|
||||
execSet(testDB, utils.ToBytesList(key, value, "ex", "1000"))
|
||||
result := execRenameNx(testDB, utils.ToBytesList(key, newKey))
|
||||
asserts.AssertIntReply(t, result, 1)
|
||||
result = Exists(testDB, utils.ToBytesList(key))
|
||||
result = execExists(testDB, utils.ToBytesList(key))
|
||||
asserts.AssertIntReply(t, result, 0)
|
||||
result = Exists(testDB, utils.ToBytesList(newKey))
|
||||
result = execExists(testDB, utils.ToBytesList(newKey))
|
||||
asserts.AssertIntReply(t, result, 1)
|
||||
result = TTL(testDB, utils.ToBytesList(newKey))
|
||||
result = execTTL(testDB, utils.ToBytesList(newKey))
|
||||
intResult, ok := result.(*reply.IntReply)
|
||||
if !ok {
|
||||
t.Error(fmt.Sprintf("expected int reply, actually %s", result.ToBytes()))
|
||||
@@ -106,14 +106,14 @@ func TestRenameNx(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestTTL(t *testing.T) {
|
||||
FlushAll(testDB, [][]byte{})
|
||||
execFlushAll(testDB, [][]byte{})
|
||||
key := utils.RandString(10)
|
||||
value := utils.RandString(10)
|
||||
Set(testDB, utils.ToBytesList(key, value))
|
||||
execSet(testDB, utils.ToBytesList(key, value))
|
||||
|
||||
result := Expire(testDB, utils.ToBytesList(key, "1000"))
|
||||
result := execExpire(testDB, utils.ToBytesList(key, "1000"))
|
||||
asserts.AssertIntReply(t, result, 1)
|
||||
result = TTL(testDB, utils.ToBytesList(key))
|
||||
result = execTTL(testDB, utils.ToBytesList(key))
|
||||
intResult, ok := result.(*reply.IntReply)
|
||||
if !ok {
|
||||
t.Error(fmt.Sprintf("expected int reply, actually %s", result.ToBytes()))
|
||||
@@ -124,14 +124,14 @@ func TestTTL(t *testing.T) {
|
||||
return
|
||||
}
|
||||
|
||||
result = Persist(testDB, utils.ToBytesList(key))
|
||||
result = execPersist(testDB, utils.ToBytesList(key))
|
||||
asserts.AssertIntReply(t, result, 1)
|
||||
result = TTL(testDB, utils.ToBytesList(key))
|
||||
result = execTTL(testDB, utils.ToBytesList(key))
|
||||
asserts.AssertIntReply(t, result, -1)
|
||||
|
||||
result = PExpire(testDB, utils.ToBytesList(key, "1000000"))
|
||||
result = execPExpire(testDB, utils.ToBytesList(key, "1000000"))
|
||||
asserts.AssertIntReply(t, result, 1)
|
||||
result = PTTL(testDB, utils.ToBytesList(key))
|
||||
result = execPTTL(testDB, utils.ToBytesList(key))
|
||||
intResult, ok = result.(*reply.IntReply)
|
||||
if !ok {
|
||||
t.Error(fmt.Sprintf("expected int reply, actually %s", result.ToBytes()))
|
||||
@@ -144,15 +144,15 @@ func TestTTL(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestExpireAt(t *testing.T) {
|
||||
FlushAll(testDB, [][]byte{})
|
||||
execFlushAll(testDB, [][]byte{})
|
||||
key := utils.RandString(10)
|
||||
value := utils.RandString(10)
|
||||
Set(testDB, utils.ToBytesList(key, value))
|
||||
execSet(testDB, utils.ToBytesList(key, value))
|
||||
|
||||
expireAt := time.Now().Add(time.Minute).Unix()
|
||||
result := ExpireAt(testDB, utils.ToBytesList(key, strconv.FormatInt(expireAt, 10)))
|
||||
result := execExpireAt(testDB, utils.ToBytesList(key, strconv.FormatInt(expireAt, 10)))
|
||||
asserts.AssertIntReply(t, result, 1)
|
||||
result = TTL(testDB, utils.ToBytesList(key))
|
||||
result = execTTL(testDB, utils.ToBytesList(key))
|
||||
intResult, ok := result.(*reply.IntReply)
|
||||
if !ok {
|
||||
t.Error(fmt.Sprintf("expected int reply, actually %s", result.ToBytes()))
|
||||
@@ -164,9 +164,9 @@ func TestExpireAt(t *testing.T) {
|
||||
}
|
||||
|
||||
expireAt = time.Now().Add(time.Minute).Unix()
|
||||
result = PExpireAt(testDB, utils.ToBytesList(key, strconv.FormatInt(expireAt*1000, 10)))
|
||||
result = execPExpireAt(testDB, utils.ToBytesList(key, strconv.FormatInt(expireAt*1000, 10)))
|
||||
asserts.AssertIntReply(t, result, 1)
|
||||
result = TTL(testDB, utils.ToBytesList(key))
|
||||
result = execTTL(testDB, utils.ToBytesList(key))
|
||||
intResult, ok = result.(*reply.IntReply)
|
||||
if !ok {
|
||||
t.Error(fmt.Sprintf("expected int reply, actually %s", result.ToBytes()))
|
||||
@@ -179,17 +179,17 @@ func TestExpireAt(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestKeys(t *testing.T) {
|
||||
FlushAll(testDB, [][]byte{})
|
||||
execFlushAll(testDB, [][]byte{})
|
||||
key := utils.RandString(10)
|
||||
value := utils.RandString(10)
|
||||
Set(testDB, utils.ToBytesList(key, value))
|
||||
Set(testDB, utils.ToBytesList("a:"+key, value))
|
||||
Set(testDB, utils.ToBytesList("b:"+key, value))
|
||||
execSet(testDB, utils.ToBytesList(key, value))
|
||||
execSet(testDB, utils.ToBytesList("a:"+key, value))
|
||||
execSet(testDB, utils.ToBytesList("b:"+key, value))
|
||||
|
||||
result := Keys(testDB, utils.ToBytesList("*"))
|
||||
result := execKeys(testDB, utils.ToBytesList("*"))
|
||||
asserts.AssertMultiBulkReplySize(t, result, 3)
|
||||
result = Keys(testDB, utils.ToBytesList("a:*"))
|
||||
result = execKeys(testDB, utils.ToBytesList("a:*"))
|
||||
asserts.AssertMultiBulkReplySize(t, result, 1)
|
||||
result = Keys(testDB, utils.ToBytesList("?:*"))
|
||||
result = execKeys(testDB, utils.ToBytesList("?:*"))
|
||||
asserts.AssertMultiBulkReplySize(t, result, 2)
|
||||
}
|
||||
|
Reference in New Issue
Block a user