From d47e826ecc10ff334b7bf7398e378535e24c0b7f Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Sun, 3 Sep 2023 11:06:03 +0200 Subject: [PATCH] TemporaryBasalExtensionKtTest --- .../core/extensions/GlucoseValueExtension.kt | 7 +- .../extensions/TemporaryBasalExtension.kt | 10 ++- .../TemporaryBasalExtensionKtTest.kt | 90 +++++++++++++++++++ 3 files changed, 97 insertions(+), 10 deletions(-) create mode 100644 plugins/insulin/src/test/java/info/nightscout/core/extensions/TemporaryBasalExtensionKtTest.kt diff --git a/core/main/src/main/java/info/nightscout/core/extensions/GlucoseValueExtension.kt b/core/main/src/main/java/info/nightscout/core/extensions/GlucoseValueExtension.kt index 845f749458..80884eb25e 100644 --- a/core/main/src/main/java/info/nightscout/core/extensions/GlucoseValueExtension.kt +++ b/core/main/src/main/java/info/nightscout/core/extensions/GlucoseValueExtension.kt @@ -1,6 +1,5 @@ package info.nightscout.core.extensions - import info.nightscout.database.entities.GlucoseValue import info.nightscout.interfaces.Constants import info.nightscout.interfaces.GlucoseUnit @@ -9,15 +8,11 @@ import info.nightscout.interfaces.utils.DecimalFormatter import info.nightscout.shared.utils.DateUtil import org.json.JSONObject -fun GlucoseValue.valueToUnits(units: GlucoseUnit): Double = - if (units == GlucoseUnit.MGDL) value - else value * Constants.MGDL_TO_MMOLL - fun GlucoseValue.valueToUnitsString(units: GlucoseUnit): String = if (units == GlucoseUnit.MGDL) DecimalFormatter.to0Decimal(value) else DecimalFormatter.to1Decimal(value * Constants.MGDL_TO_MMOLL) -fun GlucoseValue.toJson(isAdd : Boolean, dateUtil: DateUtil): JSONObject = +fun GlucoseValue.toJson(isAdd: Boolean, dateUtil: DateUtil): JSONObject = JSONObject() .put("device", sourceSensor.text) .put("date", timestamp) diff --git a/core/main/src/main/java/info/nightscout/core/extensions/TemporaryBasalExtension.kt b/core/main/src/main/java/info/nightscout/core/extensions/TemporaryBasalExtension.kt index 5f73bbadac..cc935543b9 100644 --- a/core/main/src/main/java/info/nightscout/core/extensions/TemporaryBasalExtension.kt +++ b/core/main/src/main/java/info/nightscout/core/extensions/TemporaryBasalExtension.kt @@ -61,6 +61,7 @@ fun TemporaryBasal.toStringShort(): String = else "${DecimalFormatter.to0Decimal(rate)}%" fun TemporaryBasal.iobCalc(time: Long, profile: Profile, insulinInterface: Insulin): IobTotal { + if (!isValid) return IobTotal(time) val result = IobTotal(time) val realDuration = getPassedDurationToTimeInMinutes(time) var netBasalAmount = 0.0 @@ -105,20 +106,21 @@ fun TemporaryBasal.iobCalc( time: Long, profile: Profile, lastAutosensResult: AutosensResult, - exercise_mode: Boolean, - half_basal_exercise_target: Int, + exerciseMode: Boolean, + halfBasalExerciseTarget: Int, isTempTarget: Boolean, insulinInterface: Insulin ): IobTotal { + if (!isValid) return IobTotal(time) val result = IobTotal(time) val realDuration = getPassedDurationToTimeInMinutes(time) var netBasalAmount = 0.0 var sensitivityRatio = lastAutosensResult.ratio val normalTarget = 100.0 - if (exercise_mode && isTempTarget && profile.getTargetMgdl() >= normalTarget + 5) { + if (exerciseMode && isTempTarget && profile.getTargetMgdl() >= normalTarget + 5) { // w/ target 100, temp target 110 = .89, 120 = 0.8, 140 = 0.67, 160 = .57, and 200 = .44 // e.g.: Sensitivity ratio set to 0.8 based on temp target of 120; Adjusting basal from 1.65 to 1.35; ISF from 58.9 to 73.6 - val c = half_basal_exercise_target - normalTarget + val c = halfBasalExerciseTarget - normalTarget sensitivityRatio = c / (c + profile.getTargetMgdl() - normalTarget) } if (realDuration > 0) { diff --git a/plugins/insulin/src/test/java/info/nightscout/core/extensions/TemporaryBasalExtensionKtTest.kt b/plugins/insulin/src/test/java/info/nightscout/core/extensions/TemporaryBasalExtensionKtTest.kt new file mode 100644 index 0000000000..517f9926eb --- /dev/null +++ b/plugins/insulin/src/test/java/info/nightscout/core/extensions/TemporaryBasalExtensionKtTest.kt @@ -0,0 +1,90 @@ +package info.nightscout.core.extensions + +import info.nightscout.androidaps.TestBaseWithProfile +import info.nightscout.database.entities.TemporaryBasal +import info.nightscout.insulin.InsulinLyumjevPlugin +import info.nightscout.interfaces.aps.AutosensResult +import info.nightscout.interfaces.aps.SMBDefaults +import info.nightscout.interfaces.insulin.Insulin +import info.nightscout.interfaces.plugin.ActivePlugin +import info.nightscout.interfaces.profile.ProfileFunction +import info.nightscout.interfaces.ui.UiInteraction +import info.nightscout.shared.utils.T +import org.junit.jupiter.api.Assertions +import org.junit.jupiter.api.BeforeEach +import org.junit.jupiter.api.Test +import org.mockito.Mock +import org.mockito.Mockito + +class TemporaryBasalExtensionKtTest : TestBaseWithProfile() { + + @Mock lateinit var activePlugin: ActivePlugin + @Mock lateinit var profileFunctions: ProfileFunction + @Mock lateinit var uiInteraction: UiInteraction + + private lateinit var insulin: Insulin + + private val now = 1000000L + private val dia = 7.0 + + @BeforeEach + fun setup() { + insulin = InsulinLyumjevPlugin(profileInjector, rh, profileFunctions, rxBus, aapsLogger, config, hardLimits, uiInteraction) + Mockito.`when`(activePlugin.activeInsulin).thenReturn(insulin) + Mockito.`when`(dateUtil.now()).thenReturn(now) + } + + @Test + fun iobCalc() { + val temporaryBasal = TemporaryBasal(timestamp = now - 1, rate = 200.0, isAbsolute = false, duration = T.hours(1).msecs(), type = TemporaryBasal.Type.NORMAL) + // there should zero IOB after now + Assertions.assertEquals(0.0, temporaryBasal.iobCalc(now, validProfile, insulin).basaliob, 0.01) + // there should be significant IOB at EB finish + Assertions.assertTrue(0.8 < temporaryBasal.iobCalc(now + T.hours(1).msecs(), validProfile, insulin).basaliob) + // there should be less that 5% after DIA -1 + Assertions.assertTrue(0.05 > temporaryBasal.iobCalc(now + T.hours(dia.toLong() - 1).msecs(), validProfile, insulin).basaliob) + // there should be zero after DIA + Assertions.assertEquals(0.0, temporaryBasal.iobCalc(now + T.hours(dia.toLong() + 1).msecs(), validProfile, insulin).basaliob) + // no IOB for invalid record + temporaryBasal.isValid = false + Assertions.assertEquals(0.0, temporaryBasal.iobCalc(now + T.hours(1).msecs(), validProfile, insulin).basaliob) + + temporaryBasal.isValid = true + val asResult = AutosensResult() + // there should zero IOB after now + Assertions.assertEquals(0.0, temporaryBasal.iobCalc(now, validProfile, asResult, SMBDefaults.exercise_mode, SMBDefaults.half_basal_exercise_target, true, insulin).basaliob, 0.01) + // there should be significant IOB at EB finish + Assertions.assertTrue(0.8 < temporaryBasal.iobCalc(now + T.hours(1).msecs(), validProfile, asResult, SMBDefaults.exercise_mode, SMBDefaults.half_basal_exercise_target, true, insulin).basaliob) + // there should be less that 5% after DIA -1 + Assertions.assertTrue( + 0.05 > temporaryBasal.iobCalc( + now + T.hours(dia.toLong() - 1).msecs(), + validProfile, + asResult, + SMBDefaults.exercise_mode, + SMBDefaults.half_basal_exercise_target, + true, + insulin + ).basaliob + ) + // there should be zero after DIA + Assertions.assertEquals( + 0.0, + temporaryBasal.iobCalc( + now + T.hours(dia.toLong() + 1).msecs(), + validProfile, + asResult, + SMBDefaults.exercise_mode, + SMBDefaults.half_basal_exercise_target, + true, + insulin + ).basaliob + ) + // no IOB for invalid record + temporaryBasal.isValid = false + Assertions.assertEquals( + 0.0, + temporaryBasal.iobCalc(now + T.hours(1).msecs(), validProfile, asResult, SMBDefaults.exercise_mode, SMBDefaults.half_basal_exercise_target, true, insulin).basaliob + ) + } +} \ No newline at end of file