Species - delete

This commit is contained in:
Matthew Dillon 2014-10-24 10:42:31 -08:00
parent 22d2b8b41d
commit 4669aff3c2
9 changed files with 161 additions and 0 deletions

View file

@ -32,6 +32,9 @@ type SpeciesService interface {
// Update an existing species
Update(id int64, species *Species) (updated bool, err error)
// Delete an existing species
Delete(id int64) (deleted bool, err error)
}
var (
@ -129,11 +132,34 @@ func (s *speciesService) Update(id int64, species *Species) (bool, error) {
return resp.StatusCode == http.StatusOK, nil
}
func (s *speciesService) Delete(id int64) (bool, error) {
strId := strconv.FormatInt(id, 10)
url, err := s.client.url(router.DeleteSpecies, 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 species *Species
resp, err := s.client.Do(req, &species)
if err != nil {
return false, err
}
return resp.StatusCode == http.StatusOK, nil
}
type MockSpeciesService struct {
Get_ func(id int64) (*Species, error)
List_ func(opt *SpeciesListOptions) ([]*Species, error)
Create_ func(species *Species) (bool, error)
Update_ func(id int64, species *Species) (bool, error)
Delete_ func(id int64) (bool, error)
}
var _ SpeciesService = &MockSpeciesService{}
@ -165,3 +191,10 @@ func (s *MockSpeciesService) Update(id int64, species *Species) (bool, error) {
}
return s.Update_(id, species)
}
func (s *MockSpeciesService) Delete(id int64) (bool, error) {
if s.Delete_ == nil {
return false, nil
}
return s.Delete_(id)
}

View file

@ -136,3 +136,32 @@ func TestSpeciesService_Update(t *testing.T) {
t.Fatal("!called")
}
}
func TestSpeciesService_Delete(t *testing.T) {
setup()
defer teardown()
want := &Species{Id: 1, GenusId: 1, SpeciesName: "Test Species"}
var called bool
mux.HandleFunc(urlPath(t, router.DeleteSpecies, 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.Species.Delete(1)
if err != nil {
t.Errorf("Species.Delete returned error: %v", err)
}
if !deleted {
t.Error("!deleted")
}
if !called {
t.Fatal("!called")
}
}