Skip to content

Commit 49ad956

Browse files
committed
BACK-3110 Validate maximum number of segments of ISF, Carb Ratio and Correction Range
1 parent 881dba1 commit 49ad956

File tree

11 files changed

+241
-134
lines changed

11 files changed

+241
-134
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ require (
2121
github.com/prometheus/client_golang v1.19.1
2222
github.com/rinchsan/device-check-go v1.3.0
2323
github.com/tidepool-org/clinic/client v0.0.0-20240629034458-1365c8963143
24-
github.com/tidepool-org/devices/api v0.0.0-20240711091104-8b8e20a3e2c7
24+
github.com/tidepool-org/devices/api v0.0.0-20240806072455-2b18f22c9cf5
2525
github.com/tidepool-org/go-common v0.12.2-0.20240612192926-de6d5c5a742c
2626
github.com/tidepool-org/hydrophone/client v0.0.0-20240613043503-6c32828b1ace
2727
github.com/urfave/cli v1.22.15

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,8 @@ github.com/tidepool-org/clinic/client v0.0.0-20240629034458-1365c8963143 h1:A6qF
178178
github.com/tidepool-org/clinic/client v0.0.0-20240629034458-1365c8963143/go.mod h1:7BpAdFdGJNB3aw/xvCz5XnWjSWRoUtWIX4xcMc4Bsko=
179179
github.com/tidepool-org/devices/api v0.0.0-20240711091104-8b8e20a3e2c7 h1:sfE4V+umi8HM8T2RJLuCd/Dfx/Ba0XREXbfTamke+Aw=
180180
github.com/tidepool-org/devices/api v0.0.0-20240711091104-8b8e20a3e2c7/go.mod h1:xuQ8k0mLR1ZyEmwe/m0v2BuXctqQuCZeR43urSQpTUM=
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=
181183
github.com/tidepool-org/go-common v0.12.2-0.20240612192926-de6d5c5a742c h1:hJZyiHNGeqyLA/5p60/0H9CZtJi4fAuzOuyQF0TpF7E=
182184
github.com/tidepool-org/go-common v0.12.2-0.20240612192926-de6d5c5a742c/go.mod h1:mIzYteUyPf//fhee4e2KEZhmcm2iE4IQ/2dyQr5pRKA=
183185
github.com/tidepool-org/hydrophone/client v0.0.0-20240613043503-6c32828b1ace h1:L0UiCj2eL/NOpLa19Tf5IgoK6feILmdA+zK3nCTIhqU=

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)