-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: command - better encapsulation and simpler tests
- Loading branch information
Showing
121 changed files
with
2,038 additions
and
2,498 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package conn_test | ||
package conn | ||
|
||
import ( | ||
"testing" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,37 @@ | ||
package conn | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/nalgeon/redka/internal/parser" | ||
"github.com/nalgeon/redka/internal/redis" | ||
) | ||
|
||
const ( | ||
PONG = "PONG" | ||
) | ||
|
||
// Returns PONG if no argument is provided, otherwise return a copy of the argument as a bulk | ||
// https://redis.io/commands/ping | ||
type Ping struct { | ||
redis.BaseCmd | ||
Parts []string | ||
} | ||
|
||
|
||
func ParsePing(b redis.BaseCmd) (*Ping, error) { | ||
cmd := &Ping{BaseCmd: b} | ||
err := parser.New( | ||
parser.Strings(&cmd.Parts), | ||
).Required(0).Run(cmd.Args()) | ||
if err != nil { | ||
return cmd, err | ||
} | ||
return cmd, nil | ||
} | ||
|
||
func (c *Ping) Run(w redis.Writer, _ redis.Redka) (any, error) { | ||
if len(c.Parts) == 0 { | ||
w.WriteAny(PONG) | ||
return PONG, nil | ||
} | ||
out := strings.Join(c.Parts, " ") | ||
w.WriteAny(out) | ||
return out, nil | ||
} | ||
package conn | ||
|
||
import ( | ||
"github.com/nalgeon/redka/internal/parser" | ||
"github.com/nalgeon/redka/internal/redis" | ||
) | ||
|
||
const ( | ||
PONG = "PONG" | ||
) | ||
|
||
// Returns the server's liveliness response. | ||
// https://redis.io/commands/ping | ||
type Ping struct { | ||
redis.BaseCmd | ||
message string | ||
} | ||
|
||
func ParsePing(b redis.BaseCmd) (*Ping, error) { | ||
cmd := &Ping{BaseCmd: b} | ||
err := parser.New( | ||
parser.String(&cmd.message), | ||
).Required(0).Run(cmd.Args()) | ||
if err != nil { | ||
return cmd, err | ||
} | ||
return cmd, nil | ||
} | ||
|
||
func (c *Ping) Run(w redis.Writer, _ redis.Redka) (any, error) { | ||
if c.message == "" { | ||
w.WriteAny(PONG) | ||
return PONG, nil | ||
} | ||
w.WriteBulkString(c.message) | ||
return c.message, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,89 +1,75 @@ | ||
package conn_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/nalgeon/redka/internal/command" | ||
"github.com/nalgeon/redka/internal/command/conn" | ||
"github.com/nalgeon/redka/internal/redis" | ||
"github.com/nalgeon/redka/internal/testx" | ||
) | ||
|
||
func TestPingParse(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
args [][]byte | ||
want []string | ||
err error | ||
}{ | ||
{ | ||
name: "ping", | ||
args: command.BuildArgs("ping"), | ||
want: []string(nil), | ||
err: nil, | ||
}, | ||
{ | ||
name: "ping hello", | ||
args: command.BuildArgs("ping", "hello"), | ||
want: []string{"hello"}, | ||
err: nil, | ||
}, | ||
{ | ||
name: "ping one two", | ||
args: command.BuildArgs("ping", "one", "two"), | ||
want: []string{"one", "two"}, | ||
err: nil, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
cmd, err := command.Parse(test.args) | ||
testx.AssertEqual(t, err, test.err) | ||
if err == nil { | ||
testx.AssertEqual(t, cmd.(*conn.Ping).Parts, test.want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestPingExec(t *testing.T) { | ||
db, red := getDB(t) | ||
defer db.Close() | ||
|
||
tests := []struct { | ||
name string | ||
cmd *conn.Ping | ||
res any | ||
out string | ||
}{ | ||
{ | ||
name: "ping", | ||
cmd: command.MustParse[*conn.Ping]("ping"), | ||
res: "PONG", | ||
out: "PONG", | ||
}, | ||
{ | ||
name: "ping hello", | ||
cmd: command.MustParse[*conn.Ping]("ping hello"), | ||
res: "hello", | ||
out: "hello", | ||
}, | ||
{ | ||
name: "ping one two", | ||
cmd: command.MustParse[*conn.Ping]("ping one two"), | ||
res: "one two", | ||
out: "one two", | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
conn := redis.NewFakeConn() | ||
res, err := test.cmd.Run(conn, red) | ||
testx.AssertNoErr(t, err) | ||
testx.AssertEqual(t, res, test.res) | ||
testx.AssertEqual(t, conn.Out(), test.out) | ||
}) | ||
} | ||
} | ||
package conn | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/nalgeon/redka/internal/redis" | ||
"github.com/nalgeon/redka/internal/testx" | ||
) | ||
|
||
func TestPingParse(t *testing.T) { | ||
tests := []struct { | ||
cmd string | ||
want string | ||
err error | ||
}{ | ||
{ | ||
cmd: "ping", | ||
want: "", | ||
err: nil, | ||
}, | ||
{ | ||
cmd: "ping hello", | ||
want: "hello", | ||
err: nil, | ||
}, | ||
{ | ||
cmd: "ping one two", | ||
want: "", | ||
err: redis.ErrSyntaxError, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.cmd, func(t *testing.T) { | ||
cmd, err := redis.Parse(ParsePing, test.cmd) | ||
testx.AssertEqual(t, err, test.err) | ||
if err == nil { | ||
testx.AssertEqual(t, cmd.message, test.want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestPingExec(t *testing.T) { | ||
db, red := getDB(t) | ||
defer db.Close() | ||
|
||
tests := []struct { | ||
cmd string | ||
res any | ||
out string | ||
}{ | ||
{ | ||
cmd: "ping", | ||
res: "PONG", | ||
out: "PONG", | ||
}, | ||
{ | ||
cmd: "ping hello", | ||
res: "hello", | ||
out: "hello", | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.cmd, func(t *testing.T) { | ||
conn := redis.NewFakeConn() | ||
cmd := redis.MustParse(ParsePing, 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) | ||
}) | ||
} | ||
} |
Oops, something went wrong.