couple more tests

This commit is contained in:
Linh Vo 2017-06-13 13:56:08 -05:00
parent 5ce6b5d684
commit 2d3f244a7b
4 changed files with 90 additions and 2 deletions

View file

@ -1537,6 +1537,10 @@ func (h *Handler) handlePostDefinition(w http.ResponseWriter, r *http.Request) {
if err != nil {
h.logger().Printf("problem sending CreateInputDefinition message: %s", err)
}
if err := json.NewEncoder(w).Encode(postInputDefinitionResponse{}); err != nil {
h.logger().Printf("response encoding error: %s", err)
}
}
func (h *Handler) handleGetDefinition(w http.ResponseWriter, r *http.Request) {
@ -1599,3 +1603,5 @@ type InputDefinitionInfo struct {
Frames []InputFrame `json:"frames"`
Fields []Field `json:"fields"`
}
type postInputDefinitionResponse struct{}

View file

@ -1066,3 +1066,51 @@ func MustReadAll(r io.Reader) []byte {
}
return buf
}
// Ensure handler can delete a frame.
func TestHandler_CreateInputDefinition(t *testing.T) {
hldr := MustOpenHolder()
defer hldr.Close()
hldr.MustCreateIndexIfNotExists("i0", pilosa.IndexOptions{})
h := NewHandler()
h.Holder = hldr.Holder
h.Cluster = NewCluster(1)
w := httptest.NewRecorder()
inputBody := []byte(`
{
"frames":[{
"name":"event-time",
"options":{
"timeQuantum": "YMD",
"inverseEnabled": false,
"cacheType": "ranked"
}
}],
"fields": [
{
"name": "id",
"primaryKey": true
},
{
"name": "cabType",
"actions": [
{
"frame": "cab-type",
"valueDestination": "mapping",
"valueMap": {
"Green": 1,
"Yellow": 2
}
}
]
}
]
}`)
h.ServeHTTP(w, MustNewHTTPRequest("POST", "/index/i0/input-definition/input1", bytes.NewBuffer(inputBody)))
if w.Code != http.StatusOK {
t.Fatalf("unexpected status code: %d", w.Code)
} else if body := w.Body.String(); body != `{}`+"\n" {
t.Fatalf("unexpected body: %s", body)
}
}

View file

@ -348,6 +348,16 @@ func (i *Index) Frame(name string) *Frame {
return i.frame(name)
}
// InputDefinition returns an input definition in the index by name.
func (i *Index) InputDefinition(name string) *InputDefinition {
i.mu.Lock()
defer i.mu.Unlock()
if inputDef, ok := i.inputDefinitions[name]; ok {
return inputDef
}
return nil
}
func (i *Index) frame(name string) *Frame { return i.frames[name] }
func (i *Index) inputDefinition(name string) *InputDefinition { return i.inputDefinitions[name] }

View file

@ -267,7 +267,7 @@ func TestIndex_CreateExistingInputDefinition(t *testing.T) {
frames := pilosa.InputFrame{Name: "f", Options: pilosa.FrameOptions{RowLabel: "row"}}
action := pilosa.Action{Frame: "f", ValueDestination: "map", ValueMap: map[string]uint64{"Green": 1}}
fields := pilosa.Field{Name: "id", PrimaryKey: true, Actions: []pilosa.Action{action}}
_ , err := index.CreateInputDefinition("test", []pilosa.InputFrame{frames}, []pilosa.Field{fields})
_, err := index.CreateInputDefinition("test", []pilosa.InputFrame{frames}, []pilosa.Field{fields})
if err != nil {
t.Fatal(err)
}
@ -282,8 +282,32 @@ func TestIndex_CreateEmptyInputDefinition(t *testing.T) {
defer index.Close()
// Create Input Definition.
_ , err := index.CreateInputDefinition("test", []pilosa.InputFrame{}, []pilosa.Field{})
_, err := index.CreateInputDefinition("test", []pilosa.InputFrame{}, []pilosa.Field{})
if err.Error() != "frames and fields are required" {
t.Fatal(err)
}
}
func TestIndex_DeleteInputDefinition(t *testing.T) {
index := MustOpenIndex()
defer index.Close()
// Create Input Definition.
frames := pilosa.InputFrame{Name: "f", Options: pilosa.FrameOptions{RowLabel: "row"}}
action := pilosa.Action{Frame: "f", ValueDestination: "map", ValueMap: map[string]uint64{"Green": 1}}
fields := pilosa.Field{Name: "id", PrimaryKey: true, Actions: []pilosa.Action{action}}
_, err := index.CreateInputDefinition("test", []pilosa.InputFrame{frames}, []pilosa.Field{fields})
if err != nil {
t.Fatal(err)
} else if index.InputDefinition("test") == nil {
t.Fatal("No input definition created")
}
err = index.DeleteInputDefinition("test")
if err != nil {
t.Fatal(err)
} else if index.InputDefinition("test") != nil {
t.Fatal("input definition isn't deleted")
}
}