Skip to content

Commit

Permalink
migrate jellyfish pumpSettings bolus if present
Browse files Browse the repository at this point in the history
  • Loading branch information
jh-bate committed Nov 8, 2023
1 parent 9c8dfa4 commit 08387b4
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 4 deletions.
32 changes: 29 additions & 3 deletions migrations/back_37/back_37.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,23 @@ func createDatumHash(bsonData bson.M) (string, error) {
return deduplicator.GenerateIdentityHash(identityFields)
}

func updateIfExistsPumpSettingsBolus(bsonData bson.M) (interface{}, error) {
dataType, err := getValidatedString(bsonData, "type")
if err != nil {
return nil, err
}
if dataType == "pumpSettings" {
if bolus := bsonData["bolus"]; bolus != nil {
boluses, ok := bolus.(map[string]interface{})
if !ok {
return nil, errors.Newf("pumpSettings.bolus is not the expected type %v", bolus)
}
return boluses, nil
}
}
return nil, nil
}

func getBGValuePlatformPrecision(mmolVal float64) float64 {
if len(fmt.Sprintf("%v", mmolVal)) > 7 {
mgdlVal := mmolVal * glucose.MmolLToMgdLConversionFactor
Expand Down Expand Up @@ -233,17 +250,26 @@ func (m *Migration) migrateDocument(jfDatum bson.M) (bool, error) {
return false, err
}

updates := bson.M{}
hash, err := createDatumHash(jfDatum)
if err != nil {
return false, err
}
update := bson.M{
"$set": bson.M{"_deduplicator": bson.M{"hash": hash}},

updates["_deduplicator"] = bson.M{"hash": hash}

if boluses, err := updateIfExistsPumpSettingsBolus(jfDatum); err != nil {
return false, err
} else if boluses != nil {
updates["pumpSettings"] = bson.M{"boluses": boluses}
}

result, err := m.dataRepository.UpdateOne(m.ctx, bson.M{
"_id": datumID,
"modifiedTime": jfDatum["modifiedTime"],
}, update)
}, bson.M{
"$set": updates,
})

if err != nil {
return false, err
Expand Down
72 changes: 71 additions & 1 deletion migrations/back_37/back_37_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
package main

import "testing"
import (
"reflect"
"testing"

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

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

func Test_getBGValuePlatformPrecision(t *testing.T) {

Expand Down Expand Up @@ -38,3 +45,66 @@ func Test_getBGValuePlatformPrecision(t *testing.T) {
})
}
}

func Test_updateIfExistsPumpSettingsBolus(t *testing.T) {
type args struct {
bsonData bson.M
}

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

tests := []struct {
name string
args args
want interface{}
wantErr bool
}{
{
name: "when not pumpSettings",
args: args{
bsonData: bson.M{"type": "other"},
},
want: nil,
wantErr: false,
},
{
name: "pumpSettings but no bolus",
args: args{
bsonData: bson.M{"type": "pumpSettings"},
},
want: nil,
wantErr: false,
},
{
name: "pumpSettings bolus wrong type",
args: args{
bsonData: bson.M{"type": "pumpSettings", "bolus": "wrong"},
},
want: nil,
wantErr: true,
},
{
name: "pumpSettings bolus valid type",
args: args{
bsonData: bson.M{"type": "pumpSettings", "bolus": bolusData},
},
want: bolusData,
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := updateIfExistsPumpSettingsBolus(tt.args.bsonData)
if (err != nil) != tt.wantErr {
t.Errorf("updateIfExistsPumpSettingsBolus() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("updateIfExistsPumpSettingsBolus() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit 08387b4

Please sign in to comment.