From 3f86788eb4b21f0441614edcade6db1e7d395446 Mon Sep 17 00:00:00 2001 From: Roumen Georgiev Date: Tue, 18 Jun 2019 14:47:44 +0300 Subject: [PATCH] Added DeltaType tests and negative delta values allowed --- .../automation/triggers/TriggerDelta.java | 23 ++++------- .../automation/triggers/TriggerDeltaTest.java | 40 +++++++------------ 2 files changed, 21 insertions(+), 42 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerDelta.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerDelta.java index cc5651f41b..ce5c922aa8 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerDelta.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerDelta.java @@ -36,7 +36,7 @@ public class TriggerDelta extends Trigger { private double maxValue = 1d; private double step = 1; private DecimalFormat decimalFormat = new DecimalFormat("1"); - private String units = ProfileFunctions.getInstance().getProfileUnits(); + private String units; private DeltaType deltaType; private InputDelta value = new InputDelta( (double) minValue,(double) minValue, (double) maxValue, step, decimalFormat, deltaType); @@ -44,11 +44,13 @@ public class TriggerDelta extends Trigger { public TriggerDelta() { super(); + this.units = ProfileFunctions.getInstance().getProfileUnits(); initializer(); } private TriggerDelta(TriggerDelta triggerDelta) { super(); + this.units = ProfileFunctions.getInstance().getProfileUnits(); initializer(); value = triggerDelta.value; lastRun = triggerDelta.lastRun; @@ -60,7 +62,7 @@ public class TriggerDelta extends Trigger { } public DeltaType getType() { - return value.getDeltaType(); + return deltaType; } public String getUnits() { @@ -157,6 +159,7 @@ public class TriggerDelta extends Trigger { TriggerDelta setValue(double requestedValue, DeltaType requestedType) { this.value.setValue(requestedValue, requestedType); + this.deltaType = requestedType; return this; } @@ -168,13 +171,13 @@ public class TriggerDelta extends Trigger { void initializer(){ if (this.units.equals(Constants.MMOL)) { this.maxValue = 4d; - this.minValue = 0.1d; + this.minValue = -4d; this.step = 0.1d; this.decimalFormat = new DecimalFormat("0.1"); this.deltaType = DeltaType.DELTA; } else { this.maxValue = 72d; - this.minValue = 2d; + this.minValue = -72d; this.step = 1d; this.deltaType = DeltaType.DELTA; } @@ -201,16 +204,4 @@ public class TriggerDelta extends Trigger { .build(root); } - //Used for testing deltaType - public double deltaValue(){ - GlucoseStatus glucoseStatus = GlucoseStatus.getGlucoseStatusData(); - - if (deltaType == DeltaType.SHORT_AVERAGE) - return glucoseStatus.short_avgdelta; - else if (deltaType == DeltaType.LONG_AVERAGE) - return glucoseStatus.long_avgdelta; - else - return glucoseStatus.delta; - } - } \ No newline at end of file diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerDeltaTest.java b/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerDeltaTest.java index 5a09a19700..d93cd97989 100644 --- a/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerDeltaTest.java +++ b/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerDeltaTest.java @@ -20,7 +20,6 @@ import info.AAPSMocker; import info.nightscout.androidaps.Constants; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; -import info.nightscout.androidaps.data.Profile; import info.nightscout.androidaps.db.BgReading; import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions; import info.nightscout.androidaps.plugins.general.automation.elements.Comparator; @@ -41,13 +40,18 @@ public class TriggerDeltaTest { @Test public void shouldRunTest() { when(IobCobCalculatorPlugin.getPlugin().getBgReadings()).thenReturn(generateValidBgData()); - - TriggerDelta t = new TriggerDelta().setUnits(Constants.MGDL).setValue(73d, DeltaType.DELTA).comparator(Comparator.Compare.IS_EQUAL); + //Test if time passed is less than 5 min + TriggerDelta t = new TriggerDelta().setUnits(Constants.MGDL).setValue(-3d, DeltaType.DELTA).comparator(Comparator.Compare.IS_EQUAL_OR_GREATER).lastRun(now-1); Assert.assertFalse(t.shouldRun()); - t = new TriggerDelta().setUnits(Constants.MGDL).setValue(-2d, DeltaType.LONG_AVERAGE).comparator(Comparator.Compare.IS_EQUAL); - Assert.assertTrue(t.shouldRun()); - t = new TriggerDelta().setUnits(Constants.MGDL).setValue(-3d, DeltaType.LONG_AVERAGE).comparator(Comparator.Compare.IS_EQUAL_OR_GREATER); + t = new TriggerDelta().setUnits(Constants.MGDL).setValue(73d, DeltaType.LONG_AVERAGE).comparator(Comparator.Compare.IS_EQUAL); + Assert.assertFalse(t.shouldRun()); + Assert.assertEquals(DeltaType.LONG_AVERAGE, t.getType()); + t = new TriggerDelta().setUnits(Constants.MGDL).setValue(-2d, DeltaType.SHORT_AVERAGE).comparator(Comparator.Compare.IS_EQUAL); + Assert.assertFalse(t.shouldRun()); + Assert.assertEquals(DeltaType.SHORT_AVERAGE, t.getType()); + t = new TriggerDelta().setUnits(Constants.MGDL).setValue(-3d, DeltaType.DELTA).comparator(Comparator.Compare.IS_EQUAL_OR_GREATER); Assert.assertTrue(t.shouldRun()); + Assert.assertEquals(DeltaType.DELTA, t.getType()); t = new TriggerDelta().setUnits(Constants.MGDL).setValue(2d, DeltaType.LONG_AVERAGE).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER); Assert.assertTrue(t.shouldRun()); t = new TriggerDelta().setUnits(Constants.MGDL).setValue(2d, DeltaType.LONG_AVERAGE).comparator(Comparator.Compare.IS_EQUAL); @@ -70,6 +74,8 @@ public class TriggerDeltaTest { t = new TriggerDelta().setUnits(Constants.MGDL).setValue(214, DeltaType.DELTA).comparator(Comparator.Compare.IS_EQUAL).lastRun(now - 1); Assert.assertFalse(t.shouldRun()); + + } @Test @@ -113,22 +119,6 @@ public class TriggerDeltaTest { Assert.assertEquals(Optional.of(R.drawable.as), new TriggerDelta().icon()); } - @Test - public void deltaTypeTest() { - when(IobCobCalculatorPlugin.getPlugin().getBgReadings()).thenReturn(generateValidBgData()); - TriggerDelta t = new TriggerDelta().setUnits(Constants.MGDL).setValue(213, DeltaType.DELTA).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER); - Assert.assertEquals(DeltaType.DELTA, t.getType()); - Assert.assertEquals(-2d, t.deltaValue(), 0d); - Assert.assertTrue(t.shouldRun()); - t = new TriggerDelta().setUnits(Constants.MGDL).setValue(213, DeltaType.SHORT_AVERAGE).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER); - Assert.assertEquals(DeltaType.SHORT_AVERAGE, t.getType()); - Assert.assertEquals(-2d, t.deltaValue(), 0d); - t = new TriggerDelta().setUnits(Constants.MGDL).setValue(213, DeltaType.LONG_AVERAGE).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER); - Assert.assertEquals(DeltaType.LONG_AVERAGE, t.getType()); - Assert.assertEquals(-2d, t.deltaValue(), 0d); - - } - @Before public void mock() { AAPSMocker.mockMainApp(); @@ -142,14 +132,12 @@ public class TriggerDeltaTest { } + @Test public void initializerTest(){ PowerMockito.when(ProfileFunctions.getInstance().getProfileUnits()).thenReturn(Constants.MMOL); TriggerDelta t = new TriggerDelta(); Assert.assertTrue(t.getUnits().equals(Constants.MMOL)); - Assert.assertTrue(t.getUnits().equals(Constants.MGDL)); - Assert.assertNull(t.getUnits()); - Assert.assertNotNull(t.getUnits()); - when(ProfileFunctions.getInstance().getProfileUnits()).thenReturn(Constants.MGDL); + PowerMockito.when(ProfileFunctions.getInstance().getProfileUnits()).thenReturn(Constants.MGDL); t = new TriggerDelta(); Assert.assertEquals(Constants.MGDL, t.getUnits()); }