Update an observation

This commit is contained in:
Matthew Dillon 2014-11-21 16:48:55 -09:00
parent dac0721a41
commit b90da728fe
9 changed files with 158 additions and 0 deletions

View file

@ -46,3 +46,26 @@ func (s *observationsStore) List(opt *models.ObservationListOptions) ([]*models.
}
return observations, nil
}
func (s *observationsStore) Update(id int64, observation *models.Observation) (bool, error) {
_, err := s.Get(id)
if err != nil {
return false, err
}
if id != observation.Id {
return false, models.ErrObservationNotFound
}
observation.UpdatedAt = time.Now()
changed, err := s.dbh.Update(observation)
if err != nil {
return false, err
}
if changed == 0 {
return false, ErrNoRowsUpdated
}
return true, nil
}

View file

@ -88,3 +88,23 @@ func TestObservationsStore_List_db(t *testing.T) {
t.Errorf("got observations %+v, want %+v", observations, want)
}
}
func TestObservationsStore_Update_db(t *testing.T) {
tx, _ := DB.Begin()
defer tx.Rollback()
observation := insertObservation(t, tx)
d := NewDatastore(tx)
// Tweak it
observation.ObservationName = "Updated Obs"
updated, err := d.Observations.Update(observation.Id, observation)
if err != nil {
t.Fatal(err)
}
if !updated {
t.Error("!updated")
}
}