List unit types

This commit is contained in:
Matthew Dillon 2014-11-30 15:33:28 -09:00
parent b7caffa373
commit d535cd85ff
9 changed files with 152 additions and 0 deletions

View file

@ -31,6 +31,9 @@ type UnitTypesService interface {
// Get a unit type
Get(id int64) (*UnitType, error)
// List all unit types
List(opt *UnitTypeListOptions) ([]*UnitType, error)
// Create a unit type
Create(unit_type *UnitType) (bool, error)
}
@ -84,8 +87,33 @@ func (s *unitTypesService) Create(unit_type *UnitType) (bool, error) {
return resp.StatusCode == http.StatusCreated, nil
}
type UnitTypeListOptions struct {
ListOptions
}
func (s *unitTypesService) List(opt *UnitTypeListOptions) ([]*UnitType, error) {
url, err := s.client.url(router.UnitTypes, nil, opt)
if err != nil {
return nil, err
}
req, err := s.client.NewRequest("GET", url.String(), nil)
if err != nil {
return nil, err
}
var unit_types []*UnitType
_, err = s.client.Do(req, &unit_types)
if err != nil {
return nil, err
}
return unit_types, nil
}
type MockUnitTypesService struct {
Get_ func(id int64) (*UnitType, error)
List_ func(opt *UnitTypeListOptions) ([]*UnitType, error)
Create_ func(unit_type *UnitType) (bool, error)
}
@ -104,3 +132,10 @@ func (s *MockUnitTypesService) Create(unit_type *UnitType) (bool, error) {
}
return s.Create_(unit_type)
}
func (s *MockUnitTypesService) List(opt *UnitTypeListOptions) ([]*UnitType, error) {
if s.List_ == nil {
return nil, nil
}
return s.List_(opt)
}

View file

@ -79,3 +79,36 @@ func TestUnitTypeService_Create(t *testing.T) {
t.Errorf("UnitTypes.Create returned %+v, want %+v", unit_type, want)
}
}
func TestUnitTypeService_List(t *testing.T) {
setup()
defer teardown()
want := []*UnitType{newUnitType()}
var called bool
mux.HandleFunc(urlPath(t, router.UnitTypes, nil), func(w http.ResponseWriter, r *http.Request) {
called = true
testMethod(t, r, "GET")
testFormValues(t, r, values{})
writeJSON(w, want)
})
unit_types, err := client.UnitTypes.List(nil)
if err != nil {
t.Errorf("UnitTypes.List returned error: %v", err)
}
if !called {
t.Fatal("!called")
}
for _, u := range want {
normalizeTime(&u.CreatedAt, &u.UpdatedAt, &u.DeletedAt)
}
if !reflect.DeepEqual(unit_types, want) {
t.Errorf("UnitTypes.List return %+v, want %+v", unit_types, want)
}
}