2019-02-26 20:38:27 +01:00
|
|
|
package info.nightscout.androidaps.utils;
|
2018-01-07 22:16:08 +01:00
|
|
|
|
|
|
|
import org.junit.Assert;
|
|
|
|
import org.junit.Test;
|
|
|
|
import org.junit.runner.RunWith;
|
2019-06-16 09:00:47 +02:00
|
|
|
import org.mockito.Mockito;
|
2018-01-07 22:16:08 +01:00
|
|
|
import org.powermock.api.mockito.PowerMockito;
|
|
|
|
import org.powermock.core.classloader.annotations.PrepareForTest;
|
|
|
|
import org.powermock.modules.junit4.PowerMockRunner;
|
|
|
|
|
2018-09-17 17:43:42 +02:00
|
|
|
import info.AAPSMocker;
|
2018-01-07 22:16:08 +01:00
|
|
|
import info.nightscout.androidaps.MainApp;
|
2019-06-16 09:00:47 +02:00
|
|
|
import info.nightscout.androidaps.data.ConstraintChecker;
|
|
|
|
import info.nightscout.androidaps.interfaces.Constraint;
|
2019-11-12 17:37:44 +01:00
|
|
|
import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions;
|
2019-04-16 20:36:18 +02:00
|
|
|
import info.nightscout.androidaps.plugins.iob.iobCobCalculator.GlucoseStatus;
|
2018-01-07 22:16:08 +01:00
|
|
|
import info.nightscout.androidaps.data.IobTotal;
|
|
|
|
import info.nightscout.androidaps.data.Profile;
|
|
|
|
import info.nightscout.androidaps.interfaces.PumpInterface;
|
2019-02-28 23:16:50 +01:00
|
|
|
import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin;
|
|
|
|
import info.nightscout.androidaps.plugins.treatments.TreatmentsPlugin;
|
2019-02-26 20:38:27 +01:00
|
|
|
import info.nightscout.androidaps.plugins.pump.mdi.MDIPlugin;
|
2018-01-07 22:16:08 +01:00
|
|
|
|
2019-06-16 09:00:47 +02:00
|
|
|
import static org.mockito.ArgumentMatchers.any;
|
2018-01-07 22:16:08 +01:00
|
|
|
import static org.mockito.Mockito.mock;
|
|
|
|
import static org.mockito.Mockito.when;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Created by kuchjir on 12/12/2017.
|
|
|
|
*/
|
|
|
|
@RunWith(PowerMockRunner.class)
|
2019-11-12 17:37:44 +01:00
|
|
|
@PrepareForTest({MainApp.class, GlucoseStatus.class, ConfigBuilderPlugin.class, TreatmentsPlugin.class, ConstraintChecker.class, ProfileFunctions.class})
|
2018-01-07 22:16:08 +01:00
|
|
|
public class BolusWizardTest {
|
|
|
|
private static final double PUMP_BOLUS_STEP = 0.1;
|
|
|
|
|
|
|
|
@Test
|
|
|
|
/** Should calculate the same bolus when different blood glucose but both in target range */
|
2019-06-16 09:00:47 +02:00
|
|
|
public void shouldCalculateTheSameBolusWhenBGsInRange() throws Exception {
|
2018-01-07 22:16:08 +01:00
|
|
|
Profile profile = setupProfile(4d, 8d, 20d, 12d);
|
|
|
|
|
2019-06-16 09:00:47 +02:00
|
|
|
BolusWizard bw = new BolusWizard(profile, "", null, 20, 0.0, 4.2, 0d, 100d, true, true, true, true, false, false, false);
|
2019-06-21 13:23:37 +02:00
|
|
|
Double bolusForBg42 = bw.getCalculatedTotalInsulin();
|
2019-06-16 09:00:47 +02:00
|
|
|
bw = new BolusWizard(profile, "", null, 20, 0.0, 5.4, 0d, 100d, true, true, true, true, false, false, false);
|
2019-06-21 13:23:37 +02:00
|
|
|
Double bolusForBg54 = bw.getCalculatedTotalInsulin();
|
2018-01-07 22:16:08 +01:00
|
|
|
Assert.assertEquals(bolusForBg42, bolusForBg54);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2019-06-16 09:00:47 +02:00
|
|
|
public void shouldCalculateHigherBolusWhenHighBG() throws Exception {
|
2018-01-07 22:16:08 +01:00
|
|
|
Profile profile = setupProfile(4d, 8d, 20d, 12d);
|
|
|
|
|
2019-06-16 09:00:47 +02:00
|
|
|
BolusWizard bw = new BolusWizard(profile, "", null, 20, 0.0, 9.8, 0d, 100d, true, true, true, true, false, false, false);
|
2019-06-21 13:23:37 +02:00
|
|
|
Double bolusForHighBg = bw.getCalculatedTotalInsulin();
|
2019-06-16 09:00:47 +02:00
|
|
|
bw = new BolusWizard(profile, "", null, 20, 0.0, 5.4, 0d, 100d, true, true, true, true, false, false, false);
|
2019-06-21 13:23:37 +02:00
|
|
|
Double bolusForBgInRange = bw.getCalculatedTotalInsulin();
|
2018-01-07 22:16:08 +01:00
|
|
|
Assert.assertTrue(bolusForHighBg > bolusForBgInRange);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2019-06-16 09:00:47 +02:00
|
|
|
public void shouldCalculateLowerBolusWhenLowBG() throws Exception {
|
2018-01-07 22:16:08 +01:00
|
|
|
Profile profile = setupProfile(4d, 8d, 20d, 12d);
|
|
|
|
|
2019-06-16 09:00:47 +02:00
|
|
|
BolusWizard bw = new BolusWizard(profile, "", null, 20, 0.0, 3.6, 0d, 100d, true, true, true, true, false, false, false);
|
2019-06-21 13:23:37 +02:00
|
|
|
Double bolusForLowBg = bw.getCalculatedTotalInsulin();
|
2019-06-16 09:00:47 +02:00
|
|
|
bw = new BolusWizard(profile, "", null, 20, 0.0, 5.4, 0d, 100d, true, true, true, true, false, false, false);
|
2019-06-21 13:23:37 +02:00
|
|
|
Double bolusForBgInRange = bw.getCalculatedTotalInsulin();
|
2018-01-07 22:16:08 +01:00
|
|
|
Assert.assertTrue(bolusForLowBg < bolusForBgInRange);
|
|
|
|
}
|
|
|
|
|
|
|
|
private Profile setupProfile(Double targetLow, Double targetHigh, Double insulinSensitivityFactor, Double insulinToCarbRatio) {
|
|
|
|
Profile profile = mock(Profile.class);
|
2019-11-12 17:37:44 +01:00
|
|
|
when(profile.getTargetLowMgdl()).thenReturn(targetLow);
|
|
|
|
when(profile.getTargetHighMgdl()).thenReturn(targetHigh);
|
|
|
|
when(profile.getIsfMgdl()).thenReturn(insulinSensitivityFactor);
|
2018-01-07 22:16:08 +01:00
|
|
|
when(profile.getIc()).thenReturn(insulinToCarbRatio);
|
|
|
|
|
|
|
|
PowerMockito.mockStatic(GlucoseStatus.class);
|
|
|
|
when(GlucoseStatus.getGlucoseStatusData()).thenReturn(null);
|
|
|
|
|
2018-04-01 11:09:58 +02:00
|
|
|
PowerMockito.mockStatic(TreatmentsPlugin.class);
|
|
|
|
TreatmentsPlugin treatment = mock(TreatmentsPlugin.class);
|
2018-01-07 22:16:08 +01:00
|
|
|
IobTotal iobTotalZero = new IobTotal(System.currentTimeMillis());
|
|
|
|
when(treatment.getLastCalculationTreatments()).thenReturn(iobTotalZero);
|
|
|
|
when(treatment.getLastCalculationTempBasals()).thenReturn(iobTotalZero);
|
|
|
|
PowerMockito.mockStatic(MainApp.class);
|
2018-04-01 11:09:58 +02:00
|
|
|
when(TreatmentsPlugin.getPlugin()).thenReturn(treatment);
|
2018-01-07 22:16:08 +01:00
|
|
|
|
2018-09-17 17:43:42 +02:00
|
|
|
AAPSMocker.mockConfigBuilder();
|
2018-01-07 22:16:08 +01:00
|
|
|
PumpInterface pump = MDIPlugin.getPlugin();
|
|
|
|
pump.getPumpDescription().bolusStep = PUMP_BOLUS_STEP;
|
2018-09-17 17:43:42 +02:00
|
|
|
when(ConfigBuilderPlugin.getPlugin().getActivePump()).thenReturn(pump);
|
2018-01-07 22:16:08 +01:00
|
|
|
|
2019-06-16 09:00:47 +02:00
|
|
|
AAPSMocker.mockConstraintsChecker();
|
|
|
|
Mockito.doAnswer(invocation -> {
|
|
|
|
Constraint<Double> constraint = invocation.getArgument(0);
|
|
|
|
return constraint;
|
|
|
|
}).when(AAPSMocker.constraintChecker).applyBolusConstraints(any(Constraint.class));
|
|
|
|
|
2019-11-12 17:37:44 +01:00
|
|
|
AAPSMocker.mockProfileFunctions();
|
2018-01-07 22:16:08 +01:00
|
|
|
return profile;
|
|
|
|
}
|
|
|
|
}
|