custom UnmarshalJSON unit test

This commit is contained in:
Linh Vo 2017-04-05 15:40:02 -05:00
parent 22475df035
commit 07e689ed58
3 changed files with 91 additions and 99 deletions

71
handle_internal_test.go Normal file
View file

@ -0,0 +1,71 @@
package pilosa
import (
"encoding/json"
"reflect"
"testing"
)
// Test custom UnmarshalJSON for postDBRequest object
func TestPostDBRequestUnmarshalJSON(t *testing.T) {
tests := []struct {
json string
expected postDBRequest
err string
}{
{json: `{"db": "d", "options": {}}`, expected: postDBRequest{DB: "d", Options: DBOptions{}}},
{json: `{"db": "d", "options": 4}`, err: "options is not map[string]interface{}"},
{json: `{"db": "d", "option": {}}`, err: "Unknown key: option:map[]"},
{json: `{"db": "d", "options": {"columnLabel": "test"}}`, expected: postDBRequest{DB: "d", Options: DBOptions{ColumnLabel: "test"}}},
{json: `{"db": "d", "options": {"columnLabl": "test"}}`, err: "invalid key for options {columnLabl:test}"},
{json: `{"db": "d", "options": {"columnLabel": "////"}}`, err: "invalid columnLabel value: ////"},
}
for _, test := range tests {
actual := &postDBRequest{}
err := json.Unmarshal([]byte(test.json), actual)
if err != nil {
if test.err == "" || test.err != err.Error() {
t.Errorf("expected error: %v, but got result: %v", test.err, err)
}
}
if test.err == "" {
if !reflect.DeepEqual(*actual, test.expected) {
t.Errorf("expected: %v, but got: %v", test.expected, *actual)
}
}
}
}
// Test custom UnmarshalJSON for postFrameRequest object
func TestPostFrameRequestUnmarshalJSON(t *testing.T) {
tests := []struct {
json string
expected postFrameRequest
err string
}{
{json: `{"db": "d", "frame":"f", "options": {}}`, expected: postFrameRequest{DB: "d", Frame: "f", Options: FrameOptions{}}},
{json: `{"db": "d", "frame":"f", "options": 4}`, err: "options is not map[string]interface{}"},
{json: `{"db": "d", "frame":"f", "option": {}}`, err: "Unknown key: {option:map[]}"},
{json: `{"db": "d", "frame":"f", "options": {"rowLabel": "test"}}`, expected: postFrameRequest{DB: "d", Frame: "f", Options: FrameOptions{RowLabel: "test"}}},
{json: `{"db": "d", "frame":"f", "options": {"rowLabl": "test"}}`, err: "invalid key for options {rowLabl:test}"},
{json: `{"db": "d", "frame":"f", "options": {"rowLabel": "////"}}`, err: "invalid rowLabel value: ////"},
}
for _, test := range tests {
actual := &postFrameRequest{}
err := json.Unmarshal([]byte(test.json), actual)
if err != nil {
if test.err == "" || test.err != err.Error() {
t.Errorf("expected error: %v, but got result: %v", test.err, err)
}
}
if test.err == "" {
if !reflect.DeepEqual(*actual, test.expected) {
t.Errorf("expected: %v, but got: %v", test.expected, *actual)
}
}
}
}

View file

