refactor: command - better encapsulation and simpler tests

This commit is contained in:
Anton
2024-04-30 15:54:27 +05:00
parent 21951bc7e2
commit bd8208bae4
121 changed files with 2038 additions and 2498 deletions

View File

@@ -1,10 +1,8 @@
package string_test
package string
import (
"testing"
"github.com/nalgeon/redka/internal/command"
str "github.com/nalgeon/redka/internal/command/string"
"github.com/nalgeon/redka/internal/core"
"github.com/nalgeon/redka/internal/redis"
"github.com/nalgeon/redka/internal/testx"
@@ -12,38 +10,33 @@ import (
func TestMGetParse(t *testing.T) {
tests := []struct {
name string
args [][]byte
cmd string
want []string
err error
}{
{
name: "mget",
args: command.BuildArgs("mget"),
cmd: "mget",
want: nil,
err: redis.ErrInvalidArgNum,
},
{
name: "mget name",
args: command.BuildArgs("mget", "name"),
cmd: "mget name",
want: []string{"name"},
err: nil,
},
{
name: "mget name age",
args: command.BuildArgs("mget", "name", "age"),
cmd: "mget name age",
want: []string{"name", "age"},
err: nil,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
cmd, err := command.Parse(test.args)
t.Run(test.cmd, func(t *testing.T) {
cmd, err := redis.Parse(ParseMGet, test.cmd)
testx.AssertEqual(t, err, test.err)
if err == nil {
cm := cmd.(*str.MGet)
testx.AssertEqual(t, cm.Keys, test.want)
testx.AssertEqual(t, cmd.keys, test.want)
}
})
}
@@ -57,41 +50,37 @@ func TestMGetExec(t *testing.T) {
_ = db.Str().Set("age", 25)
tests := []struct {
name string
cmd *str.MGet
res any
out string
cmd string
res any
out string
}{
{
name: "single key",
cmd: command.MustParse[*str.MGet]("mget name"),
res: []core.Value{core.Value("alice")},
out: "1,alice",
cmd: "mget name",
res: []core.Value{core.Value("alice")},
out: "1,alice",
},
{
name: "multiple keys",
cmd: command.MustParse[*str.MGet]("mget name age"),
res: []core.Value{core.Value("alice"), core.Value("25")},
out: "2,alice,25",
cmd: "mget name age",
res: []core.Value{core.Value("alice"), core.Value("25")},
out: "2,alice,25",
},
{
name: "some not found",
cmd: command.MustParse[*str.MGet]("mget name city age"),
res: []core.Value{core.Value("alice"), core.Value(nil), core.Value("25")},
out: "3,alice,(nil),25",
cmd: "mget name city age",
res: []core.Value{core.Value("alice"), core.Value(nil), core.Value("25")},
out: "3,alice,(nil),25",
},
{
name: "all not found",
cmd: command.MustParse[*str.MGet]("mget one two"),
res: []core.Value{core.Value(nil), core.Value(nil)},
out: "2,(nil),(nil)",
cmd: "mget one two",
res: []core.Value{core.Value(nil), core.Value(nil)},
out: "2,(nil),(nil)",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
t.Run(test.cmd, func(t *testing.T) {
conn := redis.NewFakeConn()
res, err := test.cmd.Run(conn, red)
cmd := redis.MustParse(ParseMGet, 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)