diff --git a/datastore/species_test.go b/datastore/species_test.go index a540a99..62235ba 100644 --- a/datastore/species_test.go +++ b/datastore/species_test.go @@ -96,3 +96,38 @@ func TestSpeciesStore_List_db(t *testing.T) { t.Errorf("got species %+v, want %+v", species, want) } } + +func TestSpeciesStore_Update_db(t *testing.T) { + tx, _ := DB.Begin() + defer tx.Rollback() + + // Test on a clean database + tx.Exec(`DELETE FROM species;`) + + d := NewDatastore(nil) + // Add a new record + genus := &models.Genus{GenusName: "Test Genus"} + _, err := d.Genera.Create(genus) + if err != nil { + t.Fatal(err) + } + created := &model.Species{GenusId: genus.Id, SpeciesName: "Test Species"} + _, err := d.Species.Create(created) + if err != nil { + t.Fatal(err) + } + if !created { + t.Error("!created") + } + + // Tweak it + species.SpeciesName = "Updated Species" + updated, err := d.Species.Update(species.Id, species) + if err != nil { + t.Fatal(err) + } + + if !updated { + t.Error("!updated") + } +} diff --git a/models/species.go b/models/species.go index faf6820..b742de8 100644 --- a/models/species.go +++ b/models/species.go @@ -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) +} diff --git a/models/species_test.go b/models/species_test.go index 4e6873b..cb66f9e 100644 --- a/models/species_test.go +++ b/models/species_test.go @@ -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") + } +} diff --git a/router/api.go b/router/api.go index 2ffa19a..5bf24d2 100644 --- a/router/api.go +++ b/router/api.go @@ -21,6 +21,7 @@ func API() *mux.Router { m.Path("/species").Methods("GET").Name(SpeciesList) m.Path("/species").Methods("POST").Name(CreateSpecies) m.Path("/species/{Id:.+}").Methods("GET").Name(Species) + m.Path("/species/{Id:.+}").Methods("PUT").Name(UpdateSpecies) return m } diff --git a/router/routes.go b/router/routes.go index 4d7a9ac..0db9205 100644 --- a/router/routes.go +++ b/router/routes.go @@ -14,4 +14,5 @@ const ( Species = "species" CreateSpecies = "species:create" SpeciesList = "species:list" + UpdateSpecies = "species:update" )