Update an observation
This commit is contained in:
		
							parent
							
								
									dac0721a41
								
							
						
					
					
						commit
						b90da728fe
					
				
					 9 changed files with 158 additions and 0 deletions
				
			
		|  | @ -50,6 +50,7 @@ func Handler() *mux.Router { | ||||||
| 	m.Get(router.Observation).Handler(handler(serveObservation)) | 	m.Get(router.Observation).Handler(handler(serveObservation)) | ||||||
| 	m.Get(router.CreateObservation).Handler(handler(serveCreateObservation)) | 	m.Get(router.CreateObservation).Handler(handler(serveCreateObservation)) | ||||||
| 	m.Get(router.Observations).Handler(handler(serveObservationList)) | 	m.Get(router.Observations).Handler(handler(serveObservationList)) | ||||||
|  | 	m.Get(router.UpdateObservation).Handler(handler(serveUpdateObservation)) | ||||||
| 
 | 
 | ||||||
| 	return m | 	return m | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -57,3 +57,22 @@ func serveObservationList(w http.ResponseWriter, r *http.Request) error { | ||||||
| 
 | 
 | ||||||
| 	return writeJSON(w, observations) | 	return writeJSON(w, observations) | ||||||
| } | } | ||||||
|  | 
 | ||||||
|  | func serveUpdateObservation(w http.ResponseWriter, r *http.Request) error { | ||||||
|  | 	id, _ := strconv.ParseInt(mux.Vars(r)["Id"], 10, 0) | ||||||
|  | 	var observation models.Observation | ||||||
|  | 	err := json.NewDecoder(r.Body).Decode(&observation) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return err | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	updated, err := store.Observations.Update(id, &observation) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return err | ||||||
|  | 	} | ||||||
|  | 	if updated { | ||||||
|  | 		w.WriteHeader(http.StatusOK) | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	return writeJSON(w, observation) | ||||||
|  | } | ||||||
|  |  | ||||||
|  | @ -94,3 +94,33 @@ func TestObservation_List(t *testing.T) { | ||||||
| 		t.Errorf("got observations %+v but wanted observations %+v", observations, want) | 		t.Errorf("got observations %+v but wanted observations %+v", observations, want) | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
|  | 
 | ||||||
|  | func TestObservation_Update(t *testing.T) { | ||||||
|  | 	setup() | ||||||
|  | 
 | ||||||
|  | 	want := newObservation() | ||||||
|  | 
 | ||||||
|  | 	calledPut := false | ||||||
|  | 	store.Observations.(*models.MockObservationsService).Update_ = func(id int64, observation *models.Observation) (bool, error) { | ||||||
|  | 		if id != want.Id { | ||||||
|  | 			t.Errorf("wanted request for observation %d but got %d", want.Id, id) | ||||||
|  | 		} | ||||||
|  | 		if !normalizeDeepEqual(want, observation) { | ||||||
|  | 			t.Errorf("wanted request for observation %d but got %d", want, observation) | ||||||
|  | 		} | ||||||
|  | 		calledPut = true | ||||||
|  | 		return true, nil | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	success, err := apiClient.Observations.Update(want.Id, want) | ||||||
|  | 	if err != nil { | ||||||
|  | 		t.Fatal(err) | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	if !calledPut { | ||||||
|  | 		t.Error("!calledPut") | ||||||
|  | 	} | ||||||
|  | 	if !success { | ||||||
|  | 		t.Error("!success") | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  |  | ||||||
|  | @ -46,3 +46,26 @@ func (s *observationsStore) List(opt *models.ObservationListOptions) ([]*models. | ||||||
| 	} | 	} | ||||||
| 	return observations, nil | 	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 | ||||||
|  | } | ||||||
|  |  | ||||||
|  | @ -88,3 +88,23 @@ func TestObservationsStore_List_db(t *testing.T) { | ||||||
| 		t.Errorf("got observations %+v, want %+v", observations, want) | 		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") | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  |  | ||||||
|  | @ -35,6 +35,9 @@ type ObservationsService interface { | ||||||
| 
 | 
 | ||||||
| 	// Create an observation | 	// Create an observation | ||||||
| 	Create(observation *Observation) (bool, error) | 	Create(observation *Observation) (bool, error) | ||||||
|  | 
 | ||||||
|  | 	// Update an observation | ||||||
|  | 	Update(id int64, Observation *Observation) (updated bool, err error) | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| var ( | var ( | ||||||
|  | @ -110,10 +113,32 @@ func (s *observationsService) List(opt *ObservationListOptions) ([]*Observation, | ||||||
| 	return observations, nil | 	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 { | type MockObservationsService struct { | ||||||
| 	Get_    func(id int64) (*Observation, error) | 	Get_    func(id int64) (*Observation, error) | ||||||
| 	List_   func(opt *ObservationListOptions) ([]*Observation, error) | 	List_   func(opt *ObservationListOptions) ([]*Observation, error) | ||||||
| 	Create_ func(observation *Observation) (bool, error) | 	Create_ func(observation *Observation) (bool, error) | ||||||
|  | 	Update_ func(id int64, observation *Observation) (bool, error) | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| var _ObservationsService = &MockObservationsService{} | var _ObservationsService = &MockObservationsService{} | ||||||
|  | @ -138,3 +163,10 @@ func (s *MockObservationsService) List(opt *ObservationListOptions) ([]*Observat | ||||||
| 	} | 	} | ||||||
| 	return s.List_(opt) | 	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) | 		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") | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  |  | ||||||
|  | @ -42,6 +42,7 @@ func API() *mux.Router { | ||||||
| 	m.Path("/observations").Methods("GET").Name(Observations) | 	m.Path("/observations").Methods("GET").Name(Observations) | ||||||
| 	m.Path("/observations").Methods("POST").Name(CreateObservation) | 	m.Path("/observations").Methods("POST").Name(CreateObservation) | ||||||
| 	m.Path("/observations/{Id:.+}").Methods("GET").Name(Observation) | 	m.Path("/observations/{Id:.+}").Methods("GET").Name(Observation) | ||||||
|  | 	m.Path("/observations/{Id:.+}").Methods("PUT").Name(UpdateObservation) | ||||||
| 
 | 
 | ||||||
| 	return m | 	return m | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -32,4 +32,5 @@ const ( | ||||||
| 	Observation       = "observation:get" | 	Observation       = "observation:get" | ||||||
| 	CreateObservation = "observation:create" | 	CreateObservation = "observation:create" | ||||||
| 	Observations      = "observation:list" | 	Observations      = "observation:list" | ||||||
|  | 	UpdateObservation = "observation:update" | ||||||
| ) | ) | ||||||
|  |  | ||||||
		Reference in a new issue
	
	 Matthew Dillon
						Matthew Dillon