Skip to content

Commit

Permalink
add some int tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alicebob committed Feb 18, 2024
1 parent 8c35b9c commit 5a61f61
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 14 deletions.
35 changes: 21 additions & 14 deletions cmd_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ func commandsSet(m *Miniredis) {
m.srv.Register("SCARD", m.cmdScard)
m.srv.Register("SDIFF", m.cmdSdiff)
m.srv.Register("SDIFFSTORE", m.cmdSdiffstore)
m.srv.Register("SINTERCARD", m.cmdSintercard)
m.srv.Register("SINTER", m.cmdSinter)
m.srv.Register("SINTERSTORE", m.cmdSinterstore)
m.srv.Register("SINTERCARD", m.cmdSintercard)
m.srv.Register("SISMEMBER", m.cmdSismember)
m.srv.Register("SMEMBERS", m.cmdSmembers)
m.srv.Register("SMISMEMBER", m.cmdSmismember)
Expand Down Expand Up @@ -234,37 +234,45 @@ func (m *Miniredis) cmdSintercard(c *server.Peer, cmd string, args []string) {
return
}

var keys []string
var limit int
opts := struct {
keys []string
limit int
}{}

numKeys, err := strconv.Atoi(args[0])
if err != nil {
setDirty(c)
c.WriteError(msgInvalidInt)
c.WriteError("ERR numkeys should be greater than 0")
return
}

args = args[1:]
if len(args) < numKeys {
if numKeys < 1 {
setDirty(c)
c.WriteError(msgSyntaxError)
c.WriteError("ERR numkeys should be greater than 0")
return
}
if numKeys <= 0 {

args = args[1:]
if len(args) < numKeys {
setDirty(c)
c.WriteError("ERR at least 1 input key is needed for SINTERCARD")
c.WriteError("ERR Number of keys can't be greater than number of args")
return
}
keys = args[:numKeys]
opts.keys = args[:numKeys]

args = args[numKeys:]
if len(args) == 2 && strings.ToLower(args[0]) == "limit" {
limit, err = strconv.Atoi(args[1])
l, err := strconv.Atoi(args[1])
if err != nil {
setDirty(c)
c.WriteError(msgInvalidInt)
return
}
if l < 0 {
setDirty(c)
c.WriteError(msgLimitIsNegative)
return
}
opts.limit = l
} else if len(args) > 0 {
setDirty(c)
c.WriteError(msgSyntaxError)
Expand All @@ -274,12 +282,11 @@ func (m *Miniredis) cmdSintercard(c *server.Peer, cmd string, args []string) {
withTx(m, c, func(c *server.Peer, ctx *connCtx) {
db := m.db(ctx.selectedDB)

count, err := db.setIntercard(keys, limit)
count, err := db.setIntercard(opts.keys, opts.limit)
if err != nil {
c.WriteError(err.Error())
return
}

c.WriteInt(count)
})
}
Expand Down
40 changes: 40 additions & 0 deletions integration/set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,46 @@ func TestSetSinter(t *testing.T) {
})
}

func TestSetSintercard(t *testing.T) {
skip(t)
testRaw(t, func(c *client) {
c.Do("SADD", "s1", "aap", "noot", "mies")
c.Do("SADD", "s2", "noot", "mies", "vuur")
c.Do("SADD", "s3", "mies", "wim")
c.Do("SINTERCARD", "1", "s1")
c.Do("SINTERCARD", "2", "s1", "s2")
c.Do("SINTERCARD", "3", "s1", "s2", "s3")
c.Do("SINTERCARD", "1", "nosuch")
c.Do("SINTERCARD", "5", "s1", "nosuch", "s2", "nosuch", "s3")
c.Do("SINTERCARD", "2", "s1", "s1")

c.Do("SINTERCARD", "1", "s1", "LIMIT", "1")
c.Do("SINTERCARD", "2", "s1", "s2", "LIMIT", "0")
c.Do("SINTERCARD", "2", "s1", "s2", "LIMIT", "1")
c.Do("SINTERCARD", "2", "s1", "s2", "LIMIT", "2")
c.Do("SINTERCARD", "2", "s1", "s2", "LIMIT", "3")

// failure cases
c.Error("wrong number", "SINTERCARD")
c.Error("wrong number", "SINTERCARD", "0")
c.Error("wrong number", "SINTERCARD", "")
c.Error("wrong number", "SINTERCARD", "s1")
c.Error("greater than 0", "SINTERCARD", "s1", "s2")
c.Error("greater than 0", "SINTERCARD", "-2", "s1", "s2")
c.Error("greater than 0", "SINTERCARD", "-1", "s1", "s2")
c.Error("greater than 0", "SINTERCARD", "0", "s1", "s2")
c.Error("syntax error", "SINTERCARD", "1", "s1", "s2")
c.Error("can't be greater", "SINTERCARD", "3", "s1", "s2")
// Wrong type
c.Do("SET", "str", "I am a string")
c.Error("wrong kind", "SINTERCARD", "2", "s1", "str")
c.Error("wrong kind", "SINTERCARD", "2", "nosuch", "str")
c.Error("wrong kind", "SINTERCARD", "2", "str", "nosuch")
c.Error("wrong kind", "SINTERCARD", "2", "str", "s1")
c.Error("can't be negative", "SINTERCARD", "2", "s1", "s2", "LIMIT", "-1")
})
}

func TestSetSunion(t *testing.T) {
skip(t)
testRaw(t, func(c *client) {
Expand Down
1 change: 1 addition & 0 deletions redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ const (
msgRankIsZero = "ERR RANK can't be zero: use 1 to start from the first match, 2 from the second ... or use negative to start from the end of the list"
msgCountIsNegative = "ERR COUNT can't be negative"
msgMaxLengthIsNegative = "ERR MAXLEN can't be negative"
msgLimitIsNegative = "ERR LIMIT can't be negative"
msgMemorySubcommand = "ERR unknown subcommand '%s'. Try MEMORY HELP."
)

Expand Down

0 comments on commit 5a61f61

Please sign in to comment.