diff --git a/command/hashes_test.go b/command/hashes_test.go index 7eeaec5a..1525648d 100644 --- a/command/hashes_test.go +++ b/command/hashes_test.go @@ -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) + +} diff --git a/db/hash.go b/db/hash.go index 78bfcab0..792bb421 100644 --- a/db/hash.go +++ b/db/hash.go @@ -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 } diff --git a/db/hash_test.go b/db/hash_test.go index b34c2413..593a3180 100644 --- a/db/hash_test.go +++ b/db/hash_test.go @@ -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) } @@ -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 }