From 07e689ed586ff9b629a3820e6e6527bb9b9ca26c Mon Sep 17 00:00:00 2001 From: Linh Vo Date: Wed, 5 Apr 2017 15:40:02 -0500 Subject: [PATCH] custom UnmarshalJSON unit test --- handle_internal_test.go | 71 ++++++++++++++++++++++++++++++++++ handler.go | 35 +++++++++-------- handler_test.go | 84 ----------------------------------------- 3 files changed, 91 insertions(+), 99 deletions(-) create mode 100644 handle_internal_test.go diff --git a/handle_internal_test.go b/handle_internal_test.go new file mode 100644 index 000000000..2f79a9b1b --- /dev/null +++ b/handle_internal_test.go @@ -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) + } + } + + } +} diff --git a/handler.go b/handler.go index ca243f991..b3e5f9c22 100644 --- a/handler.go +++ b/handler.go @@ -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) } diff --git a/handler_test.go b/handler_test.go index 7a40c0a0d..20f76de05 100644 --- a/handler_test.go +++ b/handler_test.go @@ -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) - } -}