diff --git a/app/src/main/java/info/nightscout/androidaps/db/BgReading.java b/app/src/main/java/info/nightscout/androidaps/db/BgReading.java index 254c2a7318..6f87f232de 100644 --- a/app/src/main/java/info/nightscout/androidaps/db/BgReading.java +++ b/app/src/main/java/info/nightscout/androidaps/db/BgReading.java @@ -251,12 +251,16 @@ public class BgReading implements DataPointWithLabelInterface { // Copied from xDrip+ public String calculateDirection(){ - GlucoseStatus glucoseStatus = GlucoseStatus.getGlucoseStatusData(); - if (glucoseStatus == null || glucoseStatus.prev_glucose != 0) + GlucoseStatus glucoseStatus = getGlucoseStatus(); + double slope = 0; + if (glucoseStatus == null || glucoseStatus.prev_glucose == 0) return "??"; -// double slope = glucoseStatus.delta / (glucoseStatus.previous_date - glucoseStatus.date); - double slope = (glucoseStatus.glucose - glucoseStatus.prev_glucose) / (glucoseStatus.previous_date - glucoseStatus.date); + // Avoid division by 0 + if (glucoseStatus.date == glucoseStatus.previous_date) + slope = 0; + else + slope = (glucoseStatus.prev_glucose - glucoseStatus.glucose) / (glucoseStatus.previous_date - glucoseStatus.date); log.debug("Slope is :"+slope+" delta "+glucoseStatus.delta+" date difference "+(glucoseStatus.date - glucoseStatus.previous_date)); double slope_by_minute = slope * 60000; String arrow = "NONE"; @@ -281,4 +285,9 @@ public class BgReading implements DataPointWithLabelInterface { } + // Used for testing purpose + protected GlucoseStatus getGlucoseStatus() { + return GlucoseStatus.getGlucoseStatusData(); + } + } diff --git a/app/src/test/java/info/nightscout/androidaps/db/BgReadingTest.java b/app/src/test/java/info/nightscout/androidaps/db/BgReadingTest.java index 3467205fba..4a43f9fb7a 100644 --- a/app/src/test/java/info/nightscout/androidaps/db/BgReadingTest.java +++ b/app/src/test/java/info/nightscout/androidaps/db/BgReadingTest.java @@ -4,9 +4,11 @@ import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; +import org.mockito.Mockito; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; +import java.util.Date; import java.util.logging.Logger; import info.AAPSMocker; @@ -17,6 +19,7 @@ import info.nightscout.androidaps.plugins.iob.iobCobCalculator.GlucoseStatus; import info.nightscout.androidaps.utils.SP; import static org.junit.Assert.*; +import static org.powermock.api.mockito.PowerMockito.doReturn; @RunWith(PowerMockRunner.class) @PrepareForTest({MainApp.class, Logger.class, L.class, SP.class}) @@ -54,10 +57,90 @@ public class BgReadingTest { } + @Test + public void dateTest(){ + bgReading = new BgReading(); + long now = System.currentTimeMillis(); + bgReading.date = now; + Date nowDate = new Date(now); + assertEquals(now, bgReading.date(now).date); + assertEquals(now, bgReading.date(nowDate).date); + } + + @Test + public void valueTest(){ + bgReading = new BgReading(); + double valueToSet = 81; // 4.5 mmol + assertEquals(81d, bgReading.value(valueToSet).value, 0.01d); + } + + @Test + public void copyFromTest(){ + bgReading = new BgReading(); + BgReading copy = new BgReading(); + bgReading.value = 81; + long now = System.currentTimeMillis(); + bgReading.date = now; + copy.date = now; + + copy.copyFrom(bgReading); + + assertEquals(81, copy.value, 0.1d); + assertEquals(now, copy.date); + assertEquals(bgReading.directionToSymbol(), copy.directionToSymbol()); + } + + @Test + public void isEqualTest(){ + bgReading = new BgReading(); + BgReading copy = new BgReading(); + bgReading.value = 81; + long now = System.currentTimeMillis(); + bgReading.date = now; + copy.date = now; + + copy.copyFrom(bgReading); + + assertEquals(true, copy.isEqual(bgReading)); + assertEquals(false, copy.isEqual(new BgReading())); + } + @Test public void calculateDirection() throws Exception { assertEquals("??", bgReading.calculateDirection()); + bgReading = new BgReading(); + glucoseStatus = new GlucoseStatus(); + glucoseStatus.glucose = 0; + glucoseStatus.prev_glucose = 0; + glucoseStatus.date = 1000L * 60 * 12;; + glucoseStatus.previous_date = 1000L * 60 * 6; + BgReading newReading = Mockito.spy(new BgReading()); + doReturn(glucoseStatus).when(newReading).getGlucoseStatus(); + assertEquals("??", newReading.calculateDirection()); + glucoseStatus.glucose = 72; + glucoseStatus.prev_glucose = 10; + assertEquals("DoubleUp", newReading.calculateDirection()); + glucoseStatus.glucose = 72; + glucoseStatus.prev_glucose = 55; + assertEquals("SingleUp", newReading.calculateDirection()); + glucoseStatus.glucose = 72; + glucoseStatus.prev_glucose = 65; + assertEquals("FortyFiveUp", newReading.calculateDirection()); + glucoseStatus.glucose = 72; + glucoseStatus.prev_glucose = 70; + assertEquals("Flat", newReading.calculateDirection()); + glucoseStatus.glucose = 10; + glucoseStatus.prev_glucose = 72; + assertEquals("DoubleDown", newReading.calculateDirection()); + glucoseStatus.glucose = 55; + glucoseStatus.prev_glucose = 72; + assertEquals("SingleDown", newReading.calculateDirection()); + glucoseStatus.glucose = 65; + glucoseStatus.prev_glucose = 72; + assertEquals("FortyFiveDown", newReading.calculateDirection()); + + }