Renaming observations to characteristics
This commit is contained in:
parent
c1323f9c1f
commit
950b15a117
27 changed files with 889 additions and 889 deletions
92
api/characteristics.go
Normal file
92
api/characteristics.go
Normal file
|
@ -0,0 +1,92 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/thermokarst/bactdb/models"
|
||||
)
|
||||
|
||||
func serveCharacteristic(w http.ResponseWriter, r *http.Request) error {
|
||||
id, err := strconv.ParseInt(mux.Vars(r)["Id"], 10, 0)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
characteristic, err := store.Characteristics.Get(id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return writeJSON(w, characteristic)
|
||||
}
|
||||
|
||||
func serveCreateCharacteristic(w http.ResponseWriter, r *http.Request) error {
|
||||
var characteristic models.Characteristic
|
||||
err := json.NewDecoder(r.Body).Decode(&characteristic)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
created, err := store.Characteristics.Create(&characteristic)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if created {
|
||||
w.WriteHeader(http.StatusCreated)
|
||||
}
|
||||
|
||||
return writeJSON(w, characteristic)
|
||||
}
|
||||
|
||||
func serveCharacteristicList(w http.ResponseWriter, r *http.Request) error {
|
||||
var opt models.CharacteristicListOptions
|
||||
if err := schemaDecoder.Decode(&opt, r.URL.Query()); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
characteristics, err := store.Characteristics.List(&opt)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if characteristics == nil {
|
||||
characteristics = []*models.Characteristic{}
|
||||
}
|
||||
|
||||
return writeJSON(w, characteristics)
|
||||
}
|
||||
|
||||
func serveUpdateCharacteristic(w http.ResponseWriter, r *http.Request) error {
|
||||
id, _ := strconv.ParseInt(mux.Vars(r)["Id"], 10, 0)
|
||||
var characteristic models.Characteristic
|
||||
err := json.NewDecoder(r.Body).Decode(&characteristic)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
updated, err := store.Characteristics.Update(id, &characteristic)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if updated {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
|
||||
return writeJSON(w, characteristic)
|
||||
}
|
||||
|
||||
func serveDeleteCharacteristic(w http.ResponseWriter, r *http.Request) error {
|
||||
id, _ := strconv.ParseInt(mux.Vars(r)["Id"], 10, 0)
|
||||
|
||||
deleted, err := store.Characteristics.Delete(id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if deleted {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
|
||||
return writeJSON(w, &models.Characteristic{})
|
||||
}
|
153
api/characteristics_test.go
Normal file
153
api/characteristics_test.go
Normal file
|
@ -0,0 +1,153 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/thermokarst/bactdb/models"
|
||||
)
|
||||
|
||||
func newCharacteristic() *models.Characteristic {
|
||||
characteristic := models.NewCharacteristic()
|
||||
return characteristic
|
||||
}
|
||||
|
||||
func TestCharacteristic_Get(t *testing.T) {
|
||||
setup()
|
||||
|
||||
want := newCharacteristic()
|
||||
|
||||
calledGet := false
|
||||
|
||||
store.Characteristics.(*models.MockCharacteristicsService).Get_ = func(id int64) (*models.Characteristic, error) {
|
||||
if id != want.Id {
|
||||
t.Errorf("wanted request for characteristic %d but got %d", want.Id, id)
|
||||
}
|
||||
calledGet = true
|
||||
return want, nil
|
||||
}
|
||||
|
||||
got, err := apiClient.Characteristics.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 TestCharacteristic_Create(t *testing.T) {
|
||||
setup()
|
||||
|
||||
want := newCharacteristic()
|
||||
|
||||
calledPost := false
|
||||
store.Characteristics.(*models.MockCharacteristicsService).Create_ = func(characteristic *models.Characteristic) (bool, error) {
|
||||
if !normalizeDeepEqual(want, characteristic) {
|
||||
t.Errorf("wanted request for characteristic %d but got %d", want, characteristic)
|
||||
}
|
||||
calledPost = true
|
||||
return true, nil
|
||||
}
|
||||
|
||||
success, err := apiClient.Characteristics.Create(want)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if !calledPost {
|
||||
t.Error("!calledPost")
|
||||
}
|
||||
if !success {
|
||||
t.Error("!success")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCharacteristic_List(t *testing.T) {
|
||||
setup()
|
||||
|
||||
want := []*models.Characteristic{newCharacteristic()}
|
||||
wantOpt := &models.CharacteristicListOptions{ListOptions: models.ListOptions{Page: 1, PerPage: 10}}
|
||||
|
||||
calledList := false
|
||||
store.Characteristics.(*models.MockCharacteristicsService).List_ = func(opt *models.CharacteristicListOptions) ([]*models.Characteristic, error) {
|
||||
if !normalizeDeepEqual(wantOpt, opt) {
|
||||
t.Errorf("wanted options %d but got %d", wantOpt, opt)
|
||||
}
|
||||
calledList = true
|
||||
return want, nil
|
||||
}
|
||||
|
||||
characteristics, err := apiClient.Characteristics.List(wantOpt)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if !calledList {
|
||||
t.Error("!calledList")
|
||||
}
|
||||
|
||||
if !normalizeDeepEqual(&want, &characteristics) {
|
||||
t.Errorf("got characteristics %+v but wanted characteristics %+v", characteristics, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCharacteristic_Update(t *testing.T) {
|
||||
setup()
|
||||
|
||||
want := newCharacteristic()
|
||||
|
||||
calledPut := false
|
||||
store.Characteristics.(*models.MockCharacteristicsService).Update_ = func(id int64, characteristic *models.Characteristic) (bool, error) {
|
||||
if id != want.Id {
|
||||
t.Errorf("wanted request for characteristic %d but got %d", want.Id, id)
|
||||
}
|
||||
if !normalizeDeepEqual(want, characteristic) {
|
||||
t.Errorf("wanted request for characteristic %d but got %d", want, characteristic)
|
||||
}
|
||||
calledPut = true
|
||||
return true, nil
|
||||
}
|
||||
|
||||
success, err := apiClient.Characteristics.Update(want.Id, want)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if !calledPut {
|
||||
t.Error("!calledPut")
|
||||
}
|
||||
if !success {
|
||||
t.Error("!success")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCharacteristic_Delete(t *testing.T) {
|
||||
setup()
|
||||
|
||||
want := newCharacteristic()
|
||||
|
||||
calledDelete := false
|
||||
store.Characteristics.(*models.MockCharacteristicsService).Delete_ = func(id int64) (bool, error) {
|
||||
if id != want.Id {
|
||||
t.Errorf("wanted request for characteristic %d but got %d", want.Id, id)
|
||||
}
|
||||
calledDelete = true
|
||||
return true, nil
|
||||
}
|
||||
|
||||
success, err := apiClient.Characteristics.Delete(want.Id)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if !calledDelete {
|
||||
t.Error("!calledDelete")
|
||||
}
|
||||
if !success {
|
||||
t.Error("!success")
|
||||
}
|
||||
}
|
|
@ -45,11 +45,11 @@ func Handler() *mux.Router {
|
|||
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))
|
||||
m.Get(router.Observations).Handler(handler(serveObservationList))
|
||||
m.Get(router.UpdateObservation).Handler(handler(serveUpdateObservation))
|
||||
m.Get(router.DeleteObservation).Handler(handler(serveDeleteObservation))
|
||||
m.Get(router.Characteristic).Handler(handler(serveCharacteristic))
|
||||
m.Get(router.CreateCharacteristic).Handler(handler(serveCreateCharacteristic))
|
||||
m.Get(router.Characteristics).Handler(handler(serveCharacteristicList))
|
||||
m.Get(router.UpdateCharacteristic).Handler(handler(serveUpdateCharacteristic))
|
||||
m.Get(router.DeleteCharacteristic).Handler(handler(serveDeleteCharacteristic))
|
||||
|
||||
m.Get(router.TextMeasurementType).Handler(handler(serveTextMeasurementType))
|
||||
m.Get(router.CreateTextMeasurementType).Handler(handler(serveCreateTextMeasurementType))
|
||||
|
|
|
@ -11,7 +11,7 @@ func newMeasurement() *models.Measurement {
|
|||
measurement := models.NewMeasurement()
|
||||
measurement.Id = 1
|
||||
measurement.StrainId = 2
|
||||
measurement.ObservationId = 3
|
||||
measurement.CharacteristicId = 3
|
||||
measurement.TextMeasurementTypeId = models.NullInt64{sql.NullInt64{Int64: 4, Valid: false}}
|
||||
measurement.UnitTypeId = models.NullInt64{sql.NullInt64{Int64: 5, Valid: true}}
|
||||
return measurement
|
||||
|
|
|
@ -1,92 +0,0 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/thermokarst/bactdb/models"
|
||||
)
|
||||
|
||||
func serveObservation(w http.ResponseWriter, r *http.Request) error {
|
||||
id, err := strconv.ParseInt(mux.Vars(r)["Id"], 10, 0)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
observation, err := store.Observations.Get(id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return writeJSON(w, observation)
|
||||
}
|
||||
|
||||
func serveCreateObservation(w http.ResponseWriter, r *http.Request) error {
|
||||
var observation models.Observation
|
||||
err := json.NewDecoder(r.Body).Decode(&observation)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
created, err := store.Observations.Create(&observation)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if created {
|
||||
w.WriteHeader(http.StatusCreated)
|
||||
}
|
||||
|
||||
return writeJSON(w, observation)
|
||||
}
|
||||
|
||||
func serveObservationList(w http.ResponseWriter, r *http.Request) error {
|
||||
var opt models.ObservationListOptions
|
||||
if err := schemaDecoder.Decode(&opt, r.URL.Query()); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
observations, err := store.Observations.List(&opt)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if observations == nil {
|
||||
observations = []*models.Observation{}
|
||||
}
|
||||
|
||||
return writeJSON(w, observations)
|
||||
}
|
||||
|
||||
func serveUpdateObservation(w http.ResponseWriter, r *http.Request) error {
|
||||
id, _ := strconv.ParseInt(mux.Vars(r)["Id"], 10, 0)
|
||||
var observation models.Observation
|
||||
err := json.NewDecoder(r.Body).Decode(&observation)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
updated, err := store.Observations.Update(id, &observation)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if updated {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
|
||||
return writeJSON(w, observation)
|
||||
}
|
||||
|
||||
func serveDeleteObservation(w http.ResponseWriter, r *http.Request) error {
|
||||
id, _ := strconv.ParseInt(mux.Vars(r)["Id"], 10, 0)
|
||||
|
||||
deleted, err := store.Observations.Delete(id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if deleted {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
|
||||
return writeJSON(w, &models.Observation{})
|
||||
}
|
|
@ -1,153 +0,0 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/thermokarst/bactdb/models"
|
||||
)
|
||||
|
||||
func newObservation() *models.Observation {
|
||||
observation := models.NewObservation()
|
||||
return observation
|
||||
}
|
||||
|
||||
func TestObservation_Get(t *testing.T) {
|
||||
setup()
|
||||
|
||||
want := newObservation()
|
||||
|
||||
calledGet := false
|
||||
|
||||
store.Observations.(*models.MockObservationsService).Get_ = func(id int64) (*models.Observation, error) {
|
||||
if id != want.Id {
|
||||
t.Errorf("wanted request for observation %d but got %d", want.Id, id)
|
||||
}
|
||||
calledGet = true
|
||||
return want, nil
|
||||
}
|
||||
|
||||
got, err := apiClient.Observations.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 TestObservation_Create(t *testing.T) {
|
||||
setup()
|
||||
|
||||
want := newObservation()
|
||||
|
||||
calledPost := false
|
||||
store.Observations.(*models.MockObservationsService).Create_ = func(observation *models.Observation) (bool, error) {
|
||||
if !normalizeDeepEqual(want, observation) {
|
||||
t.Errorf("wanted request for observation %d but got %d", want, observation)
|
||||
}
|
||||
calledPost = true
|
||||
return true, nil
|
||||
}
|
||||
|
||||
success, err := apiClient.Observations.Create(want)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if !calledPost {
|
||||
t.Error("!calledPost")
|
||||
}
|
||||
if !success {
|
||||
t.Error("!success")
|
||||
}
|
||||
}
|
||||
|
||||
func TestObservation_List(t *testing.T) {
|
||||
setup()
|
||||
|
||||
want := []*models.Observation{newObservation()}
|
||||
wantOpt := &models.ObservationListOptions{ListOptions: models.ListOptions{Page: 1, PerPage: 10}}
|
||||
|
||||
calledList := false
|
||||
store.Observations.(*models.MockObservationsService).List_ = func(opt *models.ObservationListOptions) ([]*models.Observation, error) {
|
||||
if !normalizeDeepEqual(wantOpt, opt) {
|
||||
t.Errorf("wanted options %d but got %d", wantOpt, opt)
|
||||
}
|
||||
calledList = true
|
||||
return want, nil
|
||||
}
|
||||
|
||||
observations, err := apiClient.Observations.List(wantOpt)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if !calledList {
|
||||
t.Error("!calledList")
|
||||
}
|
||||
|
||||
if !normalizeDeepEqual(&want, &observations) {
|
||||
t.Errorf("got observations %+v but wanted observations %+v", observations, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestObservation_Update(t *testing.T) {
|
||||
setup()
|
||||
|
||||
want := newObservation()
|
||||
|
||||
calledPut := false
|
||||
store.Observations.(*models.MockObservationsService).Update_ = func(id int64, observation *models.Observation) (bool, error) {
|
||||
if id != want.Id {
|
||||
t.Errorf("wanted request for observation %d but got %d", want.Id, id)
|
||||
}
|
||||
if !normalizeDeepEqual(want, observation) {
|
||||
t.Errorf("wanted request for observation %d but got %d", want, observation)
|
||||
}
|
||||
calledPut = true
|
||||
return true, nil
|
||||
}
|
||||
|
||||
success, err := apiClient.Observations.Update(want.Id, want)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if !calledPut {
|
||||
t.Error("!calledPut")
|
||||
}
|
||||
if !success {
|
||||
t.Error("!success")
|
||||
}
|
||||
}
|
||||
|
||||
func TestObservation_Delete(t *testing.T) {
|
||||
setup()
|
||||
|
||||
want := newObservation()
|
||||
|
||||
calledDelete := false
|
||||
store.Observations.(*models.MockObservationsService).Delete_ = func(id int64) (bool, error) {
|
||||
if id != want.Id {
|
||||
t.Errorf("wanted request for observation %d but got %d", want.Id, id)
|
||||
}
|
||||
calledDelete = true
|
||||
return true, nil
|
||||
}
|
||||
|
||||
success, err := apiClient.Observations.Delete(want.Id)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if !calledDelete {
|
||||
t.Error("!calledDelete")
|
||||
}
|
||||
if !success {
|
||||
t.Error("!success")
|
||||
}
|
||||
}
|
Reference in a new issue