Renaming observation types to characteristic types
This commit is contained in:
parent
be9e6481d0
commit
c1323f9c1f
24 changed files with 873 additions and 873 deletions
92
api/characteristic_types.go
Normal file
92
api/characteristic_types.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 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{})
|
||||||
|
}
|
153
api/characteristic_types_test.go
Normal file
153
api/characteristic_types_test.go
Normal 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")
|
||||||
|
}
|
||||||
|
}
|
|
@ -39,11 +39,11 @@ func Handler() *mux.Router {
|
||||||
m.Get(router.UpdateStrain).Handler(handler(serveUpdateStrain))
|
m.Get(router.UpdateStrain).Handler(handler(serveUpdateStrain))
|
||||||
m.Get(router.DeleteStrain).Handler(handler(serveDeleteStrain))
|
m.Get(router.DeleteStrain).Handler(handler(serveDeleteStrain))
|
||||||
|
|
||||||
m.Get(router.ObservationType).Handler(handler(serveObservationType))
|
m.Get(router.CharacteristicType).Handler(handler(serveCharacteristicType))
|
||||||
m.Get(router.CreateObservationType).Handler(handler(serveCreateObservationType))
|
m.Get(router.CreateCharacteristicType).Handler(handler(serveCreateCharacteristicType))
|
||||||
m.Get(router.ObservationTypes).Handler(handler(serveObservationTypeList))
|
m.Get(router.CharacteristicTypes).Handler(handler(serveCharacteristicTypeList))
|
||||||
m.Get(router.UpdateObservationType).Handler(handler(serveUpdateObservationType))
|
m.Get(router.UpdateCharacteristicType).Handler(handler(serveUpdateCharacteristicType))
|
||||||
m.Get(router.DeleteObservationType).Handler(handler(serveDeleteObservationType))
|
m.Get(router.DeleteCharacteristicType).Handler(handler(serveDeleteCharacteristicType))
|
||||||
|
|
||||||
m.Get(router.Observation).Handler(handler(serveObservation))
|
m.Get(router.Observation).Handler(handler(serveObservation))
|
||||||
m.Get(router.CreateObservation).Handler(handler(serveCreateObservation))
|
m.Get(router.CreateObservation).Handler(handler(serveCreateObservation))
|
||||||
|
|
|
@ -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{})
|
|
||||||
}
|
|
|
@ -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")
|
|
||||||
}
|
|
||||||
}
|
|
87
datastore/characteristic_types.go
Normal file
87
datastore/characteristic_types.go
Normal 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
|
||||||
|
}
|
126
datastore/characteristic_types_test.go
Normal file
126
datastore/characteristic_types_test.go
Normal 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")
|
||||||
|
}
|
||||||
|
}
|
|
@ -13,7 +13,7 @@ type Datastore struct {
|
||||||
Genera models.GeneraService
|
Genera models.GeneraService
|
||||||
Species models.SpeciesService
|
Species models.SpeciesService
|
||||||
Strains models.StrainsService
|
Strains models.StrainsService
|
||||||
ObservationTypes models.ObservationTypesService
|
CharacteristicTypes models.CharacteristicTypesService
|
||||||
Observations models.ObservationsService
|
Observations models.ObservationsService
|
||||||
TextMeasurementTypes models.TextMeasurementTypesService
|
TextMeasurementTypes models.TextMeasurementTypesService
|
||||||
UnitTypes models.UnitTypesService
|
UnitTypes models.UnitTypesService
|
||||||
|
@ -38,7 +38,7 @@ func NewDatastore(dbh modl.SqlExecutor) *Datastore {
|
||||||
d.Genera = &generaStore{d}
|
d.Genera = &generaStore{d}
|
||||||
d.Species = &speciesStore{d}
|
d.Species = &speciesStore{d}
|
||||||
d.Strains = &strainsStore{d}
|
d.Strains = &strainsStore{d}
|
||||||
d.ObservationTypes = &observationTypesStore{d}
|
d.CharacteristicTypes = &characteristicTypesStore{d}
|
||||||
d.Observations = &observationsStore{d}
|
d.Observations = &observationsStore{d}
|
||||||
d.TextMeasurementTypes = &textMeasurementTypesStore{d}
|
d.TextMeasurementTypes = &textMeasurementTypesStore{d}
|
||||||
d.UnitTypes = &unitTypesStore{d}
|
d.UnitTypes = &unitTypesStore{d}
|
||||||
|
@ -52,7 +52,7 @@ func NewMockDatastore() *Datastore {
|
||||||
Genera: &models.MockGeneraService{},
|
Genera: &models.MockGeneraService{},
|
||||||
Species: &models.MockSpeciesService{},
|
Species: &models.MockSpeciesService{},
|
||||||
Strains: &models.MockStrainsService{},
|
Strains: &models.MockStrainsService{},
|
||||||
ObservationTypes: &models.MockObservationTypesService{},
|
CharacteristicTypes: &models.MockCharacteristicTypesService{},
|
||||||
Observations: &models.MockObservationsService{},
|
Observations: &models.MockObservationsService{},
|
||||||
TextMeasurementTypes: &models.MockTextMeasurementTypesService{},
|
TextMeasurementTypes: &models.MockTextMeasurementTypesService{},
|
||||||
UnitTypes: &models.MockUnitTypesService{},
|
UnitTypes: &models.MockUnitTypesService{},
|
||||||
|
|
|
@ -0,0 +1,5 @@
|
||||||
|
-- bactdb
|
||||||
|
-- Matthew R Dillon
|
||||||
|
|
||||||
|
DROP TABLE characteristic_types;
|
||||||
|
|
|
@ -1,14 +1,14 @@
|
||||||
-- bactdb
|
-- bactdb
|
||||||
-- Matthew R Dillon
|
-- Matthew R Dillon
|
||||||
|
|
||||||
CREATE TABLE observation_types (
|
CREATE TABLE characteristic_types (
|
||||||
id BIGSERIAL NOT NULL,
|
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,
|
created_at TIMESTAMP WITH TIME ZONE NOT NULL,
|
||||||
updated_at TIMESTAMP WITH TIME ZONE NOT NULL,
|
updated_at TIMESTAMP WITH TIME ZONE NOT NULL,
|
||||||
deleted_at TIMESTAMP WITH TIME ZONE NULL,
|
deleted_at TIMESTAMP WITH TIME ZONE NULL,
|
||||||
|
|
||||||
CONSTRAINT observation_types_pkey PRIMARY KEY (id)
|
CONSTRAINT characteristic_types_pkey PRIMARY KEY (id)
|
||||||
);
|
);
|
||||||
|
|
|
@ -1,5 +0,0 @@
|
||||||
-- bactdb
|
|
||||||
-- Matthew R Dillon
|
|
||||||
|
|
||||||
DROP TABLE observation_types;
|
|
||||||
|
|
|
@ -4,15 +4,15 @@
|
||||||
CREATE TABLE observations (
|
CREATE TABLE observations (
|
||||||
id BIGSERIAL NOT NULL,
|
id BIGSERIAL NOT NULL,
|
||||||
observation_name CHARACTER VARYING(100) 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,
|
created_at TIMESTAMP WITH TIME ZONE NOT NULL,
|
||||||
updated_at TIMESTAMP WITH TIME ZONE NOT NULL,
|
updated_at TIMESTAMP WITH TIME ZONE NOT NULL,
|
||||||
deleted_at TIMESTAMP WITH TIME ZONE NULL,
|
deleted_at TIMESTAMP WITH TIME ZONE NULL,
|
||||||
|
|
||||||
CONSTRAINT observations_pkey PRIMARY KEY (id),
|
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);
|
||||||
|
|
||||||
|
|
|
@ -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
|
|
||||||
}
|
|
|
@ -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")
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -19,10 +19,10 @@ func insertObservation(t *testing.T, tx *modl.Transaction) *models.Observation {
|
||||||
}
|
}
|
||||||
|
|
||||||
func newObservation(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.
|
// we want to create and insert an characteristic type record, too.
|
||||||
observation_type := insertObservationType(t, tx)
|
characteristic_type := insertCharacteristicType(t, tx)
|
||||||
return &models.Observation{ObservationName: "Test Observation",
|
return &models.Observation{ObservationName: "Test Observation",
|
||||||
ObservationTypeId: observation_type.Id}
|
CharacteristicTypeId: characteristic_type.Id}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestObservationsStore_Get_db(t *testing.T) {
|
func TestObservationsStore_Get_db(t *testing.T) {
|
||||||
|
|
203
models/characteristic_types.go
Normal file
203
models/characteristic_types.go
Normal file
|
@ -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)
|
||||||
|
}
|
174
models/characteristic_types_test.go
Normal file
174
models/characteristic_types_test.go
Normal file
|
@ -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")
|
||||||
|
}
|
||||||
|
}
|
|
@ -20,7 +20,7 @@ type Client struct {
|
||||||
Genera GeneraService
|
Genera GeneraService
|
||||||
Species SpeciesService
|
Species SpeciesService
|
||||||
Strains StrainsService
|
Strains StrainsService
|
||||||
ObservationTypes ObservationTypesService
|
CharacteristicTypes CharacteristicTypesService
|
||||||
Observations ObservationsService
|
Observations ObservationsService
|
||||||
TextMeasurementTypes TextMeasurementTypesService
|
TextMeasurementTypes TextMeasurementTypesService
|
||||||
UnitTypes UnitTypesService
|
UnitTypes UnitTypesService
|
||||||
|
@ -56,7 +56,7 @@ func NewClient(httpClient *http.Client) *Client {
|
||||||
c.Genera = &generaService{c}
|
c.Genera = &generaService{c}
|
||||||
c.Species = &speciesService{c}
|
c.Species = &speciesService{c}
|
||||||
c.Strains = &strainsService{c}
|
c.Strains = &strainsService{c}
|
||||||
c.ObservationTypes = &observationTypesService{c}
|
c.CharacteristicTypes = &characteristicTypesService{c}
|
||||||
c.Observations = &observationsService{c}
|
c.Observations = &observationsService{c}
|
||||||
c.TextMeasurementTypes = &textMeasurementTypesService{c}
|
c.TextMeasurementTypes = &textMeasurementTypesService{c}
|
||||||
c.UnitTypes = &unitTypesService{c}
|
c.UnitTypes = &unitTypesService{c}
|
||||||
|
|
|
@ -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)
|
|
||||||
}
|
|
|
@ -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")
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -13,7 +13,7 @@ import (
|
||||||
type Observation struct {
|
type Observation struct {
|
||||||
Id int64 `json:"id,omitempty"`
|
Id int64 `json:"id,omitempty"`
|
||||||
ObservationName string `db:"observation_name" json:"observationName"`
|
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"`
|
CreatedAt time.Time `db:"created_at" json:"createdAt"`
|
||||||
UpdatedAt time.Time `db:"updated_at" json:"updatedAt"`
|
UpdatedAt time.Time `db:"updated_at" json:"updatedAt"`
|
||||||
DeletedAt NullTime `db:"deleted_at" json:"deletedAt"`
|
DeletedAt NullTime `db:"deleted_at" json:"deletedAt"`
|
||||||
|
|
|
@ -54,7 +54,7 @@ func TestObservationService_Create(t *testing.T) {
|
||||||
mux.HandleFunc(urlPath(t, router.CreateObservation, nil), func(w http.ResponseWriter, r *http.Request) {
|
mux.HandleFunc(urlPath(t, router.CreateObservation, nil), func(w http.ResponseWriter, r *http.Request) {
|
||||||
called = true
|
called = true
|
||||||
testMethod(t, r, "POST")
|
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)
|
w.WriteHeader(http.StatusCreated)
|
||||||
writeJSON(w, want)
|
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) {
|
mux.HandleFunc(urlPath(t, router.UpdateObservation, map[string]string{"Id": "1"}), func(w http.ResponseWriter, r *http.Request) {
|
||||||
called = true
|
called = true
|
||||||
testMethod(t, r, "PUT")
|
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)
|
w.WriteHeader(http.StatusOK)
|
||||||
writeJSON(w, want)
|
writeJSON(w, want)
|
||||||
})
|
})
|
||||||
|
|
|
@ -31,12 +31,12 @@ func API() *mux.Router {
|
||||||
m.Path("/strains/{Id:.+}").Methods("PUT").Name(UpdateStrain)
|
m.Path("/strains/{Id:.+}").Methods("PUT").Name(UpdateStrain)
|
||||||
m.Path("/strains/{Id:.+}").Methods("DELETE").Name(DeleteStrain)
|
m.Path("/strains/{Id:.+}").Methods("DELETE").Name(DeleteStrain)
|
||||||
|
|
||||||
// ObservationTypes
|
// CharacteristicTypes
|
||||||
m.Path("/observation_types").Methods("GET").Name(ObservationTypes)
|
m.Path("/characteristic_types").Methods("GET").Name(CharacteristicTypes)
|
||||||
m.Path("/observation_types").Methods("POST").Name(CreateObservationType)
|
m.Path("/characteristic_types").Methods("POST").Name(CreateCharacteristicType)
|
||||||
m.Path("/observation_types/{Id:.+}").Methods("GET").Name(ObservationType)
|
m.Path("/characteristic_types/{Id:.+}").Methods("GET").Name(CharacteristicType)
|
||||||
m.Path("/observation_types/{Id:.+}").Methods("PUT").Name(UpdateObservationType)
|
m.Path("/characteristic_types/{Id:.+}").Methods("PUT").Name(UpdateCharacteristicType)
|
||||||
m.Path("/observation_types/{Id:.+}").Methods("DELETE").Name(DeleteObservationType)
|
m.Path("/characteristic_types/{Id:.+}").Methods("DELETE").Name(DeleteCharacteristicType)
|
||||||
|
|
||||||
// Observations
|
// Observations
|
||||||
m.Path("/observations").Methods("GET").Name(Observations)
|
m.Path("/observations").Methods("GET").Name(Observations)
|
||||||
|
|
|
@ -23,11 +23,11 @@ const (
|
||||||
UpdateStrain = "strain:update"
|
UpdateStrain = "strain:update"
|
||||||
DeleteStrain = "strain:delete"
|
DeleteStrain = "strain:delete"
|
||||||
|
|
||||||
ObservationType = "observation_type:get"
|
CharacteristicType = "characteristic_type:get"
|
||||||
CreateObservationType = "observation_type:create"
|
CreateCharacteristicType = "characteristic_type:create"
|
||||||
ObservationTypes = "observation_type:list"
|
CharacteristicTypes = "characteristic_type:list"
|
||||||
UpdateObservationType = "observation_type:update"
|
UpdateCharacteristicType = "characteristic_type:update"
|
||||||
DeleteObservationType = "observation_type:delete"
|
DeleteCharacteristicType = "characteristic_type:delete"
|
||||||
|
|
||||||
Observation = "observation:get"
|
Observation = "observation:get"
|
||||||
CreateObservation = "observation:create"
|
CreateObservation = "observation:create"
|
||||||
|
|
Reference in a new issue