species update wip
This commit is contained in:
parent
5244ae529a
commit
c8d1d0a84f
5 changed files with 100 additions and 0 deletions
|
@ -96,3 +96,38 @@ func TestSpeciesStore_List_db(t *testing.T) {
|
||||||
t.Errorf("got species %+v, want %+v", species, want)
|
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")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -29,6 +29,9 @@ type SpeciesService interface {
|
||||||
|
|
||||||
// Create a species record
|
// Create a species record
|
||||||
Create(species *Species) (bool, error)
|
Create(species *Species) (bool, error)
|
||||||
|
|
||||||
|
// Update an existing species
|
||||||
|
Update(id int64, species *Species) (updated bool, err error)
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@ -105,10 +108,32 @@ func (s *speciesService) List(opt *SpeciesListOptions) ([]*Species, error) {
|
||||||
return species, nil
|
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 {
|
type MockSpeciesService struct {
|
||||||
Get_ func(id int64) (*Species, error)
|
Get_ func(id int64) (*Species, error)
|
||||||
List_ func(opt *SpeciesListOptions) ([]*Species, error)
|
List_ func(opt *SpeciesListOptions) ([]*Species, error)
|
||||||
Create_ func(species *Species) (bool, error)
|
Create_ func(species *Species) (bool, error)
|
||||||
|
Update_ func(id int64, species *Species) (bool, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
var _ SpeciesService = &MockSpeciesService{}
|
var _ SpeciesService = &MockSpeciesService{}
|
||||||
|
@ -133,3 +158,10 @@ func (s *MockSpeciesService) List(opt *SpeciesListOptions) ([]*Species, error) {
|
||||||
}
|
}
|
||||||
return s.List_(opt)
|
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)
|
||||||
|
}
|
||||||
|
|
|
@ -105,3 +105,34 @@ func TestSpeciesService_List(t *testing.T) {
|
||||||
t.Errorf("Species.List return %+v, want %+v", species, want)
|
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")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -21,6 +21,7 @@ func API() *mux.Router {
|
||||||
m.Path("/species").Methods("GET").Name(SpeciesList)
|
m.Path("/species").Methods("GET").Name(SpeciesList)
|
||||||
m.Path("/species").Methods("POST").Name(CreateSpecies)
|
m.Path("/species").Methods("POST").Name(CreateSpecies)
|
||||||
m.Path("/species/{Id:.+}").Methods("GET").Name(Species)
|
m.Path("/species/{Id:.+}").Methods("GET").Name(Species)
|
||||||
|
m.Path("/species/{Id:.+}").Methods("PUT").Name(UpdateSpecies)
|
||||||
|
|
||||||
return m
|
return m
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,4 +14,5 @@ const (
|
||||||
Species = "species"
|
Species = "species"
|
||||||
CreateSpecies = "species:create"
|
CreateSpecies = "species:create"
|
||||||
SpeciesList = "species:list"
|
SpeciesList = "species:list"
|
||||||
|
UpdateSpecies = "species:update"
|
||||||
)
|
)
|
||||||
|
|
Reference in a new issue