func TestDatabaseHandler_DELETE_Name(t *testing.T) { db1 := pila.NewDatabase("mydb1") db2 := pila.NewDatabase("mydb2") p := pila.NewPila() _ = p.AddDatabase(db1) _ = p.AddDatabase(db2) conn := NewConn() conn.Pila = p request, err := http.NewRequest("DELETE", fmt.Sprintf("/databases/%s", db1.Name), nil) if err != nil { t.Fatal(err) } response := httptest.NewRecorder() databaseHandle := conn.databaseHandler(db1.Name) databaseHandle.ServeHTTP(response, request) if response.Code != http.StatusNoContent { t.Errorf("response code is %v, expected %v", response.Code, http.StatusNoContent) } if len(conn.Pila.Databases) != 1 { t.Errorf("got %d database, expected %d", len(conn.Pila.Databases), 1) } }
func TestPopStackHandler_EmptyStack(t *testing.T) { s := pila.NewStack("stack", time.Now().UTC()) db := pila.NewDatabase("db") _ = db.AddStack(s) p := pila.NewPila() _ = p.AddDatabase(db) conn := NewConn() conn.Pila = p request, err := http.NewRequest("DELETE", fmt.Sprintf("/databases/%s/stacks/%s", db.ID.String(), s.ID.String()), nil) if err != nil { t.Fatal(err) } response := httptest.NewRecorder() conn.popStackHandler(response, request, s) if response.Code != http.StatusNoContent { t.Errorf("response code is %v, expected %v", response.Code, http.StatusNoContent) } }
func TestStacksHandler_GET_BadRequest(t *testing.T) { ch := make(chan int) stack := pila.NewStack("test-stack-channel", time.Now().UTC()) stack.Push(ch) db := pila.NewDatabase("db") _ = db.AddStack(stack) p := pila.NewPila() _ = p.AddDatabase(db) conn := NewConn() conn.Pila = p request, err := http.NewRequest("GET", "/databases/db/stacks", nil) if err != nil { t.Fatal(err) } response := httptest.NewRecorder() stacksHandle := conn.stacksHandler("db") stacksHandle.ServeHTTP(response, request) if response.Code != http.StatusBadRequest { t.Errorf("response code is %v, expected %v", response.Code, http.StatusGone) } }
func TestCreateStackHandler_Conflict(t *testing.T) { s := pila.NewStack("test-stack", time.Now().UTC()) db := pila.NewDatabase("db") _ = db.AddStack(s) p := pila.NewPila() _ = p.AddDatabase(db) conn := NewConn() conn.Pila = p path := fmt.Sprintf("/databases/%s/stacks/?name=test-stack", db.ID.String()) request, err := http.NewRequest("PUT", path, nil) if err != nil { t.Fatal(err) } response := httptest.NewRecorder() conn.createStackHandler(response, request, db.ID.String()) if response.Code != http.StatusConflict { t.Errorf("response code is %v, expected %v", response.Code, http.StatusConflict) } }
func TestPushStackHandler_Empty(t *testing.T) { s := pila.NewStack("stack", time.Now().UTC()) db := pila.NewDatabase("db") _ = db.AddStack(s) p := pila.NewPila() _ = p.AddDatabase(db) conn := NewConn() conn.Pila = p request, err := http.NewRequest("POST", fmt.Sprintf("/databases/%s/stacks/%s", db.ID.String(), s.ID.String()), nil) if err != nil { t.Fatal(err) } request.Header.Set("Content-Type", "application/json") response := httptest.NewRecorder() conn.pushStackHandler(response, request, s) if pushedElement := db.Stacks[s.ID].Peek(); pushedElement != nil { t.Errorf("Pushed element is %v, expected nil", pushedElement) } if response.Code != http.StatusBadRequest { t.Errorf("response code is %v, expected %v", response.Code, http.StatusBadRequest) } }
func TestDatabasesHandler_GET(t *testing.T) { db := pila.NewDatabase("db") p := pila.NewPila() _ = p.AddDatabase(db) conn := NewConn() conn.Pila = p request, err := http.NewRequest("GET", "/databases", nil) if err != nil { t.Fatal(err) } response := httptest.NewRecorder() conn.databasesHandler(response, request) if contentType := response.Header().Get("Content-Type"); contentType != "application/json" { t.Errorf("Content-Type is %v, expected %v", contentType, "application/json") } if response.Code != http.StatusOK { t.Errorf("response code is %v, expected %v", response.Code, http.StatusOK) } databases, err := ioutil.ReadAll(response.Body) if err != nil { t.Fatal(err) } if expected := `{"number_of_databases":1,"databases":[{"id":"8cfa8cb55c92fa403369a13fd12a8e01","name":"db","number_of_stacks":0}]}`; string(databases) != expected { t.Errorf("databases are %s, expected %s", string(databases), expected) } }
func TestDeleteStackHandler(t *testing.T) { s := pila.NewStack("stack", time.Now().UTC()) db := pila.NewDatabase("db") _ = db.AddStack(s) p := pila.NewPila() _ = p.AddDatabase(db) conn := NewConn() conn.Pila = p s.Push("one") varss := []map[string]string{ { "database_id": db.ID.String(), "stack_id": s.ID.String(), }, { "database_id": db.Name, "stack_id": s.Name, }, } for _, vars := range varss { request, err := http.NewRequest("DELETE", fmt.Sprintf("/databases/%s/stacks/%s?full", vars["database_id"], vars["stack_id"]), nil) if err != nil { t.Fatal(err) } response := httptest.NewRecorder() conn.deleteStackHandler(response, request, db, s) if expectedStack := db.Stacks[uuid.UUID(vars["stack_id"])]; expectedStack != nil { t.Errorf("db contains %v, expected not to", vars["stack_id"]) } if response.Code != http.StatusNoContent { t.Errorf("response code is %v, expected %v", response.Code, http.StatusNoContent) } // restore elements for next table test iteration s = pila.NewStack("stack", time.Now().UTC()) _ = db.AddStack(s) s.Push("one") } }
func TestStacksHandler_GET_Name(t *testing.T) { now1 := time.Now().UTC() after1 := time.Now().UTC() now2 := time.Now().UTC() after2 := time.Now().UTC() s1 := pila.NewStack("stack1", now1) s1.Push("bar") s1.Update(after1) s2 := pila.NewStack("stack2", now2) s2.Push(`{"a":"b"}`) s2.Update(after2) db := pila.NewDatabase("db") _ = db.AddStack(s1) _ = db.AddStack(s2) p := pila.NewPila() _ = p.AddDatabase(db) conn := NewConn() conn.Pila = p request, err := http.NewRequest("GET", "/databases/db/stacks", nil) if err != nil { t.Fatal(err) } response := httptest.NewRecorder() stacksHandle := conn.stacksHandler("db") stacksHandle.ServeHTTP(response, request) if contentType := response.Header().Get("Content-Type"); contentType != "application/json" { t.Errorf("Content-Type is %v, expected %v", contentType, "application/json") } if response.Code != http.StatusOK { t.Errorf("response code is %v, expected %v", response.Code, http.StatusOK) } stacks, err := ioutil.ReadAll(response.Body) if err != nil { t.Fatal(err) } if expected := fmt.Sprintf(`{"stacks":[{"id":"f0306fec639bd57fc2929c8b897b9b37","name":"stack1","peek":"bar","size":1,"created_at":"%v","updated_at":"%v","read_at":"%v"},{"id":"dde8f895aea2ffa5546336146b9384e7","name":"stack2","peek":"{\"a\":\"b\"}","size":1,"created_at":"%v","updated_at":"%v","read_at":"%v"}]}`, date.Format(now1), date.Format(after1), date.Format(after1), date.Format(now2), date.Format(after2), date.Format(after2)); string(stacks) != expected { t.Errorf("stacks are %s, expected %s", string(stacks), expected) } }
func TestPeekStackHandler(t *testing.T) { s := pila.NewStack("stack", time.Now().UTC()) db := pila.NewDatabase("db") _ = db.AddStack(s) p := pila.NewPila() _ = p.AddDatabase(db) conn := NewConn() conn.Pila = p element := pila.Element{Value: "test-element"} expectedElementJSON, _ := element.ToJSON() s.Push(element.Value) request, err := http.NewRequest("GET", fmt.Sprintf("/databases/%s/stacks/%s?peek", db.ID.String(), s.ID.String()), nil) if err != nil { t.Fatal(err) } request.Header.Set("Content-Type", "application/json") response := httptest.NewRecorder() conn.peekStackHandler(response, request, s) if peekElement := db.Stacks[s.ID].Peek(); peekElement != element.Value { t.Errorf("peek element is %v, expected %v", peekElement, element.Value) } if contentType := response.Header().Get("Content-Type"); contentType != "application/json" { t.Errorf("Content-Type is %v, expected %v", contentType, "application/json") } if response.Code != http.StatusOK { t.Errorf("response code is %v, expected %v", response.Code, http.StatusOK) } elementJSON, err := ioutil.ReadAll(response.Body) if err != nil { t.Fatal(err) } if string(elementJSON) != string(expectedElementJSON) { t.Errorf("peek element is %v, expected %v", string(elementJSON), string(expectedElementJSON)) } }
func TestResourceStack_False(t *testing.T) { dbName := "db" stackName := "stack" inputs := []string{stackName, uuid.New(dbName + stackName).String()} for _, input := range inputs { db := pila.NewDatabase(dbName) _, ok := ResourceStack(db, input) if ok { t.Errorf("ok is %v, expected false", ok) } } }
func TestSizeStackHandler(t *testing.T) { s := pila.NewStack("stack", time.Now().UTC()) db := pila.NewDatabase("db") _ = db.AddStack(s) p := pila.NewPila() _ = p.AddDatabase(db) conn := NewConn() conn.Pila = p s.Push("element") expectedSizeJSON := s.SizeToJSON() request, err := http.NewRequest("GET", fmt.Sprintf("/databases/%s/stacks/%s?size", db.ID.String(), s.ID.String()), nil) if err != nil { t.Fatal(err) } request.Header.Set("Content-Type", "application/json") response := httptest.NewRecorder() conn.sizeStackHandler(response, request, s) if contentType := response.Header().Get("Content-Type"); contentType != "application/json" { t.Errorf("Content-Type is %v, expected %v", contentType, "application/json") } if response.Code != http.StatusOK { t.Errorf("response code is %v, expected %v", response.Code, http.StatusOK) } sizeJSON, err := ioutil.ReadAll(response.Body) if err != nil { t.Fatal(err) } if string(sizeJSON) != string(expectedSizeJSON) { t.Errorf("size is %v, expected %v", string(sizeJSON), string(expectedSizeJSON)) } }
func TestDatabaseHandler_GET_Name(t *testing.T) { s := pila.NewStack("stack", time.Now().UTC()) s.Push("foo") db := pila.NewDatabase("mydb") _ = db.AddStack(s) p := pila.NewPila() _ = p.AddDatabase(db) conn := NewConn() conn.Pila = p request, err := http.NewRequest("GET", fmt.Sprintf("/databases/%s", db.Name), nil) if err != nil { t.Fatal(err) } response := httptest.NewRecorder() databaseHandle := conn.databaseHandler(db.Name) databaseHandle.ServeHTTP(response, request) if contentType := response.Header().Get("Content-Type"); contentType != "application/json" { t.Errorf("Content-Type is %v, expected %v", contentType, "application/json") } if response.Code != http.StatusOK { t.Errorf("response code is %v, expected %v", response.Code, http.StatusOK) } database, err := ioutil.ReadAll(response.Body) if err != nil { t.Fatal(err) } if expected := `{"id":"c13cec0e70876381c78c616ee2d809eb","name":"mydb","number_of_stacks":1,"stacks":["b92f53fa3884305ef798fd8c5d7609ad"]}`; string(database) != expected { t.Errorf("database is %v, expected %v", string(database), expected) } }
func TestResourceStack(t *testing.T) { dbName := "db" stackName := "stack" inputs := []string{stackName, uuid.New(dbName + stackName).String()} for _, input := range inputs { expectedStack := pila.NewStack(stackName, time.Now()) db := pila.NewDatabase(dbName) _ = db.AddStack(expectedStack) stack, ok := ResourceStack(db, input) if !ok { t.Errorf("ok is %v, expected true", ok) } if !reflect.DeepEqual(expectedStack, stack) { t.Errorf("stack is %v, expected stack id %v", stack, expectedStack) } } }
// createDatabaseHandler creates a Database and returns 201 and the ID and name // of the Database. func (c *Conn) createDatabaseHandler(w http.ResponseWriter, r *http.Request) { name := r.FormValue("name") if name == "" { log.Println(r.Method, r.URL, http.StatusBadRequest, "missing name") w.WriteHeader(http.StatusBadRequest) return } db := pila.NewDatabase(name) err := c.Pila.AddDatabase(db) if err != nil { log.Println(r.Method, r.URL, http.StatusConflict, err) w.WriteHeader(http.StatusConflict) return } w.Header().Set("Content-Type", "application/json") log.Println(r.Method, r.URL, http.StatusCreated) w.WriteHeader(http.StatusCreated) w.Write(db.Status().ToJSON()) }
func TestCheckMaxStackSize(t *testing.T) { s := pila.NewStack("stack", time.Now()) s.Push("foo") db := pila.NewDatabase("mydb") _ = db.AddStack(s) p := pila.NewPila() _ = p.AddDatabase(db) conn := NewConn() conn.Pila = p f := func(w http.ResponseWriter, r *http.Request, stack *pila.Stack) { w.WriteHeader(http.StatusOK) } inputOutput := []struct { input, output int }{ {1, http.StatusNotAcceptable}, {6, http.StatusOK}, } for _, io := range inputOutput { conn.Config.Set(vars.MaxStackSize, io.input) request, err := http.NewRequest("GET", "", nil) if err != nil { t.Fatal(err) } response := httptest.NewRecorder() conn.checkMaxStackSize(f)(response, request, s) if response.Code != io.output { t.Errorf("response code is %v, expected %v", response.Code, io.output) } } }
func TestStacksHandler_PUT_Name(t *testing.T) { db := pila.NewDatabase("db") p := pila.NewPila() _ = p.AddDatabase(db) conn := NewConn() conn.Pila = p conn.opDate = time.Now().UTC() path := fmt.Sprintf("/databases/%s/stacks/?name=test-stack", db.Name) request, err := http.NewRequest("PUT", path, nil) if err != nil { t.Fatal(err) } response := httptest.NewRecorder() stacksHandle := conn.stacksHandler(db.ID.String()) stacksHandle.ServeHTTP(response, request) if contentType := response.Header().Get("Content-Type"); contentType != "application/json" { t.Errorf("Content-Type is %v, expected %v", contentType, "application/json") } if response.Code != http.StatusCreated { t.Errorf("response code is %v, expected %v", response.Code, http.StatusCreated) } stack, err := ioutil.ReadAll(response.Body) if err != nil { t.Fatal(err) } expectedStack := fmt.Sprintf(`{"id":"bb4dabeeaa6e90108583ddbf49649427","name":"test-stack","peek":null,"size":0,"created_at":"%v","updated_at":"%v","read_at":"%v"}`, date.Format(conn.opDate), date.Format(conn.opDate), date.Format(conn.opDate)) if string(stack) != expectedStack { t.Errorf("stack is %s, expected %s", string(stack), expectedStack) } }
func TestCreateStackHandler_NoName(t *testing.T) { db := pila.NewDatabase("db") p := pila.NewPila() _ = p.AddDatabase(db) conn := NewConn() conn.Pila = p path := fmt.Sprintf("/databases/%s/stacks/?name=", db.ID.String()) request, err := http.NewRequest("PUT", path, nil) if err != nil { t.Fatal(err) } response := httptest.NewRecorder() conn.createStackHandler(response, request, db.ID.String()) if response.Code != http.StatusBadRequest { t.Errorf("response code is %v, expected %v", response.Code, http.StatusBadRequest) } }
func TestStacksHandler_GET_Gone(t *testing.T) { db := pila.NewDatabase("db") p := pila.NewPila() _ = p.AddDatabase(db) conn := NewConn() conn.Pila = p request, err := http.NewRequest("GET", "/databases/nodb/stacks", nil) if err != nil { t.Fatal(err) } response := httptest.NewRecorder() stacksHandle := conn.stacksHandler("nodb") stacksHandle.ServeHTTP(response, request) if response.Code != http.StatusGone { t.Errorf("response code is %v, expected %v", response.Code, http.StatusGone) } }
func TestResourceDatabase(t *testing.T) { dbName := "db" inputs := []string{dbName, uuid.New(dbName).String()} for _, input := range inputs { expectedDB := pila.NewDatabase(dbName) p := pila.NewPila() _ = p.AddDatabase(expectedDB) conn := NewConn() conn.Pila = p db, ok := ResourceDatabase(conn, input) if !ok { t.Errorf("ok is %v, expected true", ok) } if !reflect.DeepEqual(expectedDB, db) { t.Errorf("db is %v, expected db id %v", db, expectedDB) } } }
func TestStackHandler_StackGone(t *testing.T) { now := time.Now().UTC() s := pila.NewStack("stack", now) db := pila.NewDatabase("db") _ = db.AddStack(s) p := pila.NewPila() _ = p.AddDatabase(db) conn := NewConn() conn.Pila = p conn.opDate = now request, err := http.NewRequest("GET", fmt.Sprintf("/databases/%s/stacks/%s", db.ID.String(), "non-existing-stack"), nil) if err != nil { t.Fatal(err) } request.Header.Set("Content-Type", "application/json") response := httptest.NewRecorder() params := &map[string]string{ "database_id": db.ID.String(), "stack_id": "non-existing-stack", } stackHandle := conn.stackHandler(params) stackHandle.ServeHTTP(response, request) if response.Code != http.StatusGone { t.Errorf("response code is %v, expected %v", response.Code, http.StatusOK) } }
func TestPopStackHandler(t *testing.T) { element := pila.Element{Value: "test-element"} expectedElementJSON, _ := element.ToJSON() s := pila.NewStack("stack", time.Now().UTC()) s.Push(element.Value) db := pila.NewDatabase("db") _ = db.AddStack(s) p := pila.NewPila() _ = p.AddDatabase(db) conn := NewConn() conn.Pila = p varss := []map[string]string{ { "database_id": db.ID.String(), "stack_id": s.ID.String(), }, { "database_id": db.Name, "stack_id": s.Name, }, } for _, vars := range varss { request, err := http.NewRequest("DELETE", fmt.Sprintf("/databases/%s/stacks/%s", vars["database_id"], vars["stack_id"]), nil) if err != nil { t.Fatal(err) } response := httptest.NewRecorder() conn.popStackHandler(response, request, s) if peek, ok := db.Stacks[s.ID].Pop(); ok { t.Errorf("stack contains %v, expected to be empty", peek) } if contentType := response.Header().Get("Content-Type"); contentType != "application/json" { t.Errorf("Content-Type is %v, expected %v", contentType, "application/json") } if response.Code != http.StatusOK { t.Errorf("response code is %v, expected %v", response.Code, http.StatusOK) } elementJSON, err := ioutil.ReadAll(response.Body) if err != nil { t.Fatal(err) } if string(elementJSON) != string(expectedElementJSON) { t.Errorf("popped element is %s, expected %s", string(elementJSON), string(expectedElementJSON)) } // restore element for next table test iteration s.Push(element.Value) } }
func TestStackHandler_DELETE(t *testing.T) { element := pila.Element{Value: "test-element"} expectedElementJSON, _ := element.ToJSON() s := pila.NewStack("stack", time.Now().UTC()) db := pila.NewDatabase("db") _ = db.AddStack(s) p := pila.NewPila() _ = p.AddDatabase(db) conn := NewConn() conn.Pila = p s.Push(element.Value) inputOutput := []struct { input struct { database, stack, op string } output struct { response []byte code int } }{ {struct { database, stack, op string }{db.ID.String(), s.ID.String(), ""}, struct { response []byte code int }{expectedElementJSON, http.StatusOK}, }, {struct { database, stack, op string }{db.Name, s.Name, ""}, struct { response []byte code int }{expectedElementJSON, http.StatusOK}, }, {struct { database, stack, op string }{db.Name, s.Name, "flush"}, struct { response []byte code int }{nil, http.StatusOK}, }, {struct { database, stack, op string }{db.Name, s.Name, "full"}, struct { response []byte code int }{nil, http.StatusNoContent}, }, } for _, io := range inputOutput { request, err := http.NewRequest("DELETE", fmt.Sprintf("/databases/%s/stacks/%s?%s", io.input.database, io.input.stack, io.input.op), nil) if err != nil { t.Fatal(err) } response := httptest.NewRecorder() params := map[string]string{ "database_id": io.input.database, "stack_id": io.input.stack, } stackHandle := conn.stackHandler(¶ms) stackHandle.ServeHTTP(response, request) if response.Code != io.output.code { t.Errorf("on op %s response code is %v, expected %v", io.input.op, response.Code, io.output.code) } responseJSON, err := ioutil.ReadAll(response.Body) if err != nil { t.Fatal(err) } if io.input.op == "flush" { s.Update(conn.opDate) stackStatus := s.Status() expectedStackStatusJSON, err := stackStatus.ToJSON() if err != nil { t.Fatal(err) } if string(responseJSON) != string(expectedStackStatusJSON) { t.Errorf("on op %s response is %s, expected %s", io.input.op, string(responseJSON), string(expectedStackStatusJSON)) } } else if string(responseJSON) != string(io.output.response) { t.Errorf("on op %s response is %s, expected %s", io.input.op, string(responseJSON), string(io.output.response)) } if io.input.op == "full" { if s := db.Stacks[uuid.UUID(io.input.stack)]; s != nil { t.Errorf("db contains %v, expected not to", io.input.stack) } } else { if peek, ok := db.Stacks[s.ID].Pop(); ok { t.Errorf("stack contains %v, expected to be empty", peek) } if contentType := response.Header().Get("Content-Type"); contentType != "application/json" { t.Errorf("Content-Type is %v, expected %v", contentType, "application/json") } // restore element for next table test iteration s.Push(element.Value) } } }
func TestStackHandler_GET(t *testing.T) { element := pila.Element{Value: "test-element"} expectedElementJSON, _ := element.ToJSON() createDate := time.Now().UTC() s := pila.NewStack("stack", createDate) s.Update(createDate) db := pila.NewDatabase("db") _ = db.AddStack(s) p := pila.NewPila() _ = p.AddDatabase(db) conn := NewConn() conn.Pila = p conn.opDate = time.Now().UTC() s.Push(element.Value) expectedStackStatusJSON, err := s.Status().ToJSON() if err != nil { t.Fatal(err) } expectedSizeJSON := s.SizeToJSON() inputOutput := []struct { input struct { database, stack, op string } output struct { response []byte code int } }{ {struct { database, stack, op string }{db.ID.String(), s.ID.String(), ""}, struct { response []byte code int }{expectedStackStatusJSON, http.StatusOK}, }, {struct { database, stack, op string }{db.ID.String(), s.ID.String(), "peek"}, struct { response []byte code int }{expectedElementJSON, http.StatusOK}, }, {struct { database, stack, op string }{db.ID.String(), s.ID.String(), "size"}, struct { response []byte code int }{expectedSizeJSON, http.StatusOK}, }, } for _, io := range inputOutput { request, err := http.NewRequest("GET", fmt.Sprintf("/databases/%s/stacks/%s?%s", io.input.database, io.input.stack, io.input.op), nil) if err != nil { t.Fatal(err) } response := httptest.NewRecorder() params := map[string]string{ "database_id": io.input.database, "stack_id": io.input.stack, } stackHandle := conn.stackHandler(¶ms) stackHandle.ServeHTTP(response, request) if peek := db.Stacks[s.ID].Peek(); peek != element.Value { t.Errorf("peek is %v, expected %v", peek, element.Value) } if contentType := response.Header().Get("Content-Type"); contentType != "application/json" { t.Errorf("Content-Type is %v, expected %v", contentType, "application/json") } if response.Code != io.output.code { t.Errorf("on %s response code is %v, expected %v", io.input.op, response.Code, io.output.code) } responseJSON, err := ioutil.ReadAll(response.Body) if err != nil { t.Fatal(err) } if io.input.op == "" { expectedStackStatusJSON, err := s.Status().ToJSON() if err != nil { t.Fatal(err) } else if string(responseJSON) != string(expectedStackStatusJSON) { t.Errorf("on %s response is %s, expected %s", io.input.op, string(responseJSON), string(expectedStackStatusJSON)) } } else if string(responseJSON) != string(io.output.response) { t.Errorf("on %s response is %s, expected %s", io.input.op, string(responseJSON), string(io.output.response)) } } }
func TestFlushStackHandler(t *testing.T) { s := pila.NewStack("stack", time.Now().UTC()) db := pila.NewDatabase("db") _ = db.AddStack(s) p := pila.NewPila() _ = p.AddDatabase(db) conn := NewConn() conn.Pila = p expectedStackStatusJSON, err := s.Status().ToJSON() if err != nil { t.Fatal(err) } s.Push("one") s.Push("two") s.Push("three") varss := []map[string]string{ { "database_id": db.ID.String(), "stack_id": s.ID.String(), }, { "database_id": db.Name, "stack_id": s.Name, }, } for _, vars := range varss { request, err := http.NewRequest("DELETE", fmt.Sprintf("/databases/%s/stacks/%s?flush", vars["database_id"], vars["stack_id"]), nil) if err != nil { t.Fatal(err) } response := httptest.NewRecorder() conn.flushStackHandler(response, request, s) if peek, ok := db.Stacks[s.ID].Pop(); ok { t.Errorf("stack contains %v, expected to be empty", peek) } if size := db.Stacks[s.ID].Size(); size != 0 { t.Errorf("stack has size %d, expected %d", size, 0) } if contentType := response.Header().Get("Content-Type"); contentType != "application/json" { t.Errorf("Content-Type is %v, expected %v", contentType, "application/json") } if response.Code != http.StatusOK { t.Errorf("response code is %v, expected %v", response.Code, http.StatusOK) } stackStatusJSON, err := ioutil.ReadAll(response.Body) if err != nil { t.Fatal(err) } if string(stackStatusJSON) != string(expectedStackStatusJSON) { t.Errorf("stack status is %s, expected %s", string(stackStatusJSON), string(expectedStackStatusJSON)) } // restore elements for next table test iteration s.Push("one") s.Push("two") s.Push("three") } }
func TestStackHandler_POST(t *testing.T) { s := pila.NewStack("stack", time.Now().UTC()) db := pila.NewDatabase("db") _ = db.AddStack(s) p := pila.NewPila() _ = p.AddDatabase(db) conn := NewConn() conn.Pila = p conn.opDate = time.Now().UTC() element := pila.Element{Value: "test-element"} expectedElementJSON, _ := element.ToJSON() paramss := []map[string]string{ { "database_id": db.ID.String(), "stack_id": s.ID.String(), }, { "database_id": db.Name, "stack_id": s.Name, }, } for _, params := range paramss { request, err := http.NewRequest("POST", fmt.Sprintf("/databases/%s/stacks/%s", params["database_id"], params["stack_id"]), bytes.NewBuffer(expectedElementJSON)) if err != nil { t.Fatal(err) } request.Header.Set("Content-Type", "application/json") response := httptest.NewRecorder() stackHandle := conn.stackHandler(¶ms) stackHandle.ServeHTTP(response, request) if contentType := response.Header().Get("Content-Type"); contentType != "application/json" { t.Errorf("Content-Type is %v, expected %v", contentType, "application/json") } if response.Code != http.StatusOK { t.Errorf("response code is %v, expected %v", response.Code, http.StatusOK) } elementJSON, err := ioutil.ReadAll(response.Body) if err != nil { t.Fatal(err) } if string(elementJSON) != string(expectedElementJSON) { t.Errorf("pushed element is %v, expected %v", string(elementJSON), string(expectedElementJSON)) } } }
// NewConfig creates a new Config with empty values. func NewConfig() *Config { return &Config{Values: pila.NewDatabase(CONFIG)} }
func TestStacksHandler_GET(t *testing.T) { now1 := time.Now().UTC() after1 := time.Now().UTC() now2 := time.Now().UTC() after2 := time.Now().UTC() s1 := pila.NewStack("stack1", now1) s1.Push("foo") s1.Update(after1) s2 := pila.NewStack("stack2", now2) s2.Push(1) s2.Push(8) s2.Update(after2) db := pila.NewDatabase("db") _ = db.AddStack(s1) _ = db.AddStack(s2) p := pila.NewPila() _ = p.AddDatabase(db) conn := NewConn() conn.Pila = p inputOutput := []struct { input, output string }{ {"/databases/db/stacks", fmt.Sprintf(`{"stacks":[{"id":"f0306fec639bd57fc2929c8b897b9b37","name":"stack1","peek":"foo","size":1,"created_at":"%v","updated_at":"%v","read_at":"%v"},{"id":"dde8f895aea2ffa5546336146b9384e7","name":"stack2","peek":8,"size":2,"created_at":"%v","updated_at":"%v","read_at":"%v"}]}`, date.Format(now1), date.Format(after1), date.Format(after1), date.Format(now2), date.Format(after2), date.Format(after2))}, {"/databases/db/stacks?kv", `{"stacks":{"stack1":"foo","stack2":8}}`}, } for _, io := range inputOutput { request, err := http.NewRequest("GET", io.input, nil) if err != nil { t.Fatal(err) } response := httptest.NewRecorder() stacksHandle := conn.stacksHandler(db.ID.String()) stacksHandle.ServeHTTP(response, request) if contentType := response.Header().Get("Content-Type"); contentType != "application/json" { t.Errorf("Content-Type is %v, expected %v", contentType, "application/json") } if response.Code != http.StatusOK { t.Errorf("response code is %v, expected %v", response.Code, http.StatusOK) } stacks, err := ioutil.ReadAll(response.Body) if err != nil { t.Fatal(err) } if string(stacks) != io.output { t.Errorf("stacks are %s, expected %s", string(stacks), io.output) } } }
func TestStatusStackHandler(t *testing.T) { s := pila.NewStack("stack", time.Now().UTC()) db := pila.NewDatabase("db") _ = db.AddStack(s) p := pila.NewPila() _ = p.AddDatabase(db) conn := NewConn() conn.Pila = p s.Push("one") expectedStackStatusJSON, err := s.Status().ToJSON() if err != nil { t.Fatal(err) } varss := []map[string]string{ { "database_id": db.ID.String(), "stack_id": s.ID.String(), }, { "database_id": db.Name, "stack_id": s.Name, }, } for _, vars := range varss { request, err := http.NewRequest("GET", fmt.Sprintf("/databases/%s/stacks/%s", vars["database_id"], vars["stack_id"]), nil) if err != nil { t.Fatal(err) } response := httptest.NewRecorder() conn.statusStackHandler(response, request, s) if peek := db.Stacks[s.ID].Peek(); peek != "one" { t.Errorf("peek is %v, expected %v", peek, "one") } if contentType := response.Header().Get("Content-Type"); contentType != "application/json" { t.Errorf("Content-Type is %v, expected %v", contentType, "application/json") } if response.Code != http.StatusOK { t.Errorf("response code is %v, expected %v", response.Code, http.StatusOK) } stackStatusJSON, err := ioutil.ReadAll(response.Body) if err != nil { t.Fatal(err) } if string(stackStatusJSON) != string(expectedStackStatusJSON) { t.Errorf("stack status is %s, expected %s", string(stackStatusJSON), string(expectedStackStatusJSON)) } } }