Skip to content

Commit

Permalink
sort parsing of pumpSettings sleepSchedules
Browse files Browse the repository at this point in the history
  • Loading branch information
jh-bate committed Dec 19, 2023
1 parent f4652e8 commit 3061540
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 56 deletions.
51 changes: 31 additions & 20 deletions migrations/20231128_jellyfish_migration/utils/utils.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package utils

import (
"encoding/json"
"fmt"
"log"
"slices"
Expand All @@ -24,26 +25,37 @@ import (
"github.com/tidepool-org/platform/errors"
)

func updateIfExistsPumpSettingsSleepSchedules(datum *pump.Pump) (*pump.SleepScheduleMap, error) {
sleepSchedules := datum.SleepSchedules
if sleepSchedules == nil {
return nil, nil
}
for key := range *sleepSchedules {
days := (*sleepSchedules)[key].Days
updatedDays := []string{}
for _, day := range *days {
if !slices.Contains(common.DaysOfWeek(), strings.ToLower(day)) {
return nil, errors.Newf("pumpSettings.sleepSchedules has an invalid day of week %s", day)
func updateIfExistsPumpSettingsSleepSchedules(bsonData bson.M) (*pump.SleepScheduleMap, error) {
scheduleNames := map[int]string{0: "One", 1: "Two", 2: "Three", 3: "Four", 4: "Five"}

if schedules := bsonData["sleepSchedules"]; schedules != nil {
sleepScheduleMap := pump.SleepScheduleMap{}
dataBytes, err := json.Marshal(schedules)
if err != nil {
return nil, err
}
schedulesArray := []*pump.SleepSchedule{}
err = json.Unmarshal(dataBytes, &schedulesArray)
if err != nil {
return nil, err
}
for i, schedule := range schedulesArray {
days := schedule.Days
updatedDays := []string{}
for _, day := range *days {
if !slices.Contains(common.DaysOfWeek(), strings.ToLower(day)) {
return nil, errors.Newf("pumpSettings.sleepSchedules has an invalid day of week %s", day)
}
updatedDays = append(updatedDays, strings.ToLower(day))
}
updatedDays = append(updatedDays, strings.ToLower(day))
schedule.Days = &updatedDays
sleepScheduleMap[scheduleNames[i]] = schedule
}
(*sleepSchedules)[key].Days = &updatedDays
//sorts schedules based on day
sleepScheduleMap.Normalize(normalizer.New())
return &sleepScheduleMap, nil
}
//sorts schedules based on day
sleepSchedules.Normalize(normalizer.New())
return sleepSchedules, nil

return nil, nil
}

func updateIfExistsPumpSettingsBolus(bsonData bson.M) (*pump.BolusMap, error) {
Expand Down Expand Up @@ -123,7 +135,7 @@ func GetDatumUpdates(bsonData bson.M) (string, bson.M, error) {
return errorDebug(datumID, err)
}
case pump.Type:
var datum *pump.Pump
var datum *types.Base
err = bson.Unmarshal(dataBytes, &datum)
if err != nil {
return errorDebug(datumID, err)
Expand All @@ -140,13 +152,12 @@ func GetDatumUpdates(bsonData bson.M) (string, bson.M, error) {
updates["boluses"] = boluses
}

sleepSchedules, err := updateIfExistsPumpSettingsSleepSchedules(datum)
sleepSchedules, err := updateIfExistsPumpSettingsSleepSchedules(bsonData)
if err != nil {
return errorDebug(datumID, err)
} else if sleepSchedules != nil {
updates["sleepSchedules"] = sleepSchedules
}

case selfmonitored.Type:
var datum *selfmonitored.SelfMonitored
err = bson.Unmarshal(dataBytes, &datum)
Expand Down
65 changes: 29 additions & 36 deletions migrations/20231128_jellyfish_migration/utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ var _ = Describe("back-37", func() {
theTime, _ := time.Parse(time.RFC3339, "2016-09-01T11:00:00Z")
*datum.Time = theTime
existingBolusDatum = getBSONData(datum)
existingBolusDatum["_id"] = expectedID
Expect(existingBolusDatum).ToNot(BeNil())
})

Expand Down Expand Up @@ -108,7 +109,7 @@ var _ = Describe("back-37", func() {
*pumpSettingsDatum.Time = theTime
})

Context("with incorrect jellyfish bolus", func() {
Context("with mis-named jellyfish bolus", func() {
var bolusData = &pump.BolusMap{
"bolus-1": pumpTest.NewRandomBolus(),
"bolus-2": pumpTest.NewRandomBolus(),
Expand All @@ -117,8 +118,8 @@ var _ = Describe("back-37", func() {

BeforeEach(func() {
settingsBolusDatum = getBSONData(pumpSettingsDatum)
//as currently set in jellyfish
settingsBolusDatum["bolus"] = bolusData
settingsBolusDatum["_id"] = expectedID
})

DescribeTable("should",
Expand Down Expand Up @@ -175,43 +176,36 @@ var _ = Describe("back-37", func() {
)
})
Context("unordered sleepSchedules", func() {
var sleepSchedulesExpected *pump.SleepScheduleMap
var sleepSchedulesStored *pump.SleepScheduleMap
var sleepSchedulesInvalidDays *pump.SleepScheduleMap
expectedSleepSchedulesMap := &pump.SleepScheduleMap{}
var invalidDays *pump.SleepSchedule
var s1Days *pump.SleepSchedule
var s2Days *pump.SleepSchedule
var sleepSchedulesDatum bson.M

BeforeEach(func() {
sleepSchedulesExpected = &pump.SleepScheduleMap{
"schedule-1": pumpTest.RandomSleepSchedule(),
"schedule-2": pumpTest.RandomSleepSchedule(),
s1 := pumpTest.RandomSleepSchedule()
s2 := pumpTest.RandomSleepSchedule()
(*expectedSleepSchedulesMap)["One"] = s1
(*expectedSleepSchedulesMap)["Two"] = s2

s1Days = pumpTest.CloneSleepSchedule(s1)
for key, day := range *s1Days.Days {
(*s1Days.Days)[key] = strings.ToUpper(day)
}
sleepSchedulesInvalidDays = pumpTest.CloneSleepSchedules(sleepSchedulesExpected)
(*sleepSchedulesInvalidDays)["schedule-2"].Days = &[]string{"not-a-day", common.DayFriday}

sleepSchedulesStored = pumpTest.CloneSleepSchedules(sleepSchedulesExpected)

s1Days := (*sleepSchedulesStored)["schedule-1"].Days
for key, day := range *s1Days {
(*s1Days)[key] = strings.ToUpper(day)
}
(*sleepSchedulesStored)["schedule-1"].Days = s1Days

s2Days := (*sleepSchedulesStored)["schedule-2"].Days
for key, day := range *s2Days {
(*s2Days)[key] = strings.ToUpper(day)
s2Days = pumpTest.CloneSleepSchedule(s2)
for key, day := range *s2Days.Days {
(*s2Days.Days)[key] = strings.ToUpper(day)
}
(*sleepSchedulesStored)["schedule-2"].Days = s2Days
invalidDays = pumpTest.CloneSleepSchedule(s2)
invalidDays.Days = &[]string{"not-a-day", common.DayFriday}

//ensure sorting
sleepSchedulesExpected.Normalize(normalizer.New())
//to ensure correct sorting
expectedSleepSchedulesMap.Normalize(normalizer.New())

Expect(sleepSchedulesExpected).ToNot(BeNil())
Expect(sleepSchedulesStored).ToNot(BeNil())
Expect(sleepSchedulesExpected).ToNot(Equal(sleepSchedulesStored))
Expect(sleepSchedulesInvalidDays).ToNot(BeNil())
pumpSettingsDatum.SleepSchedules = sleepSchedulesStored
Expect(expectedSleepSchedulesMap).ToNot(BeNil())
pumpSettingsDatum.SleepSchedules = nil
sleepSchedulesDatum = getBSONData(pumpSettingsDatum)
sleepSchedulesDatum["bolus"] = nil //remove as not testng here
sleepSchedulesDatum["_id"] = expectedID
sleepSchedulesDatum["bolus"] = nil //remove as not testing here
})

It("does nothing when wrong type", func() {
Expand All @@ -226,16 +220,15 @@ var _ = Describe("back-37", func() {
Expect(actual).To(Equal(bson.M{"_deduplicator": bson.M{"hash": "RlrPcuPDfRim29UwnM7Yf0Ib0Ht4F35qvHu62CCYXnM="}}))
})
It("returns updated sleepSchedules when valid", func() {
Expect(sleepSchedulesExpected).ToNot(Equal(sleepSchedulesStored))
sleepSchedulesDatum["sleepSchedules"] = sleepSchedulesStored
sleepSchedulesDatum["sleepSchedules"] = []*pump.SleepSchedule{s1Days, s2Days}
_, actual, err := utils.GetDatumUpdates(sleepSchedulesDatum)
Expect(err).To(BeNil())
Expect(actual).ToNot(BeNil())
Expect(actual["sleepSchedules"]).ToNot(BeNil())
Expect(actual["sleepSchedules"]).To(Equal(sleepSchedulesExpected))
Expect(actual["sleepSchedules"]).To(Equal(expectedSleepSchedulesMap))
})
It("returns error when sleepSchedules have invalid days", func() {
sleepSchedulesDatum["sleepSchedules"] = sleepSchedulesInvalidDays
sleepSchedulesDatum["sleepSchedules"] = []*pump.SleepSchedule{invalidDays}
_, actual, err := utils.GetDatumUpdates(sleepSchedulesDatum)
Expect(err).ToNot(BeNil())
Expect(err.Error()).To(Equal("pumpSettings.sleepSchedules has an invalid day of week not-a-day"))
Expand Down

0 comments on commit 3061540

Please sign in to comment.