List observations.
This commit is contained in:
parent
f867e5c424
commit
dac0721a41
9 changed files with 152 additions and 0 deletions
|
@ -30,6 +30,9 @@ type ObservationsService interface {
|
|||
// Get an observation
|
||||
Get(id int64) (*Observation, error)
|
||||
|
||||
// List all observations
|
||||
List(opt *ObservationListOptions) ([]*Observation, error)
|
||||
|
||||
// Create an observation
|
||||
Create(observation *Observation) (bool, error)
|
||||
}
|
||||
|
@ -83,8 +86,33 @@ func (s *observationsService) Create(observation *Observation) (bool, error) {
|
|||
return resp.StatusCode == http.StatusCreated, nil
|
||||
}
|
||||
|
||||
type ObservationListOptions struct {
|
||||
ListOptions
|
||||
}
|
||||
|
||||
func (s *observationsService) List(opt *ObservationListOptions) ([]*Observation, error) {
|
||||
url, err := s.client.url(router.Observations, nil, opt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
req, err := s.client.NewRequest("GET", url.String(), nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var observations []*Observation
|
||||
_, err = s.client.Do(req, &observations)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return observations, nil
|
||||
}
|
||||
|
||||
type MockObservationsService struct {
|
||||
Get_ func(id int64) (*Observation, error)
|
||||
List_ func(opt *ObservationListOptions) ([]*Observation, error)
|
||||
Create_ func(observation *Observation) (bool, error)
|
||||
}
|
||||
|
||||
|
@ -103,3 +131,10 @@ func (s *MockObservationsService) Create(observation *Observation) (bool, error)
|
|||
}
|
||||
return s.Create_(observation)
|
||||
}
|
||||
|
||||
func (s *MockObservationsService) List(opt *ObservationListOptions) ([]*Observation, error) {
|
||||
if s.List_ == nil {
|
||||
return nil, nil
|
||||
}
|
||||
return s.List_(opt)
|
||||
}
|
||||
|
|
|
@ -79,3 +79,36 @@ func TestObservationService_Create(t *testing.T) {
|
|||
t.Errorf("Observations.Create returned %+v, want %+v", observation, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestObservationService_List(t *testing.T) {
|
||||
setup()
|
||||
defer teardown()
|
||||
|
||||
want := []*Observation{newObservation()}
|
||||
|
||||
var called bool
|
||||
mux.HandleFunc(urlPath(t, router.Observations, nil), func(w http.ResponseWriter, r *http.Request) {
|
||||
called = true
|
||||
testMethod(t, r, "GET")
|
||||
testFormValues(t, r, values{})
|
||||
|
||||
writeJSON(w, want)
|
||||
})
|
||||
|
||||
observations, err := client.Observations.List(nil)
|
||||
if err != nil {
|
||||
t.Errorf("Observations.List returned error: %v", err)
|
||||
}
|
||||
|
||||
if !called {
|
||||
t.Fatal("!called")
|
||||
}
|
||||
|
||||
for _, u := range want {
|
||||
normalizeTime(&u.CreatedAt, &u.UpdatedAt, &u.DeletedAt)
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(observations, want) {
|
||||
t.Errorf("Observations.List return %+v, want %+v", observations, want)
|
||||
}
|
||||
}
|
||||
|
|
Reference in a new issue