Create text measurement type

This commit is contained in:
Matthew Dillon 2014-11-26 22:13:46 -09:00
parent 3e0ca9df8c
commit b1dad9bfbd
9 changed files with 154 additions and 3 deletions

View file

@ -2,6 +2,7 @@ package models
import (
"errors"
"net/http"
"strconv"
"time"
@ -27,6 +28,9 @@ func NewTextMeasurementType() *TextMeasurementType {
type TextMeasurementTypesService interface {
// Get a text measurement type
Get(id int64) (*TextMeasurementType, error)
// Create a text measurement type
Create(text_measurement_type *TextMeasurementType) (bool, error)
}
var (
@ -59,8 +63,28 @@ func (s *textMeasurementTypesService) Get(id int64) (*TextMeasurementType, error
return text_measurement_type, nil
}
func (s *textMeasurementTypesService) Create(text_measurement_type *TextMeasurementType) (bool, error) {
url, err := s.client.url(router.CreateTextMeasurementType, nil, nil)
if err != nil {
return false, err
}
req, err := s.client.NewRequest("POST", url.String(), text_measurement_type)
if err != nil {
return false, err
}
resp, err := s.client.Do(req, &text_measurement_type)
if err != nil {
return false, err
}
return resp.StatusCode == http.StatusCreated, nil
}
type MockTextMeasurementTypesService struct {
Get_ func(id int64) (*TextMeasurementType, error)
Get_ func(id int64) (*TextMeasurementType, error)
Create_ func(text_measurement_type *TextMeasurementType) (bool, error)
}
var _ TextMeasurementTypesService = &MockTextMeasurementTypesService{}
@ -71,3 +95,10 @@ func (s *MockTextMeasurementTypesService) Get(id int64) (*TextMeasurementType, e
}
return s.Get_(id)
}
func (s *MockTextMeasurementTypesService) Create(text_measurement_type *TextMeasurementType) (bool, error) {
if s.Create_ == nil {
return false, nil
}
return s.Create_(text_measurement_type)
}

View file

@ -43,3 +43,39 @@ func TestTextMeasurementTypeService_Get(t *testing.T) {
t.Errorf("TextMeasurementTypes.Get return %+v, want %+v", text_measurement_type, want)
}
}
func TestTextMeasurementTypeService_Create(t *testing.T) {
setup()
defer teardown()
want := newTextMeasurementType()
var called bool
mux.HandleFunc(urlPath(t, router.CreateTextMeasurementType, nil), func(w http.ResponseWriter, r *http.Request) {
called = true
testMethod(t, r, "POST")
testBody(t, r, `{"id":1,"textMeasurementName":"Test Text Measurement Type","createdAt":"0001-01-01T00:00:00Z","updatedAt":"0001-01-01T00:00:00Z","deletedAt":{"Time":"0001-01-01T00:00:00Z","Valid":false}}`+"\n")
w.WriteHeader(http.StatusCreated)
writeJSON(w, want)
})
text_measurement_type := newTextMeasurementType()
created, err := client.TextMeasurementTypes.Create(text_measurement_type)
if err != nil {
t.Errorf("TextMeasurementTypes.Create returned error: %v", err)
}
if !created {
t.Error("!created")
}
if !called {
t.Fatal("!called")
}
normalizeTime(&want.CreatedAt, &want.UpdatedAt, &want.DeletedAt)
if !reflect.DeepEqual(text_measurement_type, want) {
t.Errorf("TextMeasurementTypes.Create returned %+v, want %+v", text_measurement_type, want)
}
}