Skip to content

Commit

Permalink
Merge pull request #376 from WKBae/srandmember_count_nil
Browse files Browse the repository at this point in the history
Return empty array for SRANDMEMBER on nonexistent key
  • Loading branch information
alicebob authored May 27, 2024
2 parents 49d1b75 + c007f55 commit 80eb7b3
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
4 changes: 4 additions & 0 deletions cmd_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,10 @@ func (m *Miniredis) cmdSrandmember(c *server.Peer, cmd string, args []string) {
db := m.db(ctx.selectedDB)

if !db.exists(key) {
if withCount {
c.WriteLen(0)
return
}
c.WriteNull()
return
}
Expand Down
6 changes: 6 additions & 0 deletions cmd_set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,12 @@ func TestSrandmember(t *testing.T) {
"SRANDMEMBER", "nosuch",
)

// a nonexisting key with count
mustDo(t, c,
"SRANDMEMBER", "nosuch", "1",
proto.Strings(),
)

t.Run("errors", func(t *testing.T) {
s.SetAdd("chk", "aap", "noot")
s.Set("str", "value")
Expand Down
3 changes: 2 additions & 1 deletion integration/set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,8 @@ func TestSetSrandmember(t *testing.T) {
c.Do("SRANDMEMBER", "s", "-5")

c.Do("SRANDMEMBER", "s", "0")
c.Do("SPOP", "nosuch")
c.Do("SRANDMEMBER", "nosuch")
c.Do("SRANDMEMBER", "nosuch", "1")

// failure cases
c.Error("wrong number", "SRANDMEMBER")
Expand Down

0 comments on commit 80eb7b3

Please sign in to comment.