Skip to content

Commit

Permalink
tests for sleep schedule migration
Browse files Browse the repository at this point in the history
  • Loading branch information
jh-bate committed Nov 9, 2023
1 parent 2e31017 commit 15d4325
Show file tree
Hide file tree
Showing 2 changed files with 151 additions and 3 deletions.
40 changes: 39 additions & 1 deletion migrations/back_37/back_37.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ package main
import (
"context"
"fmt"
"log"
"slices"
"strconv"
"strings"
"time"

"github.com/urfave/cli"
Expand All @@ -12,13 +15,16 @@ import (
"github.com/tidepool-org/platform/application"
"github.com/tidepool-org/platform/data/blood/glucose"
"github.com/tidepool-org/platform/data/deduplicator/deduplicator"
"github.com/tidepool-org/platform/data/normalizer"
"github.com/tidepool-org/platform/data/types"
"github.com/tidepool-org/platform/data/types/basal"
"github.com/tidepool-org/platform/data/types/blood/glucose/continuous"
"github.com/tidepool-org/platform/data/types/blood/glucose/selfmonitored"
"github.com/tidepool-org/platform/data/types/blood/ketone"
"github.com/tidepool-org/platform/data/types/bolus"
"github.com/tidepool-org/platform/data/types/common"
"github.com/tidepool-org/platform/data/types/device"
"github.com/tidepool-org/platform/data/types/settings/pump"
"github.com/tidepool-org/platform/errors"
migrationMongo "github.com/tidepool-org/platform/migration/mongo"
storeStructuredMongo "github.com/tidepool-org/platform/store/structured/mongo"
Expand Down Expand Up @@ -121,12 +127,44 @@ func createDatumHash(bsonData bson.M) (string, error) {
return deduplicator.GenerateIdentityHash(identityFields)
}

func updateIfExistsPumpSettingsSleepSchedules(bsonData bson.M) (interface{}, error) {
dataType, err := getValidatedString(bsonData, "type")
if err != nil {
return nil, err
}

if dataType == pump.Type {
if sleepSchedules := bsonData["sleepSchedules"]; sleepSchedules != nil {
schedules, ok := sleepSchedules.(*pump.SleepScheduleMap)
if !ok {
return nil, errors.Newf("pumpSettings.sleepSchedules is not the expected type %s", sleepSchedules)
}
for key := range *schedules {
days := (*schedules)[key].Days
updatedDays := []string{}
for _, day := range *days {
log.Println("day is: ", day)
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))
}
(*schedules)[key].Days = &updatedDays
}
//sorts schedules based on day
schedules.Normalize(normalizer.New())
return schedules, nil
}
}
return nil, nil
}

func updateIfExistsPumpSettingsBolus(bsonData bson.M) (interface{}, error) {
dataType, err := getValidatedString(bsonData, "type")
if err != nil {
return nil, err
}
if dataType == "pumpSettings" {
if dataType == pump.Type {
if bolus := bsonData["bolus"]; bolus != nil {
boluses, ok := bolus.(map[string]interface{})
if !ok {
Expand Down
114 changes: 112 additions & 2 deletions migrations/back_37/back_37_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@ package main

import (
"reflect"
"strings"
"testing"

"go.mongodb.org/mongo-driver/bson"

"github.com/tidepool-org/platform/data/normalizer"
"github.com/tidepool-org/platform/data/types/common"
"github.com/tidepool-org/platform/data/types/settings/pump"
pumpTest "github.com/tidepool-org/platform/data/types/settings/pump/test"
)

Expand Down Expand Up @@ -52,8 +56,8 @@ func Test_updateIfExistsPumpSettingsBolus(t *testing.T) {
}

bolusData := map[string]interface{}{
"bolous-1": pumpTest.NewBolus(),
"bolous-2": pumpTest.NewBolus(),
"bolous-1": pumpTest.NewRandomBolus(),
"bolous-2": pumpTest.NewRandomBolus(),
}

tests := []struct {
Expand Down Expand Up @@ -108,3 +112,109 @@ func Test_updateIfExistsPumpSettingsBolus(t *testing.T) {
})
}
}

func Test_updateIfExistsPumpSettingsSleepSchedules(t *testing.T) {

type args struct {
bsonData bson.M
}

sleepSchedulesExpected := &pump.SleepScheduleMap{
"schedule-1": pumpTest.RandomSleepSchedule(),
"schedule-2": pumpTest.RandomSleepSchedule(),
}
sleepSchedulesStored := pumpTest.CloneSleepSchedules(sleepSchedulesExpected)
sleepSchedulesInvalidDays := pumpTest.CloneSleepSchedules(sleepSchedulesExpected)

//ensure sorting
sleepSchedulesExpected.Normalize(normalizer.New())

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)
}
(*sleepSchedulesStored)["schedule-2"].Days = s2Days

(*sleepSchedulesInvalidDays)["schedule-2"].Days = &[]string{"not-a-day", common.DayFriday}

tests := []struct {
name string
args args
want *pump.SleepScheduleMap
wantErr bool
}{
{
name: "when not pumpSettings type",
args: args{
bsonData: bson.M{"type": "other"},
},
want: nil,
wantErr: false,
},
{
name: "pumpSettings but no sleepSchedules",
args: args{
bsonData: bson.M{"type": "pumpSettings"},
},
want: nil,
wantErr: false,
},
{
name: "pumpSettings sleepSchedules wrong type",
args: args{
bsonData: bson.M{"type": "pumpSettings", "sleepSchedules": "wrong"},
},
want: nil,
wantErr: true,
},
{
name: "pumpSettings sleepSchedules days invalid",
args: args{
bsonData: bson.M{"type": "pumpSettings", "sleepSchedules": sleepSchedulesInvalidDays},
},
want: nil,
wantErr: true,
},
{
name: "pumpSettings sleepSchedules valid type",
args: args{
bsonData: bson.M{"type": "pumpSettings", "sleepSchedules": sleepSchedulesStored},
},
want: sleepSchedulesExpected,
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := updateIfExistsPumpSettingsSleepSchedules(tt.args.bsonData)
if (err != nil) != tt.wantErr {
t.Errorf("updateIfExistsPumpSettingsSleepSchedules() error = %v, wantErr %v", err, tt.wantErr)
return
}
if tt.want != nil {
gotSleepSchedules, ok := got.(*pump.SleepScheduleMap)
if !ok {
t.Errorf("updateIfExistsPumpSettingsSleepSchedules() = %v, want %v", got, tt.want)
return
}

for key, wantSchedule := range *tt.want {
if gotSchedule := (*gotSleepSchedules)[key]; gotSchedule != nil {
if !reflect.DeepEqual((*gotSchedule).Days, (*wantSchedule).Days) {
t.Errorf("updateIfExistsPumpSettingsSleepSchedules() = %v, want %v", (*gotSchedule).Days, (*wantSchedule).Days)
}
} else {
t.Errorf("missing schedule %s", key)
}
}
}
})
}
}

0 comments on commit 15d4325

Please sign in to comment.