From c1323f9c1f3a1c0365b9d24026a12abdfc7f8fdb Mon Sep 17 00:00:00 2001 From: Matthew Dillon Date: Fri, 12 Dec 2014 10:24:59 -0900 Subject: [PATCH] Renaming observation types to characteristic types --- api/characteristic_types.go | 92 ++++++++ api/characteristic_types_test.go | 153 +++++++++++++ api/handler.go | 10 +- api/observation_types.go | 92 -------- api/observation_types_test.go | 153 ------------- datastore/characteristic_types.go | 87 ++++++++ datastore/characteristic_types_test.go | 126 +++++++++++ datastore/datastore.go | 6 +- .../00005_AddCharacteristicTypes_down.sql | 5 + ...ql => 00005_AddCharacteristicTypes_up.sql} | 6 +- .../00005_AddObservationTypes_down.sql | 5 - .../migrations/00006_AddObservations_up.sql | 6 +- datastore/observation_types.go | 87 -------- datastore/observation_types_test.go | 126 ----------- datastore/observations_test.go | 6 +- models/characteristic_types.go | 203 ++++++++++++++++++ models/characteristic_types_test.go | 174 +++++++++++++++ models/client.go | 4 +- models/observation_types.go | 203 ------------------ models/observation_types_test.go | 174 --------------- models/observations.go | 2 +- models/observations_test.go | 4 +- router/api.go | 12 +- router/routes.go | 10 +- 24 files changed, 873 insertions(+), 873 deletions(-) create mode 100644 api/characteristic_types.go create mode 100644 api/characteristic_types_test.go delete mode 100644 api/observation_types.go delete mode 100644 api/observation_types_test.go create mode 100644 datastore/characteristic_types.go create mode 100644 datastore/characteristic_types_test.go create mode 100644 datastore/migrations/00005_AddCharacteristicTypes_down.sql rename datastore/migrations/{00005_AddObservationTypes_up.sql => 00005_AddCharacteristicTypes_up.sql} (57%) delete mode 100644 datastore/migrations/00005_AddObservationTypes_down.sql delete mode 100644 datastore/observation_types.go delete mode 100644 datastore/observation_types_test.go create mode 100644 models/characteristic_types.go create mode 100644 models/characteristic_types_test.go delete mode 100644 models/observation_types.go delete mode 100644 models/observation_types_test.go diff --git a/api/characteristic_types.go b/api/characteristic_types.go new file mode 100644 index 0000000..3febdb4 --- /dev/null +++ b/api/characteristic_types.go @@ -0,0 +1,92 @@ +package api + +import ( + "encoding/json" + "net/http" + "strconv" + + "github.com/gorilla/mux" + "github.com/thermokarst/bactdb/models" +) + +func serveCharacteristicType(w http.ResponseWriter, r *http.Request) error { + id, err := strconv.ParseInt(mux.Vars(r)["Id"], 10, 0) + if err != nil { + return err + } + + characteristic_type, err := store.CharacteristicTypes.Get(id) + if err != nil { + return err + } + + return writeJSON(w, characteristic_type) +} + +func serveCreateCharacteristicType(w http.ResponseWriter, r *http.Request) error { + var characteristic_type models.CharacteristicType + err := json.NewDecoder(r.Body).Decode(&characteristic_type) + if err != nil { + return err + } + + created, err := store.CharacteristicTypes.Create(&characteristic_type) + if err != nil { + return err + } + if created { + w.WriteHeader(http.StatusCreated) + } + + return writeJSON(w, characteristic_type) +} + +func serveCharacteristicTypeList(w http.ResponseWriter, r *http.Request) error { + var opt models.CharacteristicTypeListOptions + if err := schemaDecoder.Decode(&opt, r.URL.Query()); err != nil { + return err + } + + characteristic_types, err := store.CharacteristicTypes.List(&opt) + if err != nil { + return err + } + if characteristic_types == nil { + characteristic_types = []*models.CharacteristicType{} + } + + return writeJSON(w, characteristic_types) +} + +func serveUpdateCharacteristicType(w http.ResponseWriter, r *http.Request) error { + id, _ := strconv.ParseInt(mux.Vars(r)["Id"], 10, 0) + var characteristic_type models.CharacteristicType + err := json.NewDecoder(r.Body).Decode(&characteristic_type) + if err != nil { + return err + } + + updated, err := store.CharacteristicTypes.Update(id, &characteristic_type) + if err != nil { + return err + } + if updated { + w.WriteHeader(http.StatusOK) + } + + return writeJSON(w, characteristic_type) +} + +func serveDeleteCharacteristicType(w http.ResponseWriter, r *http.Request) error { + id, _ := strconv.ParseInt(mux.Vars(r)["Id"], 10, 0) + + deleted, err := store.CharacteristicTypes.Delete(id) + if err != nil { + return err + } + if deleted { + w.WriteHeader(http.StatusOK) + } + + return writeJSON(w, &models.CharacteristicType{}) +} diff --git a/api/characteristic_types_test.go b/api/characteristic_types_test.go new file mode 100644 index 0000000..3b0b7ab --- /dev/null +++ b/api/characteristic_types_test.go @@ -0,0 +1,153 @@ +package api + +import ( + "testing" + + "github.com/thermokarst/bactdb/models" +) + +func newCharacteristicType() *models.CharacteristicType { + characteristic_type := models.NewCharacteristicType() + return characteristic_type +} + +func TestCharacteristicType_Get(t *testing.T) { + setup() + + want := newCharacteristicType() + + calledGet := false + + store.CharacteristicTypes.(*models.MockCharacteristicTypesService).Get_ = func(id int64) (*models.CharacteristicType, error) { + if id != want.Id { + t.Errorf("wanted request for characteristic_type %d but got %d", want.Id, id) + } + calledGet = true + return want, nil + } + + got, err := apiClient.CharacteristicTypes.Get(want.Id) + if err != nil { + t.Fatal(err) + } + + if !calledGet { + t.Error("!calledGet") + } + if !normalizeDeepEqual(want, got) { + t.Errorf("got %+v but wanted %+v", got, want) + } +} + +func TestCharacteristicType_Create(t *testing.T) { + setup() + + want := newCharacteristicType() + + calledPost := false + store.CharacteristicTypes.(*models.MockCharacteristicTypesService).Create_ = func(characteristic_type *models.CharacteristicType) (bool, error) { + if !normalizeDeepEqual(want, characteristic_type) { + t.Errorf("wanted request for characteristic_type %d but got %d", want, characteristic_type) + } + calledPost = true + return true, nil + } + + success, err := apiClient.CharacteristicTypes.Create(want) + if err != nil { + t.Fatal(err) + } + + if !calledPost { + t.Error("!calledPost") + } + if !success { + t.Error("!success") + } +} + +func TestCharacteristicType_List(t *testing.T) { + setup() + + want := []*models.CharacteristicType{newCharacteristicType()} + wantOpt := &models.CharacteristicTypeListOptions{ListOptions: models.ListOptions{Page: 1, PerPage: 10}} + + calledList := false + store.CharacteristicTypes.(*models.MockCharacteristicTypesService).List_ = func(opt *models.CharacteristicTypeListOptions) ([]*models.CharacteristicType, error) { + if !normalizeDeepEqual(wantOpt, opt) { + t.Errorf("wanted options %d but got %d", wantOpt, opt) + } + calledList = true + return want, nil + } + + characteristic_types, err := apiClient.CharacteristicTypes.List(wantOpt) + if err != nil { + t.Fatal(err) + } + + if !calledList { + t.Error("!calledList") + } + + if !normalizeDeepEqual(&want, &characteristic_types) { + t.Errorf("got characteristic_types %+v but wanted characteristic_types %+v", characteristic_types, want) + } +} + +func TestCharacteristicType_Update(t *testing.T) { + setup() + + want := newCharacteristicType() + + calledPut := false + store.CharacteristicTypes.(*models.MockCharacteristicTypesService).Update_ = func(id int64, characteristic_type *models.CharacteristicType) (bool, error) { + if id != want.Id { + t.Errorf("wanted request for characteristic_type %d but got %d", want.Id, id) + } + if !normalizeDeepEqual(want, characteristic_type) { + t.Errorf("wanted request for characteristic_type %d but got %d", want, characteristic_type) + } + calledPut = true + return true, nil + } + + success, err := apiClient.CharacteristicTypes.Update(want.Id, want) + if err != nil { + t.Fatal(err) + } + + if !calledPut { + t.Error("!calledPut") + } + if !success { + t.Error("!success") + } +} + +func TestCharacteristicType_Delete(t *testing.T) { + setup() + + want := newCharacteristicType() + + calledDelete := false + store.CharacteristicTypes.(*models.MockCharacteristicTypesService).Delete_ = func(id int64) (bool, error) { + if id != want.Id { + t.Errorf("wanted request for characteristic_type %d but got %d", want.Id, id) + } + calledDelete = true + return true, nil + } + + success, err := apiClient.CharacteristicTypes.Delete(want.Id) + if err != nil { + t.Fatal(err) + } + + if !calledDelete { + t.Error("!calledDelete") + } + if !success { + t.Error("!success") + } +} diff --git a/api/handler.go b/api/handler.go index bd80ca4..a50cd18 100644 --- a/api/handler.go +++ b/api/handler.go @@ -39,11 +39,11 @@ func Handler() *mux.Router { m.Get(router.UpdateStrain).Handler(handler(serveUpdateStrain)) m.Get(router.DeleteStrain).Handler(handler(serveDeleteStrain)) - m.Get(router.ObservationType).Handler(handler(serveObservationType)) - m.Get(router.CreateObservationType).Handler(handler(serveCreateObservationType)) - m.Get(router.ObservationTypes).Handler(handler(serveObservationTypeList)) - m.Get(router.UpdateObservationType).Handler(handler(serveUpdateObservationType)) - m.Get(router.DeleteObservationType).Handler(handler(serveDeleteObservationType)) + m.Get(router.CharacteristicType).Handler(handler(serveCharacteristicType)) + m.Get(router.CreateCharacteristicType).Handler(handler(serveCreateCharacteristicType)) + m.Get(router.CharacteristicTypes).Handler(handler(serveCharacteristicTypeList)) + m.Get(router.UpdateCharacteristicType).Handler(handler(serveUpdateCharacteristicType)) + m.Get(router.DeleteCharacteristicType).Handler(handler(serveDeleteCharacteristicType)) m.Get(router.Observation).Handler(handler(serveObservation)) m.Get(router.CreateObservation).Handler(handler(serveCreateObservation)) diff --git a/api/observation_types.go b/api/observation_types.go deleted file mode 100644 index abdbc9f..0000000 --- a/api/observation_types.go +++ /dev/null @@ -1,92 +0,0 @@ -package api - -import ( - "encoding/json" - "net/http" - "strconv" - - "github.com/gorilla/mux" - "github.com/thermokarst/bactdb/models" -) - -func serveObservationType(w http.ResponseWriter, r *http.Request) error { - id, err := strconv.ParseInt(mux.Vars(r)["Id"], 10, 0) - if err != nil { - return err - } - - observation_type, err := store.ObservationTypes.Get(id) - if err != nil { - return err - } - - return writeJSON(w, observation_type) -} - -func serveCreateObservationType(w http.ResponseWriter, r *http.Request) error { - var observation_type models.ObservationType - err := json.NewDecoder(r.Body).Decode(&observation_type) - if err != nil { - return err - } - - created, err := store.ObservationTypes.Create(&observation_type) - if err != nil { - return err - } - if created { - w.WriteHeader(http.StatusCreated) - } - - return writeJSON(w, observation_type) -} - -func serveObservationTypeList(w http.ResponseWriter, r *http.Request) error { - var opt models.ObservationTypeListOptions - if err := schemaDecoder.Decode(&opt, r.URL.Query()); err != nil { - return err - } - - observation_types, err := store.ObservationTypes.List(&opt) - if err != nil { - return err - } - if observation_types == nil { - observation_types = []*models.ObservationType{} - } - - return writeJSON(w, observation_types) -} - -func serveUpdateObservationType(w http.ResponseWriter, r *http.Request) error { - id, _ := strconv.ParseInt(mux.Vars(r)["Id"], 10, 0) - var observation_type models.ObservationType - err := json.NewDecoder(r.Body).Decode(&observation_type) - if err != nil { - return err - } - - updated, err := store.ObservationTypes.Update(id, &observation_type) - if err != nil { - return err - } - if updated { - w.WriteHeader(http.StatusOK) - } - - return writeJSON(w, observation_type) -} - -func serveDeleteObservationType(w http.ResponseWriter, r *http.Request) error { - id, _ := strconv.ParseInt(mux.Vars(r)["Id"], 10, 0) - - deleted, err := store.ObservationTypes.Delete(id) - if err != nil { - return err - } - if deleted { - w.WriteHeader(http.StatusOK) - } - - return writeJSON(w, &models.ObservationType{}) -} diff --git a/api/observation_types_test.go b/api/observation_types_test.go deleted file mode 100644 index 62bc19b..0000000 --- a/api/observation_types_test.go +++ /dev/null @@ -1,153 +0,0 @@ -package api - -import ( - "testing" - - "github.com/thermokarst/bactdb/models" -) - -func newObservationType() *models.ObservationType { - observation_type := models.NewObservationType() - return observation_type -} - -func TestObservationType_Get(t *testing.T) { - setup() - - want := newObservationType() - - calledGet := false - - store.ObservationTypes.(*models.MockObservationTypesService).Get_ = func(id int64) (*models.ObservationType, error) { - if id != want.Id { - t.Errorf("wanted request for observation_type %d but got %d", want.Id, id) - } - calledGet = true - return want, nil - } - - got, err := apiClient.ObservationTypes.Get(want.Id) - if err != nil { - t.Fatal(err) - } - - if !calledGet { - t.Error("!calledGet") - } - if !normalizeDeepEqual(want, got) { - t.Errorf("got %+v but wanted %+v", got, want) - } -} - -func TestObservationType_Create(t *testing.T) { - setup() - - want := newObservationType() - - calledPost := false - store.ObservationTypes.(*models.MockObservationTypesService).Create_ = func(observation_type *models.ObservationType) (bool, error) { - if !normalizeDeepEqual(want, observation_type) { - t.Errorf("wanted request for observation_type %d but got %d", want, observation_type) - } - calledPost = true - return true, nil - } - - success, err := apiClient.ObservationTypes.Create(want) - if err != nil { - t.Fatal(err) - } - - if !calledPost { - t.Error("!calledPost") - } - if !success { - t.Error("!success") - } -} - -func TestObservationType_List(t *testing.T) { - setup() - - want := []*models.ObservationType{newObservationType()} - wantOpt := &models.ObservationTypeListOptions{ListOptions: models.ListOptions{Page: 1, PerPage: 10}} - - calledList := false - store.ObservationTypes.(*models.MockObservationTypesService).List_ = func(opt *models.ObservationTypeListOptions) ([]*models.ObservationType, error) { - if !normalizeDeepEqual(wantOpt, opt) { - t.Errorf("wanted options %d but got %d", wantOpt, opt) - } - calledList = true - return want, nil - } - - observation_types, err := apiClient.ObservationTypes.List(wantOpt) - if err != nil { - t.Fatal(err) - } - - if !calledList { - t.Error("!calledList") - } - - if !normalizeDeepEqual(&want, &observation_types) { - t.Errorf("got observation_types %+v but wanted observation_types %+v", observation_types, want) - } -} - -func TestObservationType_Update(t *testing.T) { - setup() - - want := newObservationType() - - calledPut := false - store.ObservationTypes.(*models.MockObservationTypesService).Update_ = func(id int64, observation_type *models.ObservationType) (bool, error) { - if id != want.Id { - t.Errorf("wanted request for observation_type %d but got %d", want.Id, id) - } - if !normalizeDeepEqual(want, observation_type) { - t.Errorf("wanted request for observation_type %d but got %d", want, observation_type) - } - calledPut = true - return true, nil - } - - success, err := apiClient.ObservationTypes.Update(want.Id, want) - if err != nil { - t.Fatal(err) - } - - if !calledPut { - t.Error("!calledPut") - } - if !success { - t.Error("!success") - } -} - -func TestObservationType_Delete(t *testing.T) { - setup() - - want := newObservationType() - - calledDelete := false - store.ObservationTypes.(*models.MockObservationTypesService).Delete_ = func(id int64) (bool, error) { - if id != want.Id { - t.Errorf("wanted request for observation_type %d but got %d", want.Id, id) - } - calledDelete = true - return true, nil - } - - success, err := apiClient.ObservationTypes.Delete(want.Id) - if err != nil { - t.Fatal(err) - } - - if !calledDelete { - t.Error("!calledDelete") - } - if !success { - t.Error("!success") - } -} diff --git a/datastore/characteristic_types.go b/datastore/characteristic_types.go new file mode 100644 index 0000000..6c75119 --- /dev/null +++ b/datastore/characteristic_types.go @@ -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 +} diff --git a/datastore/characteristic_types_test.go b/datastore/characteristic_types_test.go new file mode 100644 index 0000000..d5cb8ee --- /dev/null +++ b/datastore/characteristic_types_test.go @@ -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") + } +} diff --git a/datastore/datastore.go b/datastore/datastore.go index 7ad7b70..5a9e08c 100644 --- a/datastore/datastore.go +++ b/datastore/datastore.go @@ -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{}, diff --git a/datastore/migrations/00005_AddCharacteristicTypes_down.sql b/datastore/migrations/00005_AddCharacteristicTypes_down.sql new file mode 100644 index 0000000..a0edb5c --- /dev/null +++ b/datastore/migrations/00005_AddCharacteristicTypes_down.sql @@ -0,0 +1,5 @@ +-- bactdb +-- Matthew R Dillon + +DROP TABLE characteristic_types; + diff --git a/datastore/migrations/00005_AddObservationTypes_up.sql b/datastore/migrations/00005_AddCharacteristicTypes_up.sql similarity index 57% rename from datastore/migrations/00005_AddObservationTypes_up.sql rename to datastore/migrations/00005_AddCharacteristicTypes_up.sql index d96fe85..c98d44c 100644 --- a/datastore/migrations/00005_AddObservationTypes_up.sql +++ b/datastore/migrations/00005_AddCharacteristicTypes_up.sql @@ -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) ); diff --git a/datastore/migrations/00005_AddObservationTypes_down.sql b/datastore/migrations/00005_AddObservationTypes_down.sql deleted file mode 100644 index 39f6366..0000000 --- a/datastore/migrations/00005_AddObservationTypes_down.sql +++ /dev/null @@ -1,5 +0,0 @@ --- bactdb --- Matthew R Dillon - -DROP TABLE observation_types; - diff --git a/datastore/migrations/00006_AddObservations_up.sql b/datastore/migrations/00006_AddObservations_up.sql index 0a21b64..f5ad3f2 100644 --- a/datastore/migrations/00006_AddObservations_up.sql +++ b/datastore/migrations/00006_AddObservations_up.sql @@ -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); diff --git a/datastore/observation_types.go b/datastore/observation_types.go deleted file mode 100644 index d4b08f9..0000000 --- a/datastore/observation_types.go +++ /dev/null @@ -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 -} diff --git a/datastore/observation_types_test.go b/datastore/observation_types_test.go deleted file mode 100644 index 69dbf78..0000000 --- a/datastore/observation_types_test.go +++ /dev/null @@ -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") - } -} diff --git a/datastore/observations_test.go b/datastore/observations_test.go index 428a123..7d3423d 100644 --- a/datastore/observations_test.go +++ b/datastore/observations_test.go @@ -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) { diff --git a/models/characteristic_types.go b/models/characteristic_types.go new file mode 100644 index 0000000..d2f22a8 --- /dev/null +++ b/models/characteristic_types.go @@ -0,0 +1,203 @@ +package models + +import ( + "errors" + "net/http" + "strconv" + "time" + + "github.com/thermokarst/bactdb/router" +) + +// A Characteristic Type is a lookup type +type CharacteristicType struct { + Id int64 `json:"id,omitempty"` + CharacteristicTypeName string `db:"characteristic_type_name" json:"characteristicTypeName"` + CreatedAt time.Time `db:"created_at" json:"createdAt"` + UpdatedAt time.Time `db:"updated_at" json:"updatedAt"` + DeletedAt NullTime `db:"deleted_at" json:"deletedAt"` +} + +func NewCharacteristicType() *CharacteristicType { + return &CharacteristicType{ + CharacteristicTypeName: "Test Obs Type", + } +} + +type CharacteristicTypesService interface { + // Get a characteristic type + Get(id int64) (*CharacteristicType, error) + + // List all characteristic types + List(opt *CharacteristicTypeListOptions) ([]*CharacteristicType, error) + + // Create a characteristic type record + Create(characteristic_type *CharacteristicType) (bool, error) + + // Update an existing characteristic type + Update(id int64, characteristic_type *CharacteristicType) (updated bool, err error) + + // Delete an existing characteristic type + Delete(id int64) (deleted bool, err error) +} + +var ( + ErrCharacteristicTypeNotFound = errors.New("characteristic type not found") +) + +type characteristicTypesService struct { + client *Client +} + +func (s *characteristicTypesService) Get(id int64) (*CharacteristicType, error) { + strId := strconv.FormatInt(id, 10) + + url, err := s.client.url(router.CharacteristicType, map[string]string{"Id": strId}, nil) + if err != nil { + return nil, err + } + + req, err := s.client.NewRequest("GET", url.String(), nil) + if err != nil { + return nil, err + } + + var characteristic_type *CharacteristicType + _, err = s.client.Do(req, &characteristic_type) + if err != nil { + return nil, err + } + + return characteristic_type, nil +} + +func (s *characteristicTypesService) Create(characteristic_type *CharacteristicType) (bool, error) { + url, err := s.client.url(router.CreateCharacteristicType, nil, nil) + if err != nil { + return false, err + } + + req, err := s.client.NewRequest("POST", url.String(), characteristic_type) + if err != nil { + return false, err + } + + resp, err := s.client.Do(req, &characteristic_type) + if err != nil { + return false, err + } + + return resp.StatusCode == http.StatusCreated, nil +} + +type CharacteristicTypeListOptions struct { + ListOptions +} + +func (s *characteristicTypesService) List(opt *CharacteristicTypeListOptions) ([]*CharacteristicType, error) { + url, err := s.client.url(router.CharacteristicTypes, nil, opt) + if err != nil { + return nil, err + } + + req, err := s.client.NewRequest("GET", url.String(), nil) + if err != nil { + return nil, err + } + + var characteristic_types []*CharacteristicType + _, err = s.client.Do(req, &characteristic_types) + if err != nil { + return nil, err + } + + return characteristic_types, nil +} + +func (s *characteristicTypesService) Update(id int64, characteristic_type *CharacteristicType) (bool, error) { + strId := strconv.FormatInt(id, 10) + + url, err := s.client.url(router.UpdateCharacteristicType, map[string]string{"Id": strId}, nil) + if err != nil { + return false, err + } + + req, err := s.client.NewRequest("PUT", url.String(), characteristic_type) + if err != nil { + return false, err + } + + resp, err := s.client.Do(req, &characteristic_type) + if err != nil { + return false, err + } + + return resp.StatusCode == http.StatusOK, nil +} + +func (s *characteristicTypesService) Delete(id int64) (bool, error) { + strId := strconv.FormatInt(id, 10) + + url, err := s.client.url(router.DeleteCharacteristicType, map[string]string{"Id": strId}, nil) + if err != nil { + return false, err + } + + req, err := s.client.NewRequest("DELETE", url.String(), nil) + if err != nil { + return false, err + } + + var characteristic_type *CharacteristicType + resp, err := s.client.Do(req, &characteristic_type) + if err != nil { + return false, err + } + + return resp.StatusCode == http.StatusOK, nil +} + +type MockCharacteristicTypesService struct { + Get_ func(id int64) (*CharacteristicType, error) + List_ func(opt *CharacteristicTypeListOptions) ([]*CharacteristicType, error) + Create_ func(characteristic_type *CharacteristicType) (bool, error) + Update_ func(id int64, characteristic_type *CharacteristicType) (bool, error) + Delete_ func(id int64) (bool, error) +} + +var _ CharacteristicTypesService = &MockCharacteristicTypesService{} + +func (s *MockCharacteristicTypesService) Get(id int64) (*CharacteristicType, error) { + if s.Get_ == nil { + return nil, nil + } + return s.Get_(id) +} + +func (s *MockCharacteristicTypesService) Create(characteristic_type *CharacteristicType) (bool, error) { + if s.Create_ == nil { + return false, nil + } + return s.Create_(characteristic_type) +} + +func (s *MockCharacteristicTypesService) List(opt *CharacteristicTypeListOptions) ([]*CharacteristicType, error) { + if s.List_ == nil { + return nil, nil + } + return s.List_(opt) +} + +func (s *MockCharacteristicTypesService) Update(id int64, characteristic_type *CharacteristicType) (bool, error) { + if s.Update_ == nil { + return false, nil + } + return s.Update_(id, characteristic_type) +} + +func (s *MockCharacteristicTypesService) Delete(id int64) (bool, error) { + if s.Delete_ == nil { + return false, nil + } + return s.Delete_(id) +} diff --git a/models/characteristic_types_test.go b/models/characteristic_types_test.go new file mode 100644 index 0000000..2bd382d --- /dev/null +++ b/models/characteristic_types_test.go @@ -0,0 +1,174 @@ +package models + +import ( + "net/http" + "reflect" + "testing" + + "github.com/thermokarst/bactdb/router" +) + +func newCharacteristicType() *CharacteristicType { + characteristic_type := NewCharacteristicType() + characteristic_type.Id = 1 + return characteristic_type +} + +func TestCharacteristicTypeService_Get(t *testing.T) { + setup() + defer teardown() + + want := newCharacteristicType() + + var called bool + mux.HandleFunc(urlPath(t, router.CharacteristicType, map[string]string{"Id": "1"}), func(w http.ResponseWriter, r *http.Request) { + called = true + testMethod(t, r, "GET") + + writeJSON(w, want) + }) + + characteristic_type, err := client.CharacteristicTypes.Get(want.Id) + if err != nil { + t.Errorf("CharacteristicTypes.Get returned error: %v", err) + } + + if !called { + t.Fatal("!called") + } + + normalizeTime(&want.CreatedAt, &want.UpdatedAt, &want.DeletedAt) + + if !reflect.DeepEqual(characteristic_type, want) { + t.Errorf("CharacteristicTypes.Get return %+v, want %+v", characteristic_type, want) + } +} + +func TestCharacteristicTypeService_Create(t *testing.T) { + setup() + defer teardown() + + want := newCharacteristicType() + + var called bool + mux.HandleFunc(urlPath(t, router.CreateCharacteristicType, nil), func(w http.ResponseWriter, r *http.Request) { + called = true + testMethod(t, r, "POST") + testBody(t, r, `{"id":1,"characteristicTypeName":"Test Obs Type","createdAt":"0001-01-01T00:00:00Z","updatedAt":"0001-01-01T00:00:00Z","deletedAt":null}`+"\n") + + w.WriteHeader(http.StatusCreated) + writeJSON(w, want) + }) + + characteristic_type := newCharacteristicType() + created, err := client.CharacteristicTypes.Create(characteristic_type) + if err != nil { + t.Errorf("CharacteristicTypes.Create returned error: %v", err) + } + + if !created { + t.Error("!created") + } + + if !called { + t.Fatal("!called") + } + + normalizeTime(&want.CreatedAt, &want.UpdatedAt, &want.DeletedAt) + if !reflect.DeepEqual(characteristic_type, want) { + t.Errorf("CharacteristicTypes.Create returned %+v, want %+v", characteristic_type, want) + } +} + +func TestCharacteristicTypeService_List(t *testing.T) { + setup() + defer teardown() + + want := []*CharacteristicType{newCharacteristicType()} + + var called bool + mux.HandleFunc(urlPath(t, router.CharacteristicTypes, nil), func(w http.ResponseWriter, r *http.Request) { + called = true + testMethod(t, r, "GET") + testFormValues(t, r, values{}) + + writeJSON(w, want) + }) + + characteristic_types, err := client.CharacteristicTypes.List(nil) + if err != nil { + t.Errorf("CharacteristicTypes.List returned error: %v", err) + } + + if !called { + t.Fatal("!called") + } + + for _, u := range want { + normalizeTime(&u.CreatedAt, &u.UpdatedAt, &u.DeletedAt) + } + + if !reflect.DeepEqual(characteristic_types, want) { + t.Errorf("CharacteristicTypes.List return %+v, want %+v", characteristic_types, want) + } +} + +func TestCharacteristicTypeService_Update(t *testing.T) { + setup() + defer teardown() + + want := newCharacteristicType() + + var called bool + mux.HandleFunc(urlPath(t, router.UpdateCharacteristicType, map[string]string{"Id": "1"}), func(w http.ResponseWriter, r *http.Request) { + called = true + testMethod(t, r, "PUT") + testBody(t, r, `{"id":1,"characteristicTypeName":"Test Obs Type Updated","createdAt":"0001-01-01T00:00:00Z","updatedAt":"0001-01-01T00:00:00Z","deletedAt":null}`+"\n") + w.WriteHeader(http.StatusOK) + writeJSON(w, want) + }) + + characteristic_type := newCharacteristicType() + characteristic_type.CharacteristicTypeName = "Test Obs Type Updated" + updated, err := client.CharacteristicTypes.Update(characteristic_type.Id, characteristic_type) + if err != nil { + t.Errorf("CharacteristicTypes.Update returned error: %v", err) + } + + if !updated { + t.Error("!updated") + } + + if !called { + t.Fatal("!called") + } +} + +func TestCharacteristicTypeService_Delete(t *testing.T) { + setup() + defer teardown() + + want := newCharacteristicType() + + var called bool + mux.HandleFunc(urlPath(t, router.DeleteCharacteristicType, map[string]string{"Id": "1"}), func(w http.ResponseWriter, r *http.Request) { + called = true + testMethod(t, r, "DELETE") + + w.WriteHeader(http.StatusOK) + writeJSON(w, want) + }) + + deleted, err := client.CharacteristicTypes.Delete(want.Id) + if err != nil { + t.Errorf("CharacteristicTypes.Delete returned error: %v", err) + } + + if !deleted { + t.Error("!deleted") + } + + if !called { + t.Fatal("!called") + } +} diff --git a/models/client.go b/models/client.go index 6feaeaa..cd69525 100644 --- a/models/client.go +++ b/models/client.go @@ -20,7 +20,7 @@ type Client struct { Genera GeneraService Species SpeciesService Strains StrainsService - ObservationTypes ObservationTypesService + CharacteristicTypes CharacteristicTypesService Observations ObservationsService TextMeasurementTypes TextMeasurementTypesService UnitTypes UnitTypesService @@ -56,7 +56,7 @@ func NewClient(httpClient *http.Client) *Client { c.Genera = &generaService{c} c.Species = &speciesService{c} c.Strains = &strainsService{c} - c.ObservationTypes = &observationTypesService{c} + c.CharacteristicTypes = &characteristicTypesService{c} c.Observations = &observationsService{c} c.TextMeasurementTypes = &textMeasurementTypesService{c} c.UnitTypes = &unitTypesService{c} diff --git a/models/observation_types.go b/models/observation_types.go deleted file mode 100644 index 20aaa3b..0000000 --- a/models/observation_types.go +++ /dev/null @@ -1,203 +0,0 @@ -package models - -import ( - "errors" - "net/http" - "strconv" - "time" - - "github.com/thermokarst/bactdb/router" -) - -// An Observation Type is a lookup type -type ObservationType struct { - Id int64 `json:"id,omitempty"` - ObservationTypeName string `db:"observation_type_name" json:"observationTypeName"` - CreatedAt time.Time `db:"created_at" json:"createdAt"` - UpdatedAt time.Time `db:"updated_at" json:"updatedAt"` - DeletedAt NullTime `db:"deleted_at" json:"deletedAt"` -} - -func NewObservationType() *ObservationType { - return &ObservationType{ - ObservationTypeName: "Test Obs Type", - } -} - -type ObservationTypesService interface { - // Get an observation type - Get(id int64) (*ObservationType, error) - - // List all observation types - List(opt *ObservationTypeListOptions) ([]*ObservationType, error) - - // Create an observation type record - Create(observation_type *ObservationType) (bool, error) - - // Update an existing observation type - Update(id int64, observation_type *ObservationType) (updated bool, err error) - - // Delete an existing observation type - Delete(id int64) (deleted bool, err error) -} - -var ( - ErrObservationTypeNotFound = errors.New("observation type not found") -) - -type observationTypesService struct { - client *Client -} - -func (s *observationTypesService) Get(id int64) (*ObservationType, error) { - strId := strconv.FormatInt(id, 10) - - url, err := s.client.url(router.ObservationType, map[string]string{"Id": strId}, nil) - if err != nil { - return nil, err - } - - req, err := s.client.NewRequest("GET", url.String(), nil) - if err != nil { - return nil, err - } - - var observation_type *ObservationType - _, err = s.client.Do(req, &observation_type) - if err != nil { - return nil, err - } - - return observation_type, nil -} - -func (s *observationTypesService) Create(observation_type *ObservationType) (bool, error) { - url, err := s.client.url(router.CreateObservationType, nil, nil) - if err != nil { - return false, err - } - - req, err := s.client.NewRequest("POST", url.String(), observation_type) - if err != nil { - return false, err - } - - resp, err := s.client.Do(req, &observation_type) - if err != nil { - return false, err - } - - return resp.StatusCode == http.StatusCreated, nil -} - -type ObservationTypeListOptions struct { - ListOptions -} - -func (s *observationTypesService) List(opt *ObservationTypeListOptions) ([]*ObservationType, error) { - url, err := s.client.url(router.ObservationTypes, nil, opt) - if err != nil { - return nil, err - } - - req, err := s.client.NewRequest("GET", url.String(), nil) - if err != nil { - return nil, err - } - - var observation_types []*ObservationType - _, err = s.client.Do(req, &observation_types) - if err != nil { - return nil, err - } - - return observation_types, nil -} - -func (s *observationTypesService) Update(id int64, observation_type *ObservationType) (bool, error) { - strId := strconv.FormatInt(id, 10) - - url, err := s.client.url(router.UpdateObservationType, map[string]string{"Id": strId}, nil) - if err != nil { - return false, err - } - - req, err := s.client.NewRequest("PUT", url.String(), observation_type) - if err != nil { - return false, err - } - - resp, err := s.client.Do(req, &observation_type) - if err != nil { - return false, err - } - - return resp.StatusCode == http.StatusOK, nil -} - -func (s *observationTypesService) Delete(id int64) (bool, error) { - strId := strconv.FormatInt(id, 10) - - url, err := s.client.url(router.DeleteObservationType, map[string]string{"Id": strId}, nil) - if err != nil { - return false, err - } - - req, err := s.client.NewRequest("DELETE", url.String(), nil) - if err != nil { - return false, err - } - - var observation_type *ObservationType - resp, err := s.client.Do(req, &observation_type) - if err != nil { - return false, err - } - - return resp.StatusCode == http.StatusOK, nil -} - -type MockObservationTypesService struct { - Get_ func(id int64) (*ObservationType, error) - List_ func(opt *ObservationTypeListOptions) ([]*ObservationType, error) - Create_ func(observation_type *ObservationType) (bool, error) - Update_ func(id int64, observation_type *ObservationType) (bool, error) - Delete_ func(id int64) (bool, error) -} - -var _ ObservationTypesService = &MockObservationTypesService{} - -func (s *MockObservationTypesService) Get(id int64) (*ObservationType, error) { - if s.Get_ == nil { - return nil, nil - } - return s.Get_(id) -} - -func (s *MockObservationTypesService) Create(observation_type *ObservationType) (bool, error) { - if s.Create_ == nil { - return false, nil - } - return s.Create_(observation_type) -} - -func (s *MockObservationTypesService) List(opt *ObservationTypeListOptions) ([]*ObservationType, error) { - if s.List_ == nil { - return nil, nil - } - return s.List_(opt) -} - -func (s *MockObservationTypesService) Update(id int64, observation_type *ObservationType) (bool, error) { - if s.Update_ == nil { - return false, nil - } - return s.Update_(id, observation_type) -} - -func (s *MockObservationTypesService) Delete(id int64) (bool, error) { - if s.Delete_ == nil { - return false, nil - } - return s.Delete_(id) -} diff --git a/models/observation_types_test.go b/models/observation_types_test.go deleted file mode 100644 index 74fcbad..0000000 --- a/models/observation_types_test.go +++ /dev/null @@ -1,174 +0,0 @@ -package models - -import ( - "net/http" - "reflect" - "testing" - - "github.com/thermokarst/bactdb/router" -) - -func newObservationType() *ObservationType { - observation_type := NewObservationType() - observation_type.Id = 1 - return observation_type -} - -func TestObservationTypeService_Get(t *testing.T) { - setup() - defer teardown() - - want := newObservationType() - - var called bool - mux.HandleFunc(urlPath(t, router.ObservationType, map[string]string{"Id": "1"}), func(w http.ResponseWriter, r *http.Request) { - called = true - testMethod(t, r, "GET") - - writeJSON(w, want) - }) - - observation_type, err := client.ObservationTypes.Get(want.Id) - if err != nil { - t.Errorf("ObservationTypes.Get returned error: %v", err) - } - - if !called { - t.Fatal("!called") - } - - normalizeTime(&want.CreatedAt, &want.UpdatedAt, &want.DeletedAt) - - if !reflect.DeepEqual(observation_type, want) { - t.Errorf("ObservationTypes.Get return %+v, want %+v", observation_type, want) - } -} - -func TestObservationTypeService_Create(t *testing.T) { - setup() - defer teardown() - - want := newObservationType() - - var called bool - mux.HandleFunc(urlPath(t, router.CreateObservationType, nil), func(w http.ResponseWriter, r *http.Request) { - called = true - testMethod(t, r, "POST") - testBody(t, r, `{"id":1,"observationTypeName":"Test Obs Type","createdAt":"0001-01-01T00:00:00Z","updatedAt":"0001-01-01T00:00:00Z","deletedAt":null}`+"\n") - - w.WriteHeader(http.StatusCreated) - writeJSON(w, want) - }) - - observation_type := newObservationType() - created, err := client.ObservationTypes.Create(observation_type) - if err != nil { - t.Errorf("ObservationTypes.Create returned error: %v", err) - } - - if !created { - t.Error("!created") - } - - if !called { - t.Fatal("!called") - } - - normalizeTime(&want.CreatedAt, &want.UpdatedAt, &want.DeletedAt) - if !reflect.DeepEqual(observation_type, want) { - t.Errorf("ObservationTypes.Create returned %+v, want %+v", observation_type, want) - } -} - -func TestObservationTypeService_List(t *testing.T) { - setup() - defer teardown() - - want := []*ObservationType{newObservationType()} - - var called bool - mux.HandleFunc(urlPath(t, router.ObservationTypes, nil), func(w http.ResponseWriter, r *http.Request) { - called = true - testMethod(t, r, "GET") - testFormValues(t, r, values{}) - - writeJSON(w, want) - }) - - observation_types, err := client.ObservationTypes.List(nil) - if err != nil { - t.Errorf("ObservationTypes.List returned error: %v", err) - } - - if !called { - t.Fatal("!called") - } - - for _, u := range want { - normalizeTime(&u.CreatedAt, &u.UpdatedAt, &u.DeletedAt) - } - - if !reflect.DeepEqual(observation_types, want) { - t.Errorf("ObservationTypes.List return %+v, want %+v", observation_types, want) - } -} - -func TestObservationTypeService_Update(t *testing.T) { - setup() - defer teardown() - - want := newObservationType() - - var called bool - mux.HandleFunc(urlPath(t, router.UpdateObservationType, map[string]string{"Id": "1"}), func(w http.ResponseWriter, r *http.Request) { - called = true - testMethod(t, r, "PUT") - testBody(t, r, `{"id":1,"observationTypeName":"Test Obs Type Updated","createdAt":"0001-01-01T00:00:00Z","updatedAt":"0001-01-01T00:00:00Z","deletedAt":null}`+"\n") - w.WriteHeader(http.StatusOK) - writeJSON(w, want) - }) - - observation_type := newObservationType() - observation_type.ObservationTypeName = "Test Obs Type Updated" - updated, err := client.ObservationTypes.Update(observation_type.Id, observation_type) - if err != nil { - t.Errorf("ObservationTypes.Update returned error: %v", err) - } - - if !updated { - t.Error("!updated") - } - - if !called { - t.Fatal("!called") - } -} - -func TestObservationTypeService_Delete(t *testing.T) { - setup() - defer teardown() - - want := newObservationType() - - var called bool - mux.HandleFunc(urlPath(t, router.DeleteObservationType, map[string]string{"Id": "1"}), func(w http.ResponseWriter, r *http.Request) { - called = true - testMethod(t, r, "DELETE") - - w.WriteHeader(http.StatusOK) - writeJSON(w, want) - }) - - deleted, err := client.ObservationTypes.Delete(want.Id) - if err != nil { - t.Errorf("ObservationTypes.Delete returned error: %v", err) - } - - if !deleted { - t.Error("!deleted") - } - - if !called { - t.Fatal("!called") - } -} diff --git a/models/observations.go b/models/observations.go index e11dd2c..890bdca 100644 --- a/models/observations.go +++ b/models/observations.go @@ -13,7 +13,7 @@ import ( type Observation struct { Id int64 `json:"id,omitempty"` ObservationName string `db:"observation_name" json:"observationName"` - ObservationTypeId int64 `db:"observation_type_id" json:"observationTypeId"` + CharacteristicTypeId int64 `db:"characteristic_type_id" json:"characteristicTypeId"` CreatedAt time.Time `db:"created_at" json:"createdAt"` UpdatedAt time.Time `db:"updated_at" json:"updatedAt"` DeletedAt NullTime `db:"deleted_at" json:"deletedAt"` diff --git a/models/observations_test.go b/models/observations_test.go index 5fa88c6..fde4311 100644 --- a/models/observations_test.go +++ b/models/observations_test.go @@ -54,7 +54,7 @@ func TestObservationService_Create(t *testing.T) { mux.HandleFunc(urlPath(t, router.CreateObservation, nil), func(w http.ResponseWriter, r *http.Request) { called = true testMethod(t, r, "POST") - testBody(t, r, `{"id":1,"observationName":"Test Observation","observationTypeId":0,"createdAt":"0001-01-01T00:00:00Z","updatedAt":"0001-01-01T00:00:00Z","deletedAt":null}`+"\n") + testBody(t, r, `{"id":1,"observationName":"Test Observation","characteristicTypeId":0,"createdAt":"0001-01-01T00:00:00Z","updatedAt":"0001-01-01T00:00:00Z","deletedAt":null}`+"\n") w.WriteHeader(http.StatusCreated) writeJSON(w, want) @@ -123,7 +123,7 @@ func TestObservationService_Update(t *testing.T) { mux.HandleFunc(urlPath(t, router.UpdateObservation, map[string]string{"Id": "1"}), func(w http.ResponseWriter, r *http.Request) { called = true testMethod(t, r, "PUT") - testBody(t, r, `{"id":1,"observationName":"Test Obs Updated","observationTypeId":0,"createdAt":"0001-01-01T00:00:00Z","updatedAt":"0001-01-01T00:00:00Z","deletedAt":null}`+"\n") + testBody(t, r, `{"id":1,"observationName":"Test Obs Updated","characteristicTypeId":0,"createdAt":"0001-01-01T00:00:00Z","updatedAt":"0001-01-01T00:00:00Z","deletedAt":null}`+"\n") w.WriteHeader(http.StatusOK) writeJSON(w, want) }) diff --git a/router/api.go b/router/api.go index cbf96cb..f9e414b 100644 --- a/router/api.go +++ b/router/api.go @@ -31,12 +31,12 @@ func API() *mux.Router { m.Path("/strains/{Id:.+}").Methods("PUT").Name(UpdateStrain) m.Path("/strains/{Id:.+}").Methods("DELETE").Name(DeleteStrain) - // ObservationTypes - m.Path("/observation_types").Methods("GET").Name(ObservationTypes) - m.Path("/observation_types").Methods("POST").Name(CreateObservationType) - m.Path("/observation_types/{Id:.+}").Methods("GET").Name(ObservationType) - m.Path("/observation_types/{Id:.+}").Methods("PUT").Name(UpdateObservationType) - m.Path("/observation_types/{Id:.+}").Methods("DELETE").Name(DeleteObservationType) + // CharacteristicTypes + m.Path("/characteristic_types").Methods("GET").Name(CharacteristicTypes) + m.Path("/characteristic_types").Methods("POST").Name(CreateCharacteristicType) + m.Path("/characteristic_types/{Id:.+}").Methods("GET").Name(CharacteristicType) + m.Path("/characteristic_types/{Id:.+}").Methods("PUT").Name(UpdateCharacteristicType) + m.Path("/characteristic_types/{Id:.+}").Methods("DELETE").Name(DeleteCharacteristicType) // Observations m.Path("/observations").Methods("GET").Name(Observations) diff --git a/router/routes.go b/router/routes.go index f93fc86..1b4b81f 100644 --- a/router/routes.go +++ b/router/routes.go @@ -23,11 +23,11 @@ const ( UpdateStrain = "strain:update" DeleteStrain = "strain:delete" - ObservationType = "observation_type:get" - CreateObservationType = "observation_type:create" - ObservationTypes = "observation_type:list" - UpdateObservationType = "observation_type:update" - DeleteObservationType = "observation_type:delete" + CharacteristicType = "characteristic_type:get" + CreateCharacteristicType = "characteristic_type:create" + CharacteristicTypes = "characteristic_type:list" + UpdateCharacteristicType = "characteristic_type:update" + DeleteCharacteristicType = "characteristic_type:delete" Observation = "observation:get" CreateObservation = "observation:create"