Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BACK-37][BACK-2520][BACK-2728] Jellyfish migration updates required for platfrom #683

Closed
wants to merge 15 commits into from
Closed
11 changes: 11 additions & 0 deletions data/types/common/common_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package common_test

import (
"testing"

"github.com/tidepool-org/platform/test"
)

func TestSuite(t *testing.T) {
test.Test(t)
}
57 changes: 57 additions & 0 deletions data/types/common/day.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package common

const (
DaySunday = "sunday"
DayMonday = "monday"
DayTuesday = "tuesday"
DayWednesday = "wednesday"
DayThursday = "thursday"
DayFriday = "friday"
DaySaturday = "saturday"
)

func DaysOfWeek() []string {
return []string{
DaySunday,
DayMonday,
DayTuesday,
DayWednesday,
DayThursday,
DayFriday,
DaySaturday,
}
}

type DaysOfWeekByDayIndex []string

func (d DaysOfWeekByDayIndex) Len() int {
return len(d)
}
func (d DaysOfWeekByDayIndex) Swap(i int, j int) {
d[i], d[j] = d[j], d[i]
}

func (d DaysOfWeekByDayIndex) Less(i int, j int) bool {
return DayIndex(d[i]) < DayIndex(d[j])
}

func DayIndex(day string) int {
switch day {
case DaySunday:
return 1
case DayMonday:
return 2
case DayTuesday:
return 3
case DayWednesday:
return 4
case DayThursday:
return 5
case DayFriday:
return 6
case DaySaturday:
return 7
default:
return 0
}
}
76 changes: 76 additions & 0 deletions data/types/common/day_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package common_test

import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

"github.com/tidepool-org/platform/data/types/common"
)

var _ = Describe("Day", func() {

It("DaySunday is expected", func() {
Expect(common.DaySunday).To(Equal("sunday"))
})

It("DayMonday is expected", func() {
Expect(common.DayMonday).To(Equal("monday"))
})

It("DayTuesday is expected", func() {
Expect(common.DayTuesday).To(Equal("tuesday"))
})

It("DayWednesday is expected", func() {
Expect(common.DayWednesday).To(Equal("wednesday"))
})

It("DayThursday is expected", func() {
Expect(common.DayThursday).To(Equal("thursday"))
})

It("DayFriday is expected", func() {
Expect(common.DayFriday).To(Equal("friday"))
})

It("DaySaturday is expected", func() {
Expect(common.DaySaturday).To(Equal("saturday"))
})

It("DaysOfWeek returns expected", func() {
Expect(common.DaysOfWeek()).To(Equal([]string{"sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday"}))
Expect(common.DaysOfWeek()).To(Equal([]string{
common.DaySunday,
common.DayMonday,
common.DayTuesday,
common.DayWednesday,
common.DayThursday,
common.DayFriday,
common.DaySaturday,
}))
})

Context("DayIndex", func() {
DescribeTable("return the expected index when the day",
func(day string, expectedIndex int) {
Expect(common.DayIndex(day)).To(Equal(expectedIndex))
},
Entry("is an empty string", "", 0),
Entry("is sunday", "sunday", 1),
Entry("is constant sunday", common.DaySunday, 1),
Entry("is monday", "monday", 2),
Entry("is constant monday", common.DayMonday, 2),
Entry("is tuesday", "tuesday", 3),
Entry("is constant tuesday", common.DayTuesday, 3),
Entry("is wednesday", "wednesday", 4),
Entry("isconstant wednesday", common.DayWednesday, 4),
Entry("is thursday", "thursday", 5),
Entry("is constant thursday", common.DayThursday, 5),
Entry("is friday", "friday", 6),
Entry("is constant friday", common.DayFriday, 6),
Entry("is saturday", "saturday", 7),
Entry("is constant saturday", common.DaySaturday, 7),
Entry("is an invalid string", "invalid", 0),
)
})
})
23 changes: 2 additions & 21 deletions data/types/settings/cgm/scheduled_alert.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cgm
import (
"strconv"

"github.com/tidepool-org/platform/data/types/common"
"github.com/tidepool-org/platform/structure"
structureValidator "github.com/tidepool-org/platform/structure/validator"
)
Expand All @@ -12,33 +13,13 @@ const (

ScheduledAlertNameLengthMaximum = 100

ScheduledAlertDaysSunday = "sunday"
ScheduledAlertDaysMonday = "monday"
ScheduledAlertDaysTuesday = "tuesday"
ScheduledAlertDaysWednesday = "wednesday"
ScheduledAlertDaysThursday = "thursday"
ScheduledAlertDaysFriday = "friday"
ScheduledAlertDaysSaturday = "saturday"

ScheduledAlertStartMaximum = 86400000
ScheduledAlertStartMinimum = 0

ScheduledAlertEndMaximum = 86400000
ScheduledAlertEndMinimum = 0
)

