Skip to content

Commit

Permalink
Merge pull request redis#156 from go-redis/fix/SRandMemberN
Browse files Browse the repository at this point in the history
Add SRandMemberN. Fixes redis#155.
  • Loading branch information
vmihailenco committed Aug 26, 2015
2 parents fd51850 + 9ead45f commit 5dfdaaf
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 deletions.
8 changes: 8 additions & 0 deletions commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -944,12 +944,20 @@ func (c *commandable) SPop(key string) *StringCmd {
return cmd
}

// Redis `SRANDMEMBER key` command.
func (c *commandable) SRandMember(key string) *StringCmd {
cmd := NewStringCmd("SRANDMEMBER", key)
c.Process(cmd)
return cmd
}

// Redis `SRANDMEMBER key count` command.
func (c *commandable) SRandMemberN(key string, count int64) *StringSliceCmd {
cmd := NewStringSliceCmd("SRANDMEMBER", key, formatInt(count))
c.Process(cmd)
return cmd
}

func (c *commandable) SRem(key string, members ...string) *IntCmd {
args := make([]interface{}, 2+len(members))
args[0] = "SREM"
Expand Down
30 changes: 17 additions & 13 deletions commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1733,21 +1733,25 @@ var _ = Describe("Commands", func() {
Expect(sMembers.Val()).To(HaveLen(2))
})

It("should SRandMember", func() {
sAdd := client.SAdd("set", "one")
Expect(sAdd.Err()).NotTo(HaveOccurred())
sAdd = client.SAdd("set", "two")
Expect(sAdd.Err()).NotTo(HaveOccurred())
sAdd = client.SAdd("set", "three")
Expect(sAdd.Err()).NotTo(HaveOccurred())
It("should SRandMember and SRandMemberN", func() {
err := client.SAdd("set", "one").Err()
Expect(err).NotTo(HaveOccurred())
err = client.SAdd("set", "two").Err()
Expect(err).NotTo(HaveOccurred())
err = client.SAdd("set", "three").Err()
Expect(err).NotTo(HaveOccurred())

sRandMember := client.SRandMember("set")
Expect(sRandMember.Err()).NotTo(HaveOccurred())
Expect(sRandMember.Val()).NotTo(Equal(""))
members, err := client.SMembers("set").Result()
Expect(err).NotTo(HaveOccurred())
Expect(members).To(HaveLen(3))

sMembers := client.SMembers("set")
Expect(sMembers.Err()).NotTo(HaveOccurred())
Expect(sMembers.Val()).To(HaveLen(3))
member, err := client.SRandMember("set").Result()
Expect(err).NotTo(HaveOccurred())
Expect(member).NotTo(Equal(""))

members, err = client.SRandMemberN("set", 2).Result()
Expect(err).NotTo(HaveOccurred())
Expect(members).To(HaveLen(2))
})

It("should SRem", func() {
Expand Down

0 comments on commit 5dfdaaf

Please sign in to comment.