List Observation types
This commit is contained in:
parent
9326a06699
commit
186edad1db
9 changed files with 152 additions and 0 deletions
|
@ -29,6 +29,9 @@ type ObservationTypesService interface {
|
|||
// Get an observation type
|
||||
Get(id int64) (*ObservationType, error)
|
||||
|
||||
// List all observation types
|
||||
List(opt *ObservationTypeListOptions) ([]*ObservationType, error)
|
||||
|
||||
// Create an observation type record
|
||||
Create(observation_type *ObservationType) (bool, error)
|
||||
}
|
||||
|
@ -82,8 +85,33 @@ func (s *observationTypesService) Create(observation_type *ObservationType) (boo
|
|||
return resp.StatusCode == http.StatusCreated, nil
|
||||
}
|
||||
|
||||
type ObservationTypeListOptions struct {
|
||||
ListOptions
|
||||
}
|
||||
|
||||
func (s *observationTypesService) List(opt *ObservationTypeListOptions) ([]*ObservationType, error) {
|
||||
url, err := s.client.url(router.ObservationTypes, nil, opt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
req, err := s.client.NewRequest("GET", url.String(), nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var observation_types []*ObservationType
|
||||
_, err = s.client.Do(req, &observation_types)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return observation_types, nil
|
||||
}
|
||||
|
||||
type MockObservationTypesService struct {
|
||||
Get_ func(id int64) (*ObservationType, error)
|
||||
List_ func(opt *ObservationTypeListOptions) ([]*ObservationType, error)
|
||||
Create_ func(observation_type *ObservationType) (bool, error)
|
||||
}
|
||||
|
||||
|
@ -102,3 +130,10 @@ func (s *MockObservationTypesService) Create(observation_type *ObservationType)
|
|||
}
|
||||
return s.Create_(observation_type)
|
||||
}
|
||||
|
||||
func (s *MockObservationTypesService) List(opt *ObservationTypeListOptions) ([]*ObservationType, error) {
|
||||
if s.List_ == nil {
|
||||
return nil, nil
|
||||
}
|
||||
return s.List_(opt)
|
||||
}
|
||||
|
|
|
@ -79,3 +79,36 @@ func TestObservationTypeService_Create(t *testing.T) {
|
|||
t.Errorf("ObservationTypes.Create returned %+v, want %+v", observation_type, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestObservationTypeService_List(t *testing.T) {
|
||||
setup()
|
||||
defer teardown()
|
||||
|
||||
want := []*ObservationType{newObservationType()}
|
||||
|
||||
var called bool
|
||||
mux.HandleFunc(urlPath(t, router.ObservationTypes, nil), func(w http.ResponseWriter, r *http.Request) {
|
||||
called = true
|
||||
testMethod(t, r, "GET")
|
||||
testFormValues(t, r, values{})
|
||||
|
||||
writeJSON(w, want)
|
||||
})
|
||||
|
||||
observation_types, err := client.ObservationTypes.List(nil)
|
||||
if err != nil {
|
||||
t.Errorf("ObservationTypes.List returned error: %v", err)
|
||||
}
|
||||
|
||||
if !called {
|
||||
t.Fatal("!called")
|
||||
}
|
||||
|
||||
for _, u := range want {
|
||||
normalizeTime(&u.CreatedAt, &u.UpdatedAt, &u.DeletedAt)
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(observation_types, want) {
|
||||
t.Errorf("ObservationTypes.List return %+v, want %+v", observation_types, want)
|
||||
}
|
||||
}
|
||||
|
|
Reference in a new issue