Renaming observation types to characteristic types

This commit is contained in:
Matthew Dillon 2014-12-12 10:24:59 -09:00
parent be9e6481d0
commit c1323f9c1f
24 changed files with 873 additions and 873 deletions

View file

@ -0,0 +1,87 @@
package datastore
import (
"time"
"github.com/thermokarst/bactdb/models"
)
func init() {
DB.AddTableWithName(models.CharacteristicType{}, "characteristic_types").SetKeys(true, "Id")
}
type characteristicTypesStore struct {
*Datastore
}
func (s *characteristicTypesStore) Get(id int64) (*models.CharacteristicType, error) {
var characteristic_type []*models.CharacteristicType
if err := s.dbh.Select(&characteristic_type, `SELECT * FROM characteristic_types WHERE id=$1;`, id); err != nil {
return nil, err
}
if len(characteristic_type) == 0 {
return nil, models.ErrCharacteristicTypeNotFound
}
return characteristic_type[0], nil
}
func (s *characteristicTypesStore) Create(characteristic_type *models.CharacteristicType) (bool, error) {
currentTime := time.Now()
characteristic_type.CreatedAt = currentTime
characteristic_type.UpdatedAt = currentTime
if err := s.dbh.Insert(characteristic_type); err != nil {
return false, err
}
return true, nil
}
func (s *characteristicTypesStore) List(opt *models.CharacteristicTypeListOptions) ([]*models.CharacteristicType, error) {
if opt == nil {
opt = &models.CharacteristicTypeListOptions{}
}
var characteristic_types []*models.CharacteristicType
err := s.dbh.Select(&characteristic_types, `SELECT * FROM characteristic_types LIMIT $1 OFFSET $2;`, opt.PerPageOrDefault(), opt.Offset())
if err != nil {
return nil, err
}
return characteristic_types, nil
}
func (s *characteristicTypesStore) Update(id int64, characteristic_type *models.CharacteristicType) (bool, error) {
_, err := s.Get(id)
if err != nil {
return false, err
}
if id != characteristic_type.Id {
return false, models.ErrCharacteristicTypeNotFound
}
characteristic_type.UpdatedAt = time.Now()
changed, err := s.dbh.Update(characteristic_type)
if err != nil {
return false, err
}
if changed == 0 {
return false, ErrNoRowsUpdated
}
return true, nil
}
func (s *characteristicTypesStore) Delete(id int64) (bool, error) {
characteristic_type, err := s.Get(id)
if err != nil {
return false, err
}
deleted, err := s.dbh.Delete(characteristic_type)
if err != nil {
return false, err
}
if deleted == 0 {
return false, ErrNoRowsDeleted
}
return true, nil
}

View file

