Skip to content

Commit

Permalink
Merge pull request #7 from crowdriff/lists
Browse files Browse the repository at this point in the history
Add list commands
  • Loading branch information
abh1nav committed Apr 6, 2016
2 parents b49b4e6 + 1e15938 commit 5fb209a
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 0 deletions.
36 changes: 36 additions & 0 deletions wredis_lists.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package wredis

import (
"github.com/garyburd/redigo/redis"
)

// LPush inserts the provided item(s) at the head of the list stored at key. For
// more information, see http://redis.io/commands/lpush.
func (w *Wredis) LPush(key string, items ...string) (int64, error) {
if key == "" {
return int64Error("key cannot be empty")
}
if len(items) == 0 {
return int64Error("must provide at least one item")
}
for _, i := range items {
if i == "" {
return int64Error("an item cannot be empty")
}
}
return w.ExecInt64(func(conn redis.Conn) (int64, error) {
args := redis.Args{}.Add(key).AddFlat(items)
return redis.Int64(conn.Do("LPUSH", args...))
})
}

// RPop removes and returns the last element of the list stored at key. For more
// information, see http://redis.io/commands/rpop.
func (w *Wredis) RPop(key string) (string, error) {
if key == "" {
return stringError("key cannot be empty")
}
return w.ExecString(func(conn redis.Conn) (string, error) {
return redis.String(conn.Do("RPOP", key))
})
}
79 changes: 79 additions & 0 deletions wredis_lists_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package wredis_test

import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

var _ = Describe("WredisLists", func() {

var testList = "wredis::test::list"

Context("LPush", func() {
BeforeEach(func() {
unsafe.Del(testList)
})

It("should return an error when no key provided", func() {
_, err := safe.LPush("")
Ω(err).Should(HaveOccurred())
Ω(err.Error()).Should(Equal("key cannot be empty"))
})

It("should return an error when no items provided", func() {
_, err := safe.LPush(testList)
Ω(err).Should(HaveOccurred())
Ω(err.Error()).Should(Equal("must provide at least one item"))
})

It("should return an error when an item is empty", func() {
_, err := safe.LPush(testList, "test", "")
Ω(err).Should(HaveOccurred())
Ω(err.Error()).Should(Equal("an item cannot be empty"))
})

It("should push an item to a new list", func() {
n, err := safe.LPush(testList, "testing")
Ω(err).ShouldNot(HaveOccurred())
Ω(n).Should(Equal(int64(1)))
})

It("should push multiple items to a new list", func() {
n, err := safe.LPush(testList, "1", "2")
Ω(err).ShouldNot(HaveOccurred())
Ω(n).Should(Equal(int64(2)))

n, err = safe.LPush(testList, "3", "4")
Ω(err).ShouldNot(HaveOccurred())
Ω(n).Should(Equal(int64(4)))
})
})

Context("RPop", func() {
BeforeEach(func() {
unsafe.Del(testList)
})

It("should return an error when no key provided", func() {
_, err := safe.RPop("")
Ω(err).Should(HaveOccurred())
Ω(err.Error()).Should(Equal("key cannot be empty"))
})

It("should return an error when popping from an empty list", func() {
_, err := safe.RPop(testList)
Ω(err).Should(HaveOccurred())
Ω(err.Error()).Should(Equal("redigo: nil returned"))
})

It("should return the last item in a list", func() {
n, err := safe.LPush(testList, "1", "2", "3")
Ω(err).ShouldNot(HaveOccurred())
Ω(n).Should(Equal(int64(n)))

i, err := safe.RPop(testList)
Ω(err).ShouldNot(HaveOccurred())
Ω(i).Should(Equal("1"))
})
})
})

0 comments on commit 5fb209a

Please sign in to comment.