Better OUTPUT tests

This commit is contained in:
tidwall 2022-09-27 14:19:57 -07:00
parent 77b4efa55f
commit e1df4dbf78
3 changed files with 20 additions and 18 deletions

View File

@ -7,35 +7,31 @@ import (
"github.com/tidwall/resp" "github.com/tidwall/resp"
) )
func (s *Server) cmdOutput(msg *Message) (res resp.Value, err error) { // OUTPUT [resp|json]
func (s *Server) cmdOUTPUT(msg *Message) (resp.Value, error) {
start := time.Now() start := time.Now()
vs := msg.Args[1:]
var arg string
var ok bool
if len(vs) != 0 { args := msg.Args
if _, arg, ok = tokenval(vs); !ok || arg == "" { switch len(args) {
return NOMessage, errInvalidNumberOfArguments case 1:
if msg.OutputType == JSON {
return resp.StringValue(`{"ok":true,"output":"json","elapsed":` +
time.Since(start).String() + `}`), nil
} }
return resp.StringValue("resp"), nil
case 2:
// Setting the original message output type will be picked up by the // Setting the original message output type will be picked up by the
// server prior to the next command being executed. // server prior to the next command being executed.
switch strings.ToLower(arg) { switch strings.ToLower(args[1]) {
default: default:
return NOMessage, errInvalidArgument(arg) return retrerr(errInvalidArgument(args[1]))
case "json": case "json":
msg.OutputType = JSON msg.OutputType = JSON
case "resp": case "resp":
msg.OutputType = RESP msg.OutputType = RESP
} }
return OKMessage(msg, start), nil return OKMessage(msg, start), nil
}
// return the output
switch msg.OutputType {
default: default:
return NOMessage, nil return retrerr(errInvalidNumberOfArguments)
case JSON:
return resp.StringValue(`{"ok":true,"output":"json","elapsed":` + time.Since(start).String() + `}`), nil
case RESP:
return resp.StringValue("resp"), nil
} }
} }

View File

@ -1215,7 +1215,7 @@ func (s *Server) command(msg *Message, client *Client) (
case "keys": case "keys":
res, err = s.cmdKEYS(msg) res, err = s.cmdKEYS(msg)
case "output": case "output":
res, err = s.cmdOutput(msg) res, err = s.cmdOUTPUT(msg)
case "aof": case "aof":
res, err = s.cmdAOF(msg) res, err = s.cmdAOF(msg)
case "aofmd5": case "aofmd5":

View File

@ -18,8 +18,14 @@ func subTestClient(g *testGroup) {
func client_OUTPUT_test(mc *mockServer) error { func client_OUTPUT_test(mc *mockServer) error {
if err := mc.DoBatch( if err := mc.DoBatch(
// tests removal of "elapsed" member. // tests removal of "elapsed" member.
Do("OUTPUT", "json", "yaml").Err(`wrong number of arguments for 'output' command`),
Do("OUTPUT", "json").Str(`{"ok":true}`), Do("OUTPUT", "json").Str(`{"ok":true}`),
Do("OUTPUT").JSON().Str(`{"ok":true,"output":"json"}`),
Do("OUTPUT").Str(`resp`), // this is due to the internal Do test
Do("OUTPUT", "resp").OK(), Do("OUTPUT", "resp").OK(),
Do("OUTPUT", "yaml").Err(`invalid argument 'yaml'`),
Do("OUTPUT").Str(`resp`),
Do("OUTPUT").JSON().Str(`{"ok":true,"output":"json"}`),
); err != nil { ); err != nil {
return err return err
} }