Delete an observation
This commit is contained in:
		
							parent
							
								
									b90da728fe
								
							
						
					
					
						commit
						952224e2df
					
				
					 9 changed files with 141 additions and 0 deletions
				
			
		|  | @ -51,6 +51,7 @@ func Handler() *mux.Router { | ||||||
| 	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)) | 	m.Get(router.UpdateObservation).Handler(handler(serveUpdateObservation)) | ||||||
|  | 	m.Get(router.DeleteObservation).Handler(handler(serveDeleteObservation)) | ||||||
| 
 | 
 | ||||||
| 	return m | 	return m | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -76,3 +76,17 @@ func serveUpdateObservation(w http.ResponseWriter, r *http.Request) error { | ||||||
| 
 | 
 | ||||||
| 	return writeJSON(w, observation) | 	return writeJSON(w, observation) | ||||||
| } | } | ||||||
|  | 
 | ||||||
|  | func serveDeleteObservation(w http.ResponseWriter, r *http.Request) error { | ||||||
|  | 	id, _ := strconv.ParseInt(mux.Vars(r)["Id"], 10, 0) | ||||||
|  | 
 | ||||||
|  | 	deleted, err := store.Observations.Delete(id) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return err | ||||||
|  | 	} | ||||||
|  | 	if deleted { | ||||||
|  | 		w.WriteHeader(http.StatusOK) | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	return writeJSON(w, &models.Observation{}) | ||||||
|  | } | ||||||
|  |  | ||||||
|  | @ -124,3 +124,30 @@ func TestObservation_Update(t *testing.T) { | ||||||
| 		t.Error("!success") | 		t.Error("!success") | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
|  | 
 | ||||||
|  | func TestObservation_Delete(t *testing.T) { | ||||||
|  | 	setup() | ||||||
|  | 
 | ||||||
|  | 	want := newObservation() | ||||||
|  | 
 | ||||||
|  | 	calledDelete := false | ||||||
|  | 	store.Observations.(*models.MockObservationsService).Delete_ = func(id int64) (bool, error) { | ||||||
|  | 		if id != want.Id { | ||||||
|  | 			t.Errorf("wanted request for observation %d but got %d", want.Id, id) | ||||||
|  | 		} | ||||||
|  | 		calledDelete = true | ||||||
|  | 		return true, nil | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	success, err := apiClient.Observations.Delete(want.Id) | ||||||
|  | 	if err != nil { | ||||||
|  | 		t.Fatal(err) | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	if !calledDelete { | ||||||
|  | 		t.Error("!calledDelete") | ||||||
|  | 	} | ||||||
|  | 	if !success { | ||||||
|  | 		t.Error("!success") | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  |  | ||||||
|  | @ -69,3 +69,19 @@ func (s *observationsStore) Update(id int64, observation *models.Observation) (b | ||||||
| 
 | 
 | ||||||
| 	return true, nil | 	return true, nil | ||||||
| } | } | ||||||
|  | 
 | ||||||
|  | func (s *observationsStore) Delete(id int64) (bool, error) { | ||||||
|  | 	observation, err := s.Get(id) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return false, err | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	deleted, err := s.dbh.Delete(observation) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return false, err | ||||||
|  | 	} | ||||||
|  | 	if deleted == 0 { | ||||||
|  | 		return false, ErrNoRowsDeleted | ||||||
|  | 	} | ||||||
|  | 	return true, nil | ||||||
|  | } | ||||||
|  |  | ||||||
|  | @ -108,3 +108,22 @@ func TestObservationsStore_Update_db(t *testing.T) { | ||||||
| 		t.Error("!updated") | 		t.Error("!updated") | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
|  | 
 | ||||||
|  | func TestObservationsStore_Delete_db(t *testing.T) { | ||||||
|  | 	tx, _ := DB.Begin() | ||||||
|  | 	defer tx.Rollback() | ||||||
|  | 
 | ||||||
|  | 	observation := insertObservation(t, tx) | ||||||
|  | 
 | ||||||
|  | 	d := NewDatastore(tx) | ||||||
|  | 
 | ||||||
|  | 	// Delete it | ||||||
|  | 	deleted, err := d.Observations.Delete(observation.Id) | ||||||
|  | 	if err != nil { | ||||||
|  | 		t.Fatal(err) | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	if !deleted { | ||||||
|  | 		t.Error("!delete") | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  |  | ||||||
|  | @ -38,6 +38,9 @@ type ObservationsService interface { | ||||||
| 
 | 
 | ||||||
| 	// Update an observation | 	// Update an observation | ||||||
| 	Update(id int64, Observation *Observation) (updated bool, err error) | 	Update(id int64, Observation *Observation) (updated bool, err error) | ||||||
|  | 
 | ||||||
|  | 	// Delete an observation | ||||||
|  | 	Delete(id int64) (deleted bool, err error) | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| var ( | var ( | ||||||
|  | @ -134,11 +137,34 @@ func (s *observationsService) Update(id int64, observation *Observation) (bool, | ||||||
| 	return resp.StatusCode == http.StatusOK, nil | 	return resp.StatusCode == http.StatusOK, nil | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | func (s *observationsService) Delete(id int64) (bool, error) { | ||||||
|  | 	strId := strconv.FormatInt(id, 10) | ||||||
|  | 
 | ||||||
|  | 	url, err := s.client.url(router.DeleteObservation, map[string]string{"Id": strId}, nil) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return false, err | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	req, err := s.client.NewRequest("DELETE", url.String(), nil) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return false, err | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	var observation *Observation | ||||||
|  | 	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) | 	Update_ func(id int64, observation *Observation) (bool, error) | ||||||
|  | 	Delete_ func(id int64) (bool, error) | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| var _ObservationsService = &MockObservationsService{} | var _ObservationsService = &MockObservationsService{} | ||||||
|  | @ -170,3 +196,10 @@ func (s *MockObservationsService) Update(id int64, observation *Observation) (bo | ||||||
| 	} | 	} | ||||||
| 	return s.Update_(id, observation) | 	return s.Update_(id, observation) | ||||||
| } | } | ||||||
|  | 
 | ||||||
|  | func (s *MockObservationsService) Delete(id int64) (bool, error) { | ||||||
|  | 	if s.Delete_ == nil { | ||||||
|  | 		return false, nil | ||||||
|  | 	} | ||||||
|  | 	return s.Delete_(id) | ||||||
|  | } | ||||||
|  |  | ||||||
|  | @ -143,3 +143,32 @@ func TestObservationService_Update(t *testing.T) { | ||||||
| 		t.Fatal("!called") | 		t.Fatal("!called") | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
|  | 
 | ||||||
|  | func TestObservationService_Delete(t *testing.T) { | ||||||
|  | 	setup() | ||||||
|  | 	defer teardown() | ||||||
|  | 
 | ||||||
|  | 	want := newObservation() | ||||||
|  | 
 | ||||||
|  | 	var called bool | ||||||
|  | 	mux.HandleFunc(urlPath(t, router.DeleteObservation, map[string]string{"Id": "1"}), func(w http.ResponseWriter, r *http.Request) { | ||||||
|  | 		called = true | ||||||
|  | 		testMethod(t, r, "DELETE") | ||||||
|  | 
 | ||||||
|  | 		w.WriteHeader(http.StatusOK) | ||||||
|  | 		writeJSON(w, want) | ||||||
|  | 	}) | ||||||
|  | 
 | ||||||
|  | 	deleted, err := client.Observations.Delete(want.Id) | ||||||
|  | 	if err != nil { | ||||||
|  | 		t.Errorf("Observations.Delete returned error: %v", err) | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	if !deleted { | ||||||
|  | 		t.Error("!deleted") | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	if !called { | ||||||
|  | 		t.Fatal("!called") | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  |  | ||||||
|  | @ -43,6 +43,7 @@ func API() *mux.Router { | ||||||
| 	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) | 	m.Path("/observations/{Id:.+}").Methods("PUT").Name(UpdateObservation) | ||||||
|  | 	m.Path("/observations/{Id:.+}").Methods("DELETE").Name(DeleteObservation) | ||||||
| 
 | 
 | ||||||
| 	return m | 	return m | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -33,4 +33,5 @@ const ( | ||||||
| 	CreateObservation = "observation:create" | 	CreateObservation = "observation:create" | ||||||
| 	Observations      = "observation:list" | 	Observations      = "observation:list" | ||||||
| 	UpdateObservation = "observation:update" | 	UpdateObservation = "observation:update" | ||||||
|  | 	DeleteObservation = "observation:delete" | ||||||
| ) | ) | ||||||
|  |  | ||||||
		Reference in a new issue
	
	 Matthew Dillon
						Matthew Dillon