diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/treatments/TreatmentsPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/treatments/TreatmentsPlugin.java index 9e40ade91b..c3de444efd 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/treatments/TreatmentsPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/treatments/TreatmentsPlugin.java @@ -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()); diff --git a/app/src/test/java/info/nightscout/androidaps/TestBaseWithProfile.kt b/app/src/test/java/info/nightscout/androidaps/TestBaseWithProfile.kt index d36c2f961b..6272726ff1 100644 --- a/app/src/test/java/info/nightscout/androidaps/TestBaseWithProfile.kt +++ b/app/src/test/java/info/nightscout/androidaps/TestBaseWithProfile.kt @@ -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 + } } } diff --git a/app/src/test/java/info/nightscout/androidaps/interfaces/ConstraintsCheckerTest.kt b/app/src/test/java/info/nightscout/androidaps/interfaces/ConstraintsCheckerTest.kt index a532ced011..f90c83cdcc 100644 --- a/app/src/test/java/info/nightscout/androidaps/interfaces/ConstraintsCheckerTest.kt +++ b/app/src/test/java/info/nightscout/androidaps/interfaces/ConstraintsCheckerTest.kt @@ -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 diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/aps/loop/APSResultTest.kt b/app/src/test/java/info/nightscout/androidaps/plugins/aps/loop/APSResultTest.kt index 5db025db4c..df6e328489 100644 --- a/app/src/test/java/info/nightscout/androidaps/plugins/aps/loop/APSResultTest.kt +++ b/app/src/test/java/info/nightscout/androidaps/plugins/aps/loop/APSResultTest.kt @@ -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) diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionsTestBase.kt b/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionsTestBase.kt index 1afef7136c..55173656b2 100644 --- a/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionsTestBase.kt +++ b/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionsTestBase.kt @@ -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 diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerTestBase.kt b/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerTestBase.kt index d130d031f2..a886ef27c6 100644 --- a/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerTestBase.kt +++ b/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerTestBase.kt @@ -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 diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/general/smsCommunicator/SmsCommunicatorPluginTest.kt b/app/src/test/java/info/nightscout/androidaps/plugins/general/smsCommunicator/SmsCommunicatorPluginTest.kt index 2ce83f0164..aa3f522264 100644 --- a/app/src/test/java/info/nightscout/androidaps/plugins/general/smsCommunicator/SmsCommunicatorPluginTest.kt +++ b/app/src/test/java/info/nightscout/androidaps/plugins/general/smsCommunicator/SmsCommunicatorPluginTest.kt @@ -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 diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/pump/danaRS/DanaRSPluginTest.kt b/app/src/test/java/info/nightscout/androidaps/plugins/pump/danaRS/DanaRSPluginTest.kt index a9a13dbc90..6df25b94c7 100644 --- a/app/src/test/java/info/nightscout/androidaps/plugins/pump/danaRS/DanaRSPluginTest.kt +++ b/app/src/test/java/info/nightscout/androidaps/plugins/pump/danaRS/DanaRSPluginTest.kt @@ -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 diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/pump/danaRS/comm/DanaRS_Packet_Bolus_Set_Step_Bolus_StartTest.kt b/app/src/test/java/info/nightscout/androidaps/plugins/pump/danaRS/comm/DanaRS_Packet_Bolus_Set_Step_Bolus_StartTest.kt index 420b0a73d8..6c1f96cdc7 100644 --- a/app/src/test/java/info/nightscout/androidaps/plugins/pump/danaRS/comm/DanaRS_Packet_Bolus_Set_Step_Bolus_StartTest.kt +++ b/app/src/test/java/info/nightscout/androidaps/plugins/pump/danaRS/comm/DanaRS_Packet_Bolus_Set_Step_Bolus_StartTest.kt @@ -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 diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/pump/danaRS/comm/DanaRS_Packet_Bolus_Set_Step_Bolus_StopTest.kt b/app/src/test/java/info/nightscout/androidaps/plugins/pump/danaRS/comm/DanaRS_Packet_Bolus_Set_Step_Bolus_StopTest.kt index d4854c12cd..ac5b33158b 100644 --- a/app/src/test/java/info/nightscout/androidaps/plugins/pump/danaRS/comm/DanaRS_Packet_Bolus_Set_Step_Bolus_StopTest.kt +++ b/app/src/test/java/info/nightscout/androidaps/plugins/pump/danaRS/comm/DanaRS_Packet_Bolus_Set_Step_Bolus_StopTest.kt @@ -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 diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/pump/danaRS/comm/DanaRS_Packet_Notify_Delivery_CompleteTest.kt b/app/src/test/java/info/nightscout/androidaps/plugins/pump/danaRS/comm/DanaRS_Packet_Notify_Delivery_CompleteTest.kt index a7246493e6..790117afc2 100644 --- a/app/src/test/java/info/nightscout/androidaps/plugins/pump/danaRS/comm/DanaRS_Packet_Notify_Delivery_CompleteTest.kt +++ b/app/src/test/java/info/nightscout/androidaps/plugins/pump/danaRS/comm/DanaRS_Packet_Notify_Delivery_CompleteTest.kt @@ -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 { diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/pump/danaRS/comm/DanaRS_Packet_Notify_Delivery_Rate_DisplayTest.kt b/app/src/test/java/info/nightscout/androidaps/plugins/pump/danaRS/comm/DanaRS_Packet_Notify_Delivery_Rate_DisplayTest.kt index 06f69cadbd..37a1197b4f 100644 --- a/app/src/test/java/info/nightscout/androidaps/plugins/pump/danaRS/comm/DanaRS_Packet_Notify_Delivery_Rate_DisplayTest.kt +++ b/app/src/test/java/info/nightscout/androidaps/plugins/pump/danaRS/comm/DanaRS_Packet_Notify_Delivery_Rate_DisplayTest.kt @@ -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 diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/treatments/TreatmentsPluginTest.kt b/app/src/test/java/info/nightscout/androidaps/plugins/treatments/TreatmentsPluginTest.kt new file mode 100644 index 0000000000..7cf920af02 --- /dev/null +++ b/app/src/test/java/info/nightscout/androidaps/plugins/treatments/TreatmentsPluginTest.kt @@ -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 = 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 = 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 = 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) + } +} \ No newline at end of file diff --git a/app/src/test/java/info/nightscout/androidaps/queue/CommandQueueTest.kt b/app/src/test/java/info/nightscout/androidaps/queue/CommandQueueTest.kt index 4e81ef83b4..5dd11ba4a0 100644 --- a/app/src/test/java/info/nightscout/androidaps/queue/CommandQueueTest.kt +++ b/app/src/test/java/info/nightscout/androidaps/queue/CommandQueueTest.kt @@ -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 @Mock lateinit var activePlugin: ActivePluginProvider @Mock lateinit var context: Context