Skip to content

Commit

Permalink
Fixed Zadd, improved error handling of Hgetall, added tests for sorte…
Browse files Browse the repository at this point in the history
…d sets
  • Loading branch information
Michael Hoisie committed Jun 1, 2010
1 parent 48ceadb commit 58bc97f
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 3 deletions.
8 changes: 5 additions & 3 deletions redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand Down
65 changes: 65 additions & 0 deletions redis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit 58bc97f

Please sign in to comment.