List unit types
This commit is contained in:
parent
b7caffa373
commit
d535cd85ff
9 changed files with 152 additions and 0 deletions
|
@ -61,6 +61,7 @@ func Handler() *mux.Router {
|
|||
|
||||
m.Get(router.UnitType).Handler(handler(serveUnitType))
|
||||
m.Get(router.CreateUnitType).Handler(handler(serveCreateUnitType))
|
||||
m.Get(router.UnitTypes).Handler(handler(serveUnitTypeList))
|
||||
|
||||
return m
|
||||
}
|
||||
|
|
|
@ -40,3 +40,20 @@ func serveCreateUnitType(w http.ResponseWriter, r *http.Request) error {
|
|||
|
||||
return writeJSON(w, unit_type)
|
||||
}
|
||||
|
||||
func serveUnitTypeList(w http.ResponseWriter, r *http.Request) error {
|
||||
var opt models.UnitTypeListOptions
|
||||
if err := schemaDecoder.Decode(&opt, r.URL.Query()); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
unit_types, err := store.UnitTypes.List(&opt)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if unit_types == nil {
|
||||
unit_types = []*models.UnitType{}
|
||||
}
|
||||
|
||||
return writeJSON(w, unit_types)
|
||||
}
|
||||
|
|
|
@ -65,3 +65,32 @@ func TestUnitType_Create(t *testing.T) {
|
|||
t.Error("!success")
|
||||
}
|
||||
}
|
||||
|
||||
func TestUnitType_List(t *testing.T) {
|
||||
setup()
|
||||
|
||||
want := []*models.UnitType{newUnitType()}
|
||||
wantOpt := &models.UnitTypeListOptions{ListOptions: models.ListOptions{Page: 1, PerPage: 10}}
|
||||
|
||||
calledList := false
|
||||
store.UnitTypes.(*models.MockUnitTypesService).List_ = func(opt *models.UnitTypeListOptions) ([]*models.UnitType, error) {
|
||||
if !normalizeDeepEqual(wantOpt, opt) {
|
||||
t.Errorf("wanted options %d but got %d", wantOpt, opt)
|
||||
}
|
||||
calledList = true
|
||||
return want, nil
|
||||
}
|
||||
|
||||
unit_types, err := apiClient.UnitTypes.List(wantOpt)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if !calledList {
|
||||
t.Error("!calledList")
|
||||
}
|
||||
|
||||
if !normalizeDeepEqual(&want, &unit_types) {
|
||||
t.Errorf("got unit_types %+v but wanted unit_types %+v", unit_types, want)
|
||||
}
|
||||
}
|
||||
|
|
Reference in a new issue