List Observation types
This commit is contained in:
parent
9326a06699
commit
186edad1db
9 changed files with 152 additions and 0 deletions
|
@ -43,6 +43,7 @@ func Handler() *mux.Router {
|
|||
|
||||
m.Get(router.ObservationType).Handler(handler(serveObservationType))
|
||||
m.Get(router.CreateObservationType).Handler(handler(serveCreateObservationType))
|
||||
m.Get(router.ObservationTypes).Handler(handler(serveObservationTypeList))
|
||||
|
||||
return m
|
||||
}
|
||||
|
|
|
@ -40,3 +40,20 @@ func serveCreateObservationType(w http.ResponseWriter, r *http.Request) error {
|
|||
|
||||
return writeJSON(w, observation_type)
|
||||
}
|
||||
|
||||
func serveObservationTypeList(w http.ResponseWriter, r *http.Request) error {
|
||||
var opt models.ObservationTypeListOptions
|
||||
if err := schemaDecoder.Decode(&opt, r.URL.Query()); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
observation_types, err := store.ObservationTypes.List(&opt)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if observation_types == nil {
|
||||
observation_types = []*models.ObservationType{}
|
||||
}
|
||||
|
||||
return writeJSON(w, observation_types)
|
||||
}
|
||||
|
|
|
@ -65,3 +65,32 @@ func TestObservationType_Create(t *testing.T) {
|
|||
t.Error("!success")
|
||||
}
|
||||
}
|
||||
|
||||
func TestObservationType_List(t *testing.T) {
|
||||
setup()
|
||||
|
||||
want := []*models.ObservationType{newObservationType()}
|
||||
wantOpt := &models.ObservationTypeListOptions{ListOptions: models.ListOptions{Page: 1, PerPage: 10}}
|
||||
|
||||
calledList := false
|
||||
store.ObservationTypes.(*models.MockObservationTypesService).List_ = func(opt *models.ObservationTypeListOptions) ([]*models.ObservationType, error) {
|
||||
if !normalizeDeepEqual(wantOpt, opt) {
|
||||
t.Errorf("wanted options %d but got %d", wantOpt, opt)
|
||||
}
|
||||
calledList = true
|
||||
return want, nil
|
||||
}
|
||||
|
||||
observation_types, err := apiClient.ObservationTypes.List(wantOpt)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if !calledList {
|
||||
t.Error("!calledList")
|
||||
}
|
||||
|
||||
if !normalizeDeepEqual(&want, &observation_types) {
|
||||
t.Errorf("got observation_types %+v but wanted observation_types %+v", observation_types, want)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -34,3 +34,15 @@ func (s *observationTypesStore) Create(observation_type *models.ObservationType)
|
|||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func (s *observationTypesStore) List(opt *models.ObservationTypeListOptions) ([]*models.ObservationType, error) {
|
||||
if opt == nil {
|
||||
opt = &models.ObservationTypeListOptions{}
|
||||
}
|
||||
var observation_types []*models.ObservationType
|
||||
err := s.dbh.Select(&observation_types, `SELECT * FROM observation_types LIMIT $1 OFFSET $2;`, opt.PerPageOrDefault(), opt.Offset())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return observation_types, nil
|
||||
}
|
||||
|
|
|
@ -62,3 +62,26 @@ func TestObservationTypesStore_Create_db(t *testing.T) {
|
|||
t.Error("want nonzero observation_type.Id after submitting")
|
||||
}
|
||||
}
|
||||
|
||||
func TestObservationTypesStore_List_db(t *testing.T) {
|
||||
tx, _ := DB.Begin()
|
||||
defer tx.Rollback()
|
||||
|
||||
want_observation_type := insertObservationType(t, tx)
|
||||
want := []*models.ObservationType{want_observation_type}
|
||||
|
||||
d := NewDatastore(tx)
|
||||
|
||||
observation_types, err := d.ObservationTypes.List(&models.ObservationTypeListOptions{ListOptions: models.ListOptions{Page: 1, PerPage: 10}})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
for i := range want {
|
||||
normalizeTime(&want[i].CreatedAt, &want[i].UpdatedAt, &want[i].DeletedAt)
|
||||
normalizeTime(&observation_types[i].CreatedAt, &observation_types[i].UpdatedAt, &observation_types[i].DeletedAt)
|
||||
}
|
||||
if !reflect.DeepEqual(observation_types, want) {
|
||||
t.Errorf("got observation_types %+v, want %+v", observation_types, want)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -32,6 +32,7 @@ func API() *mux.Router {
|
|||
m.Path("/strains/{Id:.+}").Methods("DELETE").Name(DeleteStrain)
|
||||
|
||||
// ObservationTypes
|
||||
m.Path("/observation_types").Methods("GET").Name(ObservationTypes)
|
||||
m.Path("/observation_types").Methods("POST").Name(CreateObservationType)
|
||||
m.Path("/observation_types/{Id:.+}").Methods("GET").Name(ObservationType)
|
||||
|
||||
|
|
|
@ -25,4 +25,5 @@ const (
|
|||
|
||||
ObservationType = "observation_type:get"
|
||||
CreateObservationType = "observation_type:create"
|
||||
ObservationTypes = "observation_type:list"
|
||||
)
|
||||
|
|
Reference in a new issue