Update observation types.
This commit is contained in:
parent
186edad1db
commit
73f7b580c1
9 changed files with 158 additions and 0 deletions
|
@ -34,6 +34,9 @@ type ObservationTypesService interface {
|
|||
|
||||
// Create an observation type record
|
||||
Create(observation_type *ObservationType) (bool, error)
|
||||
|
||||
// Update an existing observation type
|
||||
Update(id int64, observation_type *ObservationType) (updated bool, err error)
|
||||
}
|
||||
|
||||
var (
|
||||
|
@ -109,10 +112,32 @@ func (s *observationTypesService) List(opt *ObservationTypeListOptions) ([]*Obse
|
|||
return observation_types, nil
|
||||
}
|
||||
|
||||
func (s *observationTypesService) Update(id int64, observation_type *ObservationType) (bool, error) {
|
||||
strId := strconv.FormatInt(id, 10)
|
||||
|
||||
url, err := s.client.url(router.UpdateObservationType, map[string]string{"Id": strId}, nil)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
req, err := s.client.NewRequest("PUT", 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.StatusOK, nil
|
||||
}
|
||||
|
||||
type MockObservationTypesService struct {
|
||||
Get_ func(id int64) (*ObservationType, error)
|
||||
List_ func(opt *ObservationTypeListOptions) ([]*ObservationType, error)
|
||||
Create_ func(observation_type *ObservationType) (bool, error)
|
||||
Update_ func(id int64, observation_type *ObservationType) (bool, error)
|
||||
}
|
||||
|
||||
var _ ObservationTypesService = &MockObservationTypesService{}
|
||||
|
@ -137,3 +162,10 @@ func (s *MockObservationTypesService) List(opt *ObservationTypeListOptions) ([]*
|
|||
}
|
||||
return s.List_(opt)
|
||||
}
|
||||
|
||||
func (s *MockObservationTypesService) Update(id int64, observation_type *ObservationType) (bool, error) {
|
||||
if s.Update_ == nil {
|
||||
return false, nil
|
||||
}
|
||||
return s.Update_(id, observation_type)
|
||||
}
|
||||
|
|
|
@ -112,3 +112,34 @@ func TestObservationTypeService_List(t *testing.T) {
|
|||
t.Errorf("ObservationTypes.List return %+v, want %+v", observation_types, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestObservationTypeService_Update(t *testing.T) {
|
||||
setup()
|
||||
defer teardown()
|
||||
|
||||
want := newObservationType()
|
||||
|
||||
var called bool
|
||||
mux.HandleFunc(urlPath(t, router.UpdateObservationType, map[string]string{"Id": "1"}), func(w http.ResponseWriter, r *http.Request) {
|
||||
called = true
|
||||
testMethod(t, r, "PUT")
|
||||
testBody(t, r, `{"id":1,"observation_type_name":"Test Obs Type Updated","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.StatusOK)
|
||||
writeJSON(w, want)
|
||||
})
|
||||
|
||||
observation_type := newObservationType()
|
||||
observation_type.ObservationTypeName = "Test Obs Type Updated"
|
||||
updated, err := client.ObservationTypes.Update(observation_type.Id, observation_type)
|
||||
if err != nil {
|
||||
t.Errorf("ObservationTypes.Update returned error: %v", err)
|
||||
}
|
||||
|
||||
if !updated {
|
||||
t.Error("!updated")
|
||||
}
|
||||
|
||||
if !called {
|
||||
t.Fatal("!called")
|
||||
}
|
||||
}
|
||||
|
|
Reference in a new issue