mirror of
https://github.com/HDT3213/godis.git
synced 2025-10-05 16:57:06 +08:00
add some unittest
This commit is contained in:
@@ -5,7 +5,7 @@ import (
|
||||
"github.com/hdt3213/godis/redis/reply"
|
||||
)
|
||||
|
||||
// TODO: support multiplex slots
|
||||
// Rename renames a key, the origin and the destination must within the same slot
|
||||
func Rename(cluster *Cluster, c redis.Connection, args [][]byte) redis.Reply {
|
||||
if len(args) != 3 {
|
||||
return reply.MakeErrReply("ERR wrong number of arguments for 'rename' command")
|
||||
|
64
cluster/rename_test.go
Normal file
64
cluster/rename_test.go
Normal file
@@ -0,0 +1,64 @@
|
||||
package cluster
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/hdt3213/godis/db"
|
||||
"github.com/hdt3213/godis/lib/utils"
|
||||
"github.com/hdt3213/godis/redis/reply"
|
||||
"github.com/hdt3213/godis/redis/reply/asserts"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestRename(t *testing.T) {
|
||||
testDB := testCluster.db
|
||||
db.FlushAll(testDB, [][]byte{})
|
||||
key := utils.RandString(10)
|
||||
value := utils.RandString(10)
|
||||
newKey := key + utils.RandString(2)
|
||||
db.Set(testDB, utils.ToBytesList(key, value, "ex", "1000"))
|
||||
result := Rename(testCluster, nil, utils.ToBytesList("RENAME", key, newKey))
|
||||
if _, ok := result.(*reply.OkReply); !ok {
|
||||
t.Error("expect ok")
|
||||
return
|
||||
}
|
||||
result = db.Exists(testDB, utils.ToBytesList(key))
|
||||
asserts.AssertIntReply(t, result, 0)
|
||||
result = db.Exists(testDB, utils.ToBytesList(newKey))
|
||||
asserts.AssertIntReply(t, result, 1)
|
||||
// check ttl
|
||||
result = db.TTL(testDB, utils.ToBytesList(newKey))
|
||||
intResult, ok := result.(*reply.IntReply)
|
||||
if !ok {
|
||||
t.Error(fmt.Sprintf("expected int reply, actually %s", result.ToBytes()))
|
||||
return
|
||||
}
|
||||
if intResult.Code <= 0 {
|
||||
t.Errorf("expected ttl more than 0, actual: %d", intResult.Code)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func TestRenameNx(t *testing.T) {
|
||||
testDB := testCluster.db
|
||||
db.FlushAll(testDB, [][]byte{})
|
||||
key := utils.RandString(10)
|
||||
value := utils.RandString(10)
|
||||
newKey := key + utils.RandString(2)
|
||||
db.Set(testCluster.db, utils.ToBytesList(key, value, "ex", "1000"))
|
||||
result := RenameNx(testCluster, nil, utils.ToBytesList("RENAMENX", key, newKey))
|
||||
asserts.AssertIntReply(t, result, 1)
|
||||
result = db.Exists(testDB, utils.ToBytesList(key))
|
||||
asserts.AssertIntReply(t, result, 0)
|
||||
result = db.Exists(testDB, utils.ToBytesList(newKey))
|
||||
asserts.AssertIntReply(t, result, 1)
|
||||
result = db.TTL(testDB, utils.ToBytesList(newKey))
|
||||
intResult, ok := result.(*reply.IntReply)
|
||||
if !ok {
|
||||
t.Error(fmt.Sprintf("expected int reply, actually %s", result.ToBytes()))
|
||||
return
|
||||
}
|
||||
if intResult.Code <= 0 {
|
||||
t.Errorf("expected ttl more than 0, actual: %d", intResult.Code)
|
||||
return
|
||||
}
|
||||
}
|
@@ -1,6 +1,7 @@
|
||||
package dict
|
||||
|
||||
import (
|
||||
"github.com/hdt3213/godis/lib/utils"
|
||||
"strconv"
|
||||
"sync"
|
||||
"testing"
|
||||
@@ -267,3 +268,14 @@ func TestConcurrentRandomKey(t *testing.T) {
|
||||
t.Errorf("get duplicated keys in result")
|
||||
}
|
||||
}
|
||||
|
||||
func TestConcurrentDict_Keys(t *testing.T) {
|
||||
d := MakeConcurrent(0)
|
||||
size := 10
|
||||
for i := 0; i < size; i++ {
|
||||
d.Put(utils.RandString(5), utils.RandString(5))
|
||||
}
|
||||
if len(d.Keys()) != size {
|
||||
t.Errorf("expect %d keys, actual: %d", size, len(d.Keys()))
|
||||
}
|
||||
}
|
39
datastruct/dict/simple_test.go
Normal file
39
datastruct/dict/simple_test.go
Normal file
@@ -0,0 +1,39 @@
|
||||
package dict
|
||||
|
||||
import (
|
||||
"github.com/hdt3213/godis/lib/utils"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestSimpleDict_Keys(t *testing.T) {
|
||||
d := MakeSimple()
|
||||
size := 10
|
||||
for i := 0; i < size; i++ {
|
||||
d.Put(utils.RandString(5), utils.RandString(5))
|
||||
}
|
||||
if len(d.Keys()) != size {
|
||||
t.Errorf("expect %d keys, actual: %d", size, len(d.Keys()))
|
||||
}
|
||||
}
|
||||
|
||||
func TestSimpleDict_PutIfExists(t *testing.T) {
|
||||
d := MakeSimple()
|
||||
key := utils.RandString(5)
|
||||
val := key + "1"
|
||||
ret := d.PutIfExists(key, val)
|
||||
if ret != 0 {
|
||||
t.Error("expect 0")
|
||||
return
|
||||
}
|
||||
d.Put(key, val)
|
||||
val = key + "2"
|
||||
ret = d.PutIfExists(key, val)
|
||||
if ret != 1 {
|
||||
t.Error("expect 1")
|
||||
return
|
||||
}
|
||||
if v, _ := d.Get(key); v != val {
|
||||
t.Error("wrong value")
|
||||
return
|
||||
}
|
||||
}
|
@@ -85,4 +85,19 @@ func TestGeoDist(t *testing.T) {
|
||||
if dist < 166.274 || dist > 166.275 {
|
||||
t.Errorf("expected 166.274, actual: %f", dist)
|
||||
}
|
||||
|
||||
result = GeoDist(testDB, utils.ToBytesList(key, pos1, pos2, "m"))
|
||||
bulkReply, ok = result.(*reply.BulkReply)
|
||||
if !ok {
|
||||
t.Error(fmt.Sprintf("expected bulk reply, actually %s", result.ToBytes()))
|
||||
return
|
||||
}
|
||||
dist, err = strconv.ParseFloat(string(bulkReply.Arg), 10)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
if dist < 166274 || dist > 166275 {
|
||||
t.Errorf("expected 166274, actual: %f", dist)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user