create an observation type
This commit is contained in:
parent
4fd7bf8eba
commit
0e767390b5
9 changed files with 155 additions and 4 deletions
|
@ -2,6 +2,7 @@ package models
|
|||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
|
@ -27,6 +28,9 @@ func NewObservationType() *ObservationType {
|
|||
type ObservationTypesService interface {
|
||||
// Get an observation type
|
||||
Get(id int64) (*ObservationType, error)
|
||||
|
||||
// Create an observation type record
|
||||
Create(observation_type *ObservationType) (bool, error)
|
||||
}
|
||||
|
||||
var (
|
||||
|
@ -59,8 +63,28 @@ func (s *observationTypesService) Get(id int64) (*ObservationType, error) {
|
|||
return observation_type, nil
|
||||
}
|
||||
|
||||
func (s *observationTypesService) Create(observation_type *ObservationType) (bool, error) {
|
||||
url, err := s.client.url(router.CreateObservationType, nil, nil)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
req, err := s.client.NewRequest("POST", url.String(), observation_type)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
resp, err := s.client.Do(req, &observation_type)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
return resp.StatusCode == http.StatusCreated, nil
|
||||
}
|
||||
|
||||
type MockObservationTypesService struct {
|
||||
Get_ func(id int64) (*ObservationType, error)
|
||||
Get_ func(id int64) (*ObservationType, error)
|
||||
Create_ func(observation_type *ObservationType) (bool, error)
|
||||
}
|
||||
|
||||
var _ ObservationTypesService = &MockObservationTypesService{}
|
||||
|
@ -71,3 +95,10 @@ func (s *MockObservationTypesService) Get(id int64) (*ObservationType, error) {
|
|||
}
|
||||
return s.Get_(id)
|
||||
}
|
||||
|
||||
func (s *MockObservationTypesService) Create(observation_type *ObservationType) (bool, error) {
|
||||
if s.Create_ == nil {
|
||||
return false, nil
|
||||
}
|
||||
return s.Create_(observation_type)
|
||||
}
|
||||
|
|
|
@ -14,7 +14,7 @@ func newObservationType() *ObservationType {
|
|||
return observation_type
|
||||
}
|
||||
|
||||
func TestObservation_TypeService_Get(t *testing.T) {
|
||||
func TestObservationTypeService_Get(t *testing.T) {
|
||||
setup()
|
||||
defer teardown()
|
||||
|
||||
|
@ -43,3 +43,39 @@ func TestObservation_TypeService_Get(t *testing.T) {
|
|||
t.Errorf("ObservationTypes.Get return %+v, want %+v", observation_type, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestObservationTypeService_Create(t *testing.T) {
|
||||
setup()
|
||||
defer teardown()
|
||||
|
||||
want := newObservationType()
|
||||
|
||||
var called bool
|
||||
mux.HandleFunc(urlPath(t, router.CreateObservationType, nil), func(w http.ResponseWriter, r *http.Request) {
|
||||
called = true
|
||||
testMethod(t, r, "POST")
|
||||
testBody(t, r, `{"id":1,"observation_type_name":"Test Obs Type","created_at":"0001-01-01T00:00:00Z","updated_at":"0001-01-01T00:00:00Z","deleted_at":{"Time":"0001-01-01T00:00:00Z","Valid":false}}`+"\n")
|
||||
|
||||
w.WriteHeader(http.StatusCreated)
|
||||
writeJSON(w, want)
|
||||
})
|
||||
|
||||
observation_type := newObservationType()
|
||||
created, err := client.ObservationTypes.Create(observation_type)
|
||||
if err != nil {
|
||||
t.Errorf("ObservationTypes.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(observation_type, want) {
|
||||
t.Errorf("ObservationTypes.Create returned %+v, want %+v", observation_type, want)
|
||||
}
|
||||
}
|
||||
|
|
Reference in a new issue