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,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{})
}

View file

@ -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")
}
}

View file

@ -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))

View file

@ -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{})
}

View file

@ -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")
}
}