Delete strain.

This commit is contained in:
Matthew Dillon 2014-10-29 14:11:50 -08:00
parent ff0e37d2ef
commit df95dfc930
9 changed files with 141 additions and 0 deletions

View file

@ -40,6 +40,9 @@ type StrainsService interface {
// Update an existing strain
Update(id int64, strain *Strain) (updated bool, err error)
// Delete an existing strain
Delete(id int64) (deleted bool, err error)
}
var (
@ -136,11 +139,34 @@ func (s *strainsService) Update(id int64, strain *Strain) (bool, error) {
return resp.StatusCode == http.StatusOK, nil
}
func (s *strainsService) Delete(id int64) (bool, error) {
strId := strconv.FormatInt(id, 10)
url, err := s.client.url(router.DeleteStrain, 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 strain *Strain
resp, err := s.client.Do(req, &strain)
if err != nil {
return false, err
}
return resp.StatusCode == http.StatusOK, nil
}
type MockStrainsService struct {
Get_ func(id int64) (*Strain, error)
List_ func(opt *StrainListOptions) ([]*Strain, error)
Create_ func(strain *Strain) (bool, error)
Update_ func(id int64, strain *Strain) (bool, error)
Delete_ func(id int64) (bool, error)
}
var _ StrainsService = &MockStrainsService{}
@ -172,3 +198,10 @@ func (s *MockStrainsService) Update(id int64, strain *Strain) (bool, error) {
}
return s.Update_(id, strain)
}
func (s *MockStrainsService) Delete(id int64) (bool, error) {
if s.Delete_ == nil {
return false, nil
}
return s.Delete_(id)
}

View file

@ -148,3 +148,32 @@ func TestStrainService_Update(t *testing.T) {
t.Fatal("!called")
}
}
func TestStrainService_Delete(t *testing.T) {
setup()
defer teardown()
want := newStrain()
var called bool
mux.HandleFunc(urlPath(t, router.DeleteStrain, 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.Strains.Delete(want.Id)
if err != nil {
t.Errorf("Strains.Delete returned error: %v", err)
}
if !deleted {
t.Error("!deleted")
}
if !called {
t.Fatal("!called")
}
}