Measurements can be one of three types.
This commit is contained in:
parent
641203ceb9
commit
d092637b33
4 changed files with 23 additions and 14 deletions
|
@ -28,10 +28,10 @@ func newMeasurement(t *testing.T, tx *modl.Transaction) *models.Measurement {
|
|||
unit_type := insertUnitType(t, tx)
|
||||
|
||||
return &models.Measurement{
|
||||
StrainId: strain.Id,
|
||||
ObservationId: observation.Id,
|
||||
MeasurementValue: sql.NullFloat64{Float64: 1.23, Valid: true},
|
||||
UnitTypeId: sql.NullInt64{Int64: unit_type.Id, Valid: true},
|
||||
StrainId: strain.Id,
|
||||
ObservationId: observation.Id,
|
||||
NumValue: sql.NullFloat64{Float64: 1.23, Valid: true},
|
||||
UnitTypeId: sql.NullInt64{Int64: unit_type.Id, Valid: true},
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -108,7 +108,7 @@ func TestMeasurementsStore_Update_db(t *testing.T) {
|
|||
d := NewDatastore(tx)
|
||||
|
||||
// Tweak it
|
||||
measurement.MeasurementValue = sql.NullFloat64{Float64: 4.56, Valid: true}
|
||||
measurement.NumValue = sql.NullFloat64{Float64: 4.56, Valid: true}
|
||||
updated, err := d.Measurements.Update(measurement.Id, measurement)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
|
|
|
@ -6,7 +6,8 @@ CREATE TABLE measurements (
|
|||
strain_id BIGINT,
|
||||
observation_id BIGINT,
|
||||
text_measurement_type_id BIGINT NULL,
|
||||
measurement_value NUMERIC(8, 3) NULL,
|
||||
txt_value CHARACTER VARYING(255) NULL,
|
||||
num_value NUMERIC(8, 3) NULL,
|
||||
confidence_interval NUMERIC(8, 3) NULL,
|
||||
unit_type_id BIGINT NULL,
|
||||
notes CHARACTER VARYING(255) NULL,
|
||||
|
@ -21,12 +22,19 @@ CREATE TABLE measurements (
|
|||
FOREIGN KEY (unit_type_id) REFERENCES unit_types(id),
|
||||
CONSTRAINT exclusive_data_type CHECK (
|
||||
(text_measurement_type_id IS NOT NULL
|
||||
AND measurement_value IS NULL
|
||||
AND txt_value IS NULL
|
||||
AND num_value IS NULL
|
||||
AND confidence_interval IS NULL
|
||||
AND unit_type_id IS NULL)
|
||||
OR
|
||||
(text_measurement_type_id IS NULL
|
||||
AND measurement_value IS NOT NULL))
|
||||
|
||||
AND txt_value IS NULL
|
||||
AND num_value IS NOT NULL)
|
||||
OR
|
||||
(text_measurement_type_id IS NULL
|
||||
AND txt_value IS NOT NULL
|
||||
AND num_value IS NULL
|
||||
AND confidence_interval IS NULL
|
||||
AND unit_type_id IS NULL))
|
||||
);
|
||||
|
||||
|
|
|
@ -19,7 +19,8 @@ type Measurement struct {
|
|||
StrainId int64 `db:"strain_id" json:"strainId"`
|
||||
ObservationId int64 `db:"observation_id" json:"observationId"`
|
||||
TextMeasurementTypeId sql.NullInt64 `db:"text_measurement_type_id" json:"textMeasurementTypeId"`
|
||||
MeasurementValue sql.NullFloat64 `db:"measurement_value" json:"measurementValue"`
|
||||
TxtValue sql.NullString `db:"txt_value" json:"txtValue"`
|
||||
NumValue sql.NullFloat64 `db:"num_value" json:"numValue"`
|
||||
ConfidenceInterval sql.NullFloat64 `db:"confidence_interval" json:"confidenceInterval"`
|
||||
UnitTypeId sql.NullInt64 `db:"unit_type_id" json:"unitTypeId"`
|
||||
Notes sql.NullString `db:"notes" json:"notes"`
|
||||
|
@ -29,7 +30,7 @@ type Measurement struct {
|
|||
|
||||
func NewMeasurement() *Measurement {
|
||||
return &Measurement{
|
||||
MeasurementValue: sql.NullFloat64{Float64: 1.23, Valid: true},
|
||||
NumValue: sql.NullFloat64{Float64: 1.23, Valid: true},
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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":{"Int64":0,"Valid":false},"measurementValue":{"Float64":1.23,"Valid":true},"confidenceInterval":{"Float64":0,"Valid":false},"unitTypeId":{"Int64":1,"Valid":true},"notes":{"String":"","Valid":false},"createdAt":"0001-01-01T00:00:00Z","updatedAt":"0001-01-01T00:00:00Z"}`+"\n")
|
||||
testBody(t, r, `{"id":1,"strainId":1,"observationId":1,"textMeasurementTypeId":{"Int64":0,"Valid":false},"txtValue":{"String":"","Valid":false},"numValue":{"Float64":1.23,"Valid":true},"confidenceInterval":{"Float64":0,"Valid":false},"unitTypeId":{"Int64":1,"Valid":true},"notes":{"String":"","Valid":false},"createdAt":"0001-01-01T00:00:00Z","updatedAt":"0001-01-01T00:00:00Z"}`+"\n")
|
||||
|
||||
w.WriteHeader(http.StatusCreated)
|
||||
writeJSON(w, want)
|
||||
|
@ -127,13 +127,13 @@ 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":{"Int64":0,"Valid":false},"measurementValue":{"Float64":4.56,"Valid":true},"confidenceInterval":{"Float64":0,"Valid":false},"unitTypeId":{"Int64":1,"Valid":true},"notes":{"String":"","Valid":false},"createdAt":"0001-01-01T00:00:00Z","updatedAt":"0001-01-01T00:00:00Z"}`+"\n")
|
||||
testBody(t, r, `{"id":1,"strainId":1,"observationId":1,"textMeasurementTypeId":{"Int64":0,"Valid":false},"txtValue":{"String":"","Valid":false},"numValue":{"Float64":4.56,"Valid":true},"confidenceInterval":{"Float64":0,"Valid":false},"unitTypeId":{"Int64":1,"Valid":true},"notes":{"String":"","Valid":false},"createdAt":"0001-01-01T00:00:00Z","updatedAt":"0001-01-01T00:00:00Z"}`+"\n")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
writeJSON(w, want)
|
||||
})
|
||||
|
||||
measurement := newMeasurement()
|
||||
measurement.MeasurementValue = sql.NullFloat64{Float64: 4.56, Valid: true}
|
||||
measurement.NumValue = sql.NullFloat64{Float64: 4.56, Valid: true}
|
||||
updated, err := client.Measurements.Update(measurement.Id, measurement)
|
||||
if err != nil {
|
||||
t.Errorf("Measurements.Update returned error: %v", err)
|
||||
|
|
Reference in a new issue