Skip to content

Commit e72e3dc

Browse files
author
Linh Vo
committed
more handle tests
1 parent 00dec45 commit e72e3dc

5 files changed

+114
-27
lines changed

handler.go

+8-7
Original file line numberDiff line numberDiff line change
@@ -1550,9 +1550,10 @@ func (h *Handler) handleGetDefinition(w http.ResponseWriter, r *http.Request) {
15501550
return
15511551
}
15521552
inputDef, _ := index.inputDefinitions[inputDefName]
1553-
inputInfo := InputDefinitionInfo{Frames: inputDef.frames, Fields: inputDef.fields}
1554-
if err := json.NewEncoder(w).Encode(getInputDefinitionResponse{
1555-
InputDefinition: inputInfo,
1553+
//inputInfo := InputDefinitionInfo{Frames: inputDef.frames, Fields: inputDef.fields}
1554+
if err := json.NewEncoder(w).Encode(InputDefinitionInfo{
1555+
Frames: inputDef.frames,
1556+
Fields: inputDef.fields,
15561557
}); err != nil {
15571558
h.logger().Printf("write status response error: %s", err)
15581559
}
@@ -1577,17 +1578,17 @@ func (h *Handler) handleDeleteDefinition(w http.ResponseWriter, r *http.Request)
15771578
http.Error(w, err.Error(), http.StatusInternalServerError)
15781579
return
15791580
}
1581+
1582+
if err := json.NewEncoder(w).Encode(postInputDefinitionResponse{}); err != nil {
1583+
h.logger().Printf("response encoding error: %s", err)
1584+
}
15801585
}
15811586

15821587
type InputFrame struct {
15831588
Name string `json:"name,omitempty"`
15841589
Options FrameOptions `json:"options,omitempty"`
15851590
}
15861591

1587-
type getInputDefinitionResponse struct {
1588-
InputDefinition InputDefinitionInfo `json:"input-definition"`
1589-
}
1590-
15911592
type InputDefinitionInfo struct {
15921593
Frames []InputFrame `json:"frames"`
15931594
Fields []Field `json:"fields"`

handler_test.go

+64-6
Original file line numberDiff line numberDiff line change
@@ -1067,16 +1067,11 @@ func MustReadAll(r io.Reader) []byte {
10671067
return buf
10681068
}
10691069

1070-
// Ensure handler can delete a frame.
1070+
// Ensure handler can create a input definition.
10711071
func TestHandler_CreateInputDefinition(t *testing.T) {
10721072
hldr := MustOpenHolder()
10731073
defer hldr.Close()
10741074
hldr.MustCreateIndexIfNotExists("i0", pilosa.IndexOptions{})
1075-
1076-
h := NewHandler()
1077-
h.Holder = hldr.Holder
1078-
h.Cluster = NewCluster(1)
1079-
w := httptest.NewRecorder()
10801075
inputBody := []byte(`
10811076
{
10821077
"frames":[{
@@ -1107,10 +1102,73 @@ func TestHandler_CreateInputDefinition(t *testing.T) {
11071102
}
11081103
]
11091104
}`)
1105+
h := NewHandler()
1106+
h.Holder = hldr.Holder
1107+
h.Cluster = NewCluster(1)
1108+
w := httptest.NewRecorder()
11101109
h.ServeHTTP(w, MustNewHTTPRequest("POST", "/index/i0/input-definition/input1", bytes.NewBuffer(inputBody)))
11111110
if w.Code != http.StatusOK {
11121111
t.Fatalf("unexpected status code: %d", w.Code)
11131112
} else if body := w.Body.String(); body != `{}`+"\n" {
11141113
t.Fatalf("unexpected body: %s", body)
11151114
}
11161115
}
1116+
1117+
// Ensure handler can delete a input definition.
1118+
func TestHandler_DeleteInputDefinition(t *testing.T) {
1119+
hldr := MustOpenHolder()
1120+
defer hldr.Close()
1121+
index := hldr.MustCreateIndexIfNotExists("i0", pilosa.IndexOptions{})
1122+
1123+
frames := pilosa.InputFrame{Name: "f", Options: pilosa.FrameOptions{RowLabel: "row"}}
1124+
action := pilosa.Action{Frame: "f", ValueDestination: "map", ValueMap: map[string]uint64{"Green": 1}}
1125+
fields := pilosa.Field{Name: "id", PrimaryKey: true, Actions: []pilosa.Action{action}}
1126+
_, err := index.CreateInputDefinition("test", []pilosa.InputFrame{frames}, []pilosa.Field{fields})
1127+
if err != nil {
1128+
t.Fatal(err)
1129+
}
1130+
1131+
h := NewHandler()
1132+
h.Holder = hldr.Holder
1133+
h.Cluster = NewCluster(1)
1134+
w := httptest.NewRecorder()
1135+
h.ServeHTTP(w, MustNewHTTPRequest("DELETE", "/index/i0/input-definition/test", strings.NewReader("")))
1136+
if w.Code != http.StatusOK {
1137+
t.Fatalf("unexpected status code: %d", w.Code)
1138+
} else if body := w.Body.String(); body != `{}`+"\n" {
1139+
t.Fatalf("unexpected body: %s", body)
1140+
} else if index.InputDefinition("test") != nil {
1141+
t.Fatalf("unexpected result: %s", index.InputDefinition("test"))
1142+
}
1143+
}
1144+
1145+
// Return existing input definition
1146+
func TestHandler_GetInputDefinition(t *testing.T) {
1147+
hldr := MustOpenHolder()
1148+
defer hldr.Close()
1149+
index := hldr.MustCreateIndexIfNotExists("i0", pilosa.IndexOptions{})
1150+
1151+
frames := pilosa.InputFrame{Name: "f", Options: pilosa.FrameOptions{RowLabel: "row"}}
1152+
action := pilosa.Action{Frame: "f", ValueDestination: "map", ValueMap: map[string]uint64{"Green": 1}}
1153+
fields := pilosa.Field{Name: "id", PrimaryKey: true, Actions: []pilosa.Action{action}}
1154+
inputDef, err := index.CreateInputDefinition("test", []pilosa.InputFrame{frames}, []pilosa.Field{fields})
1155+
if err != nil {
1156+
t.Fatal(err)
1157+
}
1158+
1159+
response := &pilosa.InputDefinitionInfo{Frames: inputDef.Frames(), Fields: inputDef.Fields()}
1160+
expect, err := json.Marshal(response)
1161+
if err != nil {
1162+
t.Fatal(err)
1163+
}
1164+
h := NewHandler()
1165+
h.Holder = hldr.Holder
1166+
h.Cluster = NewCluster(1)
1167+
w := httptest.NewRecorder()
1168+
h.ServeHTTP(w, MustNewHTTPRequest("GET", "/index/i0/input-definition/test", strings.NewReader("")))
1169+
if w.Code != http.StatusOK {
1170+
t.Fatalf("unexpected status code: %d", w.Code)
1171+
} else if body := w.Body.String(); body != string(expect)+"\n" {
1172+
t.Fatalf("unexpected body: %s, expect: %s", body, string(expect))
1173+
}
1174+
}

index.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -662,14 +662,14 @@ func (i *Index) newInputDefinition(path, name string) (*InputDefinition, error)
662662
return f, nil
663663
}
664664

