github.com/rohankumardubey/aresdb@v0.0.2-0.20190517170215-e54e3ca06b9c/api/enum_handler_test.go (about)

     1  //  Copyright (c) 2017-2018 Uber Technologies, Inc.
     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 api
    16  
    17  import (
    18  	"bytes"
    19  	"encoding/json"
    20  	"errors"
    21  	"fmt"
    22  	"io/ioutil"
    23  	"net/http"
    24  	"net/http/httptest"
    25  
    26  	"github.com/uber/aresdb/memstore"
    27  	memMocks "github.com/uber/aresdb/memstore/mocks"
    28  	metaCom "github.com/uber/aresdb/metastore/common"
    29  	"github.com/uber/aresdb/metastore/mocks"
    30  
    31  	"github.com/gorilla/mux"
    32  	"github.com/onsi/ginkgo"
    33  	. "github.com/onsi/gomega"
    34  	"github.com/stretchr/testify/mock"
    35  )
    36  
    37  var _ = ginkgo.Describe("EnumHandler", func() {
    38  
    39  	var testServer *httptest.Server
    40  	var hostPort string
    41  	var testTable = metaCom.Table{
    42  		Name: "testTable",
    43  		Columns: []metaCom.Column{
    44  			{
    45  				Name: "col1",
    46  				Type: "Int32",
    47  			},
    48  		},
    49  	}
    50  	var testTableSchema = memstore.TableSchema{
    51  		EnumDicts: map[string]memstore.EnumDict{
    52  			"testColumn": {
    53  				ReverseDict: []string{"a", "b", "c"},
    54  				Dict: map[string]int{
    55  					"a": 0,
    56  					"b": 1,
    57  					"c": 2,
    58  				},
    59  			},
    60  		},
    61  		Schema: testTable,
    62  	}
    63  
    64  	testMetastore := &mocks.MetaStore{}
    65  	var testMemStore *memMocks.MemStore
    66  
    67  	ginkgo.BeforeEach(func() {
    68  		testMemStore = CreateMemStore(&testTableSchema, 0, nil, nil)
    69  		enumHandler := NewEnumHandler(testMemStore, testMetastore)
    70  		testRouter := mux.NewRouter()
    71  		enumHandler.Register(testRouter.PathPrefix("/schema").Subrouter())
    72  		testServer = httptest.NewUnstartedServer(WithPanicHandling(testRouter))
    73  		testServer.Start()
    74  		hostPort = testServer.Listener.Addr().String()
    75  	})
    76  
    77  	ginkgo.AfterEach(func() {
    78  		testServer.Close()
    79  	})
    80  
    81  	ginkgo.It("ListEnumCases should work", func() {
    82  		resp, _ := http.Get(fmt.Sprintf("http://%s/schema/tables/%s/columns/%s/enum-cases", hostPort, "testTable", "testColumn"))
    83  		Ω(resp.StatusCode).Should(Equal(http.StatusOK))
    84  		respBody, err := ioutil.ReadAll(resp.Body)
    85  		Ω(err).Should(BeNil())
    86  		enumCases := []string{}
    87  		json.Unmarshal(respBody, &enumCases)
    88  		Ω(enumCases).Should(Equal([]string{"a", "b", "c"}))
    89  
    90  		resp, _ = http.Get(fmt.Sprintf("http://%s/schema/tables/%s/columns/%s/enum-cases", hostPort, "unknown", "testColumn"))
    91  		Ω(resp.StatusCode).Should(Equal(http.StatusBadRequest))
    92  
    93  		resp, _ = http.Get(fmt.Sprintf("http://%s/schema/tables/%s/columns/%s/enum-cases", hostPort, "testTable", "unknown"))
    94  		Ω(resp.StatusCode).Should(Equal(http.StatusBadRequest))
    95  	})
    96  
    97  	ginkgo.It("AddEnumCase should work", func() {
    98  		enumCases := []byte(`{"enumCases": ["a"]}`)
    99  		errousEnumCases := []byte(`{"enumCases": ["a"`)
   100  		testEnumID := []int{1}
   101  
   102  		testMetastore.On("ExtendEnumDict", mock.Anything, mock.Anything, mock.Anything).Return(testEnumID, nil).Once()
   103  		resp, _ := http.Post(fmt.Sprintf("http://%s/schema/tables/%s/columns/%s/enum-cases", hostPort, "testTable", "testColumn"), "application/json", bytes.NewBuffer(enumCases))
   104  		Ω(resp.StatusCode).Should(Equal(http.StatusOK))
   105  
   106  		resp, _ = http.Post(fmt.Sprintf("http://%s/schema/tables/%s/columns/%s/enum-cases", hostPort, "testTable", "testColumn"), "application/json", bytes.NewBuffer(errousEnumCases))
   107  		Ω(resp.StatusCode).Should(Equal(http.StatusBadRequest))
   108  
   109  		testMetastore.On("ExtendEnumDict", mock.Anything, mock.Anything, mock.Anything).Return(0, errors.New("Failed to extend enums")).Once()
   110  		resp, _ = http.Post(fmt.Sprintf("http://%s/schema/tables/%s/columns/%s/enum-cases", hostPort, "testTable", "testColumn"), "application/json", bytes.NewBuffer(enumCases))
   111  		Ω(resp.StatusCode).Should(Equal(http.StatusInternalServerError))
   112  	})
   113  })