added test for 1977 fix

This commit is contained in:
Yuce Tekol 2019-05-28 13:54:12 +03:00
parent b5e4b90438
commit 5f4c5d4d35
No known key found for this signature in database
GPG key ID: CB59E46D2FB90573
3 changed files with 123 additions and 1 deletions

2
api.go
View file

@ -213,7 +213,7 @@ func (api *API) CreateField(ctx context.Context, indexName string, fieldName str
for _, opt := range opts {
err := opt(&fo)
if err != nil {
return nil, errors.Wrap(err, "applying option")
return nil, NewBadRequestError(errors.Wrap(err, "applying option"))
}
}

View file

@ -782,6 +782,12 @@ func (h *Handler) handlePostField(w http.ResponseWriter, r *http.Request) {
}
_, err = h.api.CreateField(r.Context(), indexName, fieldName, fos...)
if err != nil {
if _, ok := err.(pilosa.BadRequestError); ok {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
}
resp.write(w, err)
}

View file

@ -22,6 +22,7 @@ import (
"fmt"
"io"
"io/ioutil"
"math"
gohttp "net/http"
"net/http/httptest"
"reflect"
@ -542,6 +543,104 @@ func TestHandler_Endpoints(t *testing.T) {
}
})
t.Run("Query int field unbounded", func(t *testing.T) {
w := httptest.NewRecorder()
fieldName := "f-int-ubound"
h.ServeHTTP(w, test.MustNewHTTPRequest("POST", fmt.Sprintf("/index/i0/field/%s", fieldName),
strings.NewReader(`{"options":{"type":"int"}}`)))
if w.Code != gohttp.StatusOK {
t.Fatalf("unexpected status code: %d", w.Code)
}
w = httptest.NewRecorder()
h.ServeHTTP(w, test.MustNewHTTPRequest("GET", "/schema", strings.NewReader("")))
if w.Code != gohttp.StatusOK {
t.Fatalf("unexpected status code: %d", w.Code)
}
rsp := getSchemaResponse{}
if err := json.Unmarshal([]byte(w.Body.String()), &rsp); err != nil {
t.Fatalf("json decode: %s", err)
}
field := rsp.findField("i0", fieldName)
if field == nil {
t.Fatalf("field not found: %s", fieldName)
}
if math.MinInt64 != field.Options.Min {
t.Fatalf("field min %d != %d", math.MinInt64, field.Options.Min)
}
if math.MaxInt64 != field.Options.Max {
t.Fatalf("field max %d != %d", math.MaxInt64, field.Options.Max)
}
})
t.Run("Query int field unbounded min", func(t *testing.T) {
w := httptest.NewRecorder()
fieldName := "f-int-ubound-min"
h.ServeHTTP(w, test.MustNewHTTPRequest("POST", fmt.Sprintf("/index/i0/field/%s", fieldName),
strings.NewReader(`{"options":{"type":"int", "max": 10}}`)))
if w.Code != gohttp.StatusOK {
t.Fatalf("unexpected status code: %d", w.Code)
}
w = httptest.NewRecorder()
h.ServeHTTP(w, test.MustNewHTTPRequest("GET", "/schema", strings.NewReader("")))
if w.Code != gohttp.StatusOK {
t.Fatalf("unexpected status code: %d", w.Code)
}
rsp := getSchemaResponse{}
if err := json.Unmarshal([]byte(w.Body.String()), &rsp); err != nil {
t.Fatalf("json decode: %s", err)
}
field := rsp.findField("i0", fieldName)
if field == nil {
t.Fatalf("field not found: %s", fieldName)
}
if math.MinInt64 != field.Options.Min {
t.Fatalf("field min %d != %d", math.MinInt64, field.Options.Min)
}
if 10 != field.Options.Max {
t.Fatalf("field max %d != %d", 10, field.Options.Max)
}
})
t.Run("Query int field unbounded max", func(t *testing.T) {
w := httptest.NewRecorder()
fieldName := "f-int-ubound-max"
h.ServeHTTP(w, test.MustNewHTTPRequest("POST", fmt.Sprintf("/index/i0/field/%s", fieldName),
strings.NewReader(`{"options":{"type":"int", "min": -10}}`)))
if w.Code != gohttp.StatusOK {
t.Fatalf("unexpected status code: %d", w.Code)
}
w = httptest.NewRecorder()
h.ServeHTTP(w, test.MustNewHTTPRequest("GET", "/schema", strings.NewReader("")))
if w.Code != gohttp.StatusOK {
t.Fatalf("unexpected status code: %d", w.Code)
}
rsp := getSchemaResponse{}
if err := json.Unmarshal([]byte(w.Body.String()), &rsp); err != nil {
t.Fatalf("json decode: %s", err)
}
field := rsp.findField("i0", fieldName)
if field == nil {
t.Fatalf("field not found: %s", fieldName)
}
if -10 != field.Options.Min {
t.Fatalf("field min %d != %d", 10, field.Options.Min)
}
if math.MaxInt64 != field.Options.Max {
t.Fatalf("field max %d != %d", math.MaxInt64, field.Options.Max)
}
})
t.Run("Query int field min > max return 400", func(t *testing.T) {
w := httptest.NewRecorder()
fieldName := "f-int-ubound-err"
h.ServeHTTP(w, test.MustNewHTTPRequest("POST", fmt.Sprintf("/index/i0/field/%s", fieldName),
strings.NewReader(`{"options":{"type":"int", "min": 10, "max": -10}}`)))
fmt.Println("body", w.Body.String())
if w.Code != gohttp.StatusBadRequest {
t.Fatalf("unexpected status code: %d", w.Code)
}
})
t.Run("Method not allowed", func(t *testing.T) {
w := httptest.NewRecorder()
h.ServeHTTP(w, test.MustNewHTTPRequest("GET", "/index/i0/query", nil))
@ -999,3 +1098,20 @@ func mustJSONDecodeSlice(t *testing.T, r io.Reader) (ret []interface{}) {
}
return ret
}
type getSchemaResponse struct {
Indexes []*pilosa.IndexInfo `json:"indexes"`
}
func (r getSchemaResponse) findField(indexName, fieldName string) *pilosa.FieldInfo {
for _, index := range r.Indexes {
if index.Name == indexName {
for _, field := range index.Fields {
if field.Name == fieldName {
return field
}
}
}
}
return nil
}