Species - delete

This commit is contained in:
Matthew Dillon 2014-10-24 10:42:31 -08:00
parent 22d2b8b41d
commit 4669aff3c2
9 changed files with 161 additions and 0 deletions

View file

@ -61,3 +61,19 @@ func (s *speciesStore) Update(id int64, species *models.Species) (bool, error) {
return true, nil
}
func (s *speciesStore) Delete(id int64) (bool, error) {
species, err := s.Get(id)
if err != nil {
return false, err
}
deleted, err := s.dbh.Delete(species)
if err != nil {
return false, err
}
if deleted == 0 {
return false, ErrNoRowsDeleted
}
return true, nil
}

View file

@ -13,6 +13,7 @@ func TestSpeciesStore_Get_db(t *testing.T) {
// Test on a clean database
tx.Exec(`DELETE FROM species;`)
tx.Exec(`DELETE FROM genera;`)
wantGenus := &models.Genus{GenusName: "Test Genus"}
if err := tx.Insert(wantGenus); err != nil {
@ -42,6 +43,7 @@ func TestSpeciesStore_Create_db(t *testing.T) {
// Test on a clean database
tx.Exec(`DELETE FROM species;`)
tx.Exec(`DELETE FROM genera;`)
genus := &models.Genus{}
if err := tx.Insert(genus); err != nil {
@ -70,6 +72,7 @@ func TestSpeciesStore_List_db(t *testing.T) {
// Test on a clean database
tx.Exec(`DELETE FROM species;`)
tx.Exec(`DELETE FROM genera;`)
genus := &models.Genus{}
@ -103,6 +106,7 @@ func TestSpeciesStore_Update_db(t *testing.T) {
// Test on a clean database
tx.Exec(`DELETE FROM species;`)
tx.Exec(`DELETE FROM genera;`)
d := NewDatastore(nil)
// Add a new record
@ -131,3 +135,38 @@ func TestSpeciesStore_Update_db(t *testing.T) {
t.Error("!updated")
}
}
func TestSpeciesStore_Delete_db(t *testing.T) {
tx, _ := DB.Begin()
defer tx.Rollback()
// Test on a clean database
tx.Exec(`DELETE FROM species;`)
tx.Exec(`DELETE FROM genera;`)
d := NewDatastore(tx)
// Add a new record
genus := &models.Genus{GenusName: "Test Genus"}
_, err := d.Genera.Create(genus)
if err != nil {
t.Fatal(err)
}
species := &models.Species{GenusId: genus.Id, SpeciesName: "Test Species"}
created, err := d.Species.Create(species)
if err != nil {
t.Fatal(err)
}
if !created {
t.Error("!created")
}
// Delete it
deleted, err := d.Species.Delete(species.Id)
if err != nil {
t.Fatal(err)
}
if !deleted {
t.Error("!delete")
}
}