This commit is contained in:
Milos Kozak 2020-03-12 19:47:54 +01:00
parent 6e86a32210
commit bac9de1f66
11 changed files with 412 additions and 394 deletions

View file

@ -36,10 +36,9 @@ import javax.inject.Inject
import kotlin.math.abs import kotlin.math.abs
class BolusWizard @Inject constructor( class BolusWizard @Inject constructor(
injector: HasAndroidInjector val injector: HasAndroidInjector
) { ) {
@Inject lateinit var injector: HasAndroidInjector
@Inject lateinit var aapsLogger: AAPSLogger @Inject lateinit var aapsLogger: AAPSLogger
@Inject lateinit var resourceHelper: ResourceHelper @Inject lateinit var resourceHelper: ResourceHelper
@Inject lateinit var rxBus: RxBusWrapper @Inject lateinit var rxBus: RxBusWrapper

View file

@ -1,41 +0,0 @@
package info.nightscout.androidaps.data;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.modules.junit4.PowerMockRunner;
/**
* Created by mike on 27.03.2018.
*/
@RunWith(PowerMockRunner.class)
public class IobTest {
@Test
public void plusTest() {
Iob a = new Iob().iobContrib(1).activityContrib(2);
Iob b = new Iob().iobContrib(3).activityContrib(4);
a.plus(b);
Assert.assertEquals(4d, a.iobContrib, 0.01d);
Assert.assertEquals(6d, a.activityContrib, 0.01d);
}
@Test
public void equalTest() {
Iob a1 = new Iob().iobContrib(1).activityContrib(2);
Iob a2 = new Iob().iobContrib(1).activityContrib(2);
Iob b = new Iob().iobContrib(3).activityContrib(4);
Assert.assertEquals(true, a1.equals(a1));
Assert.assertEquals(true, a1.equals(a2));
Assert.assertEquals(false, a1.equals(b));
Assert.assertEquals(false, a1.equals(null));
Assert.assertEquals(false, a1.equals(new Object()));
}
@Test
public void hashCodeTest() {
Iob a = new Iob().iobContrib(1).activityContrib(2);
Assert.assertNotEquals(0, a.hashCode());
}
}

View file

@ -0,0 +1,34 @@
package info.nightscout.androidaps.data
import org.junit.Assert
import org.junit.Test
import org.junit.runner.RunWith
import org.powermock.modules.junit4.PowerMockRunner
@RunWith(PowerMockRunner::class)
class IobTest {
@Test fun plusTest() {
val a = Iob().iobContrib(1.0).activityContrib(2.0)
val b = Iob().iobContrib(3.0).activityContrib(4.0)
a.plus(b)
Assert.assertEquals(4.0, a.iobContrib, 0.01)
Assert.assertEquals(6.0, a.activityContrib, 0.01)
}
@Test fun equalTest() {
val a1 = Iob().iobContrib(1.0).activityContrib(2.0)
val a2 = Iob().iobContrib(1.0).activityContrib(2.0)
val b = Iob().iobContrib(3.0).activityContrib(4.0)
Assert.assertTrue(a1 == a1)
Assert.assertTrue(a1 == a2)
Assert.assertFalse(a1 == b)
Assert.assertFalse(a1 == null)
Assert.assertFalse(a1 == Any())
}
@Test fun hashCodeTest() {
val a = Iob().iobContrib(1.0).activityContrib(2.0)
Assert.assertNotEquals(0, a.hashCode().toLong())
}
}

View file

@ -1,143 +0,0 @@
package info.nightscout.androidaps.data;
import org.json.JSONObject;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.modules.junit4.PowerMockRunner;
import info.nightscout.androidaps.utils.DateUtil;
/**
* Created by mike on 27.03.2018.
*/
@RunWith(PowerMockRunner.class)
public class IobTotalTest {
long now = DateUtil.now();
@Test
public void copytest() {
IobTotal a = new IobTotal(now);
a.iob = 10;
IobTotal b = a.copy();
Assert.assertEquals(a.iob, b.iob, 0.01d);
}
@Test
public void plusTest() {
IobTotal a = new IobTotal(now);
a.iob = 10;
a.activity = 10;
a.bolussnooze = 10;
a.basaliob = 10;
a.netbasalinsulin = 10;
a.hightempinsulin = 10;
a.netInsulin = 10;
a.extendedBolusInsulin = 10;
a.plus(a.copy());
Assert.assertEquals(20, a.iob, 0.01d);
Assert.assertEquals(20, a.activity, 0.01d);
Assert.assertEquals(20, a.bolussnooze, 0.01d);
Assert.assertEquals(20, a.basaliob, 0.01d);
Assert.assertEquals(20, a.netbasalinsulin, 0.01d);
Assert.assertEquals(20, a.hightempinsulin, 0.01d);
Assert.assertEquals(20, a.netInsulin, 0.01d);
Assert.assertEquals(20, a.extendedBolusInsulin, 0.01d);
}
@Test
public void combineTest() {
IobTotal a = new IobTotal(now);
a.iob = 10;
a.activity = 11;
a.bolussnooze = 12;
a.basaliob = 13;
a.netbasalinsulin = 14;
a.hightempinsulin = 15;
a.netInsulin = 16;
a.extendedBolusInsulin = 17;
IobTotal b = a.copy();
IobTotal c = IobTotal.combine(a, b);
Assert.assertEquals(a.time, c.time, 0.01d);
Assert.assertEquals(23, c.iob, 0.01d);
Assert.assertEquals(22, c.activity, 0.01d);
Assert.assertEquals(12, c.bolussnooze, 0.01d);
Assert.assertEquals(26, c.basaliob, 0.01d);
Assert.assertEquals(28, c.netbasalinsulin, 0.01d);
Assert.assertEquals(30, c.hightempinsulin, 0.01d);
Assert.assertEquals(32, c.netInsulin, 0.01d);
Assert.assertEquals(34, c.extendedBolusInsulin, 0.01d);
}
@Test
public void roundTest() {
IobTotal a = new IobTotal(now);
a.iob = 1.1111111111111;
a.activity = 1.1111111111111;
a.bolussnooze = 1.1111111111111;
a.basaliob = 1.1111111111111;
a.netbasalinsulin = 1.1111111111111;
a.hightempinsulin = 1.1111111111111;
a.netInsulin = 1.1111111111111;
a.extendedBolusInsulin = 1.1111111111111;
a.round();
Assert.assertEquals(1.111d, a.iob, 0.00001d);
Assert.assertEquals(1.1111d, a.activity, 0.00001d);
Assert.assertEquals(1.1111d, a.bolussnooze, 0.00001d);
Assert.assertEquals(1.111d, a.basaliob, 0.00001d);
Assert.assertEquals(1.111d, a.netbasalinsulin, 0.00001d);
Assert.assertEquals(1.111d, a.hightempinsulin, 0.00001d);
Assert.assertEquals(1.111d, a.netInsulin, 0.00001d);
Assert.assertEquals(1.111d, a.extendedBolusInsulin, 0.00001d);
}
@Test
public void jsonTest() {
IobTotal a = new IobTotal(now);
a.iob = 10;
a.activity = 11;
a.bolussnooze = 12;
a.basaliob = 13;
a.netbasalinsulin = 14;
a.hightempinsulin = 15;
a.netInsulin = 16;
a.extendedBolusInsulin = 17;
try {
JSONObject j = a.json();
Assert.assertEquals(a.iob, j.getDouble("iob"), 0.0000001d);
Assert.assertEquals(a.basaliob, j.getDouble("basaliob"), 0.0000001d);
Assert.assertEquals(a.activity, j.getDouble("activity"), 0.0000001d);
Assert.assertEquals(now, DateUtil.fromISODateString(j.getString("time")).getTime(), 1000);
} catch (Exception e) {
Assert.fail("Exception: " + e.getMessage());
}
}
@Test
public void determineBasalJsonTest() {
IobTotal a = new IobTotal(now);
a.iob = 10;
a.activity = 11;
a.bolussnooze = 12;
a.basaliob = 13;
a.netbasalinsulin = 14;
a.hightempinsulin = 15;
a.netInsulin = 16;
a.extendedBolusInsulin = 17;
a.iobWithZeroTemp = new IobTotal(now);
try {
JSONObject j = a.determineBasalJson();
Assert.assertEquals(a.iob, j.getDouble("iob"), 0.0000001d);
Assert.assertEquals(a.basaliob, j.getDouble("basaliob"), 0.0000001d);
Assert.assertEquals(a.bolussnooze, j.getDouble("bolussnooze"), 0.0000001d);
Assert.assertEquals(a.activity, j.getDouble("activity"), 0.0000001d);
Assert.assertEquals(0, j.getLong("lastBolusTime"));
Assert.assertEquals(now, DateUtil.fromISODateString(j.getString("time")).getTime(), 1000);
Assert.assertNotNull(j.getJSONObject("iobWithZeroTemp"));
} catch (Exception e) {
Assert.fail("Exception: " + e.getMessage());
}
}
}

View file

@ -0,0 +1,130 @@
package info.nightscout.androidaps.data
import info.nightscout.androidaps.utils.DateUtil
import org.junit.Assert
import org.junit.Test
import org.junit.runner.RunWith
import org.powermock.modules.junit4.PowerMockRunner
@RunWith(PowerMockRunner::class)
class IobTotalTest {
var now = DateUtil.now()
@Test fun copytest() {
val a = IobTotal(now)
a.iob = 10.0
val b = a.copy()
Assert.assertEquals(a.iob, b.iob, 0.01)
}
@Test fun plusTest() {
val a = IobTotal(now)
a.iob = 10.0
a.activity = 10.0
a.bolussnooze = 10.0
a.basaliob = 10.0
a.netbasalinsulin = 10.0
a.hightempinsulin = 10.0
a.netInsulin = 10.0
a.extendedBolusInsulin = 10.0
a.plus(a.copy())
Assert.assertEquals(20.0, a.iob, 0.01)
Assert.assertEquals(20.0, a.activity, 0.01)
Assert.assertEquals(20.0, a.bolussnooze, 0.01)
Assert.assertEquals(20.0, a.basaliob, 0.01)
Assert.assertEquals(20.0, a.netbasalinsulin, 0.01)
Assert.assertEquals(20.0, a.hightempinsulin, 0.01)
Assert.assertEquals(20.0, a.netInsulin, 0.01)
Assert.assertEquals(20.0, a.extendedBolusInsulin, 0.01)
}
@Test fun combineTest() {
val a = IobTotal(now)
a.iob = 10.0
a.activity = 11.0
a.bolussnooze = 12.0
a.basaliob = 13.0
a.netbasalinsulin = 14.0
a.hightempinsulin = 15.0
a.netInsulin = 16.0
a.extendedBolusInsulin = 17.0
val b = a.copy()
val c = IobTotal.combine(a, b)
Assert.assertEquals(a.time.toDouble(), c.time.toDouble(), 0.01)
Assert.assertEquals(23.0, c.iob, 0.01)
Assert.assertEquals(22.0, c.activity, 0.01)
Assert.assertEquals(12.0, c.bolussnooze, 0.01)
Assert.assertEquals(26.0, c.basaliob, 0.01)
Assert.assertEquals(28.0, c.netbasalinsulin, 0.01)
Assert.assertEquals(30.0, c.hightempinsulin, 0.01)
Assert.assertEquals(32.0, c.netInsulin, 0.01)
Assert.assertEquals(34.0, c.extendedBolusInsulin, 0.01)
}
@Test fun roundTest() {
val a = IobTotal(now)
a.iob = 1.1111111111111
a.activity = 1.1111111111111
a.bolussnooze = 1.1111111111111
a.basaliob = 1.1111111111111
a.netbasalinsulin = 1.1111111111111
a.hightempinsulin = 1.1111111111111
a.netInsulin = 1.1111111111111
a.extendedBolusInsulin = 1.1111111111111
a.round()
Assert.assertEquals(1.111, a.iob, 0.00001)
Assert.assertEquals(1.1111, a.activity, 0.00001)
Assert.assertEquals(1.1111, a.bolussnooze, 0.00001)
Assert.assertEquals(1.111, a.basaliob, 0.00001)
Assert.assertEquals(1.111, a.netbasalinsulin, 0.00001)
Assert.assertEquals(1.111, a.hightempinsulin, 0.00001)
Assert.assertEquals(1.111, a.netInsulin, 0.00001)
Assert.assertEquals(1.111, a.extendedBolusInsulin, 0.00001)
}
@Test fun jsonTest() {
val a = IobTotal(now)
a.iob = 10.0
a.activity = 11.0
a.bolussnooze = 12.0
a.basaliob = 13.0
a.netbasalinsulin = 14.0
a.hightempinsulin = 15.0
a.netInsulin = 16.0
a.extendedBolusInsulin = 17.0
try {
val j = a.json()
Assert.assertEquals(a.iob, j.getDouble("iob"), 0.0000001)
Assert.assertEquals(a.basaliob, j.getDouble("basaliob"), 0.0000001)
Assert.assertEquals(a.activity, j.getDouble("activity"), 0.0000001)
Assert.assertEquals(now.toFloat(), DateUtil.fromISODateString(j.getString("time")).time.toFloat(), 1000f)
} catch (e: Exception) {
Assert.fail("Exception: " + e.message)
}
}
@Test fun determineBasalJsonTest() {
val a = IobTotal(now)
a.iob = 10.0
a.activity = 11.0
a.bolussnooze = 12.0
a.basaliob = 13.0
a.netbasalinsulin = 14.0
a.hightempinsulin = 15.0
a.netInsulin = 16.0
a.extendedBolusInsulin = 17.0
a.iobWithZeroTemp = IobTotal(now)
try {
val j = a.determineBasalJson()
Assert.assertEquals(a.iob, j.getDouble("iob"), 0.0000001)
Assert.assertEquals(a.basaliob, j.getDouble("basaliob"), 0.0000001)
Assert.assertEquals(a.bolussnooze, j.getDouble("bolussnooze"), 0.0000001)
Assert.assertEquals(a.activity, j.getDouble("activity"), 0.0000001)
Assert.assertEquals(0, j.getLong("lastBolusTime"))
Assert.assertEquals(now.toFloat(), DateUtil.fromISODateString(j.getString("time")).time.toFloat(), 1000f)
Assert.assertNotNull(j.getJSONObject("iobWithZeroTemp"))
} catch (e: Exception) {
Assert.fail("Exception: " + e.message)
}
}
}

View file

@ -1,35 +0,0 @@
package info.nightscout.androidaps.plugins.general.automation.elements;
import junit.framework.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import info.nightscout.androidaps.MainApp;
@RunWith(PowerMockRunner.class)
@PrepareForTest({MainApp.class})
public class LayoutBuilderTest {
@Test
public void addTest() {
LayoutBuilder layoutBuilder = new LayoutBuilder();
InputInsulin inputInsulin = new InputInsulin();
layoutBuilder.add(inputInsulin);
Assert.assertEquals(1, layoutBuilder.mElements.size());
}
@Test
public void addConditionalTest() {
LayoutBuilder layoutBuilder = new LayoutBuilder();
InputInsulin inputInsulin = new InputInsulin();
layoutBuilder.add(inputInsulin, true);
Assert.assertEquals(1, layoutBuilder.mElements.size());
layoutBuilder.add(inputInsulin, false);
Assert.assertEquals(1, layoutBuilder.mElements.size());
}
}

View file

@ -0,0 +1,31 @@
package info.nightscout.androidaps.plugins.general.automation.elements
import dagger.android.AndroidInjector
import dagger.android.HasAndroidInjector
import info.TestBase
import org.junit.Assert
import org.junit.Test
import org.junit.runner.RunWith
import org.powermock.modules.junit4.PowerMockRunner
@RunWith(PowerMockRunner::class)
class LayoutBuilderTest : TestBase() {
val injector = HasAndroidInjector { AndroidInjector { } }
@Test fun addTest() {
val layoutBuilder = LayoutBuilder()
val inputInsulin = InputInsulin(injector)
layoutBuilder.add(inputInsulin)
Assert.assertEquals(1, layoutBuilder.mElements.size)
}
@Test fun addConditionalTest() {
val layoutBuilder = LayoutBuilder()
val inputInsulin = InputInsulin(injector)
layoutBuilder.add(inputInsulin, true)
Assert.assertEquals(1, layoutBuilder.mElements.size)
layoutBuilder.add(inputInsulin, false)
Assert.assertEquals(1, layoutBuilder.mElements.size)
}
}

View file

@ -1,70 +0,0 @@
package info.nightscout.androidaps.plugins.pump.combo;
import android.content.Context;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import info.AAPSMocker;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.interfaces.Constraint;
import info.nightscout.androidaps.interfaces.PluginType;
import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin;
import info.nightscout.androidaps.plugins.pump.combo.ruffyscripter.history.Bolus;
import info.nightscout.androidaps.queue.CommandQueue;
import info.nightscout.androidaps.utils.ToastUtils;
import static org.junit.Assert.assertEquals;
@RunWith(PowerMockRunner.class)
@PrepareForTest({MainApp.class, ConfigBuilderPlugin.class, ToastUtils.class, Context.class, CommandQueue.class})
public class ComboPluginTest {
ComboPlugin comboPlugin;
@Test
public void invalidBasalRateOnComboPumpShouldLimitLoopInvokation() throws Exception {
comboPlugin.setPluginEnabled(PluginType.PUMP, true);
comboPlugin.setValidBasalRateProfileSelectedOnPump(false);
Constraint<Boolean> c = new Constraint<>(true);
c = comboPlugin.isLoopInvocationAllowed(c);
Assert.assertEquals("Combo: No valid basal rate read from pump", c.getReasons());
Assert.assertEquals(Boolean.FALSE, c.value());
comboPlugin.setPluginEnabled(PluginType.PUMP, false);
}
@Test
public void calculateFakePumpTimestamp() throws Exception {
long now = System.currentTimeMillis();
long pumpTimestamp = now - now % 1000;
// same timestamp, different bolus leads to different fake timestamp
Assert.assertNotEquals(
comboPlugin.calculateFakeBolusDate(new Bolus(pumpTimestamp, 0.1, true)),
comboPlugin.calculateFakeBolusDate(new Bolus(pumpTimestamp, 0.3, true))
);
// different timestamp, same bolus leads to different fake timestamp
Assert.assertNotEquals(
comboPlugin.calculateFakeBolusDate(new Bolus(pumpTimestamp, 0.3, true)),
comboPlugin.calculateFakeBolusDate(new Bolus(pumpTimestamp + 60 * 1000, 0.3, true))
);
// generated timestamp has second-precision
Bolus bolus = new Bolus(pumpTimestamp, 0.2, true);
long calculatedTimestamp = comboPlugin.calculateFakeBolusDate(bolus);
assertEquals(calculatedTimestamp, calculatedTimestamp - calculatedTimestamp % 1000);
}
@Before
public void prepareMocks() throws Exception {
AAPSMocker.mockMainApp();
AAPSMocker.mockConfigBuilder();
AAPSMocker.mockStrings();
AAPSMocker.mockCommandQueue();
comboPlugin = ComboPlugin.getPlugin();
}
}

View file

@ -0,0 +1,90 @@
package info.nightscout.androidaps.plugins.pump.combo
import android.content.Context
import dagger.android.AndroidInjector
import dagger.android.HasAndroidInjector
import info.AAPSMocker
import info.TestBase
import info.nightscout.androidaps.MainApp
import info.nightscout.androidaps.interfaces.ActivePluginProvider
import info.nightscout.androidaps.interfaces.CommandQueueProvider
import info.nightscout.androidaps.interfaces.Constraint
import info.nightscout.androidaps.interfaces.PluginType
import info.nightscout.androidaps.logging.AAPSLogger
import info.nightscout.androidaps.plugins.bus.RxBusWrapper
import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin
import info.nightscout.androidaps.plugins.configBuilder.ConstraintChecker
import info.nightscout.androidaps.plugins.configBuilder.ProfileFunction
import info.nightscout.androidaps.plugins.pump.combo.ruffyscripter.history.Bolus
import info.nightscout.androidaps.plugins.treatments.TreatmentsPlugin
import info.nightscout.androidaps.queue.CommandQueue
import info.nightscout.androidaps.utils.resources.ResourceHelper
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.Mock
import org.powermock.core.classloader.annotations.PrepareForTest
import org.powermock.modules.junit4.PowerMockRunner
@RunWith(PowerMockRunner::class)
@PrepareForTest(MainApp::class, ConfigBuilderPlugin::class, ConstraintChecker::class, Context::class, CommandQueue::class)
class ComboPluginTest : TestBase() {
@Mock lateinit var aapsLogger: AAPSLogger
@Mock lateinit var resourceHelper: ResourceHelper
@Mock lateinit var profileFunction: ProfileFunction
@Mock lateinit var constraintChecker: ConstraintChecker
@Mock lateinit var activePlugin: ActivePluginProvider
@Mock lateinit var commandQueue: CommandQueueProvider
@Mock lateinit var treatmentsPlugin: TreatmentsPlugin
@Mock lateinit var mainApp: MainApp
@Mock lateinit var sp: SP
val injector = HasAndroidInjector {
AndroidInjector {
}
}
private lateinit var comboPlugin: ComboPlugin
@Test @Throws(Exception::class)
fun invalidBasalRateOnComboPumpShouldLimitLoopInvocation() {
comboPlugin.setPluginEnabled(PluginType.PUMP, true)
comboPlugin.setValidBasalRateProfileSelectedOnPump(false)
var c = Constraint(true)
c = comboPlugin.isLoopInvocationAllowed(c)
Assert.assertEquals("Combo: No valid basal rate read from pump", c.getReasons(aapsLogger))
Assert.assertEquals(false, c.value())
comboPlugin.setPluginEnabled(PluginType.PUMP, false)
}
@Test @Throws(Exception::class)
fun calculateFakePumpTimestamp() {
val now = System.currentTimeMillis()
val pumpTimestamp = now - now % 1000
// same timestamp, different bolus leads to different fake timestamp
Assert.assertNotEquals(
comboPlugin.calculateFakeBolusDate(Bolus(pumpTimestamp, 0.1, true)),
comboPlugin.calculateFakeBolusDate(Bolus(pumpTimestamp, 0.3, true))
)
// different timestamp, same bolus leads to different fake timestamp
Assert.assertNotEquals(
comboPlugin.calculateFakeBolusDate(Bolus(pumpTimestamp, 0.3, true)),
comboPlugin.calculateFakeBolusDate(Bolus(pumpTimestamp + 60 * 1000, 0.3, true))
)
// generated timestamp has second-precision
val bolus = Bolus(pumpTimestamp, 0.2, true)
val calculatedTimestamp = comboPlugin.calculateFakeBolusDate(bolus)
Assert.assertEquals(calculatedTimestamp, calculatedTimestamp - calculatedTimestamp % 1000)
}
@Before @Throws(Exception::class) fun prepareMocks() {
AAPSMocker.mockMainApp()
AAPSMocker.mockConfigBuilder()
AAPSMocker.mockStrings()
AAPSMocker.mockCommandQueue()
comboPlugin = ComboPlugin(injector, aapsLogger, RxBusWrapper(), mainApp, resourceHelper, constraintChecker, profileFunction, treatmentsPlugin, sp, commandQueue)
}
}

View file

@ -1,103 +0,0 @@
package info.nightscout.androidaps.utils;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import info.AAPSMocker;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.plugins.configBuilder.ConstraintChecker;
import info.nightscout.androidaps.interfaces.Constraint;
import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions;
import info.nightscout.androidaps.plugins.iob.iobCobCalculator.GlucoseStatus;
import info.nightscout.androidaps.data.IobTotal;
import info.nightscout.androidaps.data.Profile;
import info.nightscout.androidaps.interfaces.PumpInterface;
import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin;
import info.nightscout.androidaps.plugins.treatments.TreatmentsPlugin;
import info.nightscout.androidaps.plugins.pump.mdi.MDIPlugin;
import info.nightscout.androidaps.utils.wizard.BolusWizard;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
/**
* Created by kuchjir on 12/12/2017.
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest({MainApp.class, GlucoseStatus.class, ConfigBuilderPlugin.class, TreatmentsPlugin.class, ConstraintChecker.class, ProfileFunctions.class})
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 */
public void shouldCalculateTheSameBolusWhenBGsInRange() throws Exception {
Profile profile = setupProfile(4d, 8d, 20d, 12d);
BolusWizard bw = new BolusWizard(profile, "", null, 20, 0.0, 4.2, 0d, 100d, true, true, true, true, false, false, false);
Double bolusForBg42 = bw.getCalculatedTotalInsulin();
bw = new BolusWizard(profile, "", null, 20, 0.0, 5.4, 0d, 100d, true, true, true, true, false, false, false);
Double bolusForBg54 = bw.getCalculatedTotalInsulin();
Assert.assertEquals(bolusForBg42, bolusForBg54);
}
@Test
public void shouldCalculateHigherBolusWhenHighBG() throws Exception {
Profile profile = setupProfile(4d, 8d, 20d, 12d);
BolusWizard bw = new BolusWizard(profile, "", null, 20, 0.0, 9.8, 0d, 100d, true, true, true, true, false, false, false);
Double bolusForHighBg = bw.getCalculatedTotalInsulin();
bw = new BolusWizard(profile, "", null, 20, 0.0, 5.4, 0d, 100d, true, true, true, true, false, false, false);
Double bolusForBgInRange = bw.getCalculatedTotalInsulin();
Assert.assertTrue(bolusForHighBg > bolusForBgInRange);
}
@Test
public void shouldCalculateLowerBolusWhenLowBG() throws Exception {
Profile profile = setupProfile(4d, 8d, 20d, 12d);
BolusWizard bw = new BolusWizard(profile, "", null, 20, 0.0, 3.6, 0d, 100d, true, true, true, true, false, false, false);
Double bolusForLowBg = bw.getCalculatedTotalInsulin();
bw = new BolusWizard(profile, "", null, 20, 0.0, 5.4, 0d, 100d, true, true, true, true, false, false, false);
Double bolusForBgInRange = bw.getCalculatedTotalInsulin();
Assert.assertTrue(bolusForLowBg < bolusForBgInRange);
}
private Profile setupProfile(Double targetLow, Double targetHigh, Double insulinSensitivityFactor, Double insulinToCarbRatio) {
Profile profile = mock(Profile.class);
when(profile.getTargetLowMgdl()).thenReturn(targetLow);
when(profile.getTargetHighMgdl()).thenReturn(targetHigh);
when(profile.getIsfMgdl()).thenReturn(insulinSensitivityFactor);
when(profile.getIc()).thenReturn(insulinToCarbRatio);
PowerMockito.mockStatic(GlucoseStatus.class);
when(GlucoseStatus.getGlucoseStatusData()).thenReturn(null);
PowerMockito.mockStatic(TreatmentsPlugin.class);
TreatmentsPlugin treatment = mock(TreatmentsPlugin.class);
IobTotal iobTotalZero = new IobTotal(System.currentTimeMillis());
when(treatment.getLastCalculationTreatments()).thenReturn(iobTotalZero);
when(treatment.getLastCalculationTempBasals()).thenReturn(iobTotalZero);
PowerMockito.mockStatic(MainApp.class);
when(TreatmentsPlugin.getPlugin()).thenReturn(treatment);
AAPSMocker.mockConfigBuilder();
PumpInterface pump = MDIPlugin.getPlugin();
pump.getPumpDescription().bolusStep = PUMP_BOLUS_STEP;
when(ConfigBuilderPlugin.getPlugin().getActivePump()).thenReturn(pump);
AAPSMocker.mockConstraintsChecker();
Mockito.doAnswer(invocation -> {
Constraint<Double> constraint = invocation.getArgument(0);
return constraint;
}).when(AAPSMocker.constraintChecker).applyBolusConstraints(any(Constraint.class));
AAPSMocker.mockProfileFunctions();
return profile;
}
}

View file

@ -0,0 +1,126 @@
package info.nightscout.androidaps.utils.wizard
import android.content.Context
import dagger.android.AndroidInjector
import dagger.android.HasAndroidInjector
import info.TestBase
import info.nightscout.androidaps.Constants
import info.nightscout.androidaps.data.IobTotal
import info.nightscout.androidaps.data.Profile
import info.nightscout.androidaps.interfaces.ActivePluginProvider
import info.nightscout.androidaps.interfaces.CommandQueueProvider
import info.nightscout.androidaps.interfaces.Constraint
import info.nightscout.androidaps.interfaces.PumpDescription
import info.nightscout.androidaps.logging.AAPSLogger
import info.nightscout.androidaps.plugins.aps.loop.LoopPlugin
import info.nightscout.androidaps.plugins.bus.RxBusWrapper
import info.nightscout.androidaps.plugins.configBuilder.ConstraintChecker
import info.nightscout.androidaps.plugins.configBuilder.ProfileFunction
import info.nightscout.androidaps.plugins.iob.iobCobCalculator.GlucoseStatus
import info.nightscout.androidaps.plugins.iob.iobCobCalculator.IobCobCalculatorPlugin
import info.nightscout.androidaps.plugins.pump.virtual.VirtualPumpPlugin
import info.nightscout.androidaps.plugins.treatments.TreatmentsPlugin
import info.nightscout.androidaps.utils.resources.ResourceHelper
import org.junit.Assert
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mock
import org.mockito.Mockito
import org.mockito.Mockito.`when`
import org.mockito.invocation.InvocationOnMock
import org.powermock.core.classloader.annotations.PrepareForTest
import org.powermock.modules.junit4.PowerMockRunner
@RunWith(PowerMockRunner::class)
@PrepareForTest(ConstraintChecker::class, VirtualPumpPlugin::class)
class BolusWizardTest : TestBase() {
private val PUMP_BOLUS_STEP = 0.1
@Mock lateinit var aapsLogger: AAPSLogger
@Mock lateinit var resourceHelper: ResourceHelper
@Mock lateinit var profileFunction: ProfileFunction
@Mock lateinit var constraintChecker: ConstraintChecker
@Mock lateinit var context: Context
@Mock lateinit var activePlugin: ActivePluginProvider
@Mock lateinit var commandQueue: CommandQueueProvider
@Mock lateinit var loopPlugin: LoopPlugin
@Mock lateinit var iobCobCalculatorPlugin: IobCobCalculatorPlugin
@Mock lateinit var treatmentsPlugin: TreatmentsPlugin
@Mock lateinit var virtualPumpPlugin: VirtualPumpPlugin
val injector = HasAndroidInjector {
AndroidInjector {
if (it is BolusWizard) {
it.aapsLogger = aapsLogger
it.resourceHelper = resourceHelper
it.rxBus = RxBusWrapper()
it.profileFunction = profileFunction
it.constraintChecker = constraintChecker
it.context = context
it.activePlugin = activePlugin
it.commandQueue = commandQueue
it.loopPlugin = loopPlugin
it.iobCobCalculatorPlugin = iobCobCalculatorPlugin
}
if (it is GlucoseStatus) {
it.aapsLogger = aapsLogger
it.iobCobCalculatorPlugin = iobCobCalculatorPlugin
}
}
}
private fun setupProfile(targetLow: Double, targetHigh: Double, insulinSensitivityFactor: Double, insulinToCarbRatio: Double): Profile {
val profile = Mockito.mock(Profile::class.java)
`when`(profile.targetLowMgdl).thenReturn(targetLow)
`when`(profile.targetHighMgdl).thenReturn(targetHigh)
`when`(profile.isfMgdl).thenReturn(insulinSensitivityFactor)
`when`(profile.ic).thenReturn(insulinToCarbRatio)
`when`(profileFunction.getUnits()).thenReturn(Constants.MGDL)
`when`(iobCobCalculatorPlugin.dataLock).thenReturn(Unit)
`when`(activePlugin.activeTreatments).thenReturn(treatmentsPlugin)
`when`(treatmentsPlugin.lastCalculationTreatments).thenReturn(IobTotal(System.currentTimeMillis()))
`when`(treatmentsPlugin.lastCalculationTempBasals).thenReturn(IobTotal(System.currentTimeMillis()))
`when`(activePlugin.activePump).thenReturn(virtualPumpPlugin)
val pumpDescription = PumpDescription()
pumpDescription.bolusStep = PUMP_BOLUS_STEP
`when`(virtualPumpPlugin.pumpDescription).thenReturn(pumpDescription)
Mockito.doAnswer { invocation: InvocationOnMock ->
invocation.getArgument<Constraint<Double>>(0)
}.`when`(constraintChecker).applyBolusConstraints(anyObject())
return profile
}
@Test
/** Should calculate the same bolus when different blood glucose but both in target range */
fun shouldCalculateTheSameBolusWhenBGsInRange() {
val profile = setupProfile(4.0, 8.0, 20.0, 12.0)
var bw = BolusWizard(injector).doCalc(profile, "", null, 20, 0.0, 4.2, 0.0, 100.0, true, true, true, true, false, false, false)
val bolusForBg42 = bw.calculatedTotalInsulin
bw = BolusWizard(injector).doCalc(profile, "", null, 20, 0.0, 5.4, 0.0, 100.0, true, true, true, true, false, false, false)
val bolusForBg54 = bw.calculatedTotalInsulin
Assert.assertEquals(bolusForBg42, bolusForBg54, 0.01)
}
@Test
fun shouldCalculateHigherBolusWhenHighBG() {
val profile = setupProfile(4.0, 8.0, 20.0, 12.0)
var bw = BolusWizard(injector).doCalc(profile, "", null, 20, 0.0, 9.8, 0.0, 100.0, true, true, true, true, false, false, false)
val bolusForHighBg = bw.calculatedTotalInsulin
bw = BolusWizard(injector).doCalc(profile, "", null, 20, 0.0, 5.4, 0.0, 100.0, true, true, true, true, false, false, false)
val bolusForBgInRange = bw.calculatedTotalInsulin
Assert.assertTrue(bolusForHighBg > bolusForBgInRange)
}
@Test
fun shouldCalculateLowerBolusWhenLowBG() {
val profile = setupProfile(4.0, 8.0, 20.0, 12.0)
var bw = BolusWizard(injector).doCalc(profile, "", null, 20, 0.0, 3.6, 0.0, 100.0, true, true, true, true, false, false, false)
val bolusForLowBg = bw.calculatedTotalInsulin
bw = BolusWizard(injector).doCalc(profile, "", null, 20, 0.0, 5.4, 0.0, 100.0, true, true, true, true, false, false, false)
val bolusForBgInRange = bw.calculatedTotalInsulin
Assert.assertTrue(bolusForLowBg < bolusForBgInRange)
}
}