Get a unit type
This commit is contained in:
parent
431ba7ed91
commit
c2bfc8b93b
11 changed files with 260 additions and 0 deletions
|
@ -16,6 +16,7 @@ type Datastore struct {
|
|||
ObservationTypes models.ObservationTypesService
|
||||
Observations models.ObservationsService
|
||||
TextMeasurementTypes models.TextMeasurementTypesService
|
||||
UnitTypes models.UnitTypesService
|
||||
dbh modl.SqlExecutor
|
||||
}
|
||||
|
||||
|
@ -39,6 +40,7 @@ func NewDatastore(dbh modl.SqlExecutor) *Datastore {
|
|||
d.ObservationTypes = &observationTypesStore{d}
|
||||
d.Observations = &observationsStore{d}
|
||||
d.TextMeasurementTypes = &textMeasurementTypesStore{d}
|
||||
d.UnitTypes = &unitTypesStore{d}
|
||||
return d
|
||||
}
|
||||
|
||||
|
@ -51,5 +53,6 @@ func NewMockDatastore() *Datastore {
|
|||
ObservationTypes: &models.MockObservationTypesService{},
|
||||
Observations: &models.MockObservationsService{},
|
||||
TextMeasurementTypes: &models.MockTextMeasurementTypesService{},
|
||||
UnitTypes: &models.MockUnitTypesService{},
|
||||
}
|
||||
}
|
||||
|
|
22
datastore/unit_types.go
Normal file
22
datastore/unit_types.go
Normal file
|
@ -0,0 +1,22 @@
|
|||
package datastore
|
||||
|
||||
import "github.com/thermokarst/bactdb/models"
|
||||
|
||||
func init() {
|
||||
DB.AddTableWithName(models.UnitType{}, "unit_types").SetKeys(true, "Id")
|
||||
}
|
||||
|
||||
type unitTypesStore struct {
|
||||
*Datastore
|
||||
}
|
||||
|
||||
func (s *unitTypesStore) Get(id int64) (*models.UnitType, error) {
|
||||
var unit_type []*models.UnitType
|
||||
if err := s.dbh.Select(&unit_type, `SELECT * FROM unit_types WHERE id=$1;`, id); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(unit_type) == 0 {
|
||||
return nil, models.ErrUnitTypeNotFound
|
||||
}
|
||||
return unit_type[0], nil
|
||||
}
|
44
datastore/unit_types_test.go
Normal file
44
datastore/unit_types_test.go
Normal file
|
@ -0,0 +1,44 @@
|
|||
package datastore
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/jmoiron/modl"
|
||||
"github.com/thermokarst/bactdb/models"
|
||||
)
|
||||
|
||||
func insertUnitType(t *testing.T, tx *modl.Transaction) *models.UnitType {
|
||||
// clean up our target table
|
||||
tx.Exec(`DELETE FROM unit_types;`)
|
||||
unit_type := newUnitType(t, tx)
|
||||
if err := tx.Insert(unit_type); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return unit_type
|
||||
}
|
||||
|
||||
func newUnitType(t *testing.T, tx *modl.Transaction) *models.UnitType {
|
||||
return &models.UnitType{Name: "Test Unit Type", Symbol: "x"}
|
||||
}
|
||||
|
||||
func TestUnitTypesStore_Get_db(t *testing.T) {
|
||||
tx, _ := DB.Begin()
|
||||
defer tx.Rollback()
|
||||
|
||||
want := insertUnitType(t, tx)
|
||||
|
||||
d := NewDatastore(tx)
|
||||
|
||||
unit_type, err := d.UnitTypes.Get(want.Id)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
normalizeTime(&want.CreatedAt, &want.UpdatedAt, &want.DeletedAt)
|
||||
normalizeTime(&unit_type.CreatedAt, &unit_type.UpdatedAt, &unit_type.DeletedAt)
|
||||
|
||||
if !reflect.DeepEqual(unit_type, want) {
|
||||
t.Errorf("got unit_type %+v, want %+v", unit_type, want)
|
||||
}
|
||||
}
|
Reference in a new issue