Delete a unit type

This commit is contained in:
Matthew Dillon 2014-11-30 16:10:38 -09:00
parent 76baee1fa7
commit eccbffb86d
9 changed files with 141 additions and 0 deletions

View file

@ -39,6 +39,9 @@ type UnitTypesService interface {
// Update a unit type
Update(id int64, UnitType *UnitType) (bool, error)
// Delete a unit type
Delete(id int64) (deleted bool, err error)
}
var (
@ -135,11 +138,34 @@ func (s *unitTypesService) Update(id int64, unit_type *UnitType) (bool, error) {
return resp.StatusCode == http.StatusOK, nil
}
func (s *unitTypesService) Delete(id int64) (bool, error) {
strId := strconv.FormatInt(id, 10)
url, err := s.client.url(router.DeleteUnitType, 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 unit_type *UnitType
resp, err := s.client.Do(req, &unit_type)
if err != nil {
return false, err
}
return resp.StatusCode == http.StatusOK, nil
}
type MockUnitTypesService struct {
Get_ func(id int64) (*UnitType, error)
List_ func(opt *UnitTypeListOptions) ([]*UnitType, error)
Create_ func(unit_type *UnitType) (bool, error)
Update_ func(id int64, unit_type *UnitType) (bool, error)
Delete_ func(id int64) (bool, error)
}
var _ UnitTypesService = &MockUnitTypesService{}
@ -171,3 +197,10 @@ func (s *MockUnitTypesService) Update(id int64, unit_type *UnitType) (bool, erro
}
return s.Update_(id, unit_type)
}
func (s *MockUnitTypesService) Delete(id int64) (bool, error) {
if s.Delete_ == nil {
return false, nil
}
return s.Delete_(id)
}

View file

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