TemporaryBasalExtensionKtTest

This commit is contained in:
Milos Kozak 2023-09-03 11:06:03 +02:00
parent cbe0e0cb7f
commit d47e826ecc
3 changed files with 97 additions and 10 deletions

View file

@ -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,10 +8,6 @@ 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)

View file

@ -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) {

View file

@ -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
)
}
}