From a4947a53e9c79397ad10f51e3aa1efd55e9cbb10 Mon Sep 17 00:00:00 2001 From: Sonja Happ Date: Mon, 3 Jan 2022 14:19:12 +0100 Subject: [PATCH] allow signal indices to start at 0 instead of 1 --- database/testdata.json | 14 +++++++------- routes/signal/signal_test.go | 18 +++++++++++------- routes/signal/signal_validators.go | 21 ++++++--------------- routes/user/user_test.go | 8 +++++--- 4 files changed, 29 insertions(+), 32 deletions(-) diff --git a/database/testdata.json b/database/testdata.json index de7eb81..9521cca 100644 --- a/database/testdata.json +++ b/database/testdata.json @@ -353,43 +353,43 @@ "name": "outSignal_A", "direction": "out", "unit": "V", - "index": 1 + "index": 0 }, { "name": "outSignal_B", "direction": "out", "unit": "V", - "index": 2 + "index": 1 }, { "name": "outSignal_C", "direction": "out", "unit": "---", - "index": 3 + "index": 2 }, { "name": "outSignal_D", "direction": "out", "unit": "---", - "index": 4 + "index": 3 }, { "name": "outSignal_E", "direction": "out", "unit": "---", - "index": 5 + "index": 4 }, { "name": "inSignal_A", "direction": "in", "unit": "---", - "index": 1 + "index": 0 }, { "name": "inSignal_B", "direction": "in", "unit": "---", - "index": 2 + "index": 1 } ] diff --git a/routes/signal/signal_test.go b/routes/signal/signal_test.go index aef71b1..31ca89e 100644 --- a/routes/signal/signal_test.go +++ b/routes/signal/signal_test.go @@ -44,7 +44,7 @@ var router *gin.Engine type SignalRequest struct { Name string `json:"name,omitempty"` Unit string `json:"unit,omitempty"` - Index uint `json:"index,omitempty"` + Index *uint `json:"index,omitempty"` Direction string `json:"direction,omitempty"` ScalingFactor float32 `json:"scalingFactor,omitempty"` ConfigID uint `json:"configID,omitempty"` @@ -76,11 +76,14 @@ type ScenarioRequest struct { StartParameters postgres.Jsonb `json:"startParameters,omitempty"` } +var signalIndex0 uint = 0 +var signalIndex1 uint = 1 + var newSignal1 = SignalRequest{ Name: "outSignal_A", Unit: "V", Direction: "out", - Index: 1, + Index: &signalIndex0, } func newFalse() *bool { @@ -226,6 +229,7 @@ func TestAddSignal(t *testing.T) { "/api/v2/signals", "POST", helper.KeyModels{"signal": newSignal1}) assert.NoError(t, err) assert.Equalf(t, 200, code, "Response body: \n%v\n", resp) + fmt.Println(resp) // Compare POST's response with the newSignal err = helper.CompareResponse(resp, helper.KeyModels{"signal": newSignal1}) @@ -296,7 +300,7 @@ func TestUpdateSignal(t *testing.T) { updatedSignal := SignalRequest{ Name: "outSignal_B", Unit: "A", - Index: 1, + Index: &signalIndex1, } // authenticate as normal userB who has no access to new scenario @@ -379,7 +383,7 @@ func TestDeleteSignal(t *testing.T) { Name: "insignalA", Unit: "s", Direction: "in", - Index: 1, + Index: &signalIndex0, ConfigID: configID, } code, resp, err := helper.TestEndpoint(router, token, @@ -470,7 +474,7 @@ func TestGetAllInputSignalsOfConfig(t *testing.T) { Name: "inA", Unit: "s", Direction: "in", - Index: 1, + Index: &signalIndex0, ConfigID: configID, } code, resp, err := helper.TestEndpoint(router, token, @@ -483,7 +487,7 @@ func TestGetAllInputSignalsOfConfig(t *testing.T) { Name: "inB", Unit: "m", Direction: "in", - Index: 2, + Index: &signalIndex1, ConfigID: configID, } code, resp, err = helper.TestEndpoint(router, token, @@ -503,7 +507,7 @@ func TestGetAllInputSignalsOfConfig(t *testing.T) { Name: "outB", Unit: "A", Direction: "out", - Index: 1, + Index: &signalIndex1, ConfigID: configID, } code, resp, err = helper.TestEndpoint(router, token, diff --git a/routes/signal/signal_validators.go b/routes/signal/signal_validators.go index 7bd57e5..2417674 100644 --- a/routes/signal/signal_validators.go +++ b/routes/signal/signal_validators.go @@ -30,7 +30,7 @@ var validate *validator.Validate type validNewSignal struct { Name string `form:"Name" validate:"required"` Unit string `form:"unit" validate:"omitempty"` - Index uint `form:"index" validate:"required"` + Index *uint `form:"index" validate:"required"` Direction string `form:"direction" validate:"required,oneof=in out"` ScalingFactor float32 `form:"scalingFactor" validate:"omitempty"` ConfigID uint `form:"configID" validate:"required"` @@ -39,7 +39,7 @@ type validNewSignal struct { type validUpdatedSignal struct { Name string `form:"Name" validate:"omitempty"` Unit string `form:"unit" validate:"omitempty"` - Index uint `form:"index" validate:"omitempty"` + Index *uint `form:"index" validate:"omitempty"` ScalingFactor float32 `form:"scalingFactor" validate:"omitempty"` } @@ -68,7 +68,7 @@ func (r *addSignalRequest) createSignal() Signal { s.Name = r.Signal.Name s.Unit = r.Signal.Unit - s.Index = r.Signal.Index + s.Index = *r.Signal.Index s.Direction = r.Signal.Direction s.ScalingFactor = r.Signal.ScalingFactor s.ConfigID = r.Signal.ConfigID @@ -80,18 +80,9 @@ func (r *updateSignalRequest) updatedSignal(oldSignal Signal) Signal { // Use the old Signal as a basis for the updated Signal `s` s := oldSignal - if r.Signal.Name != "" { - s.Name = r.Signal.Name - } - - if r.Signal.Index != 0 { - // TODO this implies that we start indexing at 1 - s.Index = r.Signal.Index - } - - if r.Signal.Unit != "" { - s.Unit = r.Signal.Unit - } + s.Name = r.Signal.Name + s.Index = *r.Signal.Index + s.Unit = r.Signal.Unit if r.Signal.ScalingFactor != 0 { // scaling factor of 0 is not allowed diff --git a/routes/user/user_test.go b/routes/user/user_test.go index cf2abe6..22ea944 100644 --- a/routes/user/user_test.go +++ b/routes/user/user_test.go @@ -1586,17 +1586,19 @@ func addSignals(token string) error { type SignalRequest struct { Name string `json:"name,omitempty"` Unit string `json:"unit,omitempty"` - Index uint `json:"index,omitempty"` + Index *uint `json:"index,omitempty"` Direction string `json:"direction,omitempty"` ScalingFactor float32 `json:"scalingFactor,omitempty"` ConfigID uint `json:"configID,omitempty"` } + var signalIndex0 uint = 0 + var signalIndex1 uint = 1 var newSignalOut = SignalRequest{ Name: "outSignal_A", Unit: "V", Direction: "out", - Index: 1, + Index: &signalIndex0, ConfigID: 1, } @@ -1604,7 +1606,7 @@ func addSignals(token string) error { Name: "inSignal_A", Unit: "V", Direction: "in", - Index: 2, + Index: &signalIndex1, ConfigID: 1, }