Renaming observations to characteristics
This commit is contained in:
parent
c1323f9c1f
commit
950b15a117
27 changed files with 889 additions and 889 deletions
|
@ -20,7 +20,7 @@ type CharacteristicType struct {
|
|||
|
||||
func NewCharacteristicType() *CharacteristicType {
|
||||
return &CharacteristicType{
|
||||
CharacteristicTypeName: "Test Obs Type",
|
||||
CharacteristicTypeName: "Test Char Type",
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -54,7 +54,7 @@ func TestCharacteristicTypeService_Create(t *testing.T) {
|
|||
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")
|
||||
testBody(t, r, `{"id":1,"characteristicTypeName":"Test Char Type","createdAt":"0001-01-01T00:00:00Z","updatedAt":"0001-01-01T00:00:00Z","deletedAt":null}`+"\n")
|
||||
|
||||
w.WriteHeader(http.StatusCreated)
|
||||
writeJSON(w, want)
|
||||
|
@ -123,13 +123,13 @@ func TestCharacteristicTypeService_Update(t *testing.T) {
|
|||
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")
|
||||
testBody(t, r, `{"id":1,"characteristicTypeName":"Test Char 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"
|
||||
characteristic_type.CharacteristicTypeName = "Test Char Type Updated"
|
||||
updated, err := client.CharacteristicTypes.Update(characteristic_type.Id, characteristic_type)
|
||||
if err != nil {
|
||||
t.Errorf("CharacteristicTypes.Update returned error: %v", err)
|
||||
|
|
204
models/characteristics.go
Normal file
204
models/characteristics.go
Normal file
|
@ -0,0 +1,204 @@
|
|||
package models
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/thermokarst/bactdb/router"
|
||||
)
|
||||
|
||||
// A Characteristic is a lookup type
|
||||
type Characteristic struct {
|
||||
Id int64 `json:"id,omitempty"`
|
||||
CharacteristicName string `db:"characteristic_name" json:"characteristicName"`
|
||||
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"`
|
||||
}
|
||||
|
||||
func NewCharacteristic() *Characteristic {
|
||||
return &Characteristic{
|
||||
CharacteristicName: "Test Characteristic",
|
||||
}
|
||||
}
|
||||
|
||||
type CharacteristicsService interface {
|
||||
// Get an characteristic
|
||||
Get(id int64) (*Characteristic, error)
|
||||
|
||||
// List all characteristics
|
||||
List(opt *CharacteristicListOptions) ([]*Characteristic, error)
|
||||
|
||||
// Create an characteristic
|
||||
Create(characteristic *Characteristic) (bool, error)
|
||||
|
||||
// Update an characteristic
|
||||
Update(id int64, Characteristic *Characteristic) (updated bool, err error)
|
||||
|
||||
// Delete an characteristic
|
||||
Delete(id int64) (deleted bool, err error)
|
||||
}
|
||||
|
||||
var (
|
||||
ErrCharacteristicNotFound = errors.New("characteristic not found")
|
||||
)
|
||||
|
||||
type characteristicsService struct {
|
||||
client *Client
|
||||
}
|
||||
|
||||
func (s *characteristicsService) Get(id int64) (*Characteristic, error) {
|
||||
strId := strconv.FormatInt(id, 10)
|
||||
|
||||
url, err := s.client.url(router.Characteristic, 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 *Characteristic
|
||||
_, err = s.client.Do(req, &characteristic)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return characteristic, nil
|
||||
}
|
||||
|
||||
func (s *characteristicsService) Create(characteristic *Characteristic) (bool, error) {
|
||||
url, err := s.client.url(router.CreateCharacteristic, nil, nil)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
req, err := s.client.NewRequest("POST", url.String(), characteristic)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
resp, err := s.client.Do(req, &characteristic)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
return resp.StatusCode == http.StatusCreated, nil
|
||||
}
|
||||
|
||||
type CharacteristicListOptions struct {
|
||||
ListOptions
|
||||
}
|
||||
|
||||
func (s *characteristicsService) List(opt *CharacteristicListOptions) ([]*Characteristic, error) {
|
||||
url, err := s.client.url(router.Characteristics, nil, opt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
req, err := s.client.NewRequest("GET", url.String(), nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var characteristics []*Characteristic
|
||||
_, err = s.client.Do(req, &characteristics)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return characteristics, nil
|
||||
}
|
||||
|
||||
func (s *characteristicsService) Update(id int64, characteristic *Characteristic) (bool, error) {
|
||||
strId := strconv.FormatInt(id, 10)
|
||||
|
||||
url, err := s.client.url(router.UpdateCharacteristic, map[string]string{"Id": strId}, nil)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
req, err := s.client.NewRequest("PUT", url.String(), characteristic)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
resp, err := s.client.Do(req, &characteristic)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
return resp.StatusCode == http.StatusOK, nil
|
||||
}
|
||||
|
||||
func (s *characteristicsService) Delete(id int64) (bool, error) {
|
||||
strId := strconv.FormatInt(id, 10)
|
||||
|
||||
url, err := s.client.url(router.DeleteCharacteristic, 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 *Characteristic
|
||||
resp, err := s.client.Do(req, &characteristic)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
return resp.StatusCode == http.StatusOK, nil
|
||||
}
|
||||
|
||||
type MockCharacteristicsService struct {
|
||||
Get_ func(id int64) (*Characteristic, error)
|
||||
List_ func(opt *CharacteristicListOptions) ([]*Characteristic, error)
|
||||
Create_ func(characteristic *Characteristic) (bool, error)
|
||||
Update_ func(id int64, characteristic *Characteristic) (bool, error)
|
||||
Delete_ func(id int64) (bool, error)
|
||||
}
|
||||
|
||||
var _ CharacteristicsService = &MockCharacteristicsService{}
|
||||
|
||||
func (s *MockCharacteristicsService) Get(id int64) (*Characteristic, error) {
|
||||
if s.Get_ == nil {
|
||||
return nil, nil
|
||||
}
|
||||
return s.Get_(id)
|
||||
}
|
||||
|
||||
func (s *MockCharacteristicsService) Create(characteristic *Characteristic) (bool, error) {
|
||||
if s.Create_ == nil {
|
||||
return false, nil
|
||||
}
|
||||
return s.Create_(characteristic)
|
||||
}
|
||||
|
||||
func (s *MockCharacteristicsService) List(opt *CharacteristicListOptions) ([]*Characteristic, error) {
|
||||
if s.List_ == nil {
|
||||
return nil, nil
|
||||
}
|
||||
return s.List_(opt)
|
||||
}
|
||||
|
||||
func (s *MockCharacteristicsService) Update(id int64, characteristic *Characteristic) (bool, error) {
|
||||
if s.Update_ == nil {
|
||||
return false, nil
|
||||
}
|
||||
return s.Update_(id, characteristic)
|
||||
}
|
||||
|
||||
func (s *MockCharacteristicsService) Delete(id int64) (bool, error) {
|
||||
if s.Delete_ == nil {
|
||||
return false, nil
|
||||
}
|
||||
return s.Delete_(id)
|
||||
}
|
174
models/characteristics_test.go
Normal file
174
models/characteristics_test.go
Normal file
|
@ -0,0 +1,174 @@
|
|||
package models
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/thermokarst/bactdb/router"
|
||||
)
|
||||
|
||||
func newCharacteristic() *Characteristic {
|
||||
characteristic := NewCharacteristic()
|
||||
characteristic.Id = 1
|
||||
return characteristic
|
||||
}
|
||||
|
||||
func TestCharacteristicService_Get(t *testing.T) {
|
||||
setup()
|
||||
defer teardown()
|
||||
|
||||
want := newCharacteristic()
|
||||
|
||||
var called bool
|
||||
mux.HandleFunc(urlPath(t, router.Characteristic, map[string]string{"Id": "1"}), func(w http.ResponseWriter, r *http.Request) {
|
||||
called = true
|
||||
testMethod(t, r, "GET")
|
||||
|
||||
writeJSON(w, want)
|
||||
})
|
||||
|
||||
characteristic, err := client.Characteristics.Get(want.Id)
|
||||
if err != nil {
|
||||
t.Errorf("Characteristics.Get returned error: %v", err)
|
||||
}
|
||||
|
||||
if !called {
|
||||
t.Fatal("!called")
|
||||
}
|
||||
|
||||
normalizeTime(&want.CreatedAt, &want.UpdatedAt, &want.DeletedAt)
|
||||
|
||||
if !reflect.DeepEqual(characteristic, want) {
|
||||
t.Errorf("Characteristics.Get return %+v, want %+v", characteristic, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCharacteristicService_Create(t *testing.T) {
|
||||
setup()
|
||||
defer teardown()
|
||||
|
||||
want := newCharacteristic()
|
||||
|
||||
var called bool
|
||||
mux.HandleFunc(urlPath(t, router.CreateCharacteristic, nil), func(w http.ResponseWriter, r *http.Request) {
|
||||
called = true
|
||||
testMethod(t, r, "POST")
|
||||
testBody(t, r, `{"id":1,"characteristicName":"Test Characteristic","characteristicTypeId":0,"createdAt":"0001-01-01T00:00:00Z","updatedAt":"0001-01-01T00:00:00Z","deletedAt":null}`+"\n")
|
||||
|
||||
w.WriteHeader(http.StatusCreated)
|
||||
writeJSON(w, want)
|
||||
})
|
||||
|
||||
characteristic := newCharacteristic()
|
||||
created, err := client.Characteristics.Create(characteristic)
|
||||
if err != nil {
|
||||
t.Errorf("Characteristics.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, want) {
|
||||
t.Errorf("Characteristics.Create returned %+v, want %+v", characteristic, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCharacteristicService_List(t *testing.T) {
|
||||
setup()
|
||||
defer teardown()
|
||||
|
||||
want := []*Characteristic{newCharacteristic()}
|
||||
|
||||
var called bool
|
||||
mux.HandleFunc(urlPath(t, router.Characteristics, nil), func(w http.ResponseWriter, r *http.Request) {
|
||||
called = true
|
||||
testMethod(t, r, "GET")
|
||||
testFormValues(t, r, values{})
|
||||
|
||||
writeJSON(w, want)
|
||||
})
|
||||
|
||||
characteristics, err := client.Characteristics.List(nil)
|
||||
if err != nil {
|
||||
t.Errorf("Characteristics.List returned error: %v", err)
|
||||
}
|
||||
|
||||
if !called {
|
||||
t.Fatal("!called")
|
||||
}
|
||||
|
||||
for _, u := range want {
|
||||
normalizeTime(&u.CreatedAt, &u.UpdatedAt, &u.DeletedAt)
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(characteristics, want) {
|
||||
t.Errorf("Characteristics.List return %+v, want %+v", characteristics, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCharacteristicService_Update(t *testing.T) {
|
||||
setup()
|
||||
defer teardown()
|
||||
|
||||
want := newCharacteristic()
|
||||
|
||||
var called bool
|
||||
mux.HandleFunc(urlPath(t, router.UpdateCharacteristic, map[string]string{"Id": "1"}), func(w http.ResponseWriter, r *http.Request) {
|
||||
called = true
|
||||
testMethod(t, r, "PUT")
|
||||
testBody(t, r, `{"id":1,"characteristicName":"Test Char 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)
|
||||
})
|
||||
|
||||
characteristic := newCharacteristic()
|
||||
characteristic.CharacteristicName = "Test Char Updated"
|
||||
updated, err := client.Characteristics.Update(characteristic.Id, characteristic)
|
||||
if err != nil {
|
||||
t.Errorf("Characteristics.Update returned error: %v", err)
|
||||
}
|
||||
|
||||
if !updated {
|
||||
t.Error("!updated")
|
||||
}
|
||||
|
||||
if !called {
|
||||
t.Fatal("!called")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCharacteristicService_Delete(t *testing.T) {
|
||||
setup()
|
||||
defer teardown()
|
||||
|
||||
want := newCharacteristic()
|
||||
|
||||
var called bool
|
||||
mux.HandleFunc(urlPath(t, router.DeleteCharacteristic, 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.Characteristics.Delete(want.Id)
|
||||
if err != nil {
|
||||
t.Errorf("Characteristics.Delete returned error: %v", err)
|
||||
}
|
||||
|
||||
if !deleted {
|
||||
t.Error("!deleted")
|
||||
}
|
||||
|
||||
if !called {
|
||||
t.Fatal("!called")
|
||||
}
|
||||
}
|
|
@ -20,8 +20,8 @@ type Client struct {
|
|||
Genera GeneraService
|
||||
Species SpeciesService
|
||||
Strains StrainsService
|
||||
CharacteristicTypes CharacteristicTypesService
|
||||
Observations ObservationsService
|
||||
CharacteristicTypes CharacteristicTypesService
|
||||
Characteristics CharacteristicsService
|
||||
TextMeasurementTypes TextMeasurementTypesService
|
||||
UnitTypes UnitTypesService
|
||||
Measurements MeasurementsService
|
||||
|
@ -57,7 +57,7 @@ func NewClient(httpClient *http.Client) *Client {
|
|||
c.Species = &speciesService{c}
|
||||
c.Strains = &strainsService{c}
|
||||
c.CharacteristicTypes = &characteristicTypesService{c}
|
||||
c.Observations = &observationsService{c}
|
||||
c.Characteristics = &characteristicsService{c}
|
||||
c.TextMeasurementTypes = &textMeasurementTypesService{c}
|
||||
c.UnitTypes = &unitTypesService{c}
|
||||
c.Measurements = &measurementsService{c}
|
||||
|
|
|
@ -13,11 +13,11 @@ import (
|
|||
// A Measurement is the main data type for this application
|
||||
// There are two types of supported measurements: text & numerical. The table
|
||||
// has a constraint that will allow one or the other for a particular
|
||||
// combination of strain & observation, but not both.
|
||||
// combination of strain & characteristic, but not both.
|
||||
type Measurement struct {
|
||||
Id int64 `json:"id,omitempty"`
|
||||
StrainId int64 `db:"strain_id" json:"strainId"`
|
||||
ObservationId int64 `db:"observation_id" json:"observationId"`
|
||||
CharacteristicId int64 `db:"characteristic_id" json:"characteristicId"`
|
||||
TextMeasurementTypeId NullInt64 `db:"text_measurement_type_id" json:"textMeasurementTypeId"`
|
||||
TxtValue NullString `db:"txt_value" json:"txtValue"`
|
||||
NumValue NullFloat64 `db:"num_value" json:"numValue"`
|
||||
|
|
|
@ -13,7 +13,7 @@ func newMeasurement() *Measurement {
|
|||
measurement := NewMeasurement()
|
||||
measurement.Id = 1
|
||||
measurement.StrainId = 1
|
||||
measurement.ObservationId = 1
|
||||
measurement.CharacteristicId = 1
|
||||
measurement.UnitTypeId = NullInt64{sql.NullInt64{Int64: 1, Valid: true}}
|
||||
return measurement
|
||||
}
|
||||
|
@ -58,7 +58,7 @@ func TestMeasurementService_Create(t *testing.T) {
|
|||
mux.HandleFunc(urlPath(t, router.CreateMeasurement, nil), func(w http.ResponseWriter, r *http.Request) {
|
||||
called = true
|
||||
testMethod(t, r, "POST")
|
||||
testBody(t, r, `{"id":1,"strainId":1,"observationId":1,"textMeasurementTypeId":null,"txtValue":null,"numValue":1.23,"confidenceInterval":null,"unitTypeId":1,"notes":null,"testMethodId":null,"createdAt":"0001-01-01T00:00:00Z","updatedAt":"0001-01-01T00:00:00Z"}`+"\n")
|
||||
testBody(t, r, `{"id":1,"strainId":1,"characteristicId":1,"textMeasurementTypeId":null,"txtValue":null,"numValue":1.23,"confidenceInterval":null,"unitTypeId":1,"notes":null,"testMethodId":null,"createdAt":"0001-01-01T00:00:00Z","updatedAt":"0001-01-01T00:00:00Z"}`+"\n")
|
||||
|
||||
w.WriteHeader(http.StatusCreated)
|
||||
writeJSON(w, want)
|
||||
|
@ -127,7 +127,7 @@ func TestMeasurementService_Update(t *testing.T) {
|
|||
mux.HandleFunc(urlPath(t, router.UpdateMeasurement, map[string]string{"Id": "1"}), func(w http.ResponseWriter, r *http.Request) {
|
||||
called = true
|
||||
testMethod(t, r, "PUT")
|
||||
testBody(t, r, `{"id":1,"strainId":1,"observationId":1,"textMeasurementTypeId":null,"txtValue":null,"numValue":4.56,"confidenceInterval":null,"unitTypeId":1,"notes":null,"testMethodId":null,"createdAt":"0001-01-01T00:00:00Z","updatedAt":"0001-01-01T00:00:00Z"}`+"\n")
|
||||
testBody(t, r, `{"id":1,"strainId":1,"characteristicId":1,"textMeasurementTypeId":null,"txtValue":null,"numValue":4.56,"confidenceInterval":null,"unitTypeId":1,"notes":null,"testMethodId":null,"createdAt":"0001-01-01T00:00:00Z","updatedAt":"0001-01-01T00:00:00Z"}`+"\n")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
writeJSON(w, want)
|
||||
})
|
||||
|
|
|
@ -1,204 +0,0 @@
|
|||
package models
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/thermokarst/bactdb/router"
|
||||
)
|
||||
|
||||
// An Observation is a lookup type
|
||||
type Observation struct {
|
||||
Id int64 `json:"id,omitempty"`
|
||||
ObservationName string `db:"observation_name" json:"observationName"`
|
||||
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"`
|
||||
}
|
||||
|
||||
func NewObservation() *Observation {
|
||||
return &Observation{
|
||||
ObservationName: "Test Observation",
|
||||
}
|
||||
}
|
||||
|
||||
type ObservationsService interface {
|
||||
// Get an observation
|
||||
Get(id int64) (*Observation, error)
|
||||
|
||||
// List all observations
|
||||
List(opt *ObservationListOptions) ([]*Observation, error)
|
||||
|
||||
// Create an observation
|
||||
Create(observation *Observation) (bool, error)
|
||||
|
||||
// Update an observation
|
||||
Update(id int64, Observation *Observation) (updated bool, err error)
|
||||
|
||||
// Delete an observation
|
||||
Delete(id int64) (deleted bool, err error)
|
||||
}
|
||||
|
||||
var (
|
||||
ErrObservationNotFound = errors.New("observation not found")
|
||||
)
|
||||
|
||||
type observationsService struct {
|
||||
client *Client
|
||||
}
|
||||
|
||||
func (s *observationsService) Get(id int64) (*Observation, error) {
|
||||
strId := strconv.FormatInt(id, 10)
|
||||
|
||||
url, err := s.client.url(router.Observation, 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 *Observation
|
||||
_, err = s.client.Do(req, &observation)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return observation, nil
|
||||
}
|
||||
|
||||
func (s *observationsService) Create(observation *Observation) (bool, error) {
|
||||
url, err := s.client.url(router.CreateObservation, nil, nil)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
req, err := s.client.NewRequest("POST", url.String(), observation)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
resp, err := s.client.Do(req, &observation)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
return resp.StatusCode == http.StatusCreated, nil
|
||||
}
|
||||
|
||||
type ObservationListOptions struct {
|
||||
ListOptions
|
||||
}
|
||||
|
||||
func (s *observationsService) List(opt *ObservationListOptions) ([]*Observation, error) {
|
||||
url, err := s.client.url(router.Observations, nil, opt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
req, err := s.client.NewRequest("GET", url.String(), nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var observations []*Observation
|
||||
_, err = s.client.Do(req, &observations)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return observations, nil
|
||||
}
|
||||
|
||||
func (s *observationsService) Update(id int64, observation *Observation) (bool, error) {
|
||||
strId := strconv.FormatInt(id, 10)
|
||||
|
||||
url, err := s.client.url(router.UpdateObservation, map[string]string{"Id": strId}, nil)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
req, err := s.client.NewRequest("PUT", url.String(), observation)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
resp, err := s.client.Do(req, &observation)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
return resp.StatusCode == http.StatusOK, nil
|
||||
}
|
||||
|
||||
func (s *observationsService) Delete(id int64) (bool, error) {
|
||||
strId := strconv.FormatInt(id, 10)
|
||||
|
||||
url, err := s.client.url(router.DeleteObservation, 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 *Observation
|
||||
resp, err := s.client.Do(req, &observation)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
return resp.StatusCode == http.StatusOK, nil
|
||||
}
|
||||
|
||||
type MockObservationsService struct {
|
||||
Get_ func(id int64) (*Observation, error)
|
||||
List_ func(opt *ObservationListOptions) ([]*Observation, error)
|
||||
Create_ func(observation *Observation) (bool, error)
|
||||
Update_ func(id int64, observation *Observation) (bool, error)
|
||||
Delete_ func(id int64) (bool, error)
|
||||
}
|
||||
|
||||
var _ ObservationsService = &MockObservationsService{}
|
||||
|
||||
func (s *MockObservationsService) Get(id int64) (*Observation, error) {
|
||||
if s.Get_ == nil {
|
||||
return nil, nil
|
||||
}
|
||||
return s.Get_(id)
|
||||
}
|
||||
|
||||
func (s *MockObservationsService) Create(observation *Observation) (bool, error) {
|
||||
if s.Create_ == nil {
|
||||
return false, nil
|
||||
}
|
||||
return s.Create_(observation)
|
||||
}
|
||||
|
||||
func (s *MockObservationsService) List(opt *ObservationListOptions) ([]*Observation, error) {
|
||||
if s.List_ == nil {
|
||||
return nil, nil
|
||||
}
|
||||
return s.List_(opt)
|
||||
}
|
||||
|
||||
func (s *MockObservationsService) Update(id int64, observation *Observation) (bool, error) {
|
||||
if s.Update_ == nil {
|
||||
return false, nil
|
||||
}
|
||||
return s.Update_(id, observation)
|
||||
}
|
||||
|
||||
func (s *MockObservationsService) 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 newObservation() *Observation {
|
||||
observation := NewObservation()
|
||||
observation.Id = 1
|
||||
return observation
|
||||
}
|
||||
|
||||
func TestObservationService_Get(t *testing.T) {
|
||||
setup()
|
||||
defer teardown()
|
||||
|
||||
want := newObservation()
|
||||
|
||||
var called bool
|
||||
mux.HandleFunc(urlPath(t, router.Observation, map[string]string{"Id": "1"}), func(w http.ResponseWriter, r *http.Request) {
|
||||
called = true
|
||||
testMethod(t, r, "GET")
|
||||
|
||||
writeJSON(w, want)
|
||||
})
|
||||
|
||||
observation, err := client.Observations.Get(want.Id)
|
||||
if err != nil {
|
||||
t.Errorf("Observations.Get returned error: %v", err)
|
||||
}
|
||||
|
||||
if !called {
|
||||
t.Fatal("!called")
|
||||
}
|
||||
|
||||
normalizeTime(&want.CreatedAt, &want.UpdatedAt, &want.DeletedAt)
|
||||
|
||||
if !reflect.DeepEqual(observation, want) {
|
||||
t.Errorf("Observations.Get return %+v, want %+v", observation, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestObservationService_Create(t *testing.T) {
|
||||
setup()
|
||||
defer teardown()
|
||||
|
||||
want := newObservation()
|
||||
|
||||
var called bool
|
||||
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","characteristicTypeId":0,"createdAt":"0001-01-01T00:00:00Z","updatedAt":"0001-01-01T00:00:00Z","deletedAt":null}`+"\n")
|
||||
|
||||
w.WriteHeader(http.StatusCreated)
|
||||
writeJSON(w, want)
|
||||
})
|
||||
|
||||
observation := newObservation()
|
||||
created, err := client.Observations.Create(observation)
|
||||
if err != nil {
|
||||
t.Errorf("Observations.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, want) {
|
||||
t.Errorf("Observations.Create returned %+v, want %+v", observation, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestObservationService_List(t *testing.T) {
|
||||
setup()
|
||||
defer teardown()
|
||||
|
||||
want := []*Observation{newObservation()}
|
||||
|
||||
var called bool
|
||||
mux.HandleFunc(urlPath(t, router.Observations, nil), func(w http.ResponseWriter, r *http.Request) {
|
||||
called = true
|
||||
testMethod(t, r, "GET")
|
||||
testFormValues(t, r, values{})
|
||||
|
||||
writeJSON(w, want)
|
||||
})
|
||||
|
||||
observations, err := client.Observations.List(nil)
|
||||
if err != nil {
|
||||
t.Errorf("Observations.List returned error: %v", err)
|
||||
}
|
||||
|
||||
if !called {
|
||||
t.Fatal("!called")
|
||||
}
|
||||
|
||||
for _, u := range want {
|
||||
normalizeTime(&u.CreatedAt, &u.UpdatedAt, &u.DeletedAt)
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(observations, want) {
|
||||
t.Errorf("Observations.List return %+v, want %+v", observations, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestObservationService_Update(t *testing.T) {
|
||||
setup()
|
||||
defer teardown()
|
||||
|
||||
want := newObservation()
|
||||
|
||||
var called bool
|
||||
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","characteristicTypeId":0,"createdAt":"0001-01-01T00:00:00Z","updatedAt":"0001-01-01T00:00:00Z","deletedAt":null}`+"\n")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
writeJSON(w, want)
|
||||
})
|
||||
|
||||
observation := newObservation()
|
||||
observation.ObservationName = "Test Obs Updated"
|
||||
updated, err := client.Observations.Update(observation.Id, observation)
|
||||
if err != nil {
|
||||
t.Errorf("Observations.Update returned error: %v", err)
|
||||
}
|
||||
|
||||
if !updated {
|
||||
t.Error("!updated")
|
||||
}
|
||||
|
||||
if !called {
|
||||
t.Fatal("!called")
|
||||
}
|
||||
}
|
||||
|
||||
func TestObservationService_Delete(t *testing.T) {
|
||||
setup()
|
||||
defer teardown()
|
||||
|
||||
want := newObservation()
|
||||
|
||||
var called bool
|
||||
mux.HandleFunc(urlPath(t, router.DeleteObservation, 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.Observations.Delete(want.Id)
|
||||
if err != nil {
|
||||
t.Errorf("Observations.Delete returned error: %v", err)
|
||||
}
|
||||
|
||||
if !deleted {
|
||||
t.Error("!deleted")
|
||||
}
|
||||
|
||||
if !called {
|
||||
t.Fatal("!called")
|
||||
}
|
||||
}
|
Reference in a new issue