Skip to content

Commit

Permalink
Changed clip errors and json result type
Browse files Browse the repository at this point in the history
  • Loading branch information
tidwall committed Feb 12, 2019
1 parent 62f44ed commit fb7259b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
21 changes: 13 additions & 8 deletions internal/server/test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// TEST command: spatial tests without walking the tree.
package server

// TEST command: spatial tests without walking the tree.

import (
"bytes"
"fmt"
Expand All @@ -16,7 +17,7 @@ import (
"github.com/tidwall/tile38/internal/clip"
)

func (s * Server) parseArea(ovs[]string, doClip bool) (vs []string, o geojson.Object, err error) {
func (s *Server) parseArea(ovs []string, doClip bool) (vs []string, o geojson.Object, err error) {
var ok bool
var typ string
vs = ovs[:]
Expand Down Expand Up @@ -48,7 +49,7 @@ func (s * Server) parseArea(ovs[]string, doClip bool) (vs []string, o geojson.Ob
o = geojson.NewPoint(geometry.Point{X: lon, Y: lat})
case "circle":
if doClip {
err = errInvalidArgument("cannot clip with " + ltyp)
err = fmt.Errorf("invalid clip type '%s'", typ)
return
}
var slat, slon, smeters string
Expand Down Expand Up @@ -84,7 +85,7 @@ func (s * Server) parseArea(ovs[]string, doClip bool) (vs []string, o geojson.Ob
o = geojson.NewCircle(geometry.Point{X: lon, Y: lat}, meters, defaultCircleSteps)
case "object":
if doClip {
err = errInvalidArgument("cannot clip with " + ltyp)
err = fmt.Errorf("invalid clip type '%s'", typ)
return
}
var obj string
Expand Down Expand Up @@ -198,7 +199,7 @@ func (s * Server) parseArea(ovs[]string, doClip bool) (vs []string, o geojson.Ob
})
case "get":
if doClip {
err = errInvalidArgument("cannot clip with " + ltyp)
err = fmt.Errorf("invalid clip type '%s'", typ)
return
}
var key, id string
Expand All @@ -224,7 +225,7 @@ func (s * Server) parseArea(ovs[]string, doClip bool) (vs []string, o geojson.Ob
return
}

func (s *Server) cmdTest (msg *Message) (res resp.Value, err error) {
func (s *Server) cmdTest(msg *Message) (res resp.Value, err error) {
start := time.Now()
vs := msg.Args[1:]

Expand Down Expand Up @@ -252,7 +253,7 @@ func (s *Server) cmdTest (msg *Message) (res resp.Value, err error) {
case "clip":
vs = nvs
if lTest != "intersects" {
err = errInvalidArgument("cannot clip with" + wtok)
err = errInvalidArgument(wtok)
return
}
doClip = true
Expand Down Expand Up @@ -282,7 +283,11 @@ func (s *Server) cmdTest (msg *Message) (res resp.Value, err error) {
case JSON:
var buf bytes.Buffer
buf.WriteString(`{"ok":true`)
buf.WriteString(fmt.Sprintf(`,"result":%d`, result))
if result != 0 {
buf.WriteString(`,"result":true`)
} else {
buf.WriteString(`,"result":false`)
}
if clipped != nil {
buf.WriteString(`,"object":` + clipped.JSON())
}
Expand Down
7 changes: 4 additions & 3 deletions tests/testcmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,10 @@ func testcmd_INTERSECTS_CLIP_test(mc *mockServer) error {
return mc.DoBatch([][]interface{}{
{"SET", "mykey", "point1", "POINT", 37.7335, -122.4412}, {"OK"},

{"TEST", "OBJECT", poly9, "INTERSECTS", "CLIP", "OBJECT", "{}"}, {"ERR invalid argument 'cannot clip with object'"},
{"TEST", "OBJECT", poly9, "INTERSECTS", "CLIP", "CIRCLE", "1", "2", "3"}, {"ERR invalid argument 'cannot clip with circle'"},
{"TEST", "OBJECT", poly9, "INTERSECTS", "CLIP", "GET", "mykey", "point1"}, {"ERR invalid argument 'cannot clip with get'"},
{"TEST", "OBJECT", poly9, "INTERSECTS", "CLIP", "OBJECT", "{}"}, {"ERR invalid clip type 'OBJECT'"},
{"TEST", "OBJECT", poly9, "INTERSECTS", "CLIP", "CIRCLE", "1", "2", "3"}, {"ERR invalid clip type 'CIRCLE'"},
{"TEST", "OBJECT", poly9, "INTERSECTS", "CLIP", "GET", "mykey", "point1"}, {"ERR invalid clip type 'GET'"},
{"TEST", "OBJECT", poly9, "WITHIN", "CLIP", "BOUNDS", 10, 10, 20, 20}, {"ERR invalid argument 'CLIP'"},

{"TEST", "OBJECT", poly9, "INTERSECTS", "CLIP", "BOUNDS", 37.732906137107, -122.44126439094543, 37.73421283683962, -122.43980526924135}, {"[1 " + poly9 + "]"},
{"TEST", "OBJECT", poly8, "INTERSECTS", "CLIP", "BOUNDS", 37.733, -122.4408378, 37.7341129, -122.44}, {"[1 " + poly8 + "]"},
Expand Down

0 comments on commit fb7259b

Please sign in to comment.