Skip to content

Commit f8811e4

Browse files
authored
Merge pull request #750 from tidepool-org/tk/update-guardrails
[BACK-3039] [BACK-3110] Validate basal rate, ISF, Carb Ratio and Correction Range maximum number of segments
2 parents 7703f6e + 4f3cdf7 commit f8811e4

38 files changed

+799
-684
lines changed

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ require (
2222
github.com/prometheus/client_golang v1.19.1
2323
github.com/rinchsan/device-check-go v1.3.0
2424
github.com/tidepool-org/clinic/client v0.0.0-20240802193352-3f912afe2109
25-
github.com/tidepool-org/devices/api v0.0.0-20240412011010-75b16d8daec0
25+
github.com/tidepool-org/devices/api v0.0.0-20240806072455-2b18f22c9cf5
2626
github.com/tidepool-org/go-common v0.12.2-0.20240612192926-de6d5c5a742c
2727
github.com/tidepool-org/hydrophone/client v0.0.0-20240613043503-6c32828b1ace
2828
github.com/urfave/cli v1.22.15
@@ -35,7 +35,7 @@ require (
3535
golang.org/x/sync v0.7.0
3636
golang.org/x/tools v0.22.0
3737
gonum.org/v1/gonum v0.15.0
38-
google.golang.org/grpc v1.64.0
38+
google.golang.org/grpc v1.65.0
3939
gopkg.in/yaml.v2 v2.4.0
4040
syreclabs.com/go/faker v1.2.3
4141
)

go.sum

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,8 @@ github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsT
178178
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
179179
github.com/tidepool-org/clinic/client v0.0.0-20240802193352-3f912afe2109 h1:AKup7wHFkLcWv13GaKfKQcWjYSvUYilUPcbloP0JXX4=
180180
github.com/tidepool-org/clinic/client v0.0.0-20240802193352-3f912afe2109/go.mod h1:7BpAdFdGJNB3aw/xvCz5XnWjSWRoUtWIX4xcMc4Bsko=
181-
github.com/tidepool-org/devices/api v0.0.0-20240412011010-75b16d8daec0 h1:SnIsHwaJE2kltz9xMfjC+L9SWhQS3w6t+mSIIoFiwag=
182-
github.com/tidepool-org/devices/api v0.0.0-20240412011010-75b16d8daec0/go.mod h1:pu8FmuhxtBbOyswB94Mr15l2heCxY357lVHOVVAEZq8=
181+
github.com/tidepool-org/devices/api v0.0.0-20240806072455-2b18f22c9cf5 h1:1kiZtHhs++yXayRD/Mh/3POLwtmxV99YR2bSCle1Q74=
182+
github.com/tidepool-org/devices/api v0.0.0-20240806072455-2b18f22c9cf5/go.mod h1:xuQ8k0mLR1ZyEmwe/m0v2BuXctqQuCZeR43urSQpTUM=
183183
github.com/tidepool-org/go-common v0.12.2-0.20240612192926-de6d5c5a742c h1:hJZyiHNGeqyLA/5p60/0H9CZtJi4fAuzOuyQF0TpF7E=
184184
github.com/tidepool-org/go-common v0.12.2-0.20240612192926-de6d5c5a742c/go.mod h1:mIzYteUyPf//fhee4e2KEZhmcm2iE4IQ/2dyQr5pRKA=
185185
github.com/tidepool-org/hydrophone/client v0.0.0-20240613043503-6c32828b1ace h1:L0UiCj2eL/NOpLa19Tf5IgoK6feILmdA+zK3nCTIhqU=
@@ -296,8 +296,8 @@ google.golang.org/genproto/googleapis/api v0.0.0-20240624140628-dc46fd24d27d h1:
296296
google.golang.org/genproto/googleapis/api v0.0.0-20240624140628-dc46fd24d27d/go.mod h1:Od4k8V1LQSizPRUK4OzZ7TBE/20k+jPczUDAEyvn69Y=
297297
google.golang.org/genproto/googleapis/rpc v0.0.0-20240624140628-dc46fd24d27d h1:k3zyW3BYYR30e8v3x0bTDdE9vpYFjZHK+HcyqkrppWk=
298298
google.golang.org/genproto/googleapis/rpc v0.0.0-20240624140628-dc46fd24d27d/go.mod h1:Ue6ibwXGpU+dqIcODieyLOcgj7z8+IcskoNIgZxtrFY=
299-
google.golang.org/grpc v1.64.0 h1:KH3VH9y/MgNQg1dE7b3XfVK0GsPSIzJwdF617gUSbvY=
300-
google.golang.org/grpc v1.64.0/go.mod h1:oxjF8E3FBnjp+/gVFYdWacaLDx9na1aqy9oovLpxQYg=
299+
google.golang.org/grpc v1.65.0 h1:bs/cUb4lp1G5iImFFd3u5ixQzweKizoZJAwBNLR42lc=
300+
google.golang.org/grpc v1.65.0/go.mod h1:WgYC2ypjlB0EiQi6wdKixMqukr6lBc0Vo+oOgjrM5ZQ=
301301
google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg=
302302
google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw=
303303
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=

guardrails/basal_rate_schedule.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,18 @@ package guardrails
33
import (
44
devices "github.com/tidepool-org/devices/api"
55

6+
structureValidator "github.com/tidepool-org/platform/structure/validator"
7+
68
"strconv"
79

810
"github.com/tidepool-org/platform/data/types/settings/pump"
911
"github.com/tidepool-org/platform/structure"
1012
)
1113

1214
func ValidateBasalRateSchedule(basalRateSchedule pump.BasalRateStartArray, guardRail *devices.BasalRatesGuardRail, validator structure.Validator) {
15+
if guardRail.MaxSegments != nil && len(basalRateSchedule) > int(*guardRail.MaxSegments) {
16+
validator.ReportError(structureValidator.ErrorLengthNotLessThanOrEqualTo(len(basalRateSchedule), int(*guardRail.MaxSegments)))
17+
}
1318
validValues := generateAllValidValues(guardRail.AbsoluteBounds)
1419
for i, basalRate := range basalRateSchedule {
1520
ValidateValueIfNotNil(basalRate.Rate, validValues, validator.WithReference(strconv.Itoa(i)).WithReference("rate"))

guardrails/basal_rate_schedule_test.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ var _ = Describe("ValidateBasalRateSchedule", func() {
6262
errorsTest.ExpectEqual(validator.Error(), expected)
6363
})
6464

65-
It("returns an error with a value low than the pump min supported value", func() {
65+
It("returns an error with a value lower than the pump min supported value", func() {
6666
var schedule pump.BasalRateStartArray = []*pump.BasalRateStart{
6767
{Rate: pointer.FromFloat64(0.55)},
6868
{Rate: pointer.FromFloat64(0.0)},
@@ -72,4 +72,18 @@ var _ = Describe("ValidateBasalRateSchedule", func() {
7272
guardrails.ValidateBasalRateSchedule(schedule, guardRail, validator)
7373
errorsTest.ExpectEqual(validator.Error(), expected)
7474
})
75+
76+
It("returns an error when the number of segments is higher than the guardrail", func() {
77+
maxSegments := int32(2)
78+
guardRail.MaxSegments = &maxSegments
79+
var schedule pump.BasalRateStartArray = []*pump.BasalRateStart{
80+
{Rate: pointer.FromFloat64(0.55)},
81+
{Rate: pointer.FromFloat64(15.55)},
82+
{Rate: pointer.FromFloat64(16.55)},
83+
}
84+
85+
expected := errorsTest.WithPointerSource(structureValidator.ErrorLengthNotLessThanOrEqualTo(3, 2), "")
86+
guardrails.ValidateBasalRateSchedule(schedule, guardRail, validator)
87+
errorsTest.ExpectEqual(validator.Error(), expected)
88+
})
7589
})

guardrails/carb_ratio_schedule.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,18 @@ package guardrails
33
import (
44
devices "github.com/tidepool-org/devices/api"
55

6+
structureValidator "github.com/tidepool-org/platform/structure/validator"
7+
68
"strconv"
79

810
"github.com/tidepool-org/platform/data/types/settings/pump"
911
"github.com/tidepool-org/platform/structure"
1012
)
1113

1214
func ValidateCarbohydrateRatioSchedule(carbohydrateRatioSchedule pump.CarbohydrateRatioStartArray, guardRail *devices.CarbohydrateRatioGuardRail, validator structure.Validator) {
15+
if guardRail.MaxSegments != nil && len(carbohydrateRatioSchedule) > int(*guardRail.MaxSegments) {
16+
validator.ReportError(structureValidator.ErrorLengthNotLessThanOrEqualTo(len(carbohydrateRatioSchedule), int(*guardRail.MaxSegments)))
17+
}
1318
validValues := generateValidValuesFromAbsoluteBounds(guardRail.AbsoluteBounds)
1419
for i, carbRatio := range carbohydrateRatioSchedule {
1520
ValidateValueIfNotNil(carbRatio.Amount, validValues, validator.WithReference(strconv.Itoa(i)).WithReference("amount"))

guardrails/carb_ratio_schedule_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,17 @@ var _ = Describe("ValidateCarbohydrateRatioSchedule", func() {
5050
guardrails.ValidateCarbohydrateRatioSchedule(schedule, guardRail, validator)
5151
errorsTest.ExpectEqual(validator.Error(), expected)
5252
})
53+
54+
It("returns an error when the number of segments is higher than the guardrail", func() {
55+
maxSegments := int32(2)
56+
guardRail.MaxSegments = &maxSegments
57+
var schedule pump.CarbohydrateRatioStartArray = []*pump.CarbohydrateRatioStart{
58+
{Amount: pointer.FromFloat64(120.01)},
59+
{Amount: pointer.FromFloat64(110.01)},
60+
{Amount: pointer.FromFloat64(10.00)},
61+
}
62+
expected := errorsTest.WithPointerSource(structureValidator.ErrorLengthNotLessThanOrEqualTo(3, 2), "")
63+
guardrails.ValidateCarbohydrateRatioSchedule(schedule, guardRail, validator)
64+
errorsTest.ExpectEqual(validator.Error(), expected)
65+
})
5366
})

guardrails/correction_range.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"math"
55
"strconv"
66

7+
structureValidator "github.com/tidepool-org/platform/structure/validator"
8+
79
devices "github.com/tidepool-org/devices/api"
810

911
"github.com/tidepool-org/platform/data/blood/glucose"
@@ -12,6 +14,9 @@ import (
1214
)
1315

1416
func ValidateBloodGlucoseTargetSchedule(bloodGlucoseTargetSchedule pump.BloodGlucoseTargetStartArray, glucoseSafetyLimit *float64, guardRail *devices.CorrectionRangeGuardRail, validator structure.Validator) {
17+
if guardRail.MaxSegments != nil && len(bloodGlucoseTargetSchedule) > int(*guardRail.MaxSegments) {
18+
validator.ReportError(structureValidator.ErrorLengthNotLessThanOrEqualTo(len(bloodGlucoseTargetSchedule), int(*guardRail.MaxSegments)))
19+
}
1520
for i, bloodGlucoseTargetStart := range bloodGlucoseTargetSchedule {
1621
ValidateBloodGlucoseTarget(bloodGlucoseTargetStart.Target, glucoseSafetyLimit, guardRail, validator.WithReference(strconv.Itoa(i)))
1722
}

guardrails/correction_range_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,19 @@ var _ = Describe("Correction Range", func() {
9595
guardrails.ValidateBloodGlucoseTargetSchedule(schedule, glucoseSafetyLimit, guardRail, validator)
9696
errorsTest.ExpectEqual(validator.Error(), expected...)
9797
})
98+
99+
It("returns an error when the number of segments is higher than the guardrail", func() {
100+
maxSegments := int32(2)
101+
guardRail.MaxSegments = &maxSegments
102+
var schedule pump.BloodGlucoseTargetStartArray = []*pump.BloodGlucoseTargetStart{
103+
{Target: glucose.Target{Low: pointer.FromFloat64(90), High: pointer.FromFloat64(91)}},
104+
{Target: glucose.Target{Low: pointer.FromFloat64(100), High: pointer.FromFloat64(105)}},
105+
{Target: glucose.Target{Low: pointer.FromFloat64(101), High: pointer.FromFloat64(104)}},
106+
}
107+
expected := errorsTest.WithPointerSource(structureValidator.ErrorLengthNotLessThanOrEqualTo(3, 2), "")
108+
guardrails.ValidateBloodGlucoseTargetSchedule(schedule, glucoseSafetyLimit, guardRail, validator)
109+
errorsTest.ExpectEqual(validator.Error(), expected)
110+
})
98111
})
99112

100113
Context("With Target values", func() {

guardrails/insulin_sensitivity_schedule.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,18 @@ package guardrails
33
import (
44
devices "github.com/tidepool-org/devices/api"
55

6+
structureValidator "github.com/tidepool-org/platform/structure/validator"
7+
68
"strconv"
79

810
"github.com/tidepool-org/platform/data/types/settings/pump"
911
"github.com/tidepool-org/platform/structure"
1012
)
1113

1214
func ValidateInsulinSensitivitySchedule(insulinSensitivitySchedule pump.InsulinSensitivityStartArray, guardRail *devices.InsulinSensitivityGuardRail, validator structure.Validator) {
15+
if guardRail.MaxSegments != nil && len(insulinSensitivitySchedule) > int(*guardRail.MaxSegments) {
16+
validator.ReportError(structureValidator.ErrorLengthNotLessThanOrEqualTo(len(insulinSensitivitySchedule), int(*guardRail.MaxSegments)))
17+
}
1318
validValues := generateValidValuesFromAbsoluteBounds(guardRail.AbsoluteBounds)
1419
for i, insulinSensitivity := range insulinSensitivitySchedule {
1520
ValidateValueIfNotNil(insulinSensitivity.Amount, validValues, validator.WithReference(strconv.Itoa(i)).WithReference("amount"))

guardrails/insulin_sensitivity_schedule_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,18 @@ var _ = Describe("ValidateInsulinSensitivitySchedule", func() {
5050
guardrails.ValidateInsulinSensitivitySchedule(schedule, guardRail, validator)
5151
errorsTest.ExpectEqual(validator.Error(), expected)
5252
})
53+
54+
It("returns an error when the number of segments is higher than the guardrail", func() {
55+
maxSegments := int32(2)
56+
guardRail.MaxSegments = &maxSegments
57+
var schedule pump.InsulinSensitivityStartArray = []*pump.InsulinSensitivityStart{
58+
{Amount: pointer.FromFloat64(120.00)},
59+
{Amount: pointer.FromFloat64(110.00)},
60+
{Amount: pointer.FromFloat64(10.00)},
61+
}
62+
63+
expected := errorsTest.WithPointerSource(structureValidator.ErrorLengthNotLessThanOrEqualTo(3, 2), "")
64+
guardrails.ValidateInsulinSensitivitySchedule(schedule, guardRail, validator)
65+
errorsTest.ExpectEqual(validator.Error(), expected)
66+
})
5367
})

0 commit comments

Comments
 (0)