@ -353,25 +353,28 @@ func (p *postDBRequest) UnmarshalJSON(b []byte) error {
for key, value := range data {
switch key {
case "db":
if val, ok := data["db"].(string); !ok {
val, ok := data["db"].(string)
if !ok {
return errors.New("db required and must be a string")
} else {
p.DB = val
}
p.DB = val
case "options":
options, ok := data["options"].(map[string]interface{})
if !ok {
return errors.New("options is not map[string]interface{}")
}
if len(options) == 0 {
return nil
}
err := validateOptions(options, "columnLabel")
if err != nil {
return err
p.Options = DBOptions{}
} else {
p.Options = DBOptions{ColumnLabel: options["columnLabel"].(string)}
err := validateOptions(options, "columnLabel")
if err != nil {
return err
} else {
p.Options = DBOptions{ColumnLabel: options["columnLabel"].(string)}
}
}
default:
return fmt.Errorf("Unknown key: %v:%v", key, value)
}
@ -590,14 +593,16 @@ func (p *postFrameRequest) UnmarshalJSON(b []byte) error {
return errors.New("options is not map[string]interface{}")
}
if len(options) == 0 {
return nil
}
err := validateOptions(options, "rowLabel")
if err != nil {
return err
p.Options = FrameOptions{}
} else {
p.Options = FrameOptions{RowLabel: options["rowLabel"].(string)}
err := validateOptions(options, "rowLabel")
if err != nil {
return err
} else {
p.Options = FrameOptions{RowLabel: options["rowLabel"].(string)}
}
}
default:
return fmt.Errorf("Unknown key: {%v:%v}", key, value)
}

View file

@ -868,87 +868,3 @@ func MustReadAll(r io.Reader) []byte {
}
return buf
}
// Ensure that options needs to be provided to set columnLabel when create DB
func TestHandler_DB_Options(t *testing.T) {
idx := MustOpenIndex()
defer idx.Close()
s := NewServer()
s.Handler.Index = idx.Index
defer s.Close()
resp, err := http.DefaultClient.Do(MustNewHTTPRequest("POST", s.URL+"/db", strings.NewReader(`{"db": "sample-db", "columnLabel": "location"}`)))
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()
// Verify body response.
if resp.StatusCode != http.StatusBadRequest {
t.Fatalf("unexpected status: %d", resp.StatusCode)
} else if buf, err := ioutil.ReadAll(resp.Body); err != nil {
t.Fatal(err)
} else if string(buf) != "Unknown key: columnLabel:location"+"\n" {
t.Fatalf("unexpected response body: %s", buf)
}
}
// Ensure that rowLabel is provided as an options when create frame
func TestHandler_Frame_Options(t *testing.T) {
idx := MustOpenIndex()
defer idx.Close()
s := NewServer()
s.Handler.Index = idx.Index
defer s.Close()
// Create database.
if _, err := idx.CreateDBIfNotExists("sample-db", pilosa.DBOptions{}); err != nil {
t.Fatal(err)
}
resp, err := http.DefaultClient.Do(MustNewHTTPRequest("POST", s.URL+"/frame", strings.NewReader(`{"db": "sample-db", "frame": "test", "options": {"columnLabel": "location"}}`)))
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()
// Verify body response.
if resp.StatusCode != http.StatusBadRequest {
t.Fatalf("unexpected status: %d", resp.StatusCode)
} else if buf, err := ioutil.ReadAll(resp.Body); err != nil {
t.Fatal(err)
} else if string(buf) != "invalid key for options {columnLabel:location}"+"\n" {
t.Fatalf("unexpected response body: %s", buf)
}
}
// Ensure that rowLabel is provided as an options when create frame
func TestHandler_OptionsValue(t *testing.T) {
idx := MustOpenIndex()
defer idx.Close()
s := NewServer()
s.Handler.Index = idx.Index
defer s.Close()
// Create database.
if _, err := idx.CreateDBIfNotExists("sample-db", pilosa.DBOptions{}); err != nil {
t.Fatal(err)
}
resp, err := http.DefaultClient.Do(MustNewHTTPRequest("POST", s.URL+"/frame", strings.NewReader(`{"db": "sample-db", "frame": "test", "options": {"rowLabel": "///"}}`)))
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()
// Verify body response.
if resp.StatusCode != http.StatusBadRequest {
t.Fatalf("unexpected status: %d", resp.StatusCode)
} else if buf, err := ioutil.ReadAll(resp.Body); err != nil {
t.Fatal(err)
} else if string(buf) != "invalid rowLabel value: ///"+"\n" {
t.Fatalf("unexpected response body: %s", buf)
}
}