From 62b2d83a1818c6e6ec70c6ff9e8850cf8fbeb05c Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Fri, 10 May 2019 08:47:32 +0200 Subject: [PATCH] cleanup (#68) --- .../nightscout/androidaps/db/BgReading.java | 51 +++++++++---------- .../iob/iobCobCalculator/GlucoseStatus.java | 6 ++- .../androidaps/db/BgReadingTest.java | 45 ++++++++-------- 3 files changed, 52 insertions(+), 50 deletions(-) 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 6f87f232de..bfedc94ad0 100644 --- a/app/src/main/java/info/nightscout/androidaps/db/BgReading.java +++ b/app/src/main/java/info/nightscout/androidaps/db/BgReading.java @@ -23,7 +23,7 @@ import info.nightscout.androidaps.utils.DecimalFormatter; @DatabaseTable(tableName = DatabaseHelper.DATABASE_BGREADINGS) public class BgReading implements DataPointWithLabelInterface { - private static Logger log = LoggerFactory.getLogger(L.DATABASE); + private static Logger log = LoggerFactory.getLogger(L.GLUCOSE); @DatabaseField(id = true) public long date; @@ -74,10 +74,10 @@ public class BgReading implements DataPointWithLabelInterface { public String directionToSymbol() { String symbol = ""; - if (direction == null) { + if (direction == null) direction = calculateDirection(); - this.directionToSymbol(); // possible endless loop ?!? - } else if (direction.compareTo("DoubleDown") == 0) { + + if (direction.compareTo("DoubleDown") == 0) { symbol = "\u21ca"; } else if (direction.compareTo("SingleDown") == 0) { symbol = "\u2193"; @@ -97,18 +97,13 @@ public class BgReading implements DataPointWithLabelInterface { return symbol; } - public static boolean isSlopeNameInvalid(String direction) { - if (direction.compareTo("NOT_COMPUTABLE") == 0 || + private static boolean isSlopeNameInvalid(String direction) { + return direction.compareTo("NOT_COMPUTABLE") == 0 || direction.compareTo("NOT COMPUTABLE") == 0 || direction.compareTo("OUT_OF_RANGE") == 0 || direction.compareTo("OUT OF RANGE") == 0 || direction.compareTo("NONE") == 0 || - direction.compareTo("NotComputable") == 0 - ) { - return true; - } else { - return false; - } + direction.compareTo("NotComputable") == 0; } @@ -125,7 +120,8 @@ public class BgReading implements DataPointWithLabelInterface { public boolean isDataChanging(BgReading other) { if (date != other.date) { - log.error("Comparing different"); + if (L.isEnabled(L.GLUCOSE)) + log.error("Comparing different"); return false; } if (value != other.value) @@ -135,7 +131,8 @@ public class BgReading implements DataPointWithLabelInterface { public boolean isEqual(BgReading other) { if (date != other.date) { - log.error("Comparing different"); + if (L.isEnabled(L.GLUCOSE)) + log.error("Comparing different"); return false; } if (value != other.value) @@ -151,7 +148,8 @@ public class BgReading implements DataPointWithLabelInterface { public void copyFrom(BgReading other) { if (date != other.date) { - log.error("Copying different"); + if (L.isEnabled(L.GLUCOSE)) + log.error("Copying different"); return; } value = other.value; @@ -250,18 +248,21 @@ public class BgReading implements DataPointWithLabelInterface { // Copied from xDrip+ - public String calculateDirection(){ - GlucoseStatus glucoseStatus = getGlucoseStatus(); - double slope = 0; + String calculateDirection() { + GlucoseStatus glucoseStatus = GlucoseStatus.getGlucoseStatusData(); + double slope; if (glucoseStatus == null || glucoseStatus.prev_glucose == 0) - return "??"; + return "NONE"; // 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)); + + if (L.isEnabled(L.GLUCOSE)) + log.debug("Slope is :" + slope + " delta " + glucoseStatus.delta + " date difference " + (glucoseStatus.date - glucoseStatus.previous_date)); + double slope_by_minute = slope * 60000; String arrow = "NONE"; @@ -280,14 +281,8 @@ public class BgReading implements DataPointWithLabelInterface { } else if (slope_by_minute <= (40)) { arrow = "DoubleUp"; } - log.debug("Direction set to: "+arrow); + if (L.isEnabled(L.GLUCOSE)) + log.debug("Direction set to: " + arrow); return arrow; - } - - // Used for testing purpose - protected GlucoseStatus getGlucoseStatus() { - return GlucoseStatus.getGlucoseStatusData(); - } - } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/iob/iobCobCalculator/GlucoseStatus.java b/app/src/main/java/info/nightscout/androidaps/plugins/iob/iobCobCalculator/GlucoseStatus.java index f2cb7502f0..54cac18e70 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/iob/iobCobCalculator/GlucoseStatus.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/iob/iobCobCalculator/GlucoseStatus.java @@ -98,8 +98,8 @@ public class GlucoseStatus { status.long_avgdelta = 0d; status.avgdelta = 0d; // for OpenAPS MA status.date = now_date; - status.previous_date = prevDate; // setting the previous value date for slope calculation - status.prev_glucose = prevValue; + status.previous_date = 0; // setting the previous value date for slope calculation + status.prev_glucose = 0; if (L.isEnabled(L.GLUCOSE)) log.debug("sizeRecords==1"); @@ -171,6 +171,8 @@ public class GlucoseStatus { status.long_avgdelta = average(long_deltas); status.avgdelta = status.short_avgdelta; // for OpenAPS MA + status.previous_date = prevDate; // setting the previous value date for slope calculation + status.prev_glucose = prevValue; if (L.isEnabled(L.GLUCOSE)) log.debug(status.log()); 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 4a43f9fb7a..b1b61285f4 100644 --- a/app/src/test/java/info/nightscout/androidaps/db/BgReadingTest.java +++ b/app/src/test/java/info/nightscout/androidaps/db/BgReadingTest.java @@ -5,6 +5,7 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.Mockito; +import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; @@ -18,21 +19,23 @@ import info.nightscout.androidaps.logging.L; 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; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.when; @RunWith(PowerMockRunner.class) -@PrepareForTest({MainApp.class, Logger.class, L.class, SP.class}) +@PrepareForTest({MainApp.class, Logger.class, L.class, SP.class, GlucoseStatus.class}) public class BgReadingTest { private BgReading bgReading = new BgReading(); - @Mock + GlucoseStatus glucoseStatus; @Test public void valueToUnits() { bgReading.value = 18; - assertEquals(18, bgReading.valueToUnits(Constants.MGDL)*1, 0.01d); - assertEquals(1, bgReading.valueToUnits(Constants.MMOL)*1, 0.01d); + assertEquals(18, bgReading.valueToUnits(Constants.MGDL) * 1, 0.01d); + assertEquals(1, bgReading.valueToUnits(Constants.MMOL) * 1, 0.01d); } @Test @@ -58,7 +61,7 @@ public class BgReadingTest { } @Test - public void dateTest(){ + public void dateTest() { bgReading = new BgReading(); long now = System.currentTimeMillis(); bgReading.date = now; @@ -68,14 +71,14 @@ public class BgReadingTest { } @Test - public void valueTest(){ + public void valueTest() { bgReading = new BgReading(); double valueToSet = 81; // 4.5 mmol assertEquals(81d, bgReading.value(valueToSet).value, 0.01d); } @Test - public void copyFromTest(){ + public void copyFromTest() { bgReading = new BgReading(); BgReading copy = new BgReading(); bgReading.value = 81; @@ -91,7 +94,7 @@ public class BgReadingTest { } @Test - public void isEqualTest(){ + public void isEqualTest() { bgReading = new BgReading(); BgReading copy = new BgReading(); bgReading.value = 81; @@ -101,23 +104,26 @@ public class BgReadingTest { copy.copyFrom(bgReading); - assertEquals(true, copy.isEqual(bgReading)); - assertEquals(false, copy.isEqual(new BgReading())); + assertTrue(copy.isEqual(bgReading)); + assertFalse(copy.isEqual(new BgReading())); } @Test - public void calculateDirection() throws Exception { - assertEquals("??", bgReading.calculateDirection()); + public void calculateDirection() { + assertEquals("NONE", bgReading.calculateDirection()); - bgReading = new BgReading(); glucoseStatus = new GlucoseStatus(); glucoseStatus.glucose = 0; glucoseStatus.prev_glucose = 0; - glucoseStatus.date = 1000L * 60 * 12;; + 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()); + + BgReading newReading = new BgReading(); + + PowerMockito.mockStatic(GlucoseStatus.class); + when(GlucoseStatus.getGlucoseStatusData()).thenReturn(glucoseStatus); + + assertEquals("NONE", newReading.calculateDirection()); glucoseStatus.glucose = 72; glucoseStatus.prev_glucose = 10; assertEquals("DoubleUp", newReading.calculateDirection()); @@ -141,7 +147,6 @@ public class BgReadingTest { assertEquals("FortyFiveDown", newReading.calculateDirection()); - } @Before