TemporaryBasalExtensionKtTest
This commit is contained in:
parent
cbe0e0cb7f
commit
d47e826ecc
|
@ -1,6 +1,5 @@
|
||||||
package info.nightscout.core.extensions
|
package info.nightscout.core.extensions
|
||||||
|
|
||||||
|
|
||||||
import info.nightscout.database.entities.GlucoseValue
|
import info.nightscout.database.entities.GlucoseValue
|
||||||
import info.nightscout.interfaces.Constants
|
import info.nightscout.interfaces.Constants
|
||||||
import info.nightscout.interfaces.GlucoseUnit
|
import info.nightscout.interfaces.GlucoseUnit
|
||||||
|
@ -9,15 +8,11 @@ import info.nightscout.interfaces.utils.DecimalFormatter
|
||||||
import info.nightscout.shared.utils.DateUtil
|
import info.nightscout.shared.utils.DateUtil
|
||||||
import org.json.JSONObject
|
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 =
|
fun GlucoseValue.valueToUnitsString(units: GlucoseUnit): String =
|
||||||
if (units == GlucoseUnit.MGDL) DecimalFormatter.to0Decimal(value)
|
if (units == GlucoseUnit.MGDL) DecimalFormatter.to0Decimal(value)
|
||||||
else DecimalFormatter.to1Decimal(value * Constants.MGDL_TO_MMOLL)
|
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()
|
JSONObject()
|
||||||
.put("device", sourceSensor.text)
|
.put("device", sourceSensor.text)
|
||||||
.put("date", timestamp)
|
.put("date", timestamp)
|
||||||
|
|
|
@ -61,6 +61,7 @@ fun TemporaryBasal.toStringShort(): String =
|
||||||
else "${DecimalFormatter.to0Decimal(rate)}%"
|
else "${DecimalFormatter.to0Decimal(rate)}%"
|
||||||
|
|
||||||
fun TemporaryBasal.iobCalc(time: Long, profile: Profile, insulinInterface: Insulin): IobTotal {
|
fun TemporaryBasal.iobCalc(time: Long, profile: Profile, insulinInterface: Insulin): IobTotal {
|
||||||
|
if (!isValid) return IobTotal(time)
|
||||||
val result = IobTotal(time)
|
val result = IobTotal(time)
|
||||||
val realDuration = getPassedDurationToTimeInMinutes(time)
|
val realDuration = getPassedDurationToTimeInMinutes(time)
|
||||||
var netBasalAmount = 0.0
|
var netBasalAmount = 0.0
|
||||||
|
@ -105,20 +106,21 @@ fun TemporaryBasal.iobCalc(
|
||||||
time: Long,
|
time: Long,
|
||||||
profile: Profile,
|
profile: Profile,
|
||||||
lastAutosensResult: AutosensResult,
|
lastAutosensResult: AutosensResult,
|
||||||
exercise_mode: Boolean,
|
exerciseMode: Boolean,
|
||||||
half_basal_exercise_target: Int,
|
halfBasalExerciseTarget: Int,
|
||||||
isTempTarget: Boolean,
|
isTempTarget: Boolean,
|
||||||
insulinInterface: Insulin
|
insulinInterface: Insulin
|
||||||
): IobTotal {
|
): IobTotal {
|
||||||
|
if (!isValid) return IobTotal(time)
|
||||||
val result = IobTotal(time)
|
val result = IobTotal(time)
|
||||||
val realDuration = getPassedDurationToTimeInMinutes(time)
|
val realDuration = getPassedDurationToTimeInMinutes(time)
|
||||||
var netBasalAmount = 0.0
|
var netBasalAmount = 0.0
|
||||||
var sensitivityRatio = lastAutosensResult.ratio
|
var sensitivityRatio = lastAutosensResult.ratio
|
||||||
val normalTarget = 100.0
|
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
|
// 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
|
// 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)
|
sensitivityRatio = c / (c + profile.getTargetMgdl() - normalTarget)
|
||||||
}
|
}
|
||||||
if (realDuration > 0) {
|
if (realDuration > 0) {
|
||||||
|
|
|
@ -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
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue