Create a unit type

This commit is contained in:
Matthew Dillon 2014-11-30 15:11:51 -09:00
parent c2bfc8b93b
commit b7caffa373
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.UnitType{}, "unit_types").SetKeys(true, "Id")
@ -20,3 +24,13 @@ func (s *unitTypesStore) Get(id int64) (*models.UnitType, error) {
}
return unit_type[0], nil
}
func (s *unitTypesStore) Create(unit_type *models.UnitType) (bool, error) {
currentTime := time.Now()
unit_type.CreatedAt = currentTime
unit_type.UpdatedAt = currentTime
if err := s.dbh.Insert(unit_type); err != nil {
return false, err
}
return true, nil
}

View file

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