Genera: update record.

This commit is contained in:
Matthew Dillon 2014-10-13 12:22:50 -08:00
parent c271f4b6b3
commit 33194ffd2f
8 changed files with 131 additions and 4 deletions

View file

@ -28,6 +28,9 @@ type GeneraService interface {
// Create a new genus. The newly created genus's ID is written to genus.Id
Create(genus *Genus) (created bool, err error)
// Update an existing genus.
Update(id int64, genus *Genus) (updated bool, err error)
}
var (
@ -105,10 +108,32 @@ func (s *generaService) List(opt *GenusListOptions) ([]*Genus, error) {
return genera, nil
}
func (s *generaService) Update(id int64, genus *Genus) (bool, error) {
strId := strconv.FormatInt(id, 10)
url, err := s.client.url(router.UpdateGenus, map[string]string{"Id": strId}, nil)
if err != nil {
return false, err
}
req, err := s.client.NewRequest("PUT", url.String(), genus)
if err != nil {
return false, err
}
resp, err := s.client.Do(req, &genus)
if err != nil {
return false, err
}
return resp.StatusCode == http.StatusOK, nil
}
type MockGeneraService struct {
Get_ func(id int64) (*Genus, error)
List_ func(opt *GenusListOptions) ([]*Genus, error)
Create_ func(post *Genus) (bool, error)
Create_ func(genus *Genus) (bool, error)
Update_ func(id int64, genus *Genus) (bool, error)
}
var _ GeneraService = &MockGeneraService{}
@ -133,3 +158,10 @@ func (s *MockGeneraService) List(opt *GenusListOptions) ([]*Genus, error) {
}
return s.List_(opt)
}
func (s *MockGeneraService) Update(id int64, genus *Genus) (bool, error) {
if s.Update_ == nil {
return false, nil
}
return s.Update_(id, genus)
}

View file

@ -105,3 +105,34 @@ func TestGeneraService_List(t *testing.T) {
t.Errorf("Genera.List return %+v, want %+v", genera, want)
}
}
func TestGeneraService_Update(t *testing.T) {
setup()
defer teardown()
want := &Genus{Id: 1, GenusName: "Test Genus"}
var called bool
mux.HandleFunc(urlPath(t, router.UpdateGenus, map[string]string{"Id": "1"}), func(w http.ResponseWriter, r *http.Request) {
called = true
testMethod(t, r, "PUT")
testBody(t, r, `{"id":1,"genus_name":"Test Genus Updated","created_at":"0001-01-01T00:00:00Z","updated_at":"0001-01-01T00:00:00Z","deleted_at":"0001-01-01T00:00:00Z"}`+"\n")
w.WriteHeader(http.StatusOK)
writeJSON(w, want)
})
genus := &Genus{Id: 1, GenusName: "Test Genus Updated"}
updated, err := client.Genera.Update(1, genus)
if err != nil {
t.Errorf("Genera.Update returned error: %v", err)
}
if !updated {
t.Error("!updated")
}
if !called {
t.Fatal("!called")
}
}