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

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

View file

@ -20,7 +20,7 @@ type Client struct {
Genera GeneraService
Species SpeciesService
Strains StrainsService
ObservationTypes ObservationTypesService
CharacteristicTypes CharacteristicTypesService
Observations ObservationsService
TextMeasurementTypes TextMeasurementTypesService
UnitTypes UnitTypesService
@ -56,7 +56,7 @@ func NewClient(httpClient *http.Client) *Client {
c.Genera = &generaService{c}
c.Species = &speciesService{c}
c.Strains = &strainsService{c}
c.ObservationTypes = &observationTypesService{c}
c.CharacteristicTypes = &characteristicTypesService{c}
c.Observations = &observationsService{c}
c.TextMeasurementTypes = &textMeasurementTypesService{c}
c.UnitTypes = &unitTypesService{c}

View file

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

View file

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

View file

@ -13,7 +13,7 @@ import (
type Observation struct {
Id int64 `json:"id,omitempty"`
ObservationName string `db:"observation_name" json:"observationName"`
ObservationTypeId int64 `db:"observation_type_id" json:"observationTypeId"`
CharacteristicTypeId int64 `db:"characteristic_type_id" json:"characteristicTypeId"`
CreatedAt time.Time `db:"created_at" json:"createdAt"`
UpdatedAt time.Time `db:"updated_at" json:"updatedAt"`
DeletedAt NullTime `db:"deleted_at" json:"deletedAt"`

View file

@ -54,7 +54,7 @@ func TestObservationService_Create(t *testing.T) {
mux.HandleFunc(urlPath(t, router.CreateObservation, nil), func(w http.ResponseWriter, r *http.Request) {
called = true
testMethod(t, r, "POST")
testBody(t, r, `{"id":1,"observationName":"Test Observation","observationTypeId":0,"createdAt":"0001-01-01T00:00:00Z","updatedAt":"0001-01-01T00:00:00Z","deletedAt":null}`+"\n")
testBody(t, r, `{"id":1,"observationName":"Test Observation","characteristicTypeId":0,"createdAt":"0001-01-01T00:00:00Z","updatedAt":"0001-01-01T00:00:00Z","deletedAt":null}`+"\n")
w.WriteHeader(http.StatusCreated)
writeJSON(w, want)
@ -123,7 +123,7 @@ func TestObservationService_Update(t *testing.T) {
mux.HandleFunc(urlPath(t, router.UpdateObservation, map[string]string{"Id": "1"}), func(w http.ResponseWriter, r *http.Request) {
called = true
testMethod(t, r, "PUT")
testBody(t, r, `{"id":1,"observationName":"Test Obs Updated","observationTypeId":0,"createdAt":"0001-01-01T00:00:00Z","updatedAt":"0001-01-01T00:00:00Z","deletedAt":null}`+"\n")
testBody(t, r, `{"id":1,"observationName":"Test Obs Updated","characteristicTypeId":0,"createdAt":"0001-01-01T00:00:00Z","updatedAt":"0001-01-01T00:00:00Z","deletedAt":null}`+"\n")
w.WriteHeader(http.StatusOK)
writeJSON(w, want)
})