Skip to content

Commit

Permalink
fix hash unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
zhs committed Dec 29, 2018
1 parent 81ad33e commit 74e0363
Show file tree
Hide file tree
Showing 3 changed files with 163 additions and 9 deletions.
158 changes: 151 additions & 7 deletions command/hashes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,10 +338,154 @@ func TestHMSet(t *testing.T) {
clearHashes(t, key)
}

func TestHExists(t *testing.T) {}
func TestHIncrBy(t *testing.T) {}
func TestHIncrByFloat(t *testing.T) {}
func TestHKeys(t *testing.T) {}
func TestHStrLen(t *testing.T) {}
func TestHVals(t *testing.T) {}
func TestHMSlot(t *testing.T) {}
func TestHExists(t *testing.T) {
// init
key := "hash-key"
initHashes(t, key, 3)

// case 1
ctx := ContextTest("hexists", key, "1")
Call(ctx)
lines := ctxLines(ctx.Out)
assert.Equal(t, ":1", lines[0])

//case 2
ctx = ContextTest("hdel", key, "1")
Call(ctx)
lines = ctxLines(ctx.Out)
assert.Equal(t, ":1", lines[0])
ctx = ContextTest("hexists", key, "1")
Call(ctx)
lines = ctxLines(ctx.Out)
assert.Equal(t, ":0", lines[0])

//case 3
ctx = ContextTest("hexists", "hash_no_exists_key", "1")
Call(ctx)
lines = ctxLines(ctx.Out)
assert.Equal(t, ":0", lines[0])
clearList(t, key)
}

func TestHIncrBy(t *testing.T) {
// init
key := "hash-key"

// case 1
ctx := ContextTest("hincrby", key, "one", "1")
Call(ctx)
lines := ctxLines(ctx.Out)
assert.Equal(t, ":1", lines[0])

// case 2
ctx = ContextTest("hincrby", key, "one", "-2")
Call(ctx)
lines = ctxLines(ctx.Out)
assert.Equal(t, ":-1", lines[0])
clearList(t, key)

}

func TestHIncrByFloat(t *testing.T) {
// init
key := "hash-key"

// case 1
ctx := ContextTest("hincrbyfloat", key, "one", "1.1")
Call(ctx)
lines := ctxLines(ctx.Out)
assert.Equal(t, "1.1", lines[1])

// case 2
ctx = ContextTest("hincrbyfloat", key, "one", "-2.2")
Call(ctx)
lines = ctxLines(ctx.Out)
assert.Equal(t, "-1.1", lines[1])
clearList(t, key)

}

func TestHKeys(t *testing.T) {
// init
key := "hash-key"
initHashes(t, key, 3)

// case 1
ctx := ContextTest("hkeys", key)
Call(ctx)
lines := ctxLines(ctx.Out)
assert.Equal(t, "*3", lines[0])
assert.Equal(t, "1", lines[2])
assert.Equal(t, "2", lines[4])
assert.Equal(t, "3", lines[6])

// case 2
ctx = ContextTest("hdel", key, "1")
Call(ctx)
lines = ctxLines(ctx.Out)
assert.Equal(t, ":1", lines[0])
ctx = ContextTest("hkeys", key)
Call(ctx)
lines = ctxLines(ctx.Out)
assert.Equal(t, "*2", lines[0])
assert.Equal(t, "2", lines[2])
assert.Equal(t, "3", lines[4])

clearList(t, key)

}
func TestHStrLen(t *testing.T) {
// init
key := "hash-key"

// case 1
ctx := ContextTest("hset", key, "1", "abc")
Call(ctx)
lines := ctxLines(ctx.Out)
assert.Equal(t, ":1", lines[0])
ctx = ContextTest("hstrlen", key, "1")
Call(ctx)
lines = ctxLines(ctx.Out)
assert.Equal(t, ":3", lines[0])

clearList(t, key)
}
func TestHVals(t *testing.T) {
// init
key := "hash-key"
initHashes(t, key, 3)

// case 1
ctx := ContextTest("hvals", key)
Call(ctx)
lines := ctxLines(ctx.Out)
assert.Equal(t, "bar", lines[2])
assert.Equal(t, "bar", lines[4])
assert.Equal(t, "bar", lines[6])

clearList(t, key)

}
func TestHMSlot(t *testing.T) {
// init
key := "hash-key"

// case 1
ctx := ContextTest("hmslot", key, "20")
Call(ctx)
lines := ctxLines(ctx.Out)
assert.Equal(t, "-ERR key not found", lines[0])

//case 2
ctx = ContextTest("hset", key, "1", "abc")
Call(ctx)
lines = ctxLines(ctx.Out)
assert.Equal(t, ":1", lines[0])
ctx = ContextTest("hmslot", key, "20")
Call(ctx)
lines = ctxLines(ctx.Out)
assert.Equal(t, "+OK", lines[0])

clearList(t, key)

}
4 changes: 3 additions & 1 deletion db/hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,9 @@ func (hash *Hash) HMSet(fields, values [][]byte) error {

// HMSlot sets meta slot num
func (hash *Hash) HMSlot(metaSlot int64) error {

if !hash.exists {
return ErrKeyNotFound
}
if err := hash.autoUpdateSlot(metaSlot); err != nil {
return err
}
Expand Down
10 changes: 9 additions & 1 deletion db/hash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func compareGetHash(want, get *Hash) error {
case want.meta.MetaSlot != get.meta.MetaSlot:
return fmt.Errorf("meta.MeyaSlot not equal, want=%v, get=%v", want.meta.MetaSlot, get.meta.MetaSlot)
case want.meta.Type != get.meta.Type:
return fmt.Errorf("meta.Type not equal, want=%v, get=%v", want.meta.MetaSlot, get.meta.MetaSlot)
return fmt.Errorf("meta.Type not equal, want=%v, get=%v", want.meta.Type, get.meta.Type)
case want.exists != get.exists:
return fmt.Errorf("exists not equal, want=%v, get=%v", want.exists, get.exists)
}
Expand Down Expand Up @@ -926,10 +926,18 @@ func TestHashHMSet(t *testing.T) {
}

func TestHashHMSlot(t *testing.T) {
hash, txn, err := getHash(t, []byte("TestHashHMSlot"))
assert.NoError(t, err)
assert.NotNil(t, txn)
assert.NotNil(t, hash)

hash.HSet([]byte("TestHashMSlotFiled1"), []byte("TestHashHMSlotValue1"))
txn.Commit(context.TODO())

type args struct {
metaSlot int64
}

type want struct {
len int64
}
Expand Down

0 comments on commit 74e0363

Please sign in to comment.