diff --git a/automation/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerTempTargetValueTest.kt b/automation/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerTempTargetValueTest.kt new file mode 100644 index 0000000000..084dd484de --- /dev/null +++ b/automation/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerTempTargetValueTest.kt @@ -0,0 +1,91 @@ +package info.nightscout.androidaps.plugins.general.automation.triggers + +import com.google.common.base.Optional +import info.nightscout.androidaps.automation.R +import info.nightscout.androidaps.interfaces.GlucoseUnit +import info.nightscout.androidaps.plugins.general.automation.elements.Comparator +import org.json.JSONObject +import org.junit.Assert +import org.junit.Before +import org.junit.Test +import org.mockito.Mockito.`when` + +class TriggerTempTargetValueTest : TriggerTestBase() { + + var now = 1514766900000L + + @Before + fun prepare() { + `when`(profileFunction.getUnits()).thenReturn(GlucoseUnit.MGDL) + `when`(dateUtil.now()).thenReturn(now) + } + + /* + @Test + fun shouldRunTest() { + `when`(repository.getTemporaryTargetActiveAt(dateUtil.now())).thenReturn(generateOneCurrentRecordTtData()) + var t: TriggerTempTargetValue = TriggerTempTargetValue(injector).setUnits(GlucoseUnit.MMOL).setValue(7.7).comparator(Comparator.Compare.IS_EQUAL) + Assert.assertFalse(t.shouldRun()) + t = TriggerTempTargetValue(injector).setUnits(GlucoseUnit.MGDL).setValue(140.0).comparator(Comparator.Compare.IS_EQUAL) + Assert.assertTrue(t.shouldRun()) + t = TriggerTempTargetValue(injector).setUnits(GlucoseUnit.MGDL).setValue(140.0).comparator(Comparator.Compare.IS_EQUAL_OR_GREATER) + Assert.assertTrue(t.shouldRun()) + t = TriggerTempTargetValue(injector).setUnits(GlucoseUnit.MGDL).setValue(140.0).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER) + Assert.assertTrue(t.shouldRun()) + t = TriggerTempTargetValue(injector).setUnits(GlucoseUnit.MGDL).setValue(139.0).comparator(Comparator.Compare.IS_EQUAL) + Assert.assertFalse(t.shouldRun()) + t = TriggerTempTargetValue(injector).setUnits(GlucoseUnit.MGDL).setValue(141.0).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER) + Assert.assertTrue(t.shouldRun()) + t = TriggerTempTargetValue(injector).setUnits(GlucoseUnit.MGDL).setValue(141.0).comparator(Comparator.Compare.IS_EQUAL_OR_GREATER) + Assert.assertFalse(t.shouldRun()) + t = TriggerTempTargetValue(injector).setUnits(GlucoseUnit.MGDL).setValue(139.0).comparator(Comparator.Compare.IS_EQUAL_OR_GREATER) + Assert.assertTrue(t.shouldRun()) + t = TriggerTempTargetValue(injector).setUnits(GlucoseUnit.MGDL).setValue(139.0).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER) + Assert.assertFalse(t.shouldRun()) + //`when`(autosensDataStore.getBgReadingsDataTableCopy()).thenReturn(ArrayList()) + //t = TriggerTempTargetValue(injector).setUnits(GlucoseUnit.MGDL).setValue(213.0).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER) + Assert.assertFalse(t.shouldRun()) + t = TriggerTempTargetValue(injector).comparator(Comparator.Compare.IS_NOT_AVAILABLE) + Assert.assertTrue(t.shouldRun()) + } + */ + + @Test + fun copyConstructorTest() { + val t: TriggerTempTargetValue = TriggerTempTargetValue(injector).setUnits(GlucoseUnit.MGDL).setValue(140.0).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER) + val t1 = t.duplicate() as TriggerTempTargetValue + Assert.assertEquals(140.0, t1.ttValue.value, 0.01) + Assert.assertEquals(GlucoseUnit.MGDL, t1.ttValue.units) + Assert.assertEquals(Comparator.Compare.IS_EQUAL_OR_LESSER, t.comparator.value) + } + + private var ttJson = "{\"data\":{\"tt\":7.7,\"comparator\":\"IS_EQUAL\",\"units\":\"mmol\"},\"type\":\"TriggerTempTargetValue\"}" + + @Test + fun toJSONTest() { + val t: TriggerTempTargetValue = TriggerTempTargetValue(injector).setUnits(GlucoseUnit.MMOL).setValue(7.7).comparator(Comparator.Compare.IS_EQUAL) + Assert.assertEquals(ttJson, t.toJSON()) + } + + @Test + fun fromJSONTest() { + val t: TriggerTempTargetValue = TriggerTempTargetValue(injector).setUnits(GlucoseUnit.MMOL).setValue(7.7).comparator(Comparator.Compare.IS_EQUAL) + val t2 = TriggerDummy(injector).instantiate(JSONObject(t.toJSON())) as TriggerTempTargetValue + Assert.assertEquals(Comparator.Compare.IS_EQUAL, t2.comparator.value) + Assert.assertEquals(7.7, t2.ttValue.value, 0.01) + Assert.assertEquals(GlucoseUnit.MMOL, t2.ttValue.units) + } + + @Test + fun iconTest() { + Assert.assertEquals(Optional.of(R.drawable.ic_keyboard_tab), TriggerTempTargetValue(injector).icon()) + } + + /* Different trial made to create a Single>, but none works... + private fun generateOneCurrentRecordTtData(): Single> { + val temporaryTarget = TemporaryTarget(duration = 60000, highTarget = 140.0, lowTarget = 140.0, reason = TemporaryTarget.Reason.CUSTOM, timestamp = now - 1) + return ValueWrapper.Existing(temporaryTarget) as Single> + } + */ +} +