@ -0,0 +1,126 @@
package datastore
import (
"reflect"
"testing"
"github.com/jmoiron/modl"
"github.com/thermokarst/bactdb/models"
)
func insertCharacteristicType(t *testing.T, tx *modl.Transaction) *models.CharacteristicType {
// clean up our target table
tx.Exec(`DELETE FROM characteristic_types;`)
characteristic_type := newCharacteristicType(t, tx)
if err := tx.Insert(characteristic_type); err != nil {
t.Fatal(err)
}
return characteristic_type
}
func newCharacteristicType(t *testing.T, tx *modl.Transaction) *models.CharacteristicType {
return &models.CharacteristicType{CharacteristicTypeName: "Test Obs"}
}
func TestCharacteristicTypesStore_Get_db(t *testing.T) {
tx, _ := DB.Begin()
defer tx.Rollback()
want := insertCharacteristicType(t, tx)
d := NewDatastore(tx)
characteristic_type, err := d.CharacteristicTypes.Get(want.Id)
if err != nil {
t.Fatal(err)
}
normalizeTime(&want.CreatedAt, &want.UpdatedAt, &want.DeletedAt)
normalizeTime(&characteristic_type.CreatedAt, &characteristic_type.UpdatedAt, &characteristic_type.DeletedAt)
if !reflect.DeepEqual(characteristic_type, want) {
t.Errorf("got characteristic_type %+v, want %+v", characteristic_type, want)
}
}
func TestCharacteristicTypesStore_Create_db(t *testing.T) {
tx, _ := DB.Begin()
defer tx.Rollback()
characteristic_type := newCharacteristicType(t, tx)
d := NewDatastore(tx)
created, err := d.CharacteristicTypes.Create(characteristic_type)
if err != nil {
t.Fatal(err)
}
if !created {
t.Error("!created")
}
if characteristic_type.Id == 0 {
t.Error("want nonzero characteristic_type.Id after submitting")
}
}
func TestCharacteristicTypesStore_List_db(t *testing.T) {
tx, _ := DB.Begin()
defer tx.Rollback()
want_characteristic_type := insertCharacteristicType(t, tx)
want := []*models.CharacteristicType{want_characteristic_type}
d := NewDatastore(tx)
characteristic_types, err := d.CharacteristicTypes.List(&models.CharacteristicTypeListOptions{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(&characteristic_types[i].CreatedAt, &characteristic_types[i].UpdatedAt, &characteristic_types[i].DeletedAt)
}
if !reflect.DeepEqual(characteristic_types, want) {
t.Errorf("got characteristic_types %+v, want %+v", characteristic_types, want)
}
}
func TestCharacteristicTypesStore_Update_db(t *testing.T) {
tx, _ := DB.Begin()
defer tx.Rollback()
characteristic_type := insertCharacteristicType(t, tx)
d := NewDatastore(tx)
// Tweak it
characteristic_type.CharacteristicTypeName = "Updated Obs Type"
updated, err := d.CharacteristicTypes.Update(characteristic_type.Id, characteristic_type)
if err != nil {
t.Fatal(err)
}
if !updated {
t.Error("!updated")
}
}
func TestCharacteristicTypesStore_Delete_db(t *testing.T) {
tx, _ := DB.Begin()
defer tx.Rollback()
characteristic_type := insertCharacteristicType(t, tx)
d := NewDatastore(tx)
// Delete it
deleted, err := d.CharacteristicTypes.Delete(characteristic_type.Id)
if err != nil {
t.Fatal(err)
}
if !deleted {
t.Error("!delete")
}
}

View file

@ -13,7 +13,7 @@ type Datastore struct {
Genera models.GeneraService
Species models.SpeciesService
Strains models.StrainsService
ObservationTypes models.ObservationTypesService
CharacteristicTypes models.CharacteristicTypesService
Observations models.ObservationsService
TextMeasurementTypes models.TextMeasurementTypesService
UnitTypes models.UnitTypesService
@ -38,7 +38,7 @@ func NewDatastore(dbh modl.SqlExecutor) *Datastore {
d.Genera = &generaStore{d}
d.Species = &speciesStore{d}
d.Strains = &strainsStore{d}
d.ObservationTypes = &observationTypesStore{d}
d.CharacteristicTypes = &characteristicTypesStore{d}
d.Observations = &observationsStore{d}
d.TextMeasurementTypes = &textMeasurementTypesStore{d}
d.UnitTypes = &unitTypesStore{d}
@ -52,7 +52,7 @@ func NewMockDatastore() *Datastore {
Genera: &models.MockGeneraService{},
Species: &models.MockSpeciesService{},
Strains: &models.MockStrainsService{},
ObservationTypes: &models.MockObservationTypesService{},
CharacteristicTypes: &models.MockCharacteristicTypesService{},
Observations: &models.MockObservationsService{},
TextMeasurementTypes: &models.MockTextMeasurementTypesService{},
UnitTypes: &models.MockUnitTypesService{},

View file

@ -0,0 +1,5 @@
-- bactdb
-- Matthew R Dillon
DROP TABLE characteristic_types;

View file

@ -1,14 +1,14 @@
-- bactdb
-- Matthew R Dillon
CREATE TABLE observation_types (
CREATE TABLE characteristic_types (
id BIGSERIAL NOT NULL,
observation_type_name CHARACTER VARYING(100) NOT NULL,
characteristic_type_name CHARACTER VARYING(100) 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 observation_types_pkey PRIMARY KEY (id)
CONSTRAINT characteristic_types_pkey PRIMARY KEY (id)
);

View file

@ -1,5 +0,0 @@
-- bactdb
-- Matthew R Dillon
DROP TABLE observation_types;

View file

@ -4,15 +4,15 @@
CREATE TABLE observations (
id BIGSERIAL NOT NULL,
observation_name CHARACTER VARYING(100) NOT NULL,
observation_type_id BIGINT 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),
FOREIGN KEY (observation_type_id) REFERENCES observation_types(id)
FOREIGN KEY (characteristic_type_id) REFERENCES characteristic_types(id)
);
CREATE INDEX observation_type_id_idx ON observations (observation_type_id);
CREATE INDEX characteristic_type_id_idx ON observations (characteristic_type_id);

View file

@ -1,87 +0,0 @@
package datastore
import (
"time"
"github.com/thermokarst/bactdb/models"
)
func init() {
DB.AddTableWithName(models.ObservationType{}, "observation_types").SetKeys(true, "Id")
}
type observationTypesStore struct {
*Datastore
}
func (s *observationTypesStore) Get(id int64) (*models.ObservationType, error) {
var observation_type []*models.ObservationType
if err := s.dbh.Select(&observation_type, `SELECT * FROM observation_types WHERE id=$1;`, id); err != nil {
return nil, err
}
if len(observation_type) == 0 {
return nil, models.ErrObservationTypeNotFound
}
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
}
func (s *observationTypesStore) List(opt *models.ObservationTypeListOptions) ([]*models.ObservationType, error) {
if opt == nil {
opt = &models.ObservationTypeListOptions{}
}
var observation_types []*models.ObservationType
err := s.dbh.Select(&observation_types, `SELECT * FROM observation_types LIMIT $1 OFFSET $2;`, opt.PerPageOrDefault(), opt.Offset())
if err != nil {
return nil, err
}
return observation_types, nil
}
func (s *observationTypesStore) Update(id int64, observation_type *models.ObservationType) (bool, error) {
_, err := s.Get(id)
if err != nil {
return false, err
}
if id != observation_type.Id {
return false, models.ErrObservationTypeNotFound
}
observation_type.UpdatedAt = time.Now()
changed, err := s.dbh.Update(observation_type)
if err != nil {
return false, err
}
if changed == 0 {
return false, ErrNoRowsUpdated
}
return true, nil
}
func (s *observationTypesStore) Delete(id int64) (bool, error) {
observation_type, err := s.Get(id)
if err != nil {
return false, err
}
deleted, err := s.dbh.Delete(observation_type)
if err != nil {
return false, err
}
if deleted == 0 {
return false, ErrNoRowsDeleted
}
return true, nil
}

View file

@ -1,126 +0,0 @@
package datastore
import (
"reflect"
"testing"
"github.com/jmoiron/modl"
"github.com/thermokarst/bactdb/models"
)
func insertObservationType(t *testing.T, tx *modl.Transaction) *models.ObservationType {
// clean up our target table
tx.Exec(`DELETE FROM observation_types;`)
observation_type := newObservationType(t, tx)
if err := tx.Insert(observation_type); err != nil {
t.Fatal(err)
}
return observation_type
}
func newObservationType(t *testing.T, tx *modl.Transaction) *models.ObservationType {
return &models.ObservationType{ObservationTypeName: "Test Obs"}
}
func TestObservationTypesStore_Get_db(t *testing.T) {
tx, _ := DB.Begin()
defer tx.Rollback()
want := insertObservationType(t, tx)
d := NewDatastore(tx)
observation_type, err := d.ObservationTypes.Get(want.Id)
if err != nil {
t.Fatal(err)
}
normalizeTime(&want.CreatedAt, &want.UpdatedAt, &want.DeletedAt)
normalizeTime(&observation_type.CreatedAt, &observation_type.UpdatedAt, &observation_type.DeletedAt)
if !reflect.DeepEqual(observation_type, want) {
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")
}
}
func TestObservationTypesStore_List_db(t *testing.T) {
tx, _ := DB.Begin()
defer tx.Rollback()
want_observation_type := insertObservationType(t, tx)
want := []*models.ObservationType{want_observation_type}
d := NewDatastore(tx)
observation_types, err := d.ObservationTypes.List(&models.ObservationTypeListOptions{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(&observation_types[i].CreatedAt, &observation_types[i].UpdatedAt, &observation_types[i].DeletedAt)
}
if !reflect.DeepEqual(observation_types, want) {
t.Errorf("got observation_types %+v, want %+v", observation_types, want)
}
}
func TestObservationTypesStore_Update_db(t *testing.T) {
tx, _ := DB.Begin()
defer tx.Rollback()
observation_type := insertObservationType(t, tx)
d := NewDatastore(tx)
// Tweak it
observation_type.ObservationTypeName = "Updated Obs Type"
updated, err := d.ObservationTypes.Update(observation_type.Id, observation_type)
if err != nil {
t.Fatal(err)
}
if !updated {
t.Error("!updated")
}
}
func TestObservationTypesStore_Delete_db(t *testing.T) {
tx, _ := DB.Begin()
defer tx.Rollback()
observation_type := insertObservationType(t, tx)
d := NewDatastore(tx)
// Delete it
deleted, err := d.ObservationTypes.Delete(observation_type.Id)
if err != nil {
t.Fatal(err)
}
if !deleted {
t.Error("!delete")
}
}

View file

@ -19,10 +19,10 @@ func insertObservation(t *testing.T, tx *modl.Transaction) *models.Observation {
}
func newObservation(t *testing.T, tx *modl.Transaction) *models.Observation {
// we want to create and insert an observation type record, too.
observation_type := insertObservationType(t, tx)
// we want to create and insert an characteristic type record, too.
characteristic_type := insertCharacteristicType(t, tx)
return &models.Observation{ObservationName: "Test Observation",
ObservationTypeId: observation_type.Id}
CharacteristicTypeId: characteristic_type.Id}
}
func TestObservationsStore_Get_db(t *testing.T) {