Update a unit type
This commit is contained in:
parent
dfa2dd2eba
commit
76baee1fa7
9 changed files with 158 additions and 0 deletions
|
@ -36,6 +36,9 @@ type UnitTypesService interface {
|
|||
|
||||
// Create a unit type
|
||||
Create(unit_type *UnitType) (bool, error)
|
||||
|
||||
// Update a unit type
|
||||
Update(id int64, UnitType *UnitType) (bool, error)
|
||||
}
|
||||
|
||||
var (
|
||||
|
@ -111,10 +114,32 @@ func (s *unitTypesService) List(opt *UnitTypeListOptions) ([]*UnitType, error) {
|
|||
return unit_types, nil
|
||||
}
|
||||
|
||||
func (s *unitTypesService) Update(id int64, unit_type *UnitType) (bool, error) {
|
||||
strId := strconv.FormatInt(id, 10)
|
||||
|
||||
url, err := s.client.url(router.UpdateUnitType, map[string]string{"Id": strId}, nil)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
req, err := s.client.NewRequest("PUT", url.String(), unit_type)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
resp, err := s.client.Do(req, &unit_type)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
return resp.StatusCode == http.StatusOK, nil
|
||||
}
|
||||
|
||||
type MockUnitTypesService struct {
|
||||
Get_ func(id int64) (*UnitType, error)
|
||||
List_ func(opt *UnitTypeListOptions) ([]*UnitType, error)
|
||||
Create_ func(unit_type *UnitType) (bool, error)
|
||||
Update_ func(id int64, unit_type *UnitType) (bool, error)
|
||||
}
|
||||
|
||||
var _ UnitTypesService = &MockUnitTypesService{}
|
||||
|
@ -139,3 +164,10 @@ func (s *MockUnitTypesService) List(opt *UnitTypeListOptions) ([]*UnitType, erro
|
|||
}
|
||||
return s.List_(opt)
|
||||
}
|
||||
|
||||
func (s *MockUnitTypesService) Update(id int64, unit_type *UnitType) (bool, error) {
|
||||
if s.Update_ == nil {
|
||||
return false, nil
|
||||
}
|
||||
return s.Update_(id, unit_type)
|
||||
}
|
||||
|
|
|
@ -112,3 +112,34 @@ func TestUnitTypeService_List(t *testing.T) {
|
|||
t.Errorf("UnitTypes.List return %+v, want %+v", unit_types, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUnitTypeService_Update(t *testing.T) {
|
||||
setup()
|
||||
defer teardown()
|
||||
|
||||
want := newUnitType()
|
||||
|
||||
var called bool
|
||||
mux.HandleFunc(urlPath(t, router.UpdateUnitType, map[string]string{"Id": "1"}), func(w http.ResponseWriter, r *http.Request) {
|
||||
called = true
|
||||
testMethod(t, r, "PUT")
|
||||
testBody(t, r, `{"id":1,"name":"Test Unit Type Updated","symbol":"x","createdAt":"0001-01-01T00:00:00Z","updatedAt":"0001-01-01T00:00:00Z","deletedAt":{"Time":"0001-01-01T00:00:00Z","Valid":false}}`+"\n")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
writeJSON(w, want)
|
||||
})
|
||||
|
||||
unit_type := newUnitType()
|
||||
unit_type.Name = "Test Unit Type Updated"
|
||||
updated, err := client.UnitTypes.Update(unit_type.Id, unit_type)
|
||||
if err != nil {
|
||||
t.Errorf("UnitTypes.Update returned error: %v", err)
|
||||
}
|
||||
|
||||
if !updated {
|
||||
t.Error("!updated")
|
||||
}
|
||||
|
||||
if !called {
|
||||
t.Fatal("!called")
|
||||
}
|
||||
}
|
||||
|
|
Reference in a new issue