abs insulin tests

This commit is contained in:
Milos Kozak 2020-04-20 19:27:54 +02:00
parent 2517a10a5c
commit ccfe03e177
14 changed files with 103 additions and 18 deletions

View file

@ -446,8 +446,6 @@ public class TreatmentsPlugin extends PluginBase implements TreatmentsInterface
public IobTotal getAbsoluteIOBTempBasals(long time) {
IobTotal total = new IobTotal(time);
PumpInterface pumpInterface = activePlugin.getActivePump();
for (long i = time - range(); i < time; i += T.mins(5).msecs()) {
Profile profile = profileFunction.getProfile(i);
double basal = profile.getBasal(i);
@ -456,7 +454,7 @@ public class TreatmentsPlugin extends PluginBase implements TreatmentsInterface
if (runningTBR != null) {
running = runningTBR.tempBasalConvertedToAbsolute(i, profile);
}
Treatment treatment = new Treatment();
Treatment treatment = new Treatment(getInjector());
treatment.date = i;
treatment.insulin = running * 5.0 / 60.0; // 5 min chunk
Iob iob = treatment.iobCalc(i, profile.getDia());

View file

@ -9,7 +9,10 @@ import info.nightscout.androidaps.interfaces.ActivePluginProvider
import info.nightscout.androidaps.logging.AAPSLogger
import info.nightscout.androidaps.logging.AAPSLoggerTest
import info.nightscout.androidaps.plugins.bus.RxBusWrapper
import info.nightscout.androidaps.plugins.configBuilder.ProfileFunction
import info.nightscout.androidaps.plugins.treatments.Treatment
import info.nightscout.androidaps.plugins.treatments.TreatmentsPlugin
import info.nightscout.androidaps.utils.DefaultValueHelper
import info.nightscout.androidaps.utils.FabricPrivacy
import info.nightscout.androidaps.utils.resources.ResourceHelper
import org.json.JSONObject
@ -23,6 +26,8 @@ open class TestBaseWithProfile : TestBase() {
@Mock lateinit var resourceHelper: ResourceHelper
@Mock lateinit var treatmentsPlugin: TreatmentsPlugin
@Mock lateinit var fabricPrivacy: FabricPrivacy
@Mock lateinit var profileFunction: ProfileFunction
@Mock lateinit var defaultValueHelper: DefaultValueHelper
val rxBus = RxBusWrapper()
@ -41,6 +46,12 @@ open class TestBaseWithProfile : TestBase() {
it.rxBus = rxBus
it.resourceHelper = resourceHelper
}
if (it is Treatment) {
it.activePlugin = activePluginProvider
it.profileFunction = profileFunction
it.defaultValueHelper = defaultValueHelper
it.resourceHelper = resourceHelper
}
}
}

View file

@ -49,7 +49,6 @@ class ConstraintsCheckerTest : TestBaseWithProfile() {
@Mock lateinit var activePlugin: ActivePluginProvider
@Mock lateinit var virtualPumpPlugin: VirtualPumpPlugin
@Mock lateinit var sp: SP
@Mock lateinit var profileFunction: ProfileFunction
@Mock lateinit var commandQueue: CommandQueueProvider
@Mock lateinit var detailedBolusInfoStorage: DetailedBolusInfoStorage
@Mock lateinit var context: Context

View file

@ -33,7 +33,6 @@ class APSResultTest : TestBaseWithProfile() {
@Mock lateinit var constraintChecker: ConstraintChecker
@Mock lateinit var sp: SP
@Mock lateinit var profileFunction: ProfileFunction
@Mock lateinit var virtualPumpPlugin: VirtualPumpPlugin
private var closedLoopEnabled = Constraint(false)

View file

@ -30,7 +30,6 @@ open class ActionsTestBase : TestBaseWithProfile() {
@Mock lateinit var virtualPumpPlugin: VirtualPumpPlugin
@Mock lateinit var loopPlugin: LoopPlugin
@Mock lateinit var activePlugin: ActivePluginProvider
@Mock lateinit var profileFunction: ProfileFunction
@Mock lateinit var localProfilePlugin : LocalProfilePlugin
@Mock lateinit var smsCommunicatorPlugin : SmsCommunicatorPlugin

View file

@ -22,7 +22,6 @@ import org.powermock.core.classloader.annotations.PrepareForTest
@PrepareForTest(LastLocationDataContainer::class, AutomationPlugin::class)
open class TriggerTestBase : TestBaseWithProfile() {
@Mock lateinit var profileFunction: ProfileFunction
@Mock lateinit var sp: SP
@Mock lateinit var locationDataContainer: LastLocationDataContainer
@Mock lateinit var activePlugin: ActivePluginProvider

View file

@ -50,13 +50,11 @@ class SmsCommunicatorPluginTest : TestBaseWithProfile() {
@Mock lateinit var sp: SP
@Mock lateinit var constraintChecker: ConstraintChecker
@Mock lateinit var profileFunction: ProfileFunction
@Mock lateinit var activePlugin: ActivePluginProvider
@Mock lateinit var commandQueue: CommandQueueProvider
@Mock lateinit var loopPlugin: LoopPlugin
@Mock lateinit var iobCobCalculatorPlugin: IobCobCalculatorPlugin
@Mock lateinit var virtualPumpPlugin: VirtualPumpPlugin
@Mock lateinit var defaultValueHelper: DefaultValueHelper
@Mock lateinit var localProfilePlugin: LocalProfilePlugin
@Mock lateinit var treatmentService: TreatmentService
@Mock lateinit var otp: OneTimePassword

View file

@ -30,7 +30,6 @@ class DanaRSPluginTest : DanaRSTestBase() {
@Mock lateinit var context: Context
@Mock lateinit var constraintChecker: ConstraintChecker
@Mock lateinit var profileFunction: ProfileFunction
@Mock lateinit var commandQueue: CommandQueueProvider
@Mock lateinit var detailedBolusInfoStorage: DetailedBolusInfoStorage

View file

@ -23,7 +23,6 @@ import org.powermock.modules.junit4.PowerMockRunner
@PrepareForTest(ConstraintChecker::class, DetailedBolusInfoStorage::class)
class DanaRS_Packet_Bolus_Set_Step_Bolus_StartTest : DanaRSTestBase() {
@Mock lateinit var profileFunction: ProfileFunction
@Mock lateinit var constraintChecker: ConstraintChecker
@Mock lateinit var commandQueue: CommandQueueProvider
@Mock lateinit var context: Context

View file

@ -21,8 +21,6 @@ import org.powermock.modules.junit4.PowerMockRunner
@PrepareForTest(RxBusWrapper::class)
class DanaRS_Packet_Bolus_Set_Step_Bolus_StopTest : DanaRSTestBase() {
@Mock lateinit var defaultValueHelper: DefaultValueHelper
@Mock lateinit var profileFunction: ProfileFunction
@Mock lateinit var activePlugin: ActivePluginProvider
@Mock lateinit var danaRSPlugin: DanaRSPlugin

View file

@ -22,9 +22,7 @@ import org.powermock.modules.junit4.PowerMockRunner
@PrepareForTest(RxBusWrapper::class)
class DanaRS_Packet_Notify_Delivery_CompleteTest : DanaRSTestBase() {
@Mock lateinit var defaultValueHelper: DefaultValueHelper
@Mock lateinit var danaRSPlugin: DanaRSPlugin
@Mock lateinit var profileFunction: ProfileFunction
@Mock lateinit var activePlugin: ActivePluginProvider
private var treatmentInjector: HasAndroidInjector = HasAndroidInjector {

View file

@ -27,8 +27,6 @@ import org.powermock.modules.junit4.PowerMockRunner
@PrepareForTest(ConstraintChecker::class, RxBusWrapper::class, DetailedBolusInfoStorage::class)
class DanaRS_Packet_Notify_Delivery_Rate_DisplayTest : DanaRSTestBase() {
@Mock lateinit var defaultValueHelper: DefaultValueHelper
@Mock lateinit var profileFunction: ProfileFunction
@Mock lateinit var activePlugin: ActivePluginProvider
@Mock lateinit var constraintChecker: ConstraintChecker
@Mock lateinit var commandQueue: CommandQueueProvider

View file

@ -0,0 +1,91 @@
package info.nightscout.androidaps.plugins.treatments
import android.content.Context
import info.nightscout.androidaps.MainApp
import info.nightscout.androidaps.TestBaseWithProfile
import info.nightscout.androidaps.db.DatabaseHelper
import info.nightscout.androidaps.db.TemporaryBasal
import info.nightscout.androidaps.logging.LTag
import info.nightscout.androidaps.plugins.insulin.InsulinOrefRapidActingPlugin
import info.nightscout.androidaps.utils.DateUtil
import info.nightscout.androidaps.utils.FabricPrivacy
import info.nightscout.androidaps.utils.T
import info.nightscout.androidaps.utils.sharedPreferences.SP
import org.junit.Assert
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.ArgumentMatchers
import org.mockito.Mock
import org.mockito.Mockito
import org.mockito.Mockito.`when`
import org.powermock.api.mockito.PowerMockito
import org.powermock.core.classloader.annotations.PrepareForTest
import org.powermock.modules.junit4.PowerMockRunner
@RunWith(PowerMockRunner::class)
@PrepareForTest(FabricPrivacy::class, MainApp::class, DatabaseHelper::class)
class TreatmentsPluginTest : TestBaseWithProfile() {
@Mock lateinit var context: Context
@Mock lateinit var sp: SP
@Mock lateinit var databaseHelper: DatabaseHelper
@Mock lateinit var treatmentService: TreatmentService
lateinit var insulinOrefRapidActingPlugin: InsulinOrefRapidActingPlugin
lateinit var sot: TreatmentsPlugin
@Before
fun prepare() {
PowerMockito.mockStatic(MainApp::class.java)
`when`(MainApp.getDbHelper()).thenReturn(databaseHelper)
insulinOrefRapidActingPlugin = InsulinOrefRapidActingPlugin(profileInjector, resourceHelper, profileFunction, rxBus, aapsLogger)
`when`(profileFunction.getProfile(ArgumentMatchers.anyLong())).thenReturn(validProfile)
`when`(activePluginProvider.activeInsulin).thenReturn(insulinOrefRapidActingPlugin)
sot = TreatmentsPlugin(profileInjector, aapsLogger, rxBus, resourceHelper, context, sp, profileFunction, activePluginProvider, fabricPrivacy)
sot.service = treatmentService
}
@Test
fun `zero TBR should produce zero absolute insulin`() {
val now = DateUtil.now()
val tbrs : MutableList<TemporaryBasal> = ArrayList()
tbrs.add(TemporaryBasal().date(now - T.hours(30). msecs()).duration(10000).percent(0))
`when`(databaseHelper.getTemporaryBasalsDataFromTime(ArgumentMatchers.anyLong(), ArgumentMatchers.anyBoolean())).thenReturn(tbrs)
sot.initializeData(T.hours(30). msecs())
val iob = sot.getAbsoluteIOBTempBasals(now)
Assert.assertEquals(0.0, iob.iob, 0.0)
}
@Test
fun `90% TBR and should produce less absolute insulin`() {
val now = DateUtil.now()
val tbrs : MutableList<TemporaryBasal> = ArrayList()
`when`(databaseHelper.getTemporaryBasalsDataFromTime(ArgumentMatchers.anyLong(), ArgumentMatchers.anyBoolean())).thenReturn(tbrs)
sot.initializeData(T.hours(30). msecs())
val iob100pct = sot.getAbsoluteIOBTempBasals(now)
tbrs.add(TemporaryBasal().date(now - T.hours(30). msecs()).duration(10000).percent(90))
sot.initializeData(T.hours(30). msecs())
val iob90pct = sot.getAbsoluteIOBTempBasals(now)
Assert.assertTrue(iob100pct.iob > iob90pct.iob)
}
@Test
fun `110% TBR and should produce 10% more absolute insulin`() {
val now = DateUtil.now()
val tbrs : MutableList<TemporaryBasal> = ArrayList()
`when`(databaseHelper.getTemporaryBasalsDataFromTime(ArgumentMatchers.anyLong(), ArgumentMatchers.anyBoolean())).thenReturn(tbrs)
sot.initializeData(T.hours(30). msecs())
val iob100pct = sot.getAbsoluteIOBTempBasals(now)
tbrs.add(TemporaryBasal().date(now - T.hours(30). msecs()).duration(10000).percent(110))
sot.initializeData(T.hours(30). msecs())
val iob110pct = sot.getAbsoluteIOBTempBasals(now)
Assert.assertEquals(1.1, iob110pct.iob / iob100pct.iob, 0.0001)
}
}

View file

@ -36,7 +36,6 @@ import java.util.*
class CommandQueueTest : TestBaseWithProfile() {
@Mock lateinit var constraintChecker: ConstraintChecker
@Mock lateinit var profileFunction: ProfileFunction
@Mock lateinit var lazyActivePlugin: Lazy<ActivePluginProvider>
@Mock lateinit var activePlugin: ActivePluginProvider
@Mock lateinit var context: Context