Delete a unit type

This commit is contained in:
Matthew Dillon 2014-11-30 16:10:38 -09:00
parent 76baee1fa7
commit eccbffb86d
9 changed files with 141 additions and 0 deletions

View file

@ -63,6 +63,7 @@ func Handler() *mux.Router {
m.Get(router.CreateUnitType).Handler(handler(serveCreateUnitType))
m.Get(router.UnitTypes).Handler(handler(serveUnitTypeList))
m.Get(router.UpdateUnitType).Handler(handler(serveUpdateUnitType))
m.Get(router.DeleteUnitType).Handler(handler(serveDeleteUnitType))
return m
}

View file

@ -76,3 +76,17 @@ func serveUpdateUnitType(w http.ResponseWriter, r *http.Request) error {
return writeJSON(w, unit_type)
}
func serveDeleteUnitType(w http.ResponseWriter, r *http.Request) error {
id, _ := strconv.ParseInt(mux.Vars(r)["Id"], 10, 0)
deleted, err := store.UnitTypes.Delete(id)
if err != nil {
return err
}
if deleted {
w.WriteHeader(http.StatusOK)
}
return writeJSON(w, &models.UnitType{})
}

View file

@ -124,3 +124,30 @@ func TestUnitType_Update(t *testing.T) {
t.Error("!success")
}
}
func TestUnitType_Delete(t *testing.T) {
setup()
want := newUnitType()
calledDelete := false
store.UnitTypes.(*models.MockUnitTypesService).Delete_ = func(id int64) (bool, error) {
if id != want.Id {
t.Errorf("wanted request for unit_type %d but got %d", want.Id, id)
}
calledDelete = true
return true, nil
}
success, err := apiClient.UnitTypes.Delete(want.Id)
if err != nil {
t.Fatal(err)
}
if !calledDelete {
t.Error("!calledDelete")
}
if !success {
t.Error("!success")
}
}