From 58bc97f48a1427f6990cb2da0ba108013d53fbec Mon Sep 17 00:00:00 2001 From: Michael Hoisie Date: Tue, 1 Jun 2010 11:39:37 -0700 Subject: [PATCH] Fixed Zadd, improved error handling of Hgetall, added tests for sorted sets --- redis.go | 8 ++++--- redis_test.go | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 3 deletions(-) diff --git a/redis.go b/redis.go index 4f18f00..92e8d5b 100644 --- a/redis.go +++ b/redis.go @@ -540,7 +540,6 @@ func (client *Client) Lrange(key string, start int, end int) ([][]byte, os.Error func (client *Client) Ltrim(key string, start int, end int) os.Error { _, err := client.sendCommand("LTRIM", []string{key, strconv.Itoa(start), strconv.Itoa(end)}) - if err != nil { return err } @@ -550,7 +549,6 @@ func (client *Client) Ltrim(key string, start int, end int) os.Error { func (client *Client) Lindex(key string, index int) ([]byte, os.Error) { res, err := client.sendCommand("LINDEX", []string{key, strconv.Itoa(index)}) - if err != nil { return nil, err } @@ -774,7 +772,7 @@ func (client *Client) Srandmember(key string) ([]byte, os.Error) { // sorted set commands func (client *Client) Zadd(key string, value []byte, score float64) (bool, os.Error) { - res, err := client.sendCommand("ZADD", []string{key, string(value), strconv.Ftoa64(score, 'f', -1)}) + res, err := client.sendCommand("ZADD", []string{key, strconv.Ftoa64(score, 'f', -1), string(value)}) if err != nil { return false, err } @@ -1217,7 +1215,11 @@ func (client *Client) Hgetall(key string, val interface{}) os.Error { if err != nil { return err } + data := res.([][]byte) + if data == nil || len(data) == 0 { + return RedisError("Key `" + key + "` does not exist") + } err = writeToContainer(data, reflect.NewValue(val)) if err != nil { return err diff --git a/redis_test.go b/redis_test.go index 7204294..aca427a 100644 --- a/redis_test.go +++ b/redis_test.go @@ -210,6 +210,65 @@ func verifyHash(t *testing.T, key string, expected map[string][]byte) { } } +func TestSortedSet(t *testing.T) { + svals := []string{"a", "b", "c", "d", "e"} + ranks := []float64{0.0, 1.0, 2.0, 3.0, 4.0} + vals := make([][]byte, len(svals)) + for i := 0; i < len(svals); i++ { + vals[i] = []byte(svals[i]) + _, err := client.Zadd("zs", vals[i], ranks[i]) + if err != nil { + t.Fatal("zdd failed" + err.String()) + } + } + + card, err := client.Zcard("zs") + if err != nil { + t.Fatal("zcard failed" + err.String()) + } + if card != 5 { + t.Fatal("zcard failed", card) + } + for i := 0; i <= 4; i++ { + data, _ := client.Zrange("zs", 0, i) + if !reflect.DeepEqual(data, vals[0:i+1]) { + t.Fatal("zrange failed") + } + } + for i := 0; i <= 4; i++ { + data, _ := client.Zrangebyscore("zs", 0, float64(i)) + if !reflect.DeepEqual(data, vals[0:i+1]) { + t.Fatal("zrangebyscore failed") + } + } + + //clean up + _, err = client.Zrem("zs", []byte("a")) + if err != nil { + t.Fatal("zrem failed" + err.String()) + } + + _, err = client.Zremrangebyrank("zs", 0, 1) + if err != nil { + t.Fatal("zremrangebynrank failed" + err.String()) + } + + _, err = client.Zremrangebyscore("zs", 3, 4) + if err != nil { + t.Fatal("zremrangebyscore failed" + err.String()) + } + + card, err = client.Zcard("zs") + if err != nil { + t.Fatal("zcard failed" + err.String()) + } + if card != 0 { + t.Fatal("zcard failed", card) + } + + client.Del("zs") +} + type tt struct { A, B, C, D, E string } @@ -260,6 +319,12 @@ func TestHash(t *testing.T) { t.Fatal("verifyHash Hgetall failed") } + err = client.Hgetall("hdne", &test5) + if err == nil { + t.Fatal("should be an error") + + } + client.Del("h") client.Del("h2") client.Del("h3")