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 package info.nightscout.core.extensions
import app.aaps.shared.tests.TestBaseWithProfile import app.aaps.shared.tests.TestBaseWithProfile
import com.google.common.truth.Truth.assertThat
import info.nightscout.database.entities.ExtendedBolus import info.nightscout.database.entities.ExtendedBolus
import info.nightscout.database.entities.TemporaryBasal import info.nightscout.database.entities.TemporaryBasal
import info.nightscout.insulin.InsulinLyumjevPlugin 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.profile.ProfileFunction
import info.nightscout.interfaces.ui.UiInteraction import info.nightscout.interfaces.ui.UiInteraction
import info.nightscout.shared.utils.T import info.nightscout.shared.utils.T
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.BeforeEach import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test import org.junit.jupiter.api.Test
import org.mockito.Mock import org.mockito.Mock
@ -25,72 +25,61 @@ class ExtendedBolusExtensionKtTest : TestBaseWithProfile() {
private val dia = 7.0 private val dia = 7.0
@BeforeEach @BeforeEach fun setup() {
fun setup() {
insulin = InsulinLyumjevPlugin(profileInjector, rh, profileFunctions, rxBus, aapsLogger, config, hardLimits, uiInteraction) insulin = InsulinLyumjevPlugin(profileInjector, rh, profileFunctions, rxBus, aapsLogger, config, hardLimits, uiInteraction)
Mockito.`when`(activePlugin.activeInsulin).thenReturn(insulin) Mockito.`when`(activePlugin.activeInsulin).thenReturn(insulin)
Mockito.`when`(dateUtil.now()).thenReturn(now) Mockito.`when`(dateUtil.now()).thenReturn(now)
} }
@Test @Test fun iobCalc() {
fun iobCalc() {
val bolus = ExtendedBolus(timestamp = now - 1, amount = 1.0, duration = T.hours(1).msecs()) val bolus = ExtendedBolus(timestamp = now - 1, amount = 1.0, duration = T.hours(1).msecs())
// there should zero IOB after now // 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 // 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 // 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 // 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 // no IOB for invalid record
bolus.isValid = false 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 bolus.isValid = true
val asResult = AutosensResult() val asResult = AutosensResult()
// there should zero IOB after now // 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 // 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 // there should be less that 5% after DIA -1
Assertions.assertTrue( assertThat(
0.05 > bolus.iobCalc( bolus.iobCalc(
now + T.hours(dia.toLong() - 1).msecs(), now + T.hours(dia.toLong() - 1).msecs(), validProfile, asResult, SMBDefaults.exercise_mode, SMBDefaults.half_basal_exercise_target, true, insulin
validProfile,
asResult,
SMBDefaults.exercise_mode,
SMBDefaults.half_basal_exercise_target,
true,
insulin
).iob ).iob
) ).isLessThan(0.05)
// there should be zero after DIA // there should be zero after DIA
Assertions.assertEquals( assertThat(
0.0,
bolus.iobCalc(now + T.hours(dia.toLong() + 1).msecs(), validProfile, asResult, SMBDefaults.exercise_mode, SMBDefaults.half_basal_exercise_target, true, insulin).iob 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 // no IOB for invalid record
bolus.isValid = false 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 @Test fun isInProgress() {
fun isInProgress() {
val bolus = ExtendedBolus(timestamp = now - 1, amount = 1.0, duration = T.hours(1).msecs()) val bolus = ExtendedBolus(timestamp = now - 1, amount = 1.0, duration = T.hours(1).msecs())
Mockito.`when`(dateUtil.now()).thenReturn(now) 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()) Mockito.`when`(dateUtil.now()).thenReturn(now + T.hours(2).msecs())
Assertions.assertFalse(bolus.isInProgress(dateUtil)) assertThat(bolus.isInProgress(dateUtil)).isFalse()
} }
@Test @Test fun toTemporaryBasal() {
fun toTemporaryBasal() {
val bolus = ExtendedBolus(timestamp = now - 1, amount = 1.0, duration = T.hours(1).msecs()) val bolus = ExtendedBolus(timestamp = now - 1, amount = 1.0, duration = T.hours(1).msecs())
val tbr = bolus.toTemporaryBasal(validProfile) val tbr = bolus.toTemporaryBasal(validProfile)
Assertions.assertEquals(bolus.timestamp, tbr.timestamp) assertThat(tbr.timestamp).isEqualTo(bolus.timestamp)
Assertions.assertEquals(bolus.duration, tbr.duration) assertThat(tbr.duration).isEqualTo(bolus.duration)
Assertions.assertEquals(bolus.rate + validProfile.getBasal(now), tbr.rate) assertThat(tbr.rate).isEqualTo(bolus.rate + validProfile.getBasal(now))
Assertions.assertEquals(TemporaryBasal.Type.FAKE_EXTENDED, tbr.type) assertThat(tbr.type).isEqualTo(TemporaryBasal.Type.FAKE_EXTENDED)
} }
} }