From ef95f04acad3930eb661c06d122a977e6ef08975 Mon Sep 17 00:00:00 2001 From: tidwall Date: Fri, 23 Sep 2022 07:51:05 -0700 Subject: [PATCH] Better coverage BOUNDS --- internal/server/crud.go | 53 +++++++++++++++++++------------------- internal/server/scripts.go | 2 +- internal/server/server.go | 2 +- scripts/test.sh | 5 ++-- tests/keys_test.go | 8 ++++++ tests/mock_io_test.go | 20 ++++++++++++++ 6 files changed, 58 insertions(+), 32 deletions(-) diff --git a/internal/server/crud.go b/internal/server/crud.go index eef2a2b9..a9a37877 100644 --- a/internal/server/crud.go +++ b/internal/server/crud.go @@ -17,27 +17,30 @@ import ( "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() - vs := msg.Args[1:] - var ok bool - var key string - if vs, key, ok = tokenval(vs); !ok || key == "" { - return NOMessage, errInvalidNumberOfArguments - } - if len(vs) != 0 { - return NOMessage, errInvalidNumberOfArguments + // >> Args + + args := msg.Args + if len(args) != 2 { + return retrerr(errInvalidNumberOfArguments) } + key := args[1] + + // >> Operation col, _ := s.cols.Get(key) if col == nil { if msg.OutputType == RESP { return resp.NullValue(), nil } - return NOMessage, errKeyNotFound + return retrerr(errKeyNotFound) } + // >> Response + vals := make([]resp.Value, 0, 2) var buf bytes.Buffer if msg.OutputType == JSON { @@ -52,26 +55,22 @@ func (s *Server) cmdBounds(msg *Message) (resp.Value, error) { if msg.OutputType == JSON { buf.WriteString(`,"bounds":`) buf.WriteString(string(bbox.AppendJSON(nil))) - } else { - vals = append(vals, resp.ArrayValue([]resp.Value{ - resp.ArrayValue([]resp.Value{ - resp.FloatValue(minX), - resp.FloatValue(minY), - }), - resp.ArrayValue([]resp.Value{ - resp.FloatValue(maxX), - 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 NOMessage, nil + + // RESP + vals = append(vals, resp.ArrayValue([]resp.Value{ + resp.ArrayValue([]resp.Value{ + resp.FloatValue(minX), + resp.FloatValue(minY), + }), + resp.ArrayValue([]resp.Value{ + resp.FloatValue(maxX), + resp.FloatValue(maxY), + }), + })) + return vals[0], nil } func (s *Server) cmdType(msg *Message) (resp.Value, error) { diff --git a/internal/server/scripts.go b/internal/server/scripts.go index 8b8b5fca..e97d8066 100644 --- a/internal/server/scripts.go +++ b/internal/server/scripts.go @@ -624,7 +624,7 @@ func (s *Server) commandInScript(msg *Message) ( case "search": res, err = s.cmdSearch(msg) case "bounds": - res, err = s.cmdBounds(msg) + res, err = s.cmdBOUNDS(msg) case "get": res, err = s.cmdGet(msg) case "jget": diff --git a/internal/server/server.go b/internal/server/server.go index 66a116a1..4cdc385f 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -1096,7 +1096,7 @@ func (s *Server) command(msg *Message, client *Client) ( case "search": res, err = s.cmdSearch(msg) case "bounds": - res, err = s.cmdBounds(msg) + res, err = s.cmdBOUNDS(msg) case "get": res, err = s.cmdGet(msg) case "jget": diff --git a/scripts/test.sh b/scripts/test.sh index 9b2b83a8..a5997b74 100755 --- a/scripts/test.sh +++ b/scripts/test.sh @@ -6,10 +6,9 @@ cd $(dirname "${BASH_SOURCE[0]}")/.. export CGO_ENABLED=0 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 echo "details: file:///tmp/coverage.html" cd .. -# go test -coverpkg=internal/ \ -# $(go list ./... | grep -v /vendor/ | grep -v /tests) +go test $(go list ./... | grep -v /vendor/ | grep -v /tests) diff --git a/tests/keys_test.go b/tests/keys_test.go index 9bf47e77..29e4625d 100644 --- a/tests/keys_test.go +++ b/tests/keys_test.go @@ -34,8 +34,11 @@ func subTestKeys(t *testing.T, mc *mockServer) { func keys_BOUNDS_test(mc *mockServer) error { return mc.DoBatch( + Do("BOUNDS", "mykey").String(""), + Do("BOUNDS", "mykey").JSON().Error("key not found"), Do("SET", "mykey", "myid1", "POINT", 33, -115).OK(), 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("BOUNDS", "mykey").String("[[-115 33] [-112 34]]"), 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", "myid4", "OBJECT", `{"type":"Point","coordinates":[-110,25,-8]}`).OK(), Do("BOUNDS", "mykey").String("[[-130 25] [-110 38]]"), + Do("BOUNDS", "mykey", "hello").Error("wrong number of arguments for 'bounds' command"), + Do("BOUNDS", "nada").String(""), + Do("BOUNDS", "nada").JSON().Error("key not found"), + Do("BOUNDS", "").String(""), + Do("BOUNDS", "mykey").JSON().String(`{"ok":true,"bounds":{"type":"Polygon","coordinates":[[[-130,25],[-110,25],[-110,38],[-130,38],[-130,25]]]}}`), ) } diff --git a/tests/mock_io_test.go b/tests/mock_io_test.go index f4502aa1..3f07fd91 100644 --- a/tests/mock_io_test.go +++ b/tests/mock_io_test.go @@ -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 { fset *token.FileSet ln int