Update observation types.

This commit is contained in:
Matthew Dillon 2014-11-10 15:13:45 -09:00
parent 186edad1db
commit 73f7b580c1
9 changed files with 158 additions and 0 deletions

View file

@ -44,6 +44,7 @@ func Handler() *mux.Router {
m.Get(router.ObservationType).Handler(handler(serveObservationType))
m.Get(router.CreateObservationType).Handler(handler(serveCreateObservationType))
m.Get(router.ObservationTypes).Handler(handler(serveObservationTypeList))
m.Get(router.UpdateObservationType).Handler(handler(serveUpdateObservationType))
return m
}

View file

@ -57,3 +57,22 @@ func serveObservationTypeList(w http.ResponseWriter, r *http.Request) error {
return writeJSON(w, observation_types)
}
func serveUpdateObservationType(w http.ResponseWriter, r *http.Request) error {
id, _ := strconv.ParseInt(mux.Vars(r)["Id"], 10, 0)
var observation_type models.ObservationType
err := json.NewDecoder(r.Body).Decode(&observation_type)
if err != nil {
return err
}
updated, err := store.ObservationTypes.Update(id, &observation_type)
if err != nil {
return err
}
if updated {
w.WriteHeader(http.StatusOK)
}
return writeJSON(w, observation_type)
}

View file

@ -94,3 +94,33 @@ func TestObservationType_List(t *testing.T) {
t.Errorf("got observation_types %+v but wanted observation_types %+v", observation_types, want)
}
}
func TestObservationType_Update(t *testing.T) {
setup()
want := newObservationType()
calledPut := false
store.ObservationTypes.(*models.MockObservationTypesService).Update_ = func(id int64, observation_type *models.ObservationType) (bool, error) {
if id != want.Id {
t.Errorf("wanted request for observation_type %d but got %d", want.Id, id)
}
if !normalizeDeepEqual(want, observation_type) {
t.Errorf("wanted request for observation_type %d but got %d", want, observation_type)
}
calledPut = true
return true, nil
}
success, err := apiClient.ObservationTypes.Update(want.Id, want)
if err != nil {
t.Fatal(err)
}
if !calledPut {
t.Error("!calledPut")
}
if !success {
t.Error("!success")
}
}