Rewrites ExtendedBolusExtensionKtTest with matchers

Issue #2745
This commit is contained in:
Ryan Haining 2023-09-23 18:42:06 -07:00
parent b2aa328e67
commit f83d8f0b56

View file

@ -1,6 +1,7 @@
package info.nightscout.core.extensions
import app.aaps.shared.tests.TestBaseWithProfile
import com.google.common.truth.Truth.assertThat
import info.nightscout.database.entities.ExtendedBolus
import info.nightscout.database.entities.TemporaryBasal
import info.nightscout.insulin.InsulinLyumjevPlugin
@ -10,7 +11,6 @@ import info.nightscout.interfaces.insulin.Insulin
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
@ -25,72 +25,61 @@ class ExtendedBolusExtensionKtTest : TestBaseWithProfile() {
private val dia = 7.0
@BeforeEach
fun setup() {
@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() {
@Test fun iobCalc() {
val bolus = ExtendedBolus(timestamp = now - 1, amount = 1.0, duration = T.hours(1).msecs())
// there should zero IOB after now
Assertions.assertEquals(0.0, bolus.iobCalc(now, validProfile, insulin).iob, 0.01)
assertThat(bolus.iobCalc(now, validProfile, insulin).iob).isWithin(0.01).of(0.0)
// there should be significant IOB at EB finish
Assertions.assertTrue(0.8 < bolus.iobCalc(now + T.hours(1).msecs(), validProfile, insulin).iob)
assertThat(bolus.iobCalc(now + T.hours(1).msecs(), validProfile, insulin).iob).isGreaterThan(0.8)
// there should be less that 5% after DIA -1
Assertions.assertTrue(0.05 > bolus.iobCalc(now + T.hours(dia.toLong() - 1).msecs(), validProfile, insulin).iob)
assertThat(bolus.iobCalc(now + T.hours(dia.toLong() - 1).msecs(), validProfile, insulin).iob).isLessThan(0.05)
// there should be zero after DIA
Assertions.assertEquals(0.0, bolus.iobCalc(now + T.hours(dia.toLong() + 1).msecs(), validProfile, insulin).iob)
assertThat(bolus.iobCalc(now + T.hours(dia.toLong() + 1).msecs(), validProfile, insulin).iob).isEqualTo(0.0)
// no IOB for invalid record
bolus.isValid = false
Assertions.assertEquals(0.0, bolus.iobCalc(now + T.hours(1).msecs(), validProfile, insulin).iob)
assertThat(bolus.iobCalc(now + T.hours(1).msecs(), validProfile, insulin).iob).isEqualTo(0.0)
bolus.isValid = true
val asResult = AutosensResult()
// there should zero IOB after now
Assertions.assertEquals(0.0, bolus.iobCalc(now, validProfile, asResult, SMBDefaults.exercise_mode, SMBDefaults.half_basal_exercise_target, true, insulin).iob, 0.01)
assertThat(bolus.iobCalc(now, validProfile, asResult, SMBDefaults.exercise_mode, SMBDefaults.half_basal_exercise_target, true, insulin).iob).isWithin(0.01).of(0.0)
// there should be significant IOB at EB finish
Assertions.assertTrue(0.8 < bolus.iobCalc(now + T.hours(1).msecs(), validProfile, asResult, SMBDefaults.exercise_mode, SMBDefaults.half_basal_exercise_target, true, insulin).iob)
assertThat(bolus.iobCalc(now + T.hours(1).msecs(), validProfile, asResult, SMBDefaults.exercise_mode, SMBDefaults.half_basal_exercise_target, true, insulin).iob).isGreaterThan(0.8)
// there should be less that 5% after DIA -1
Assertions.assertTrue(
0.05 > bolus.iobCalc(
now + T.hours(dia.toLong() - 1).msecs(),
validProfile,
asResult,
SMBDefaults.exercise_mode,
SMBDefaults.half_basal_exercise_target,
true,
insulin
assertThat(
bolus.iobCalc(
now + T.hours(dia.toLong() - 1).msecs(), validProfile, asResult, SMBDefaults.exercise_mode, SMBDefaults.half_basal_exercise_target, true, insulin
).iob
)
).isLessThan(0.05)
// there should be zero after DIA
Assertions.assertEquals(
0.0,
assertThat(
bolus.iobCalc(now + T.hours(dia.toLong() + 1).msecs(), validProfile, asResult, SMBDefaults.exercise_mode, SMBDefaults.half_basal_exercise_target, true, insulin).iob
)
).isEqualTo(0.0)
// no IOB for invalid record
bolus.isValid = false
Assertions.assertEquals(0.0, bolus.iobCalc(now + T.hours(1).msecs(), validProfile, asResult, SMBDefaults.exercise_mode, SMBDefaults.half_basal_exercise_target, true, insulin).iob)
assertThat(bolus.iobCalc(now + T.hours(1).msecs(), validProfile, asResult, SMBDefaults.exercise_mode, SMBDefaults.half_basal_exercise_target, true, insulin).iob).isEqualTo(0.0)
}
@Test
fun isInProgress() {
@Test fun isInProgress() {
val bolus = ExtendedBolus(timestamp = now - 1, amount = 1.0, duration = T.hours(1).msecs())
Mockito.`when`(dateUtil.now()).thenReturn(now)
Assertions.assertTrue(bolus.isInProgress(dateUtil))
assertThat(bolus.isInProgress(dateUtil)).isTrue()
Mockito.`when`(dateUtil.now()).thenReturn(now + T.hours(2).msecs())
Assertions.assertFalse(bolus.isInProgress(dateUtil))
assertThat(bolus.isInProgress(dateUtil)).isFalse()
}
@Test
fun toTemporaryBasal() {
@Test fun toTemporaryBasal() {
val bolus = ExtendedBolus(timestamp = now - 1, amount = 1.0, duration = T.hours(1).msecs())
val tbr = bolus.toTemporaryBasal(validProfile)
Assertions.assertEquals(bolus.timestamp, tbr.timestamp)
Assertions.assertEquals(bolus.duration, tbr.duration)
Assertions.assertEquals(bolus.rate + validProfile.getBasal(now), tbr.rate)
Assertions.assertEquals(TemporaryBasal.Type.FAKE_EXTENDED, tbr.type)
assertThat(tbr.timestamp).isEqualTo(bolus.timestamp)
assertThat(tbr.duration).isEqualTo(bolus.duration)
assertThat(tbr.rate).isEqualTo(bolus.rate + validProfile.getBasal(now))
assertThat(tbr.type).isEqualTo(TemporaryBasal.Type.FAKE_EXTENDED)
}
}
}