Renaming observations to characteristics
This commit is contained in:
parent
c1323f9c1f
commit
950b15a117
27 changed files with 889 additions and 889 deletions
|
@ -19,7 +19,7 @@ func insertCharacteristicType(t *testing.T, tx *modl.Transaction) *models.Charac
|
|||
}
|
||||
|
||||
func newCharacteristicType(t *testing.T, tx *modl.Transaction) *models.CharacteristicType {
|
||||
return &models.CharacteristicType{CharacteristicTypeName: "Test Obs"}
|
||||
return &models.CharacteristicType{CharacteristicTypeName: "Test Char"}
|
||||
}
|
||||
|
||||
func TestCharacteristicTypesStore_Get_db(t *testing.T) {
|
||||
|
@ -95,7 +95,7 @@ func TestCharacteristicTypesStore_Update_db(t *testing.T) {
|
|||
d := NewDatastore(tx)
|
||||
|
||||
// Tweak it
|
||||
characteristic_type.CharacteristicTypeName = "Updated Obs Type"
|
||||
characteristic_type.CharacteristicTypeName = "Updated Char Type"
|
||||
updated, err := d.CharacteristicTypes.Update(characteristic_type.Id, characteristic_type)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
|
|
87
datastore/characteristics.go
Normal file
87
datastore/characteristics.go
Normal file
|
@ -0,0 +1,87 @@
|
|||
package datastore
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/thermokarst/bactdb/models"
|
||||
)
|
||||
|
||||
func init() {
|
||||
DB.AddTableWithName(models.Characteristic{}, "characteristics").SetKeys(true, "Id")
|
||||
}
|
||||
|
||||
type characteristicsStore struct {
|
||||
*Datastore
|
||||
}
|
||||
|
||||
func (s *characteristicsStore) Get(id int64) (*models.Characteristic, error) {
|
||||
var characteristic []*models.Characteristic
|
||||
if err := s.dbh.Select(&characteristic, `SELECT * FROM characteristics WHERE id=$1;`, id); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(characteristic) == 0 {
|
||||
return nil, models.ErrCharacteristicNotFound
|
||||
}
|
||||
return characteristic[0], nil
|
||||
}
|
||||
|
||||
func (s *characteristicsStore) Create(characteristic *models.Characteristic) (bool, error) {
|
||||
currentTime := time.Now()
|
||||
characteristic.CreatedAt = currentTime
|
||||
characteristic.UpdatedAt = currentTime
|
||||
if err := s.dbh.Insert(characteristic); err != nil {
|
||||
return false, err
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func (s *characteristicsStore) List(opt *models.CharacteristicListOptions) ([]*models.Characteristic, error) {
|
||||
if opt == nil {
|
||||
opt = &models.CharacteristicListOptions{}
|
||||
}
|
||||
var characteristics []*models.Characteristic
|
||||
err := s.dbh.Select(&characteristics, `SELECT * FROM characteristics LIMIT $1 OFFSET $2;`, opt.PerPageOrDefault(), opt.Offset())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return characteristics, nil
|
||||
}
|
||||
|
||||
func (s *characteristicsStore) Update(id int64, characteristic *models.Characteristic) (bool, error) {
|
||||
_, err := s.Get(id)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
if id != characteristic.Id {
|
||||
return false, models.ErrCharacteristicNotFound
|
||||
}
|
||||
|
||||
characteristic.UpdatedAt = time.Now()
|
||||
changed, err := s.dbh.Update(characteristic)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
if changed == 0 {
|
||||
return false, ErrNoRowsUpdated
|
||||
}
|
||||
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func (s *characteristicsStore) Delete(id int64) (bool, error) {
|
||||
characteristic, err := s.Get(id)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
deleted, err := s.dbh.Delete(characteristic)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
if deleted == 0 {
|
||||
return false, ErrNoRowsDeleted
|
||||
}
|
||||
return true, nil
|
||||
}
|
129
datastore/characteristics_test.go
Normal file
129
datastore/characteristics_test.go
Normal file
|
@ -0,0 +1,129 @@
|
|||
package datastore
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/jmoiron/modl"
|
||||
"github.com/thermokarst/bactdb/models"
|
||||
)
|
||||
|
||||
func insertCharacteristic(t *testing.T, tx *modl.Transaction) *models.Characteristic {
|
||||
// clean up our target table
|
||||
tx.Exec(`DELETE FROM characteristics;`)
|
||||
characteristic := newCharacteristic(t, tx)
|
||||
if err := tx.Insert(characteristic); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return characteristic
|
||||
}
|
||||
|
||||
func newCharacteristic(t *testing.T, tx *modl.Transaction) *models.Characteristic {
|
||||
// we want to create and insert an characteristic type record, too.
|
||||
characteristic_type := insertCharacteristicType(t, tx)
|
||||
return &models.Characteristic{CharacteristicName: "Test Characteristic",
|
||||
CharacteristicTypeId: characteristic_type.Id}
|
||||
}
|
||||
|
||||
func TestCharacteristicsStore_Get_db(t *testing.T) {
|
||||
tx, _ := DB.Begin()
|
||||
defer tx.Rollback()
|
||||
|
||||
want := insertCharacteristic(t, tx)
|
||||
|
||||
d := NewDatastore(tx)
|
||||
|
||||
characteristic, err := d.Characteristics.Get(want.Id)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
normalizeTime(&want.CreatedAt, &want.UpdatedAt, &want.DeletedAt)
|
||||
normalizeTime(&characteristic.CreatedAt, &characteristic.UpdatedAt, &characteristic.DeletedAt)
|
||||
|
||||
if !reflect.DeepEqual(characteristic, want) {
|
||||
t.Errorf("got characteristic %+v, want %+v", characteristic, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCharacteristicsStore_Create_db(t *testing.T) {
|
||||
tx, _ := DB.Begin()
|
||||
defer tx.Rollback()
|
||||
|
||||
characteristic := newCharacteristic(t, tx)
|
||||
|
||||
d := NewDatastore(tx)
|
||||
|
||||
created, err := d.Characteristics.Create(characteristic)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !created {
|
||||
t.Error("!created")
|
||||
}
|
||||
if characteristic.Id == 0 {
|
||||
t.Error("want nonzero characteristic.Id after submitting")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCharacteristicsStore_List_db(t *testing.T) {
|
||||
tx, _ := DB.Begin()
|
||||
defer tx.Rollback()
|
||||
|
||||
want_characteristic := insertCharacteristic(t, tx)
|
||||
want := []*models.Characteristic{want_characteristic}
|
||||
|
||||
d := NewDatastore(tx)
|
||||
|
||||
characteristics, err := d.Characteristics.List(&models.CharacteristicListOptions{ListOptions: models.ListOptions{Page: 1, PerPage: 10}})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
for i := range want {
|
||||
normalizeTime(&want[i].CreatedAt, &want[i].UpdatedAt, &want[i].DeletedAt)
|
||||
normalizeTime(&characteristics[i].CreatedAt, &characteristics[i].UpdatedAt, &characteristics[i].DeletedAt)
|
||||
}
|
||||
if !reflect.DeepEqual(characteristics, want) {
|
||||
t.Errorf("got characteristics %+v, want %+v", characteristics, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCharacteristicsStore_Update_db(t *testing.T) {
|
||||
tx, _ := DB.Begin()
|
||||
defer tx.Rollback()
|
||||
|
||||
characteristic := insertCharacteristic(t, tx)
|
||||
|
||||
d := NewDatastore(tx)
|
||||
|
||||
// Tweak it
|
||||
characteristic.CharacteristicName = "Updated Char"
|
||||
updated, err := d.Characteristics.Update(characteristic.Id, characteristic)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if !updated {
|
||||
t.Error("!updated")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCharacteristicsStore_Delete_db(t *testing.T) {
|
||||
tx, _ := DB.Begin()
|
||||
defer tx.Rollback()
|
||||
|
||||
characteristic := insertCharacteristic(t, tx)
|
||||
|
||||
d := NewDatastore(tx)
|
||||
|
||||
// Delete it
|
||||
deleted, err := d.Characteristics.Delete(characteristic.Id)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if !deleted {
|
||||
t.Error("!delete")
|
||||
}
|
||||
}
|
|
@ -13,8 +13,8 @@ type Datastore struct {
|
|||
Genera models.GeneraService
|
||||
Species models.SpeciesService
|
||||
Strains models.StrainsService
|
||||
CharacteristicTypes models.CharacteristicTypesService
|
||||
Observations models.ObservationsService
|
||||
CharacteristicTypes models.CharacteristicTypesService
|
||||
Characteristics models.CharacteristicsService
|
||||
TextMeasurementTypes models.TextMeasurementTypesService
|
||||
UnitTypes models.UnitTypesService
|
||||
Measurements models.MeasurementsService
|
||||
|
@ -39,7 +39,7 @@ func NewDatastore(dbh modl.SqlExecutor) *Datastore {
|
|||
d.Species = &speciesStore{d}
|
||||
d.Strains = &strainsStore{d}
|
||||
d.CharacteristicTypes = &characteristicTypesStore{d}
|
||||
d.Observations = &observationsStore{d}
|
||||
d.Characteristics = &characteristicsStore{d}
|
||||
d.TextMeasurementTypes = &textMeasurementTypesStore{d}
|
||||
d.UnitTypes = &unitTypesStore{d}
|
||||
d.Measurements = &measurementsStore{d}
|
||||
|
@ -52,8 +52,8 @@ func NewMockDatastore() *Datastore {
|
|||
Genera: &models.MockGeneraService{},
|
||||
Species: &models.MockSpeciesService{},
|
||||
Strains: &models.MockStrainsService{},
|
||||
CharacteristicTypes: &models.MockCharacteristicTypesService{},
|
||||
Observations: &models.MockObservationsService{},
|
||||
CharacteristicTypes: &models.MockCharacteristicTypesService{},
|
||||
Characteristics: &models.MockCharacteristicsService{},
|
||||
TextMeasurementTypes: &models.MockTextMeasurementTypesService{},
|
||||
UnitTypes: &models.MockUnitTypesService{},
|
||||
Measurements: &models.MockMeasurementsService{},
|
||||
|
|
|
@ -22,16 +22,16 @@ func insertMeasurement(t *testing.T, tx *modl.Transaction) *models.Measurement {
|
|||
func newMeasurement(t *testing.T, tx *modl.Transaction) *models.Measurement {
|
||||
// we have a few things to take care of first...
|
||||
strain := insertStrain(t, tx)
|
||||
observation := insertObservation(t, tx)
|
||||
characteristic := insertCharacteristic(t, tx)
|
||||
|
||||
// we want to create and insert a unit type record, too.
|
||||
unit_type := insertUnitType(t, tx)
|
||||
|
||||
return &models.Measurement{
|
||||
StrainId: strain.Id,
|
||||
ObservationId: observation.Id,
|
||||
NumValue: models.NullFloat64{sql.NullFloat64{Float64: 1.23, Valid: true}},
|
||||
UnitTypeId: models.NullInt64{sql.NullInt64{Int64: unit_type.Id, Valid: true}},
|
||||
StrainId: strain.Id,
|
||||
CharacteristicId: characteristic.Id,
|
||||
NumValue: models.NullFloat64{sql.NullFloat64{Float64: 1.23, Valid: true}},
|
||||
UnitTypeId: models.NullInt64{sql.NullInt64{Int64: unit_type.Id, Valid: true}},
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
-- bactdb
|
||||
-- Matthew R Dillon
|
||||
|
||||
DROP TABLE observations;
|
||||
DROP TABLE characteristics;
|
||||
|
|
@ -1,18 +1,18 @@
|
|||
-- bactdb
|
||||
-- Matthew R Dillon
|
||||
|
||||
CREATE TABLE observations (
|
||||
CREATE TABLE characteristics (
|
||||
id BIGSERIAL NOT NULL,
|
||||
observation_name CHARACTER VARYING(100) NOT NULL,
|
||||
characteristic_name CHARACTER VARYING(100) NOT NULL,
|
||||
characteristic_type_id BIGINT NOT NULL,
|
||||
|
||||
created_at TIMESTAMP WITH TIME ZONE NOT NULL,
|
||||
updated_at TIMESTAMP WITH TIME ZONE NOT NULL,
|
||||
deleted_at TIMESTAMP WITH TIME ZONE NULL,
|
||||
|
||||
CONSTRAINT observations_pkey PRIMARY KEY (id),
|
||||
CONSTRAINT characteristics_pkey PRIMARY KEY (id),
|
||||
FOREIGN KEY (characteristic_type_id) REFERENCES characteristic_types(id)
|
||||
);
|
||||
|
||||
CREATE INDEX characteristic_type_id_idx ON observations (characteristic_type_id);
|
||||
CREATE INDEX characteristic_type_id_idx ON characteristics (characteristic_type_id);
|
||||
|
|
@ -4,7 +4,7 @@
|
|||
CREATE TABLE measurements (
|
||||
id BIGSERIAL NOT NULL,
|
||||
strain_id BIGINT NOT NULL,
|
||||
observation_id BIGINT NOT NULL,
|
||||
characteristic_id BIGINT NOT NULL,
|
||||
text_measurement_type_id BIGINT NULL,
|
||||
txt_value CHARACTER VARYING(255) NULL,
|
||||
num_value NUMERIC(8, 3) NULL,
|
||||
|
@ -16,9 +16,9 @@ CREATE TABLE measurements (
|
|||
created_at TIMESTAMP WITH TIME ZONE NOT NULL,
|
||||
updated_at TIMESTAMP WITH TIME ZONE NOT NULL,
|
||||
|
||||
CONSTRAINT strainsobsmeasurements_pkey PRIMARY KEY (id),
|
||||
CONSTRAINT strainscharmeasurements_pkey PRIMARY KEY (id),
|
||||
FOREIGN KEY (strain_id) REFERENCES strains(id),
|
||||
FOREIGN KEY (observation_id) REFERENCES observations(id),
|
||||
FOREIGN KEY (characteristic_id) REFERENCES characteristics(id),
|
||||
FOREIGN KEY (text_measurement_type_id) REFERENCES text_measurement_types(id),
|
||||
FOREIGN KEY (unit_type_id) REFERENCES unit_types(id),
|
||||
FOREIGN KEY (test_method_id) REFERENCES test_methods(id),
|
||||
|
@ -42,7 +42,7 @@ CREATE TABLE measurements (
|
|||
|
||||
CREATE INDEX strain_id_idx ON measurements (strain_id);
|
||||
|
||||
CREATE INDEX observation_id_idx ON measurements (observation_id);
|
||||
CREATE INDEX characteristic_id_idx ON measurements (characteristic_id);
|
||||
|
||||
CREATE INDEX text_measurement_type_id_idx ON measurements (text_measurement_type_id);
|
||||
|
||||
|
|
|
@ -1,87 +0,0 @@
|
|||
package datastore
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/thermokarst/bactdb/models"
|
||||
)
|
||||
|
||||
func init() {
|
||||
DB.AddTableWithName(models.Observation{}, "observations").SetKeys(true, "Id")
|
||||
}
|
||||
|
||||
type observationsStore struct {
|
||||
*Datastore
|
||||
}
|
||||
|
||||
func (s *observationsStore) Get(id int64) (*models.Observation, error) {
|
||||
var observation []*models.Observation
|
||||
if err := s.dbh.Select(&observation, `SELECT * FROM observations WHERE id=$1;`, id); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(observation) == 0 {
|
||||
return nil, models.ErrObservationNotFound
|
||||
}
|
||||
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
|
||||
}
|
||||
|
||||
func (s *observationsStore) List(opt *models.ObservationListOptions) ([]*models.Observation, error) {
|
||||
if opt == nil {
|
||||
opt = &models.ObservationListOptions{}
|
||||
}
|
||||
var observations []*models.Observation
|
||||
err := s.dbh.Select(&observations, `SELECT * FROM observations LIMIT $1 OFFSET $2;`, opt.PerPageOrDefault(), opt.Offset())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return observations, nil
|
||||
}
|
||||
|
||||
func (s *observationsStore) Update(id int64, observation *models.Observation) (bool, error) {
|
||||
_, err := s.Get(id)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
if id != observation.Id {
|
||||
return false, models.ErrObservationNotFound
|
||||
}
|
||||
|
||||
observation.UpdatedAt = time.Now()
|
||||
changed, err := s.dbh.Update(observation)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
if changed == 0 {
|
||||
return false, ErrNoRowsUpdated
|
||||
}
|
||||
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func (s *observationsStore) Delete(id int64) (bool, error) {
|
||||
observation, err := s.Get(id)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
deleted, err := s.dbh.Delete(observation)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
if deleted == 0 {
|
||||
return false, ErrNoRowsDeleted
|
||||
}
|
||||
return true, nil
|
||||
}
|
|
@ -1,129 +0,0 @@
|
|||
package datastore
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/jmoiron/modl"
|
||||
"github.com/thermokarst/bactdb/models"
|
||||
)
|
||||
|
||||
func insertObservation(t *testing.T, tx *modl.Transaction) *models.Observation {
|
||||
// clean up our target table
|
||||
tx.Exec(`DELETE FROM observations;`)
|
||||
observation := newObservation(t, tx)
|
||||
if err := tx.Insert(observation); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return observation
|
||||
}
|
||||
|
||||
func newObservation(t *testing.T, tx *modl.Transaction) *models.Observation {
|
||||
// we want to create and insert an characteristic type record, too.
|
||||
characteristic_type := insertCharacteristicType(t, tx)
|
||||
return &models.Observation{ObservationName: "Test Observation",
|
||||
CharacteristicTypeId: characteristic_type.Id}
|
||||
}
|
||||
|
||||
func TestObservationsStore_Get_db(t *testing.T) {
|
||||
tx, _ := DB.Begin()
|
||||
defer tx.Rollback()
|
||||
|
||||
want := insertObservation(t, tx)
|
||||
|
||||
d := NewDatastore(tx)
|
||||
|
||||
observation, err := d.Observations.Get(want.Id)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
normalizeTime(&want.CreatedAt, &want.UpdatedAt, &want.DeletedAt)
|
||||
normalizeTime(&observation.CreatedAt, &observation.UpdatedAt, &observation.DeletedAt)
|
||||
|
||||
if !reflect.DeepEqual(observation, want) {
|
||||
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")
|
||||
}
|
||||
}
|
||||
|
||||
func TestObservationsStore_List_db(t *testing.T) {
|
||||
tx, _ := DB.Begin()
|
||||
defer tx.Rollback()
|
||||
|
||||
want_observation := insertObservation(t, tx)
|
||||
want := []*models.Observation{want_observation}
|
||||
|
||||
d := NewDatastore(tx)
|
||||
|
||||
observations, err := d.Observations.List(&models.ObservationListOptions{ListOptions: models.ListOptions{Page: 1, PerPage: 10}})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
for i := range want {
|
||||
normalizeTime(&want[i].CreatedAt, &want[i].UpdatedAt, &want[i].DeletedAt)
|
||||
normalizeTime(&observations[i].CreatedAt, &observations[i].UpdatedAt, &observations[i].DeletedAt)
|
||||
}
|
||||
if !reflect.DeepEqual(observations, want) {
|
||||
t.Errorf("got observations %+v, want %+v", observations, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestObservationsStore_Update_db(t *testing.T) {
|
||||
tx, _ := DB.Begin()
|
||||
defer tx.Rollback()
|
||||
|
||||
observation := insertObservation(t, tx)
|
||||
|
||||
d := NewDatastore(tx)
|
||||
|
||||
// Tweak it
|
||||
observation.ObservationName = "Updated Obs"
|
||||
updated, err := d.Observations.Update(observation.Id, observation)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if !updated {
|
||||
t.Error("!updated")
|
||||
}
|
||||
}
|
||||
|
||||
func TestObservationsStore_Delete_db(t *testing.T) {
|
||||
tx, _ := DB.Begin()
|
||||
defer tx.Rollback()
|
||||
|
||||
observation := insertObservation(t, tx)
|
||||
|
||||
d := NewDatastore(tx)
|
||||
|
||||
// Delete it
|
||||
deleted, err := d.Observations.Delete(observation.Id)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if !deleted {
|
||||
t.Error("!delete")
|
||||
}
|
||||
}
|
Reference in a new issue