From fb3b9f7c458771301812cefb043f9b1ae682fd7f Mon Sep 17 00:00:00 2001 From: Jamie Date: Tue, 7 Nov 2023 18:43:58 +1300 Subject: [PATCH] just boluses --- data/types/settings/pump/bolus.go | 8 ++++---- data/types/settings/pump/pump.go | 13 ++----------- data/types/settings/pump/pump_test.go | 22 ++++++++++++---------- data/types/settings/pump/test/bolus.go | 8 +++++--- data/types/settings/pump/test/pump.go | 4 ++-- 5 files changed, 25 insertions(+), 30 deletions(-) diff --git a/data/types/settings/pump/bolus.go b/data/types/settings/pump/bolus.go index aa8201394..629d5964f 100644 --- a/data/types/settings/pump/bolus.go +++ b/data/types/settings/pump/bolus.go @@ -88,11 +88,11 @@ func (b *BolusMap) Normalize(normalizer data.Normalizer) { func (b *BolusMap) Validate(validator structure.Validator) { for _, name := range b.sortedNames() { - datumArrayValidator := validator.WithReference(name) - if datumArray := b.Get(name); datumArray != nil { - datumArray.Validate(datumArrayValidator) + datumValidator := validator.WithReference(name) + if datum := b.Get(name); datum != nil { + datum.Validate(datumValidator) } else { - datumArrayValidator.ReportError(structureValidator.ErrorValueNotExists()) + datumValidator.ReportError(structureValidator.ErrorValueNotExists()) } } } diff --git a/data/types/settings/pump/pump.go b/data/types/settings/pump/pump.go index 5f3ca91e2..bbd65d2be 100644 --- a/data/types/settings/pump/pump.go +++ b/data/types/settings/pump/pump.go @@ -41,7 +41,6 @@ type Pump struct { BloodGlucoseTargetPreprandial *dataBloodGlucose.Target `json:"bgTargetPreprandial,omitempty" bson:"bgTargetPreprandial,omitempty"` BloodGlucoseTargetSchedule *BloodGlucoseTargetStartArray `json:"bgTarget,omitempty" bson:"bgTarget,omitempty"` // TODO: Move into BolusCalculator struct; rename bloodGlucoseTarget BloodGlucoseTargetSchedules *BloodGlucoseTargetStartArrayMap `json:"bgTargets,omitempty" bson:"bgTargets,omitempty"` // TODO: Move into BolusCalculator struct; rename bloodGlucoseTargets - Bolus *Bolus `json:"bolus,omitempty" bson:"bolus,omitempty"` Boluses *BolusMap `json:"boluses,omitempty" bson:"boluses,omitempty"` CarbohydrateRatioSchedule *CarbohydrateRatioStartArray `json:"carbRatio,omitempty" bson:"carbRatio,omitempty"` // TODO: Move into BolusCalculator struct; rename carbohydrateRatio CarbohydrateRatioSchedules *CarbohydrateRatioStartArrayMap `json:"carbRatios,omitempty" bson:"carbRatios,omitempty"` // TODO: Move into BolusCalculator struct; rename carbohydrateRatios @@ -85,7 +84,6 @@ func (p *Pump) Parse(parser structure.ObjectParser) { p.BloodGlucoseTargetPreprandial = dataBloodGlucose.ParseTarget(parser.WithReferenceObjectParser("bgTargetPreprandial")) p.BloodGlucoseTargetSchedule = ParseBloodGlucoseTargetStartArray(parser.WithReferenceArrayParser("bgTarget")) p.BloodGlucoseTargetSchedules = ParseBloodGlucoseTargetStartArrayMap(parser.WithReferenceObjectParser("bgTargets")) - p.Bolus = ParseBolus(parser.WithReferenceObjectParser("bolus")) p.Boluses = ParseBolusMap(parser.WithReferenceObjectParser("boluses")) p.CarbohydrateRatioSchedule = ParseCarbohydrateRatioStartArray(parser.WithReferenceArrayParser("carbRatio")) p.CarbohydrateRatioSchedules = ParseCarbohydrateRatioStartArrayMap(parser.WithReferenceObjectParser("carbRatios")) @@ -149,12 +147,8 @@ func (p *Pump) Validate(validator structure.Validator) { } else if p.BloodGlucoseTargetSchedules != nil { p.BloodGlucoseTargetSchedules.Validate(validator.WithReference("bgTargets"), unitsBloodGlucose) } - if p.Bolus != nil { - p.Bolus.Validate(validator.WithReference("bolus")) - if p.Boluses != nil { - validator.WithReference("boluses").ReportError(structureValidator.ErrorValueExists()) - } - } else if p.Boluses != nil { + + if p.Boluses != nil { p.Boluses.Validate(validator.WithReference("boluses")) } @@ -237,9 +231,6 @@ func (p *Pump) Normalize(normalizer data.Normalizer) { if p.BloodGlucoseTargetSchedules != nil { p.BloodGlucoseTargetSchedules.Normalize(normalizer.WithReference("bgTargets"), unitsBloodGlucose) } - if p.Bolus != nil { - p.Bolus.Normalize(normalizer.WithReference("bolus")) - } if p.Boluses != nil { p.Boluses.Normalize(normalizer.WithReference("boluses")) } diff --git a/data/types/settings/pump/pump_test.go b/data/types/settings/pump/pump_test.go index 17d3bec84..863e6ed85 100644 --- a/data/types/settings/pump/pump_test.go +++ b/data/types/settings/pump/pump_test.go @@ -1,6 +1,7 @@ package pump_test import ( + "fmt" "sort" . "github.com/onsi/ginkgo/v2" @@ -42,7 +43,7 @@ var _ = Describe("Pump", func() { Expect(datum.BloodGlucoseTargetPreprandial).To(BeNil()) Expect(datum.BloodGlucoseTargetSchedule).To(BeNil()) Expect(datum.BloodGlucoseTargetSchedules).To(BeNil()) - Expect(datum.Bolus).To(BeNil()) + Expect(datum.Boluses).To(BeNil()) Expect(datum.CarbohydrateRatioSchedule).To(BeNil()) Expect(datum.CarbohydrateRatioSchedules).To(BeNil()) Expect(datum.Display).To(BeNil()) @@ -276,18 +277,21 @@ var _ = Describe("Pump", func() { datum.BloodGlucoseTargetSchedules.Set("one", pumpTest.RandomBloodGlucoseTargetStartArray(unitsBloodGlucose)) }, ), - Entry("bolus missing", + Entry("boluses missing", pointer.FromString("mmol/L"), - func(datum *pump.Pump, unitsBloodGlucose *string) { datum.Bolus = nil }, + func(datum *pump.Pump, unitsBloodGlucose *string) { datum.Boluses = nil }, ), - Entry("bolus invalid", + Entry("boluses invalid", pointer.FromString("mmol/L"), - func(datum *pump.Pump, unitsBloodGlucose *string) { datum.Bolus.Extended.Enabled = nil }, - errorsTest.WithPointerSourceAndMeta(structureValidator.ErrorValueNotExists(), "/bolus/extended/enabled", pumpTest.NewMeta()), + func(datum *pump.Pump, unitsBloodGlucose *string) { + datum.Boluses = pumpTest.NewRandomBolusMap(2, 2) + (*datum.Boluses)[pumpTest.BolusName(1)].Extended.Enabled = nil + }, + errorsTest.WithPointerSourceAndMeta(structureValidator.ErrorValueNotExists(), fmt.Sprintf("/boluses/%s/extended/enabled", pumpTest.BolusName(1)), pumpTest.NewMeta()), ), - Entry("bolus valid", + Entry("boluses valid", pointer.FromString("mmol/L"), - func(datum *pump.Pump, unitsBloodGlucose *string) { datum.Bolus = pumpTest.NewRandomBolus() }, + func(datum *pump.Pump, unitsBloodGlucose *string) { datum.Boluses = pumpTest.NewRandomBolusMap(1, 5) }, ), Entry("carbohydrate ratio schedule and carbohydrate ratio schedules missing", pointer.FromString("mmol/L"), @@ -673,7 +677,6 @@ var _ = Describe("Pump", func() { datum.BloodGlucoseTargetPhysicalActivity = dataBloodGlucose.NewTarget() datum.BloodGlucoseTargetPreprandial = dataBloodGlucose.NewTarget() datum.BloodGlucoseTargetSchedules = nil - datum.Bolus.Extended.Enabled = nil invalidCarbohydrateRatioSchedule := pumpTest.NewCarbohydrateRatioStartArray() (*invalidCarbohydrateRatioSchedule)[0].Start = nil datum.CarbohydrateRatioSchedule = invalidCarbohydrateRatioSchedule @@ -704,7 +707,6 @@ var _ = Describe("Pump", func() { errorsTest.WithPointerSourceAndMeta(structureValidator.ErrorValueNotExists(), "/bgTargetPhysicalActivity/target", &dataTypes.Meta{Type: "invalidType"}), errorsTest.WithPointerSourceAndMeta(structureValidator.ErrorValueNotExists(), "/bgTargetPreprandial/target", &dataTypes.Meta{Type: "invalidType"}), errorsTest.WithPointerSourceAndMeta(structureValidator.ErrorValueNotExists(), "/bgTarget/0/start", &dataTypes.Meta{Type: "invalidType"}), - errorsTest.WithPointerSourceAndMeta(structureValidator.ErrorValueNotExists(), "/bolus/extended/enabled", &dataTypes.Meta{Type: "invalidType"}), errorsTest.WithPointerSourceAndMeta(structureValidator.ErrorValueNotExists(), "/carbRatio/0/start", &dataTypes.Meta{Type: "invalidType"}), errorsTest.WithPointerSourceAndMeta(structureValidator.ErrorValueNotExists(), "/display/bloodGlucose/units", &dataTypes.Meta{Type: "invalidType"}), errorsTest.WithPointerSourceAndMeta(structureValidator.ErrorValueEmpty(), "/firmwareVersion", &dataTypes.Meta{Type: "invalidType"}), diff --git a/data/types/settings/pump/test/bolus.go b/data/types/settings/pump/test/bolus.go index 6bade153a..526cdfd4d 100644 --- a/data/types/settings/pump/test/bolus.go +++ b/data/types/settings/pump/test/bolus.go @@ -1,6 +1,8 @@ package test import ( + "fmt" + "github.com/tidepool-org/platform/data/types/settings/pump" "github.com/tidepool-org/platform/test" ) @@ -22,14 +24,14 @@ func CloneBolus(datum *pump.Bolus) *pump.Bolus { return clone } -func RandomBolusName() string { - return test.RandomStringFromRange(1, 20) +func BolusName(index int) string { + return fmt.Sprintf("bolus-%d", index) } func NewRandomBolusMap(minimumLength int, maximumLength int) *pump.BolusMap { datum := pump.NewBolusMap() for count := test.RandomIntFromRange(minimumLength, maximumLength); count > 0; count-- { - datum.Set(RandomBolusName(), NewRandomBolus()) + datum.Set(BolusName(count), NewRandomBolus()) } return datum } diff --git a/data/types/settings/pump/test/pump.go b/data/types/settings/pump/test/pump.go index 604286551..903628b52 100644 --- a/data/types/settings/pump/test/pump.go +++ b/data/types/settings/pump/test/pump.go @@ -48,7 +48,7 @@ func NewPump(unitsBloodGlucose *string) *pump.Pump { datum.BloodGlucoseTargetPreprandial = dataBloodGlucoseTest.RandomTarget(unitsBloodGlucose) datum.BloodGlucoseTargetSchedules = pump.NewBloodGlucoseTargetStartArrayMap() datum.BloodGlucoseTargetSchedules.Set(scheduleName, RandomBloodGlucoseTargetStartArray(unitsBloodGlucose)) - datum.Bolus = NewRandomBolus() + datum.Boluses = NewRandomBolusMap(2, 4) datum.CarbohydrateRatioSchedules = pump.NewCarbohydrateRatioStartArrayMap() datum.CarbohydrateRatioSchedules.Set(scheduleName, NewCarbohydrateRatioStartArray()) datum.Display = NewDisplay() @@ -85,7 +85,7 @@ func ClonePump(datum *pump.Pump) *pump.Pump { clone.BloodGlucoseTargetPreprandial = dataBloodGlucoseTest.CloneTarget(datum.BloodGlucoseTargetPreprandial) clone.BloodGlucoseTargetSchedule = CloneBloodGlucoseTargetStartArray(datum.BloodGlucoseTargetSchedule) clone.BloodGlucoseTargetSchedules = CloneBloodGlucoseTargetStartArrayMap(datum.BloodGlucoseTargetSchedules) - clone.Bolus = CloneBolus(datum.Bolus) + clone.Boluses = CloneBolusMap(datum.Boluses) clone.CarbohydrateRatioSchedule = CloneCarbohydrateRatioStartArray(datum.CarbohydrateRatioSchedule) clone.CarbohydrateRatioSchedules = CloneCarbohydrateRatioStartArrayMap(datum.CarbohydrateRatioSchedules) clone.Display = CloneDisplay(datum.Display)