Update an observation
This commit is contained in:
parent
dac0721a41
commit
b90da728fe
9 changed files with 158 additions and 0 deletions
|
@ -35,6 +35,9 @@ type ObservationsService interface {
|
|||
|
||||
// Create an observation
|
||||
Create(observation *Observation) (bool, error)
|
||||
|
||||
// Update an observation
|
||||
Update(id int64, Observation *Observation) (updated bool, err error)
|
||||
}
|
||||
|
||||
var (
|
||||
|
@ -110,10 +113,32 @@ func (s *observationsService) List(opt *ObservationListOptions) ([]*Observation,
|
|||
return observations, nil
|
||||
}
|
||||
|
||||
func (s *observationsService) Update(id int64, observation *Observation) (bool, error) {
|
||||
strId := strconv.FormatInt(id, 10)
|
||||
|
||||
url, err := s.client.url(router.UpdateObservation, map[string]string{"Id": strId}, nil)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
req, err := s.client.NewRequest("PUT", url.String(), observation)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
resp, err := s.client.Do(req, &observation)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
return resp.StatusCode == http.StatusOK, nil
|
||||
}
|
||||
|
||||
type MockObservationsService struct {
|
||||
Get_ func(id int64) (*Observation, error)
|
||||
List_ func(opt *ObservationListOptions) ([]*Observation, error)
|
||||
Create_ func(observation *Observation) (bool, error)
|
||||
Update_ func(id int64, observation *Observation) (bool, error)
|
||||
}
|
||||
|
||||
var _ObservationsService = &MockObservationsService{}
|
||||
|
@ -138,3 +163,10 @@ func (s *MockObservationsService) List(opt *ObservationListOptions) ([]*Observat
|
|||
}
|
||||
return s.List_(opt)
|
||||
}
|
||||
|
||||
func (s *MockObservationsService) Update(id int64, observation *Observation) (bool, error) {
|
||||
if s.Update_ == nil {
|
||||
return false, nil
|
||||
}
|
||||
return s.Update_(id, observation)
|
||||
}
|
||||
|
|
|
@ -112,3 +112,34 @@ func TestObservationService_List(t *testing.T) {
|
|||
t.Errorf("Observations.List return %+v, want %+v", observations, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestObservationService_Update(t *testing.T) {
|
||||
setup()
|
||||
defer teardown()
|
||||
|
||||
want := newObservation()
|
||||
|
||||
var called bool
|
||||
mux.HandleFunc(urlPath(t, router.UpdateObservation, map[string]string{"Id": "1"}), func(w http.ResponseWriter, r *http.Request) {
|
||||
called = true
|
||||
testMethod(t, r, "PUT")
|
||||
testBody(t, r, `{"id":1,"observation_name":"Test Obs Updated","observation_type_id":0,"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 := newObservation()
|
||||
observation.ObservationName = "Test Obs Updated"
|
||||
updated, err := client.Observations.Update(observation.Id, observation)
|
||||
if err != nil {
|
||||
t.Errorf("Observations.Update returned error: %v", err)
|
||||
}
|
||||
|
||||
if !updated {
|
||||
t.Error("!updated")
|
||||
}
|
||||
|
||||
if !called {
|
||||
t.Fatal("!called")
|
||||
}
|
||||
}
|
||||
|
|
Reference in a new issue