Skip to content

Commit

Permalink
just boluses
Browse files Browse the repository at this point in the history
  • Loading branch information
jh-bate committed Nov 7, 2023
1 parent 436c2e8 commit fb3b9f7
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 30 deletions.
8 changes: 4 additions & 4 deletions data/types/settings/pump/bolus.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
}
}
Expand Down
13 changes: 2 additions & 11 deletions data/types/settings/pump/pump.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"))
Expand Down Expand Up @@ -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"))
}

Expand Down Expand Up @@ -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"))
}
Expand Down
22 changes: 12 additions & 10 deletions data/types/settings/pump/pump_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package pump_test

import (
"fmt"
"sort"

. "github.com/onsi/ginkgo/v2"
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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"),
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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"}),
Expand Down
8 changes: 5 additions & 3 deletions data/types/settings/pump/test/bolus.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package test

import (
"fmt"

"github.com/tidepool-org/platform/data/types/settings/pump"
"github.com/tidepool-org/platform/test"
)
Expand All @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions data/types/settings/pump/test/pump.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit fb3b9f7

Please sign in to comment.