Fixed issue with NullString JSON unmarshalling.

Updated Measurements.Notes test.
This commit is contained in:
Matthew Dillon 2015-01-08 15:51:50 -09:00
parent 7da59ffef2
commit 7b7f519c01
4 changed files with 10 additions and 3 deletions

View file

@ -14,6 +14,7 @@ func newMeasurement() *models.Measurement {
measurement.CharacteristicId = 3
measurement.TextMeasurementTypeId = models.NullInt64{sql.NullInt64{Int64: 4, Valid: false}}
measurement.UnitTypeId = models.NullInt64{sql.NullInt64{Int64: 5, Valid: true}}
measurement.Notes = models.NullString{sql.NullString{String: "a note", Valid: true}}
return measurement
}

View file

@ -3,6 +3,7 @@ package models
import (
"database/sql"
"errors"
"fmt"
"net/http"
"strconv"
"time"
@ -29,6 +30,10 @@ type Measurement struct {
UpdatedAt time.Time `db:"updated_at" json:"updatedAt"`
}
func (m *Measurement) String() string {
return fmt.Sprintf("%v", *m)
}
func NewMeasurement() *Measurement {
return &Measurement{
NumValue: NullFloat64{sql.NullFloat64{Float64: 1.23, Valid: true}},

View file

@ -15,6 +15,7 @@ func newMeasurement() *Measurement {
measurement.StrainId = 1
measurement.CharacteristicId = 1
measurement.UnitTypeId = NullInt64{sql.NullInt64{Int64: 1, Valid: true}}
measurement.Notes = NullString{sql.NullString{String: "a note", Valid: true}}
return measurement
}
@ -58,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, `{"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")
testBody(t, r, `{"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")
w.WriteHeader(http.StatusCreated)
writeJSON(w, want)
@ -127,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, `{"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")
testBody(t, r, `{"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")
w.WriteHeader(http.StatusOK)
writeJSON(w, want)
})

View file

@ -30,7 +30,7 @@ func (s *NullString) UnmarshalJSON(b []byte) error {
var err error
json.Unmarshal(b, &x)
switch x.(type) {
case float64:
case string:
err = json.Unmarshal(b, &s.String)
case map[string]interface{}:
err = json.Unmarshal(b, &s.NullString)