func ScheduledAlertDays() []string {
return []string{
ScheduledAlertDaysSunday,
ScheduledAlertDaysMonday,
ScheduledAlertDaysTuesday,
ScheduledAlertDaysWednesday,
ScheduledAlertDaysThursday,
ScheduledAlertDaysFriday,
ScheduledAlertDaysSaturday,
}
}

type ScheduledAlerts []*ScheduledAlert

func ParseScheduledAlerts(parser structure.ArrayParser) *ScheduledAlerts {
Expand Down Expand Up @@ -107,7 +88,7 @@ func (s *ScheduledAlert) Parse(parser structure.ObjectParser) {

func (s *ScheduledAlert) Validate(validator structure.Validator) {
validator.String("name", s.Name).NotEmpty().LengthLessThanOrEqualTo(ScheduledAlertNameLengthMaximum)
validator.StringArray("days", s.Days).Exists().EachOneOf(ScheduledAlertDays()...).EachUnique()
validator.StringArray("days", s.Days).Exists().EachOneOf(common.DaysOfWeek()...).EachUnique()
validator.Int("start", s.Start).Exists().InRange(ScheduledAlertStartMinimum, ScheduledAlertStartMaximum)
validator.Int("end", s.End).Exists().InRange(ScheduledAlertEndMinimum, ScheduledAlertEndMaximum)
if alertsValidator := validator.WithReference("alerts"); s.Alerts != nil {
Expand Down
40 changes: 5 additions & 35 deletions data/types/settings/cgm/scheduled_alert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
. "github.com/onsi/gomega"

dataTypesSettingsCgm "github.com/tidepool-org/platform/data/types/settings/cgm"

dataTypesCommon "github.com/tidepool-org/platform/data/types/common"
dataTypesSettingsCgmTest "github.com/tidepool-org/platform/data/types/settings/cgm/test"
errorsTest "github.com/tidepool-org/platform/errors/test"
"github.com/tidepool-org/platform/pointer"
Expand All @@ -21,34 +23,6 @@ var _ = Describe("ScheduledAlert", func() {
Expect(dataTypesSettingsCgm.ScheduledAlertNameLengthMaximum).To(Equal(100))
})

It("ScheduledAlertDaysSunday is expected", func() {
Expect(dataTypesSettingsCgm.ScheduledAlertDaysSunday).To(Equal("sunday"))
})

It("ScheduledAlertDaysMonday is expected", func() {
Expect(dataTypesSettingsCgm.ScheduledAlertDaysMonday).To(Equal("monday"))
})

It("ScheduledAlertDaysTuesday is expected", func() {
Expect(dataTypesSettingsCgm.ScheduledAlertDaysTuesday).To(Equal("tuesday"))
})

It("ScheduledAlertDaysWednesday is expected", func() {
Expect(dataTypesSettingsCgm.ScheduledAlertDaysWednesday).To(Equal("wednesday"))
})

It("ScheduledAlertDaysThursday is expected", func() {
Expect(dataTypesSettingsCgm.ScheduledAlertDaysThursday).To(Equal("thursday"))
})

It("ScheduledAlertDaysFriday is expected", func() {
Expect(dataTypesSettingsCgm.ScheduledAlertDaysFriday).To(Equal("friday"))
})

It("ScheduledAlertDaysSaturday is expected", func() {
Expect(dataTypesSettingsCgm.ScheduledAlertDaysSaturday).To(Equal("saturday"))
})

It("ScheduledAlertStartMaximum is expected", func() {
Expect(dataTypesSettingsCgm.ScheduledAlertStartMaximum).To(Equal(86400000))
})
Expand All @@ -65,10 +39,6 @@ var _ = Describe("ScheduledAlert", func() {
Expect(dataTypesSettingsCgm.ScheduledAlertEndMinimum).To(Equal(0))
})

It("ScheduledAlertDays returns expected", func() {
Expect(dataTypesSettingsCgm.ScheduledAlertDays()).To(Equal([]string{"sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday"}))
})

Context("ParseScheduledAlerts", func() {
// TODO
})
Expand Down Expand Up @@ -204,20 +174,20 @@ var _ = Describe("ScheduledAlert", func() {
),
Entry("days contains invalid",
func(datum *dataTypesSettingsCgm.ScheduledAlert) {
datum.Days = pointer.FromStringArray(append([]string{"invalid"}, test.RandomStringArrayFromRangeAndArrayWithoutDuplicates(0, len(dataTypesSettingsCgm.ScheduledAlertDays())-1, dataTypesSettingsCgm.ScheduledAlertDays())...))
datum.Days = pointer.FromStringArray(append([]string{"invalid"}, test.RandomStringArrayFromRangeAndArrayWithoutDuplicates(0, len(dataTypesCommon.DaysOfWeek())-1, dataTypesCommon.DaysOfWeek())...))
},
errorsTest.WithPointerSource(structureValidator.ErrorValueStringNotOneOf("invalid", []string{"sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday"}), "/days/0"),
),
Entry("days contains duplicate",
func(datum *dataTypesSettingsCgm.ScheduledAlert) {
duplicate := test.RandomStringFromArray(dataTypesSettingsCgm.ScheduledAlertDays())
duplicate := test.RandomStringFromArray(dataTypesCommon.DaysOfWeek())
datum.Days = pointer.FromStringArray([]string{duplicate, duplicate})
},
errorsTest.WithPointerSource(structureValidator.ErrorValueDuplicate(), "/days/1"),
),
Entry("days valid",
func(datum *dataTypesSettingsCgm.ScheduledAlert) {
datum.Days = pointer.FromStringArray(test.RandomStringArrayFromRangeAndArrayWithoutDuplicates(1, len(dataTypesSettingsCgm.ScheduledAlertDays()), dataTypesSettingsCgm.ScheduledAlertDays()))
datum.Days = pointer.FromStringArray(test.RandomStringArrayFromRangeAndArrayWithoutDuplicates(1, len(dataTypesCommon.DaysOfWeek()), dataTypesCommon.DaysOfWeek()))
},
),
Entry("start missing",
Expand Down
3 changes: 2 additions & 1 deletion data/types/settings/cgm/test/scheduled_alert.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package test

import (
"github.com/tidepool-org/platform/data/types/common"
dataTypesSettingsCgm "github.com/tidepool-org/platform/data/types/settings/cgm"
"github.com/tidepool-org/platform/pointer"
"github.com/tidepool-org/platform/test"
Expand Down Expand Up @@ -39,7 +40,7 @@ func NewArrayFromScheduledAlerts(datum *dataTypesSettingsCgm.ScheduledAlerts, ob
func RandomScheduledAlert() *dataTypesSettingsCgm.ScheduledAlert {
datum := dataTypesSettingsCgm.NewScheduledAlert()
datum.Name = pointer.FromString(test.RandomStringFromRange(1, dataTypesSettingsCgm.ScheduledAlertNameLengthMaximum))
datum.Days = pointer.FromStringArray(test.RandomStringArrayFromRangeAndArrayWithoutDuplicates(1, len(dataTypesSettingsCgm.ScheduledAlertDays()), dataTypesSettingsCgm.ScheduledAlertDays()))
datum.Days = pointer.FromStringArray(test.RandomStringArrayFromRangeAndArrayWithoutDuplicates(1, len(common.DaysOfWeek()), common.DaysOfWeek()))
datum.Start = pointer.FromInt(test.RandomIntFromRange(dataTypesSettingsCgm.ScheduledAlertStartMinimum, dataTypesSettingsCgm.ScheduledAlertStartMaximum))
datum.End = pointer.FromInt(test.RandomIntFromRange(dataTypesSettingsCgm.ScheduledAlertEndMinimum, dataTypesSettingsCgm.ScheduledAlertEndMaximum))
datum.Alerts = RandomAlerts()
Expand Down
63 changes: 63 additions & 0 deletions data/types/settings/pump/bolus.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package pump

import (
"sort"

"github.com/tidepool-org/platform/data"
"github.com/tidepool-org/platform/structure"
structureValidator "github.com/tidepool-org/platform/structure/validator"
)

type Bolus struct {
Expand Down Expand Up @@ -53,3 +56,63 @@ func (b *Bolus) Normalize(normalizer data.Normalizer) {
b.Extended.Normalize(normalizer.WithReference("extended"))
}
}

type BolusMap map[string]*Bolus

func ParseBolusMap(parser structure.ObjectParser) *BolusMap {
if !parser.Exists() {
return nil
}
datum := NewBolusMap()
parser.Parse(datum)
return datum
}

func NewBolusMap() *BolusMap {
return &BolusMap{}
}

func (b *BolusMap) Parse(parser structure.ObjectParser) {
for _, reference := range parser.References() {
b.Set(reference, ParseBolus(parser.WithReferenceObjectParser(reference)))
}
}

func (b *BolusMap) Normalize(normalizer data.Normalizer) {
for _, name := range b.sortedNames() {
if datum := b.Get(name); datum != nil {
datum.Normalize(normalizer.WithReference(name))
}
}
}

func (b *BolusMap) Validate(validator structure.Validator) {
for _, name := range b.sortedNames() {
datumValidator := validator.WithReference(name)
if datum := b.Get(name); datum != nil {
datum.Validate(datumValidator)
} else {
datumValidator.ReportError(structureValidator.ErrorValueNotExists())
}
}
}

func (b *BolusMap) Get(name string) *Bolus {
if datumArray, exists := (*b)[name]; exists {
return datumArray
}
return nil
}

func (b *BolusMap) Set(name string, datum *Bolus) {
(*b)[name] = datum
}

func (b *BolusMap) sortedNames() []string {
names := []string{}
for name := range *b {
names = append(names, name)
}
sort.Strings(names)
return names
}
Loading