Better coverage BOUNDS

This commit is contained in:
tidwall 2022-09-23 07:51:05 -07:00
parent d61f0bc6c8
commit ef95f04aca
6 changed files with 58 additions and 32 deletions

View File

@ -17,27 +17,30 @@ import (
"github.com/tidwall/tile38/internal/object" "github.com/tidwall/tile38/internal/object"
) )
func (s *Server) cmdBounds(msg *Message) (resp.Value, error) { // BOUNDS key
func (s *Server) cmdBOUNDS(msg *Message) (resp.Value, error) {
start := time.Now() start := time.Now()
vs := msg.Args[1:]
var ok bool // >> Args
var key string
if vs, key, ok = tokenval(vs); !ok || key == "" { args := msg.Args
return NOMessage, errInvalidNumberOfArguments if len(args) != 2 {
} return retrerr(errInvalidNumberOfArguments)
if len(vs) != 0 {
return NOMessage, errInvalidNumberOfArguments
} }
key := args[1]
// >> Operation
col, _ := s.cols.Get(key) col, _ := s.cols.Get(key)
if col == nil { if col == nil {
if msg.OutputType == RESP { if msg.OutputType == RESP {
return resp.NullValue(), nil return resp.NullValue(), nil
} }
return NOMessage, errKeyNotFound return retrerr(errKeyNotFound)
} }
// >> Response
vals := make([]resp.Value, 0, 2) vals := make([]resp.Value, 0, 2)
var buf bytes.Buffer var buf bytes.Buffer
if msg.OutputType == JSON { if msg.OutputType == JSON {
@ -52,7 +55,11 @@ func (s *Server) cmdBounds(msg *Message) (resp.Value, error) {
if msg.OutputType == JSON { if msg.OutputType == JSON {
buf.WriteString(`,"bounds":`) buf.WriteString(`,"bounds":`)
buf.WriteString(string(bbox.AppendJSON(nil))) buf.WriteString(string(bbox.AppendJSON(nil)))
} else { buf.WriteString(`,"elapsed":"` + time.Since(start).String() + "\"}")
return resp.StringValue(buf.String()), nil
}
// RESP
vals = append(vals, resp.ArrayValue([]resp.Value{ vals = append(vals, resp.ArrayValue([]resp.Value{
resp.ArrayValue([]resp.Value{ resp.ArrayValue([]resp.Value{
resp.FloatValue(minX), resp.FloatValue(minX),
@ -63,16 +70,8 @@ func (s *Server) cmdBounds(msg *Message) (resp.Value, error) {
resp.FloatValue(maxY), resp.FloatValue(maxY),
}), }),
})) }))
}
switch msg.OutputType {
case JSON:
buf.WriteString(`,"elapsed":"` + time.Since(start).String() + "\"}")
return resp.StringValue(buf.String()), nil
case RESP:
return vals[0], nil return vals[0], nil
} }
return NOMessage, nil
}
func (s *Server) cmdType(msg *Message) (resp.Value, error) { func (s *Server) cmdType(msg *Message) (resp.Value, error) {
start := time.Now() start := time.Now()

View File

@ -624,7 +624,7 @@ func (s *Server) commandInScript(msg *Message) (
case "search": case "search":
res, err = s.cmdSearch(msg) res, err = s.cmdSearch(msg)
case "bounds": case "bounds":
res, err = s.cmdBounds(msg) res, err = s.cmdBOUNDS(msg)
case "get": case "get":
res, err = s.cmdGet(msg) res, err = s.cmdGet(msg)
case "jget": case "jget":

View File

@ -1096,7 +1096,7 @@ func (s *Server) command(msg *Message, client *Client) (
case "search": case "search":
res, err = s.cmdSearch(msg) res, err = s.cmdSearch(msg)
case "bounds": case "bounds":
res, err = s.cmdBounds(msg) res, err = s.cmdBOUNDS(msg)
case "get": case "get":
res, err = s.cmdGet(msg) res, err = s.cmdGet(msg)
case "jget": case "jget":

View File

@ -6,10 +6,9 @@ cd $(dirname "${BASH_SOURCE[0]}")/..
export CGO_ENABLED=0 export CGO_ENABLED=0
cd tests cd tests
go test -coverpkg=../internal/server -coverprofile=/tmp/coverage.out go test -coverpkg=../internal/server -coverprofile=/tmp/coverage.out $GOTEST
go tool cover -html=/tmp/coverage.out -o /tmp/coverage.html go tool cover -html=/tmp/coverage.out -o /tmp/coverage.html
echo "details: file:///tmp/coverage.html" echo "details: file:///tmp/coverage.html"
cd .. cd ..
# go test -coverpkg=internal/ \ go test $(go list ./... | grep -v /vendor/ | grep -v /tests)
# $(go list ./... | grep -v /vendor/ | grep -v /tests)

View File

@ -34,8 +34,11 @@ func subTestKeys(t *testing.T, mc *mockServer) {
func keys_BOUNDS_test(mc *mockServer) error { func keys_BOUNDS_test(mc *mockServer) error {
return mc.DoBatch( return mc.DoBatch(
Do("BOUNDS", "mykey").String("<nil>"),
Do("BOUNDS", "mykey").JSON().Error("key not found"),
Do("SET", "mykey", "myid1", "POINT", 33, -115).OK(), Do("SET", "mykey", "myid1", "POINT", 33, -115).OK(),
Do("BOUNDS", "mykey").String("[[-115 33] [-115 33]]"), Do("BOUNDS", "mykey").String("[[-115 33] [-115 33]]"),
Do("BOUNDS", "mykey").JSON().String(`{"ok":true,"bounds":{"type":"Point","coordinates":[-115,33]}}`),
Do("SET", "mykey", "myid2", "POINT", 34, -112).OK(), Do("SET", "mykey", "myid2", "POINT", 34, -112).OK(),
Do("BOUNDS", "mykey").String("[[-115 33] [-112 34]]"), Do("BOUNDS", "mykey").String("[[-115 33] [-112 34]]"),
Do("DEL", "mykey", "myid2").String("1"), Do("DEL", "mykey", "myid2").String("1"),
@ -43,6 +46,11 @@ func keys_BOUNDS_test(mc *mockServer) error {
Do("SET", "mykey", "myid3", "OBJECT", `{"type":"Point","coordinates":[-130,38,10]}`).OK(), Do("SET", "mykey", "myid3", "OBJECT", `{"type":"Point","coordinates":[-130,38,10]}`).OK(),
Do("SET", "mykey", "myid4", "OBJECT", `{"type":"Point","coordinates":[-110,25,-8]}`).OK(), Do("SET", "mykey", "myid4", "OBJECT", `{"type":"Point","coordinates":[-110,25,-8]}`).OK(),
Do("BOUNDS", "mykey").String("[[-130 25] [-110 38]]"), Do("BOUNDS", "mykey").String("[[-130 25] [-110 38]]"),
Do("BOUNDS", "mykey", "hello").Error("wrong number of arguments for 'bounds' command"),
Do("BOUNDS", "nada").String("<nil>"),
Do("BOUNDS", "nada").JSON().Error("key not found"),
Do("BOUNDS", "").String("<nil>"),
Do("BOUNDS", "mykey").JSON().String(`{"ok":true,"bounds":{"type":"Polygon","coordinates":[[[-130,25],[-110,25],[-110,38],[-130,38],[-130,25]]]}}`),
) )
} }

View File

@ -56,6 +56,26 @@ func (cmd *IO) OK() *IO {
}) })
} }
func (cmd *IO) Error(msg string) *IO {
return cmd.Custom(func(s string) error {
if cmd.json {
if gjson.Get(s, "ok").Type != gjson.False {
return errors.New("ok=true")
}
if gjson.Get(s, "err").String() != msg {
return fmt.Errorf("expected '%s', got '%s'",
msg, gjson.Get(s, "err").String())
}
} else {
s = strings.TrimPrefix(s, "ERR ")
if s != msg {
return fmt.Errorf("expected '%s', got '%s'", msg, s)
}
}
return nil
})
}
type ioVisitor struct { type ioVisitor struct {
fset *token.FileSet fset *token.FileSet
ln int ln int