species update wip

This commit is contained in:
Matthew Dillon 2014-10-23 18:25:15 -08:00
parent 5244ae529a
commit c8d1d0a84f
5 changed files with 100 additions and 0 deletions

View file

@ -29,6 +29,9 @@ type SpeciesService interface {
// Create a species record
Create(species *Species) (bool, error)
// Update an existing species
Update(id int64, species *Species) (updated bool, err error)
}
var (
@ -105,10 +108,32 @@ func (s *speciesService) List(opt *SpeciesListOptions) ([]*Species, error) {
return species, nil
}
func (s *speciesService) Update(id int64, species *Species) (bool, error) {
strId := strconv.FormatInt(id, 10)
url, err := s.client.url(router.UpdateSpecies, map[string]string{"Id": strId}, nil)
if err != nil {
return false, err
}
req, err := s.client.NewRequest("PUT", url.String(), species)
if err != nil {
return false, err
}
resp, err := s.client.Do(req, &species)
if err != nil {
return false, err
}
return resp.StatusCode == http.StatusOK, nil
}
type MockSpeciesService struct {
Get_ func(id int64) (*Species, error)
List_ func(opt *SpeciesListOptions) ([]*Species, error)
Create_ func(species *Species) (bool, error)
Update_ func(id int64, species *Species) (bool, error)
}
var _ SpeciesService = &MockSpeciesService{}
@ -133,3 +158,10 @@ func (s *MockSpeciesService) List(opt *SpeciesListOptions) ([]*Species, error) {
}
return s.List_(opt)
}
func (s *MockSpeciesService) Update(id int64, species *Species) (bool, error) {
if s.Update_ == nil {
return false, nil
}
return s.Update_(id, species)
}

View file

@ -105,3 +105,34 @@ func TestSpeciesService_List(t *testing.T) {
t.Errorf("Species.List return %+v, want %+v", species, want)
}
}
func TestSpeciesService_Update(t *testing.T) {
setup()
defer teardown()
want := &Species{Id: 1, GenusId: 1, SpeciesName: "Test Species"}
var called bool
mux.HandleFunc(urlPath(t, router.UpdateSpecies, map[string]string{"Id": "1"}), func(w http.ResponseWriter, r *http.Request) {
called = true
testMethod(t, r, "PUT")
testBody(t, r, `{"id":1,"genus_id":1,"species_name":"Test Species 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)
})
species := &Species{Id: 1, GenusId: 1, SpeciesName: "Test Species Updated"}
updated, err := client.Species.Update(1, species)
if err != nil {
t.Errorf("Species.Update returned error: %v", err)
}
if !updated {
t.Error("!updated")
}
if !called {
t.Fatal("!called")
}
}