Ember data: convey strain/meas. relationships

This commit is contained in:
Matthew Dillon 2015-01-26 11:39:26 -09:00
parent 4be150f897
commit af515e3e3c
8 changed files with 55 additions and 46 deletions

View file

@ -17,7 +17,7 @@ import (
// combination of strain & characteristic, but not both.
type Measurement struct {
Id int64 `json:"id,omitempty"`
StrainId int64 `db:"strain_id" json:"strainId"`
StrainId int64 `db:"strain_id" json:"strain"`
CharacteristicId int64 `db:"characteristic_id" json:"characteristicId"`
TextMeasurementTypeId NullInt64 `db:"text_measurement_type_id" json:"textMeasurementTypeId"`
TxtValue NullString `db:"txt_value" json:"txtValue"`

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,"strainId":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,"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")
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,"strainId":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,"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")
w.WriteHeader(http.StatusOK)
writeJSON(w, want)
})

View file

@ -22,7 +22,7 @@ type SpeciesBase struct {
type Species struct {
*SpeciesBase
Strain NullSliceInt64 `db:"strains" json:"strains"`
Strains NullSliceInt64 `db:"strains" json:"strains"`
}
type SpeciesJSON struct {

View file

@ -12,7 +12,7 @@ import (
)
// A Strain is a subclass of species
type Strain struct {
type StrainBase struct {
Id int64 `json:"id,omitempty"`
SpeciesId int64 `db:"species_id" json:"species"`
StrainName string `db:"strain_name" json:"strainName"`
@ -26,6 +26,11 @@ type Strain struct {
DeletedAt NullTime `db:"deleted_at" json:"deletedAt"`
}
type Strain struct {
*StrainBase
Measurements NullSliceInt64 `db:"measurements" json:"measurements"`
}
type StrainJSON struct {
Strain *Strain `json:"strain"`
}
@ -40,27 +45,30 @@ func (s *Strain) String() string {
func NewStrain() *Strain {
return &Strain{
StrainName: "Test Strain",
StrainType: "Test Type",
Etymology: NullString{
sql.NullString{
String: "Test Etymology",
Valid: true,
},
},
AccessionBanks: "Test Accession",
GenbankEmblDdb: NullString{
sql.NullString{
String: "Test Genbank",
Valid: true,
},
},
IsolatedFrom: NullString{
sql.NullString{
String: "",
Valid: false,
&StrainBase{
StrainName: "Test Strain",
StrainType: "Test Type",
Etymology: NullString{
sql.NullString{
String: "Test Etymology",
Valid: true,
},
},
AccessionBanks: "Test Accession",
GenbankEmblDdb: NullString{
sql.NullString{
String: "Test Genbank",
Valid: true,
},
},
IsolatedFrom: NullString{
sql.NullString{
String: "",
Valid: false,
},
},
},
make([]int64, 0),
}
}

View file

@ -55,7 +55,7 @@ func TestStrainService_Create(t *testing.T) {
mux.HandleFunc(urlPath(t, router.CreateStrain, nil), func(w http.ResponseWriter, r *http.Request) {
called = true
testMethod(t, r, "POST")
testBody(t, r, `{"strain":{"id":1,"species":1,"strainName":"Test Strain","strainType":"Test Type","etymology":"Test Etymology","accessionBanks":"Test Accession","genbankEmblDdb":"Test Genbank","isolatedFrom":null,"createdAt":"0001-01-01T00:00:00Z","updatedAt":"0001-01-01T00:00:00Z","deletedAt":null}}`+"\n")
testBody(t, r, `{"strain":{"id":1,"species":1,"strainName":"Test Strain","strainType":"Test Type","etymology":"Test Etymology","accessionBanks":"Test Accession","genbankEmblDdb":"Test Genbank","isolatedFrom":null,"createdAt":"0001-01-01T00:00:00Z","updatedAt":"0001-01-01T00:00:00Z","deletedAt":null,"measurements":[]}}`+"\n")
w.WriteHeader(http.StatusCreated)
writeJSON(w, want)
@ -124,7 +124,7 @@ func TestStrainService_Update(t *testing.T) {
mux.HandleFunc(urlPath(t, router.UpdateStrain, map[string]string{"Id": "1"}), func(w http.ResponseWriter, r *http.Request) {
called = true
testMethod(t, r, "PUT")
testBody(t, r, `{"strain":{"id":1,"species":1,"strainName":"Test Strain Updated","strainType":"Test Type Updated","etymology":"Test Etymology","accessionBanks":"Test Accession Updated","genbankEmblDdb":"Test Genbank Updated","isolatedFrom":null,"createdAt":"0001-01-01T00:00:00Z","updatedAt":"0001-01-01T00:00:00Z","deletedAt":null}}`+"\n")
testBody(t, r, `{"strain":{"id":1,"species":1,"strainName":"Test Strain Updated","strainType":"Test Type Updated","etymology":"Test Etymology","accessionBanks":"Test Accession Updated","genbankEmblDdb":"Test Genbank Updated","isolatedFrom":null,"createdAt":"0001-01-01T00:00:00Z","updatedAt":"0001-01-01T00:00:00Z","deletedAt":null,"measurements":[]}}`+"\n")
w.WriteHeader(http.StatusOK)
writeJSON(w, want)
})