mirror of
https://github.com/HDT3213/godis.git
synced 2025-10-06 17:26:52 +08:00
support multi transaction
This commit is contained in:
160
hash_test.go
160
hash_test.go
@@ -2,8 +2,7 @@ package godis
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/hdt3213/godis/datastruct/utils"
|
||||
utils2 "github.com/hdt3213/godis/lib/utils"
|
||||
"github.com/hdt3213/godis/lib/utils"
|
||||
"github.com/hdt3213/godis/redis/reply"
|
||||
"github.com/hdt3213/godis/redis/reply/asserts"
|
||||
"strconv"
|
||||
@@ -11,17 +10,17 @@ import (
|
||||
)
|
||||
|
||||
func TestHSet(t *testing.T) {
|
||||
execFlushAll(testDB, [][]byte{})
|
||||
testDB.Flush()
|
||||
size := 100
|
||||
|
||||
// test hset
|
||||
key := utils2.RandString(10)
|
||||
key := utils.RandString(10)
|
||||
values := make(map[string][]byte, size)
|
||||
for i := 0; i < size; i++ {
|
||||
value := utils2.RandString(10)
|
||||
value := utils.RandString(10)
|
||||
field := strconv.Itoa(i)
|
||||
values[field] = []byte(value)
|
||||
result := execHSet(testDB, utils2.ToBytesList(key, field, value))
|
||||
result := testDB.Exec(nil, utils.ToCmdLine("hset", key, field, value))
|
||||
if intResult, _ := result.(*reply.IntReply); intResult.Code != int64(1) {
|
||||
t.Error(fmt.Sprintf("expected %d, actually %d", 1, intResult.Code))
|
||||
}
|
||||
@@ -29,67 +28,67 @@ func TestHSet(t *testing.T) {
|
||||
|
||||
// test hget and hexists
|
||||
for field, v := range values {
|
||||
actual := execHGet(testDB, utils2.ToBytesList(key, field))
|
||||
actual := testDB.Exec(nil, utils.ToCmdLine("hget", key, field))
|
||||
expected := reply.MakeBulkReply(v)
|
||||
if !utils.BytesEquals(actual.ToBytes(), expected.ToBytes()) {
|
||||
t.Error(fmt.Sprintf("expected %s, actually %s", string(expected.ToBytes()), string(actual.ToBytes())))
|
||||
}
|
||||
actual = execHExists(testDB, utils2.ToBytesList(key, field))
|
||||
actual = testDB.Exec(nil, utils.ToCmdLine("hexists", key, field))
|
||||
if intResult, _ := actual.(*reply.IntReply); intResult.Code != int64(1) {
|
||||
t.Error(fmt.Sprintf("expected %d, actually %d", 1, intResult.Code))
|
||||
}
|
||||
}
|
||||
|
||||
// test hlen
|
||||
actual := execHLen(testDB, utils2.ToBytesList(key))
|
||||
actual := testDB.Exec(nil, utils.ToCmdLine("hlen", key))
|
||||
if intResult, _ := actual.(*reply.IntReply); intResult.Code != int64(len(values)) {
|
||||
t.Error(fmt.Sprintf("expected %d, actually %d", len(values), intResult.Code))
|
||||
}
|
||||
}
|
||||
|
||||
func TestHDel(t *testing.T) {
|
||||
execFlushAll(testDB, [][]byte{})
|
||||
testDB.Flush()
|
||||
size := 100
|
||||
|
||||
// set values
|
||||
key := utils2.RandString(10)
|
||||
key := utils.RandString(10)
|
||||
fields := make([]string, size)
|
||||
for i := 0; i < size; i++ {
|
||||
value := utils2.RandString(10)
|
||||
value := utils.RandString(10)
|
||||
field := strconv.Itoa(i)
|
||||
fields[i] = field
|
||||
execHSet(testDB, utils2.ToBytesList(key, field, value))
|
||||
testDB.Exec(nil, utils.ToCmdLine("hset", key, field, value))
|
||||
}
|
||||
|
||||
// test HDel
|
||||
args := []string{key}
|
||||
args = append(args, fields...)
|
||||
actual := execHDel(testDB, utils2.ToBytesList(args...))
|
||||
actual := testDB.Exec(nil, utils.ToCmdLine2("hdel", args...))
|
||||
if intResult, _ := actual.(*reply.IntReply); intResult.Code != int64(len(fields)) {
|
||||
t.Error(fmt.Sprintf("expected %d, actually %d", len(fields), intResult.Code))
|
||||
}
|
||||
|
||||
actual = execHLen(testDB, utils2.ToBytesList(key))
|
||||
actual = testDB.Exec(nil, utils.ToCmdLine("hlen", key))
|
||||
if intResult, _ := actual.(*reply.IntReply); intResult.Code != int64(0) {
|
||||
t.Error(fmt.Sprintf("expected %d, actually %d", 0, intResult.Code))
|
||||
}
|
||||
}
|
||||
|
||||
func TestHMSet(t *testing.T) {
|
||||
execFlushAll(testDB, [][]byte{})
|
||||
testDB.Flush()
|
||||
size := 100
|
||||
|
||||
// test hset
|
||||
key := utils2.RandString(10)
|
||||
key := utils.RandString(10)
|
||||
fields := make([]string, size)
|
||||
values := make([]string, size)
|
||||
setArgs := []string{key}
|
||||
for i := 0; i < size; i++ {
|
||||
fields[i] = utils2.RandString(10)
|
||||
values[i] = utils2.RandString(10)
|
||||
fields[i] = utils.RandString(10)
|
||||
values[i] = utils.RandString(10)
|
||||
setArgs = append(setArgs, fields[i], values[i])
|
||||
}
|
||||
result := execHMSet(testDB, utils2.ToBytesList(setArgs...))
|
||||
result := testDB.Exec(nil, utils.ToCmdLine2("hmset", setArgs...))
|
||||
if _, ok := result.(*reply.OkReply); !ok {
|
||||
t.Error(fmt.Sprintf("expected ok, actually %s", string(result.ToBytes())))
|
||||
}
|
||||
@@ -97,32 +96,32 @@ func TestHMSet(t *testing.T) {
|
||||
// test HMGet
|
||||
getArgs := []string{key}
|
||||
getArgs = append(getArgs, fields...)
|
||||
actual := HMGet(testDB, utils2.ToBytesList(getArgs...))
|
||||
expected := reply.MakeMultiBulkReply(utils2.ToBytesList(values...))
|
||||
actual := testDB.Exec(nil, utils.ToCmdLine2("hmget", getArgs...))
|
||||
expected := reply.MakeMultiBulkReply(utils.ToCmdLine(values...))
|
||||
if !utils.BytesEquals(actual.ToBytes(), expected.ToBytes()) {
|
||||
t.Error(fmt.Sprintf("expected %s, actually %s", string(expected.ToBytes()), string(actual.ToBytes())))
|
||||
}
|
||||
}
|
||||
|
||||
func TestHGetAll(t *testing.T) {
|
||||
execFlushAll(testDB, [][]byte{})
|
||||
testDB.Flush()
|
||||
size := 100
|
||||
key := utils2.RandString(10)
|
||||
key := utils.RandString(10)
|
||||
fields := make([]string, size)
|
||||
valueSet := make(map[string]bool, size)
|
||||
valueMap := make(map[string]string)
|
||||
all := make([]string, 0)
|
||||
for i := 0; i < size; i++ {
|
||||
fields[i] = utils2.RandString(10)
|
||||
value := utils2.RandString(10)
|
||||
fields[i] = utils.RandString(10)
|
||||
value := utils.RandString(10)
|
||||
all = append(all, fields[i], value)
|
||||
valueMap[fields[i]] = value
|
||||
valueSet[value] = true
|
||||
execHSet(testDB, utils2.ToBytesList(key, fields[i], value))
|
||||
execHSet(testDB, utils.ToCmdLine(key, fields[i], value))
|
||||
}
|
||||
|
||||
// test HGetAll
|
||||
result := execHGetAll(testDB, utils2.ToBytesList(key))
|
||||
result := testDB.Exec(nil, utils.ToCmdLine("hgetall", key))
|
||||
multiBulk, ok := result.(*reply.MultiBulkReply)
|
||||
if !ok {
|
||||
t.Error(fmt.Sprintf("expected MultiBulkReply, actually %s", string(result.ToBytes())))
|
||||
@@ -144,7 +143,7 @@ func TestHGetAll(t *testing.T) {
|
||||
}
|
||||
|
||||
// test HKeys
|
||||
result = execHKeys(testDB, utils2.ToBytesList(key))
|
||||
result = testDB.Exec(nil, utils.ToCmdLine("hkeys", key))
|
||||
multiBulk, ok = result.(*reply.MultiBulkReply)
|
||||
if !ok {
|
||||
t.Error(fmt.Sprintf("expected MultiBulkReply, actually %s", string(result.ToBytes())))
|
||||
@@ -160,7 +159,7 @@ func TestHGetAll(t *testing.T) {
|
||||
}
|
||||
|
||||
// test HVals
|
||||
result = execHVals(testDB, utils2.ToBytesList(key))
|
||||
result = testDB.Exec(nil, utils.ToCmdLine("hvals", key))
|
||||
multiBulk, ok = result.(*reply.MultiBulkReply)
|
||||
if !ok {
|
||||
t.Error(fmt.Sprintf("expected MultiBulkReply, actually %s", string(result.ToBytes())))
|
||||
@@ -178,39 +177,110 @@ func TestHGetAll(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestHIncrBy(t *testing.T) {
|
||||
execFlushAll(testDB, [][]byte{})
|
||||
testDB.Flush()
|
||||
|
||||
key := utils2.RandString(10)
|
||||
result := execHIncrBy(testDB, utils2.ToBytesList(key, "a", "1"))
|
||||
key := utils.RandString(10)
|
||||
result := testDB.Exec(nil, utils.ToCmdLine("hincrby", key, "a", "1"))
|
||||
if bulkResult, _ := result.(*reply.BulkReply); string(bulkResult.Arg) != "1" {
|
||||
t.Error(fmt.Sprintf("expected %s, actually %s", "1", string(bulkResult.Arg)))
|
||||
}
|
||||
result = execHIncrBy(testDB, utils2.ToBytesList(key, "a", "1"))
|
||||
result = testDB.Exec(nil, utils.ToCmdLine("hincrby", key, "a", "1"))
|
||||
if bulkResult, _ := result.(*reply.BulkReply); string(bulkResult.Arg) != "2" {
|
||||
t.Error(fmt.Sprintf("expected %s, actually %s", "2", string(bulkResult.Arg)))
|
||||
}
|
||||
|
||||
result = execHIncrByFloat(testDB, utils2.ToBytesList(key, "b", "1.2"))
|
||||
result = testDB.Exec(nil, utils.ToCmdLine("hincrbyfloat", key, "b", "1.2"))
|
||||
if bulkResult, _ := result.(*reply.BulkReply); string(bulkResult.Arg) != "1.2" {
|
||||
t.Error(fmt.Sprintf("expected %s, actually %s", "1.2", string(bulkResult.Arg)))
|
||||
}
|
||||
result = execHIncrByFloat(testDB, utils2.ToBytesList(key, "b", "1.2"))
|
||||
result = testDB.Exec(nil, utils.ToCmdLine("hincrbyfloat", key, "b", "1.2"))
|
||||
if bulkResult, _ := result.(*reply.BulkReply); string(bulkResult.Arg) != "2.4" {
|
||||
t.Error(fmt.Sprintf("expected %s, actually %s", "2.4", string(bulkResult.Arg)))
|
||||
}
|
||||
}
|
||||
|
||||
func TestHSetNX(t *testing.T) {
|
||||
execFlushAll(testDB, [][]byte{})
|
||||
key := utils2.RandString(10)
|
||||
field := utils2.RandString(10)
|
||||
value := utils2.RandString(10)
|
||||
result := execHSetNX(testDB, utils2.ToBytesList(key, field, value))
|
||||
testDB.Flush()
|
||||
key := utils.RandString(10)
|
||||
field := utils.RandString(10)
|
||||
value := utils.RandString(10)
|
||||
result := testDB.Exec(nil, utils.ToCmdLine("hsetnx", key, field, value))
|
||||
asserts.AssertIntReply(t, result, 1)
|
||||
value2 := utils2.RandString(10)
|
||||
result = execHSetNX(testDB, utils2.ToBytesList(key, field, value2))
|
||||
value2 := utils.RandString(10)
|
||||
result = testDB.Exec(nil, utils.ToCmdLine("hsetnx", key, field, value2))
|
||||
asserts.AssertIntReply(t, result, 0)
|
||||
result = execHGet(testDB, utils2.ToBytesList(key, field))
|
||||
result = testDB.Exec(nil, utils.ToCmdLine("hget", key, field))
|
||||
asserts.AssertBulkReply(t, result, value)
|
||||
|
||||
}
|
||||
|
||||
func TestUndoHDel(t *testing.T) {
|
||||
testDB.Flush()
|
||||
key := utils.RandString(10)
|
||||
field := utils.RandString(10)
|
||||
value := utils.RandString(10)
|
||||
|
||||
testDB.Exec(nil, utils.ToCmdLine("hset", key, field, value))
|
||||
cmdLine := utils.ToCmdLine("hdel", key, field)
|
||||
undoCmdLines := undoHDel(testDB, cmdLine[1:])
|
||||
testDB.Exec(nil, cmdLine)
|
||||
for _, cmdLine := range undoCmdLines {
|
||||
testDB.Exec(nil, cmdLine)
|
||||
}
|
||||
result := testDB.Exec(nil, utils.ToCmdLine("hget", key, field))
|
||||
asserts.AssertBulkReply(t, result, value)
|
||||
}
|
||||
|
||||
func TestUndoHSet(t *testing.T) {
|
||||
testDB.Flush()
|
||||
key := utils.RandString(10)
|
||||
field := utils.RandString(10)
|
||||
value := utils.RandString(10)
|
||||
value2 := utils.RandString(10)
|
||||
|
||||
testDB.Exec(nil, utils.ToCmdLine("hset", key, field, value))
|
||||
cmdLine := utils.ToCmdLine("hset", key, field, value2)
|
||||
undoCmdLines := undoHSet(testDB, cmdLine[1:])
|
||||
testDB.Exec(nil, cmdLine)
|
||||
for _, cmdLine := range undoCmdLines {
|
||||
testDB.Exec(nil, cmdLine)
|
||||
}
|
||||
result := testDB.Exec(nil, utils.ToCmdLine("hget", key, field))
|
||||
asserts.AssertBulkReply(t, result, value)
|
||||
}
|
||||
|
||||
func TestUndoHMSet(t *testing.T) {
|
||||
testDB.Flush()
|
||||
key := utils.RandString(10)
|
||||
field1 := utils.RandString(10)
|
||||
field2 := utils.RandString(10)
|
||||
value := utils.RandString(10)
|
||||
value2 := utils.RandString(10)
|
||||
|
||||
testDB.Exec(nil, utils.ToCmdLine("hmset", key, field1, value, field2, value))
|
||||
cmdLine := utils.ToCmdLine("hmset", key, field1, value2, field2, value2)
|
||||
undoCmdLines := undoHMSet(testDB, cmdLine[1:])
|
||||
testDB.Exec(nil, cmdLine)
|
||||
for _, cmdLine := range undoCmdLines {
|
||||
testDB.Exec(nil, cmdLine)
|
||||
}
|
||||
result := testDB.Exec(nil, utils.ToCmdLine("hget", key, field1))
|
||||
asserts.AssertBulkReply(t, result, value)
|
||||
result = testDB.Exec(nil, utils.ToCmdLine("hget", key, field2))
|
||||
asserts.AssertBulkReply(t, result, value)
|
||||
}
|
||||
|
||||
func TestUndoHIncr(t *testing.T) {
|
||||
testDB.Flush()
|
||||
key := utils.RandString(10)
|
||||
field := utils.RandString(10)
|
||||
|
||||
testDB.Exec(nil, utils.ToCmdLine("hset", key, field, "1"))
|
||||
cmdLine := utils.ToCmdLine("hinctby", key, field, "2")
|
||||
undoCmdLines := undoHIncr(testDB, cmdLine[1:])
|
||||
testDB.Exec(nil, cmdLine)
|
||||
for _, cmdLine := range undoCmdLines {
|
||||
testDB.Exec(nil, cmdLine)
|
||||
}
|
||||
result := testDB.Exec(nil, utils.ToCmdLine("hget", key, field))
|
||||
asserts.AssertBulkReply(t, result, "1")
|
||||
}
|
||||
|
Reference in New Issue
Block a user