Create a new observation

This commit is contained in:
Matthew Dillon 2014-11-20 10:36:21 -09:00
parent a018d2bd7a
commit f867e5c424
9 changed files with 154 additions and 3 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.Observation{}, "observations").SetKeys(true, "Id")
@ -20,3 +24,13 @@ func (s *observationsStore) Get(id int64) (*models.Observation, error) {
}
return observation[0], nil
}
func (s *observationsStore) Create(observation *models.Observation) (bool, error) {
currentTime := time.Now()
observation.CreatedAt = currentTime
observation.UpdatedAt = currentTime
if err := s.dbh.Insert(observation); err != nil {
return false, err
}
return true, nil
}

View file

@ -45,3 +45,23 @@ func TestObservationsStore_Get_db(t *testing.T) {
t.Errorf("got observation %+v, want %+v", observation, want)
}
}
func TestObservationsStore_Create_db(t *testing.T) {
tx, _ := DB.Begin()
defer tx.Rollback()
observation := newObservation(t, tx)
d := NewDatastore(tx)
created, err := d.Observations.Create(observation)
if err != nil {
t.Fatal(err)
}
if !created {
t.Error("!created")
}
if observation.Id == 0 {
t.Error("want nonzero observation.Id after submitting")
}
}