InsulinOrefBasePluginTest
This commit is contained in:
parent
ac5ea83d38
commit
ccf93d2191
|
@ -6,7 +6,7 @@ import javax.inject.Inject
|
|||
import javax.inject.Singleton
|
||||
|
||||
@Singleton
|
||||
class RxBusWrapper @Inject constructor() {
|
||||
open class RxBusWrapper @Inject constructor() {
|
||||
|
||||
private val bus: RxBus = RxBus.INSTANCE
|
||||
|
||||
|
|
|
@ -15,6 +15,7 @@ import java.util.Objects;
|
|||
|
||||
import javax.inject.Inject;
|
||||
|
||||
import dagger.android.HasAndroidInjector;
|
||||
import info.nightscout.androidaps.Constants;
|
||||
import info.nightscout.androidaps.MainApp;
|
||||
import info.nightscout.androidaps.R;
|
||||
|
@ -76,6 +77,10 @@ public class Treatment implements DataPointWithLabelInterface, DbObjectBase {
|
|||
MainApp.instance().androidInjector().inject(this); // TODO it will be removed by new database
|
||||
}
|
||||
|
||||
public Treatment(HasAndroidInjector injector) {
|
||||
injector.androidInjector().inject(this);
|
||||
}
|
||||
|
||||
public static Treatment createFromJson(JSONObject json) throws JSONException {
|
||||
Treatment treatment = new Treatment();
|
||||
treatment.source = Source.NIGHTSCOUT;
|
||||
|
|
|
@ -1,119 +0,0 @@
|
|||
package info.nightscout.androidaps.plugins.insulin;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import info.nightscout.androidaps.data.Iob;
|
||||
import info.nightscout.androidaps.plugins.treatments.Treatment;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
/**
|
||||
* Created by triplem on 07.01.18.
|
||||
*/
|
||||
|
||||
public class InsulinOrefBasePluginTest extends InsulinOrefBasePlugin {
|
||||
|
||||
private int peak;
|
||||
private double dia;
|
||||
|
||||
private boolean shortDiaNotificationSend;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
this.peak = 0;
|
||||
this.dia = InsulinOrefBasePlugin.MIN_DIA;
|
||||
this.shortDiaNotificationSend = false;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetDia() throws Exception {
|
||||
assertEquals(InsulinOrefBasePlugin.MIN_DIA, this.getDia(), 0);
|
||||
|
||||
this.dia = InsulinOrefBasePlugin.MIN_DIA + 1;
|
||||
assertEquals(InsulinOrefBasePlugin.MIN_DIA + 1, this.getDia(), 0);
|
||||
|
||||
this.dia = InsulinOrefBasePlugin.MIN_DIA - 1;
|
||||
assertEquals(InsulinOrefBasePlugin.MIN_DIA, this.getDia(), 0);
|
||||
assertTrue(this.shortDiaNotificationSend);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIobCalcForTreatment() {
|
||||
Treatment treatment = new Treatment();
|
||||
Iob expected = new Iob();
|
||||
|
||||
assertEquals(expected, this.iobCalcForTreatment(treatment, 0, 0d));
|
||||
|
||||
this.peak = 30;
|
||||
this.dia = 4;
|
||||
long time = System.currentTimeMillis();
|
||||
|
||||
// check directly after bolus
|
||||
treatment.date = time;
|
||||
treatment.insulin = 10d;
|
||||
assertEquals(10d, this.iobCalcForTreatment(treatment, time).iobContrib, 0.1);
|
||||
|
||||
// check after 1 hour
|
||||
treatment.date = time - 1 * 60 * 60 * 1000; // 1 hour
|
||||
treatment.insulin = 10d;
|
||||
assertEquals(3.92, this.iobCalcForTreatment(treatment, time).iobContrib, 0.1);
|
||||
|
||||
// check after 2 hour
|
||||
treatment.date = time - 2 * 60 * 60 * 1000; // 1 hour
|
||||
treatment.insulin = 10d;
|
||||
assertEquals(0.77, this.iobCalcForTreatment(treatment, time).iobContrib, 0.1);
|
||||
|
||||
// check after 3 hour
|
||||
treatment.date = time - 3 * 60 * 60 * 1000; // 1 hour
|
||||
treatment.insulin = 10d;
|
||||
assertEquals(0.10, this.iobCalcForTreatment(treatment, time).iobContrib, 0.1);
|
||||
|
||||
// check after dia
|
||||
treatment.date = time - 4 * 60 * 60 * 1000;
|
||||
treatment.insulin = 10d;
|
||||
assertEquals(0d, this.iobCalcForTreatment(treatment, time).iobContrib, 0.1);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* this method is implemented to allow tests of the iobCalcForTreatment calculation
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public int getPeak() {
|
||||
return this.peak;
|
||||
}
|
||||
|
||||
/**
|
||||
* Userdefined Dia is implemented to allow tests of the getDia method
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public double getUserDefinedDia() {
|
||||
return this.dia;
|
||||
}
|
||||
|
||||
public void sendShortDiaNotification(double dia) {
|
||||
this.shortDiaNotificationSend = true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getId() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getFriendlyName() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String commentStandardText() {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
// the following methods are implemented, but not needed...
|
||||
}
|
|
@ -0,0 +1,107 @@
|
|||
package info.nightscout.androidaps.plugins.insulin
|
||||
|
||||
import dagger.android.HasAndroidInjector
|
||||
import info.nightscout.androidaps.MainApp
|
||||
import info.nightscout.androidaps.data.Iob
|
||||
import info.nightscout.androidaps.logging.AAPSLogger
|
||||
import info.nightscout.androidaps.plugins.bus.RxBusWrapper
|
||||
import info.nightscout.androidaps.plugins.configBuilder.ProfileFunction
|
||||
import info.nightscout.androidaps.plugins.insulin.InsulinOrefBasePlugin.Companion.MIN_DIA
|
||||
import info.nightscout.androidaps.plugins.treatments.Treatment
|
||||
import info.nightscout.androidaps.utils.resources.ResourceHelper
|
||||
import org.junit.Assert
|
||||
import org.junit.Before
|
||||
import org.junit.Rule
|
||||
import org.junit.Test
|
||||
import org.mockito.Mock
|
||||
import org.mockito.junit.MockitoJUnit
|
||||
import org.mockito.junit.MockitoRule
|
||||
import org.powermock.core.classloader.annotations.PrepareForTest
|
||||
|
||||
class InsulinOrefBasePluginTest {
|
||||
|
||||
var testPeak = 0
|
||||
var testUserDefinedDia = 0.0
|
||||
var shortDiaNotificationSend = false
|
||||
|
||||
inner class InsulinBaseTest(
|
||||
resourceHelper: ResourceHelper,
|
||||
profileFunction: ProfileFunction,
|
||||
rxBus: RxBusWrapper,
|
||||
aapsLogger: AAPSLogger
|
||||
) : InsulinOrefBasePlugin(
|
||||
resourceHelper, profileFunction, rxBus, aapsLogger
|
||||
) {
|
||||
|
||||
override fun sendShortDiaNotification(dia: Double) {
|
||||
shortDiaNotificationSend = true
|
||||
}
|
||||
|
||||
override val userDefinedDia: Double
|
||||
get() = testUserDefinedDia
|
||||
|
||||
override val peak: Int
|
||||
get() = testPeak
|
||||
|
||||
override fun commentStandardText(): String = ""
|
||||
override fun getId(): Int = 0
|
||||
override fun getFriendlyName(): String = ""
|
||||
}
|
||||
|
||||
@get:Rule
|
||||
val mockitoRule: MockitoRule = MockitoJUnit.rule()
|
||||
|
||||
lateinit var sut: InsulinBaseTest
|
||||
|
||||
@Mock lateinit var resourceHelper: ResourceHelper
|
||||
@Mock lateinit var profileFunction: ProfileFunction
|
||||
@Mock lateinit var rxBus: RxBusWrapper
|
||||
@Mock lateinit var aapsLogger: AAPSLogger
|
||||
|
||||
@Mock lateinit var injector: HasAndroidInjector
|
||||
|
||||
@Before
|
||||
fun setUp() {
|
||||
sut = InsulinBaseTest(resourceHelper, profileFunction, rxBus, aapsLogger)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testGetDia() {
|
||||
Assert.assertEquals(MIN_DIA, sut.dia, 0.0)
|
||||
testUserDefinedDia = MIN_DIA + 1
|
||||
Assert.assertEquals(MIN_DIA + 1, sut.dia, 0.0)
|
||||
testUserDefinedDia = MIN_DIA - 1
|
||||
Assert.assertEquals(MIN_DIA, sut.dia, 0.0)
|
||||
Assert.assertTrue(shortDiaNotificationSend)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testIobCalcForTreatment() {
|
||||
val treatment = Treatment(injector)
|
||||
val expected = Iob()
|
||||
Assert.assertEquals(expected, sut.iobCalcForTreatment(treatment, 0, 0.0))
|
||||
testPeak = 30
|
||||
testUserDefinedDia = 4.0
|
||||
val time = System.currentTimeMillis()
|
||||
// check directly after bolus
|
||||
treatment.date = time
|
||||
treatment.insulin = 10.0
|
||||
Assert.assertEquals(10.0, sut.iobCalcForTreatment(treatment, time).iobContrib, 0.1)
|
||||
// check after 1 hour
|
||||
treatment.date = time - 1 * 60 * 60 * 1000 // 1 hour
|
||||
treatment.insulin = 10.0
|
||||
Assert.assertEquals(3.92, sut.iobCalcForTreatment(treatment, time).iobContrib, 0.1)
|
||||
// check after 2 hour
|
||||
treatment.date = time - 2 * 60 * 60 * 1000 // 1 hour
|
||||
treatment.insulin = 10.0
|
||||
Assert.assertEquals(0.77, sut.iobCalcForTreatment(treatment, time).iobContrib, 0.1)
|
||||
// check after 3 hour
|
||||
treatment.date = time - 3 * 60 * 60 * 1000 // 1 hour
|
||||
treatment.insulin = 10.0
|
||||
Assert.assertEquals(0.10, sut.iobCalcForTreatment(treatment, time).iobContrib, 0.1)
|
||||
// check after dia
|
||||
treatment.date = time - 4 * 60 * 60 * 1000
|
||||
treatment.insulin = 10.0
|
||||
Assert.assertEquals(0.0, sut.iobCalcForTreatment(treatment, time).iobContrib, 0.1)
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue