Create species, cleanup schema.

- create species
- species genus_id not null
This commit is contained in:
Matthew Dillon 2014-10-15 16:43:09 -08:00
parent ed2ba26654
commit 7fe5566edf
10 changed files with 155 additions and 3 deletions

View file

@ -3,7 +3,7 @@
CREATE TABLE species (
id BIGSERIAL NOT NULL,
genusid BIGINT,
genusid BIGINT NOT NULL,
speciesname CHARACTER VARYING(100),
createdat TIMESTAMP WITH TIME ZONE,

View file

@ -20,3 +20,10 @@ func (s *speciesStore) Get(id int64) (*models.Species, error) {
}
return species[0], nil
}
func (s *speciesStore) Create(species *models.Species) (bool, error) {
if err := s.dbh.Insert(species); err != nil {
return false, err
}
return true, nil
}

View file

@ -35,3 +35,31 @@ func TestSpeciesStore_Get_db(t *testing.T) {
t.Errorf("got species %+v, want %+v", species, want)
}
}
func TestSpeciesStore_Create_db(t *testing.T) {
tx, _ := DB.Begin()
defer tx.Rollback()
// Test on a clean database
tx.Exec(`DELETE FROM species;`)
genus := &models.Genus{}
if err := tx.Insert(genus); err != nil {
t.Fatal(err)
}
species := &models.Species{Id: 1, GenusId: genus.Id, SpeciesName: "Test Species"}
d := NewDatastore(tx)
created, err := d.Species.Create(species)
if err != nil {
t.Fatal(err)
}
if !created {
t.Error("!created")
}
if species.Id == 0 {
t.Error("want nonzero species.Id after submitting")
}
}