create an observation type
This commit is contained in:
parent
4fd7bf8eba
commit
0e767390b5
9 changed files with 155 additions and 4 deletions
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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")
|
||||
}
|
||||
}
|
||||
|
|
Reference in a new issue