Ember data: characteristics

This commit is contained in:
Matthew Dillon 2015-02-03 12:32:24 -09:00
parent a37a5a9400
commit 41ee2857ee
7 changed files with 56 additions and 35 deletions

View file

@ -11,7 +11,7 @@ import (
)
// A Characteristic is a lookup type
type Characteristic struct {
type CharacteristicBase struct {
Id int64 `json:"id,omitempty"`
CharacteristicName string `db:"characteristic_name" json:"characteristicName"`
CharacteristicTypeId int64 `db:"characteristic_type_id" json:"characteristicTypeId"`
@ -20,13 +20,29 @@ type Characteristic struct {
DeletedAt NullTime `db:"deleted_at" json:"deletedAt"`
}
type Characteristic struct {
*CharacteristicBase
Measurements NullSliceInt64 `db:"measurements" json:"measurements"`
}
type CharacteristicJSON struct {
Characteristic *Characteristic `json:"characteristic"`
}
type CharacteristicsJSON struct {
Characteristics []*Characteristic `json:"characteristics"`
}
func (m *Characteristic) String() string {
return fmt.Sprintf("%v", *m)
}
func NewCharacteristic() *Characteristic {
return &Characteristic{
CharacteristicName: "Test Characteristic",
&CharacteristicBase{
CharacteristicName: "Test Characteristic",
},
make([]int64, 0),
}
}
@ -68,13 +84,13 @@ func (s *characteristicsService) Get(id int64) (*Characteristic, error) {
return nil, err
}
var characteristic *Characteristic
var characteristic *CharacteristicJSON
_, err = s.client.Do(req, &characteristic)
if err != nil {
return nil, err
}
return characteristic, nil
return characteristic.Characteristic, nil
}
func (s *characteristicsService) Create(characteristic *Characteristic) (bool, error) {
@ -83,7 +99,7 @@ func (s *characteristicsService) Create(characteristic *Characteristic) (bool, e
return false, err
}
req, err := s.client.NewRequest("POST", url.String(), characteristic)
req, err := s.client.NewRequest("POST", url.String(), CharacteristicJSON{Characteristic: characteristic})
if err != nil {
return false, err
}
@ -111,13 +127,13 @@ func (s *characteristicsService) List(opt *CharacteristicListOptions) ([]*Charac
return nil, err
}
var characteristics []*Characteristic
var characteristics *CharacteristicsJSON
_, err = s.client.Do(req, &characteristics)
if err != nil {
return nil, err
}
return characteristics, nil
return characteristics.Characteristics, nil
}
func (s *characteristicsService) Update(id int64, characteristic *Characteristic) (bool, error) {
@ -128,7 +144,7 @@ func (s *characteristicsService) Update(id int64, characteristic *Characteristic
return false, err
}
req, err := s.client.NewRequest("PUT", url.String(), characteristic)
req, err := s.client.NewRequest("PUT", url.String(), CharacteristicJSON{Characteristic: characteristic})
if err != nil {
return false, err
}

View file

@ -25,7 +25,7 @@ func TestCharacteristicService_Get(t *testing.T) {
called = true
testMethod(t, r, "GET")
writeJSON(w, want)
writeJSON(w, CharacteristicJSON{Characteristic: want})
})
characteristic, err := client.Characteristics.Get(want.Id)
@ -54,7 +54,7 @@ func TestCharacteristicService_Create(t *testing.T) {
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")
testBody(t, r, `{"characteristic":{"id":1,"characteristicName":"Test Characteristic","characteristicTypeId":0,"createdAt":"0001-01-01T00:00:00Z","updatedAt":"0001-01-01T00:00:00Z","deletedAt":null,"measurements":[]}}`+"\n")
w.WriteHeader(http.StatusCreated)
writeJSON(w, want)
@ -92,7 +92,7 @@ func TestCharacteristicService_List(t *testing.T) {
testMethod(t, r, "GET")
testFormValues(t, r, values{})
writeJSON(w, want)
writeJSON(w, CharacteristicsJSON{Characteristics: want})
})
characteristics, err := client.Characteristics.List(nil)
@ -123,7 +123,7 @@ func TestCharacteristicService_Update(t *testing.T) {
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")
testBody(t, r, `{"characteristic":{"id":1,"characteristicName":"Test Char Updated","characteristicTypeId":0,"createdAt":"0001-01-01T00:00:00Z","updatedAt":"0001-01-01T00:00:00Z","deletedAt":null,"measurements":[]}}`+"\n")
w.WriteHeader(http.StatusOK)
writeJSON(w, want)
})
@ -154,6 +154,7 @@ func TestCharacteristicService_Delete(t *testing.T) {
mux.HandleFunc(urlPath(t, router.DeleteCharacteristic, map[string]string{"Id": "1"}), func(w http.ResponseWriter, r *http.Request) {
called = true
testMethod(t, r, "DELETE")
testBody(t, r, "")
w.WriteHeader(http.StatusOK)
writeJSON(w, want)

View file

@ -18,7 +18,7 @@ import (
type Measurement struct {
Id int64 `json:"id,omitempty"`
StrainId int64 `db:"strain_id" json:"strain"`
CharacteristicId int64 `db:"characteristic_id" json:"characteristicId"`
CharacteristicId int64 `db:"characteristic_id" json:"characteristic"`
TextMeasurementTypeId NullInt64 `db:"text_measurement_type_id" json:"textMeasurementTypeId"`
TxtValue NullString `db:"txt_value" json:"txtValue"`
NumValue NullFloat64 `db:"num_value" json:"numValue"`

View file

@ -59,7 +59,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, `{"measurement":{"id":1,"strain":1,"characteristicId":1,"textMeasurementTypeId":null,"txtValue":null,"numValue":1.23,"confidenceInterval":null,"unitTypeId":1,"notes":"a note","testMethodId":null,"createdAt":"0001-01-01T00:00:00Z","updatedAt":"0001-01-01T00:00:00Z"}}`+"\n")
testBody(t, r, `{"measurement":{"id":1,"strain":1,"characteristic":1,"textMeasurementTypeId":null,"txtValue":null,"numValue":1.23,"confidenceInterval":null,"unitTypeId":1,"notes":"a note","testMethodId":null,"createdAt":"0001-01-01T00:00:00Z","updatedAt":"0001-01-01T00:00:00Z"}}`+"\n")
w.WriteHeader(http.StatusCreated)
writeJSON(w, want)
@ -128,7 +128,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, `{"measurement":{"id":1,"strain":1,"characteristicId":1,"textMeasurementTypeId":null,"txtValue":null,"numValue":4.56,"confidenceInterval":null,"unitTypeId":1,"notes":"a note","testMethodId":null,"createdAt":"0001-01-01T00:00:00Z","updatedAt":"0001-01-01T00:00:00Z"}}`+"\n")
testBody(t, r, `{"measurement":{"id":1,"strain":1,"characteristic":1,"textMeasurementTypeId":null,"txtValue":null,"numValue":4.56,"confidenceInterval":null,"unitTypeId":1,"notes":"a note","testMethodId":null,"createdAt":"0001-01-01T00:00:00Z","updatedAt":"0001-01-01T00:00:00Z"}}`+"\n")
w.WriteHeader(http.StatusOK)
writeJSON(w, want)
})