create an observation type

This commit is contained in:
Matthew Dillon 2014-11-04 16:13:55 -09:00
parent 4fd7bf8eba
commit 0e767390b5
9 changed files with 155 additions and 4 deletions

View file

@ -1,6 +1,10 @@
package datastore
import "github.com/thermokarst/bactdb/models"
import (
"time"
"github.com/thermokarst/bactdb/models"
)
func init() {
DB.AddTableWithName(models.ObservationType{}, "observation_types").SetKeys(true, "Id")
@ -20,3 +24,13 @@ func (s *observationTypesStore) Get(id int64) (*models.ObservationType, error) {
}
return observation_type[0], nil
}
func (s *observationTypesStore) Create(observation_type *models.ObservationType) (bool, error) {
currentTime := time.Now()
observation_type.CreatedAt = currentTime
observation_type.UpdatedAt = currentTime
if err := s.dbh.Insert(observation_type); err != nil {
return false, err
}
return true, nil
}

View file

@ -42,3 +42,23 @@ func TestObservationTypesStore_Get_db(t *testing.T) {
t.Errorf("got observation_type %+v, want %+v", observation_type, want)
}
}
func TestObservationTypesStore_Create_db(t *testing.T) {
tx, _ := DB.Begin()
defer tx.Rollback()
observation_type := newObservationType(t, tx)
d := NewDatastore(tx)
created, err := d.ObservationTypes.Create(observation_type)
if err != nil {
t.Fatal(err)
}
if !created {
t.Error("!created")
}
if observation_type.Id == 0 {
t.Error("want nonzero observation_type.Id after submitting")
}
}