Skip to content

Commit

Permalink
command: select tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nalgeon committed Aug 9, 2024
1 parent 3037996 commit 89b3e5b
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 7 deletions.
11 changes: 4 additions & 7 deletions internal/command/conn/select.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,8 @@ import (
"github.com/nalgeon/redka/internal/redis"
)

const (
RESPONSE = "OK"
)

// Returns the server's liveliness response.
// Changes the selected database.
// SELECT index
// https://redis.io/commands/select
type Select struct {
redis.BaseCmd
Expand All @@ -28,6 +25,6 @@ func ParseSelect(b redis.BaseCmd) (Select, error) {
}

func (c Select) Run(w redis.Writer, _ redis.Redka) (any, error) {
w.WriteBulkString(RESPONSE)
return RESPONSE, nil
w.WriteString("OK")
return true, nil
}
72 changes: 72 additions & 0 deletions internal/command/conn/select_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package conn

import (
"testing"

"github.com/nalgeon/redka/internal/redis"
"github.com/nalgeon/redka/internal/testx"
)

func TestSelectParse(t *testing.T) {
tests := []struct {
cmd string
want Select
err error
}{
{
cmd: "select",
want: Select{},
err: redis.ErrInvalidArgNum,
},
{
cmd: "select 5",
want: Select{index: 5},
err: nil,
},
{
cmd: "select five",
want: Select{},
err: redis.ErrInvalidInt,
},
}

for _, test := range tests {
t.Run(test.cmd, func(t *testing.T) {
cmd, err := redis.Parse(ParseSelect, test.cmd)
testx.AssertEqual(t, err, test.err)
if err == nil {
testx.AssertEqual(t, cmd.index, test.want.index)
} else {
testx.AssertEqual(t, cmd, Select{})
}
})
}
}

func TestSelectExec(t *testing.T) {
db, red := getDB(t)
defer db.Close()

tests := []struct {
cmd string
res any
out string
}{
{
cmd: "select 5",
res: true,
out: "OK",
},
}

for _, test := range tests {
t.Run(test.cmd, func(t *testing.T) {
conn := redis.NewFakeConn()
cmd := redis.MustParse(ParseSelect, test.cmd)
res, err := cmd.Run(conn, red)
testx.AssertNoErr(t, err)
testx.AssertEqual(t, res, test.res)
testx.AssertEqual(t, conn.Out(), test.out)
})
}
}

0 comments on commit 89b3e5b

Please sign in to comment.