-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from crowdriff/lists
Add list commands
- Loading branch information
Showing
2 changed files
with
115 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")) | ||
}) | ||
}) | ||
}) |