665-
// DeleteFrame removes a frame from the index.
665+
// DeleteInputDefinition removes a input definition from the index.
666666
func (i *Index) DeleteInputDefinition(name string) error {
667667
i.mu.Lock()
668668
defer i.mu.Unlock()
669669

670670
// Ignore if input definition doesn't exist.
671-
f := i.inputDefinition(name)
672-
if f == nil {
671+
inputDef := i.inputDefinition(name)
672+
if inputDef == nil {
673673
return nil
674674
}
675675

input_definition.go

+1-10
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,10 @@ func NewInputDefinition(path, index, name string) (*InputDefinition, error) {
3131
}, nil
3232
}
3333

34-
// Name returns the name of input definition was initialized with.
35-
func (i *InputDefinition) Name() string { return i.name }
36-
37-
// Index returns the index name of the input definition was initialized with.
38-
func (i *InputDefinition) Index() string { return i.index }
39-
40-
// Path returns the path of the input definition was initialized with.
41-
func (i *InputDefinition) Path() string { return i.path }
42-
4334
// Frames returns frames of the input definition was initialized with.
4435
func (i *InputDefinition) Frames() []InputFrame { return i.frames }
4536

46-
// Fields returns frames of the input definition was initialized with.
37+
// Fields returns fields of the input definition was initialized with.
4738
func (i *InputDefinition) Fields() []Field { return i.fields }
4839

4940
func (i *InputDefinition) Open() error {

input_definition_test.go

+38-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,38 @@
1-
package pilosa
1+
// Copyright 2017 Pilosa Corp.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package pilosa_test
16+
17+
import (
18+
"github.com/pilosa/pilosa"
19+
"testing"
20+
)
21+
22+
func TestInputDefinition_Open(t *testing.T) {
23+
index := MustOpenIndex()
24+
defer index.Close()
25+
26+
// Create Input Definition.
27+
frames := pilosa.InputFrame{Name: "f", Options: pilosa.FrameOptions{RowLabel: "row"}}
28+
action := pilosa.Action{Frame: "f", ValueDestination: "map", ValueMap: map[string]uint64{"Green": 1}}
29+
fields := pilosa.Field{Name: "id", PrimaryKey: true, Actions: []pilosa.Action{action}}
30+
inputDef, err := index.CreateInputDefinition("test", []pilosa.InputFrame{frames}, []pilosa.Field{fields})
31+
if err != nil {
32+
t.Fatal(err)
33+
}
34+
err = inputDef.Open()
35+
if err != nil {
36+
t.Fatal(err)
37+
}
38+
}

0 commit comments

Comments
 (0)