fixed tests and added DeltaType testing
This commit is contained in:
parent
6e14e3df90
commit
f15ead9c6f
|
@ -93,12 +93,6 @@ public class TriggerDelta extends Trigger {
|
|||
if (lastRun > DateUtil.now() - T.mins(5).msecs())
|
||||
return false;
|
||||
|
||||
if (glucoseStatus == null && comparator.getValue().equals(Comparator.Compare.IS_NOT_AVAILABLE)) {
|
||||
if (L.isEnabled(L.AUTOMATION))
|
||||
log.debug("Ready for execution: delta is " + delta + friendlyDescription());
|
||||
return true;
|
||||
}
|
||||
|
||||
boolean doRun = comparator.getValue().check(delta, Profile.toMgdl(value.getValue(), this.units));
|
||||
if (doRun) {
|
||||
if (L.isEnabled(L.AUTOMATION))
|
||||
|
@ -117,7 +111,7 @@ public class TriggerDelta extends Trigger {
|
|||
data.put("value", getValue());
|
||||
data.put("units", units);
|
||||
data.put("lastRun", lastRun);
|
||||
data.put("type", getType());
|
||||
data.put("deltaType", getType());
|
||||
data.put("comparator", comparator.getValue().toString());
|
||||
o.put("data", data);
|
||||
} catch (JSONException e) {
|
||||
|
@ -131,8 +125,7 @@ public class TriggerDelta extends Trigger {
|
|||
try {
|
||||
JSONObject d = new JSONObject(data);
|
||||
units = JsonHelper.safeGetString(d, "units");
|
||||
int savedDeltaType = JsonHelper.safeGetInt(d, "type");
|
||||
deltaType = DeltaType.valueOf(JsonHelper.safeGetString(d, "type", ""));
|
||||
deltaType = DeltaType.valueOf(JsonHelper.safeGetString(d, "deltaType", ""));
|
||||
value.setValue(JsonHelper.safeGetDouble(d, "value"), deltaType);
|
||||
lastRun = JsonHelper.safeGetLong(d, "lastRun");
|
||||
comparator.setValue(Comparator.Compare.valueOf(JsonHelper.safeGetString(d, "comparator")));
|
||||
|
@ -149,7 +142,7 @@ public class TriggerDelta extends Trigger {
|
|||
|
||||
@Override
|
||||
public String friendlyDescription() {
|
||||
return MainApp.gs(R.string.deltacompared, MainApp.gs(comparator.getValue().getStringRes()), getValue(), deltaType);
|
||||
return MainApp.gs(R.string.deltacompared, MainApp.gs(comparator.getValue().getStringRes()), getValue(), MainApp.gs(deltaType.getStringRes()));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -1402,7 +1402,7 @@
|
|||
<string name="wifissidcompared">WiFi SSID %1$s %2$s</string>
|
||||
<string name="autosenscompared">Autosens %1$s %2$s %%</string>
|
||||
<string name="autosenslabel">Autosens %</string>
|
||||
<string name="deltacompared">BG %3$s %1$s %2$s</string>
|
||||
<string name="deltacompared">%3$s %1$s %2$s</string>
|
||||
<string name="deltalabel">BG difference</string>
|
||||
<string name="currentlocation">Current Location</string>
|
||||
<string name="location">Location</string>
|
||||
|
|
|
@ -23,6 +23,7 @@ import info.nightscout.androidaps.R;
|
|||
import info.nightscout.androidaps.db.BgReading;
|
||||
import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions;
|
||||
import info.nightscout.androidaps.plugins.general.automation.elements.Comparator;
|
||||
import info.nightscout.androidaps.plugins.general.automation.elements.InputDelta.DeltaType;
|
||||
import info.nightscout.androidaps.plugins.general.nsclient.data.NSSgv;
|
||||
import info.nightscout.androidaps.plugins.iob.iobCobCalculator.GlucoseStatus;
|
||||
import info.nightscout.androidaps.plugins.iob.iobCobCalculator.IobCobCalculatorPlugin;
|
||||
|
@ -40,69 +41,70 @@ public class TriggerDeltaTest {
|
|||
public void shouldRunTest() {
|
||||
when(IobCobCalculatorPlugin.getPlugin().getBgReadings()).thenReturn(generateValidBgData());
|
||||
|
||||
TriggerDelta t = new TriggerDelta().setUnits(Constants.MGDL).setValue(73d).comparator(Comparator.Compare.IS_EQUAL);
|
||||
TriggerDelta t = new TriggerDelta().setUnits(Constants.MGDL).setValue(73d, DeltaType.DELTA).comparator(Comparator.Compare.IS_EQUAL);
|
||||
Assert.assertFalse(t.shouldRun());
|
||||
t = new TriggerDelta().setUnits(Constants.MGDL).setValue(-2d).comparator(Comparator.Compare.IS_EQUAL);
|
||||
t = new TriggerDelta().setUnits(Constants.MGDL).setValue(-2d, DeltaType.DELTA).comparator(Comparator.Compare.IS_EQUAL);
|
||||
Assert.assertTrue(t.shouldRun());
|
||||
t = new TriggerDelta().setUnits(Constants.MGDL).setValue(-3d).comparator(Comparator.Compare.IS_EQUAL_OR_GREATER);
|
||||
t = new TriggerDelta().setUnits(Constants.MGDL).setValue(-3d, DeltaType.DELTA).comparator(Comparator.Compare.IS_EQUAL_OR_GREATER);
|
||||
Assert.assertTrue(t.shouldRun());
|
||||
t = new TriggerDelta().setUnits(Constants.MGDL).setValue(2d).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER);
|
||||
t = new TriggerDelta().setUnits(Constants.MGDL).setValue(2d, DeltaType.DELTA).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER);
|
||||
Assert.assertTrue(t.shouldRun());
|
||||
t = new TriggerDelta().setUnits(Constants.MGDL).setValue(2d).comparator(Comparator.Compare.IS_EQUAL);
|
||||
t = new TriggerDelta().setUnits(Constants.MGDL).setValue(2d, DeltaType.DELTA).comparator(Comparator.Compare.IS_EQUAL);
|
||||
Assert.assertFalse(t.shouldRun());
|
||||
t = new TriggerDelta().setUnits(Constants.MMOL).setValue(0.3d).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER);
|
||||
t = new TriggerDelta().setUnits(Constants.MMOL).setValue(0.3d, DeltaType.DELTA).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER);
|
||||
Assert.assertTrue(t.shouldRun());
|
||||
t = new TriggerDelta().setUnits(Constants.MMOL).setValue(0.1d).comparator(Comparator.Compare.IS_EQUAL_OR_GREATER);
|
||||
t = new TriggerDelta().setUnits(Constants.MMOL).setValue(0.1d, DeltaType.DELTA).comparator(Comparator.Compare.IS_EQUAL_OR_GREATER);
|
||||
Assert.assertFalse(t.shouldRun());
|
||||
t = new TriggerDelta().setUnits(Constants.MMOL).setValue(-0.5d).comparator(Comparator.Compare.IS_EQUAL_OR_GREATER);
|
||||
t = new TriggerDelta().setUnits(Constants.MMOL).setValue(-0.5d, DeltaType.DELTA).comparator(Comparator.Compare.IS_EQUAL_OR_GREATER);
|
||||
Assert.assertTrue(t.shouldRun());
|
||||
t = new TriggerDelta().setUnits(Constants.MMOL).setValue(-0.2d).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER);
|
||||
t = new TriggerDelta().setUnits(Constants.MMOL).setValue(-0.2d, DeltaType.DELTA).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER);
|
||||
Assert.assertFalse(t.shouldRun());
|
||||
|
||||
when(IobCobCalculatorPlugin.getPlugin().getBgReadings()).thenReturn(new ArrayList<>());
|
||||
t = new TriggerDelta().setUnits(Constants.MGDL).setValue(213).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER);
|
||||
t = new TriggerDelta().setUnits(Constants.MGDL).setValue(213, DeltaType.DELTA).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER);
|
||||
Assert.assertFalse(t.shouldRun());
|
||||
t = new TriggerDelta().comparator(Comparator.Compare.IS_NOT_AVAILABLE);
|
||||
Assert.assertTrue(t.shouldRun());
|
||||
|
||||
t = new TriggerDelta().setUnits(Constants.MGDL).setValue(214).comparator(Comparator.Compare.IS_EQUAL).lastRun(now - 1);
|
||||
t = new TriggerDelta().setUnits(Constants.MGDL).setValue(214, DeltaType.DELTA).comparator(Comparator.Compare.IS_EQUAL).lastRun(now - 1);
|
||||
Assert.assertFalse(t.shouldRun());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void copyConstructorTest() {
|
||||
TriggerDelta t = new TriggerDelta().setUnits(Constants.MGDL).setValue(213).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER);
|
||||
TriggerDelta t = new TriggerDelta().setUnits(Constants.MGDL).setValue(213, DeltaType.DELTA).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER);
|
||||
TriggerDelta t1 = (TriggerDelta) t.duplicate();
|
||||
Assert.assertEquals(213d, t1.getValue(), 0.01d);
|
||||
Assert.assertEquals(Constants.MGDL, t1.getUnits());
|
||||
Assert.assertEquals(DeltaType.DELTA, t.getType());
|
||||
Assert.assertEquals(Comparator.Compare.IS_EQUAL_OR_LESSER, t.getComparator().getValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void executeTest() {
|
||||
TriggerDelta t = new TriggerDelta().setUnits(Constants.MGDL).setValue(213).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER);
|
||||
TriggerDelta t = new TriggerDelta().setUnits(Constants.MGDL).setValue(213, DeltaType.DELTA).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER);
|
||||
t.executed(1);
|
||||
Assert.assertEquals(1l, t.getLastRun());
|
||||
}
|
||||
|
||||
String deltaJson = "{\"data\":{\"comparator\":\"IS_EQUAL\",\"lastRun\":0,\"units\":\"mmol\",\"type\":0,\"value\":4.1},\"type\":\"info.nightscout.androidaps.plugins.general.automation.triggers.TriggerDelta\"}";
|
||||
String deltaJson = "{\"data\":{\"comparator\":\"IS_EQUAL\",\"lastRun\":0,\"deltaType\":\"DELTA\",\"units\":\"mmol\",\"value\":4.1},\"type\":\"info.nightscout.androidaps.plugins.general.automation.triggers.TriggerDelta\"}";
|
||||
|
||||
@Test
|
||||
public void toJSONTest() {
|
||||
TriggerDelta t = new TriggerDelta().setUnits(Constants.MMOL).setValue(4.1d).comparator(Comparator.Compare.IS_EQUAL);
|
||||
TriggerDelta t = new TriggerDelta().setUnits(Constants.MMOL).setValue(4.1d, DeltaType.DELTA).comparator(Comparator.Compare.IS_EQUAL);
|
||||
Assert.assertEquals(deltaJson, t.toJSON());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void fromJSONTest() throws JSONException {
|
||||
TriggerDelta t = new TriggerDelta().setUnits(Constants.MMOL).setValue(4.1d).comparator(Comparator.Compare.IS_EQUAL);
|
||||
TriggerDelta t = new TriggerDelta().setUnits(Constants.MMOL).setValue(4.1d, DeltaType.DELTA).comparator(Comparator.Compare.IS_EQUAL);
|
||||
|
||||
TriggerDelta t2 = (TriggerDelta) Trigger.instantiate(new JSONObject(t.toJSON()));
|
||||
Assert.assertEquals(Comparator.Compare.IS_EQUAL, t2.getComparator().getValue());
|
||||
Assert.assertEquals(4.1d, t2.getValue(), 0.01d);
|
||||
Assert.assertEquals(Constants.MMOL, t2.getUnits());
|
||||
Assert.assertEquals(0d, t2.getType(), 0.01d);
|
||||
Assert.assertEquals(DeltaType.DELTA, t2.getType());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -111,14 +113,15 @@ public class TriggerDeltaTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void typeToStringTest() {
|
||||
TriggerDelta t = new TriggerDelta();
|
||||
Assert.assertEquals(MainApp.gs(R.string.delta), t.typeToString(0));
|
||||
Assert.assertEquals(MainApp.gs(R.string.short_avgdelta), t.typeToString(1));
|
||||
Assert.assertEquals(MainApp.gs(R.string.long_avgdelta), t.typeToString(2));
|
||||
public void deltaTypeTest() {
|
||||
TriggerDelta t = new TriggerDelta().setUnits(Constants.MGDL).setValue(213, DeltaType.DELTA).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER);
|
||||
Assert.assertEquals(DeltaType.DELTA, t.getType());
|
||||
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());
|
||||
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());
|
||||
}
|
||||
|
||||
|
||||
@Before
|
||||
public void mock() {
|
||||
AAPSMocker.mockMainApp();
|
||||
|
|
Loading…
Reference in a new issue