From 94b1fa355235feb7fa290f82e27c668d6219383b Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Fri, 17 Aug 2018 16:41:12 +0200 Subject: [PATCH] fix bucketed data, add some tests, fix failing tests --- .../nightscout/androidaps/db/BgReading.java | 10 ++ .../IobCobCalculatorPlugin.java | 18 +-- app/src/test/java/info/AAPSMocker.java | 3 +- .../IobCobCalculatorPluginTest.java | 114 ++++++++++++++++++ .../comm/DanaRS_Packet_History_AlarmTest.java | 2 +- ...DanaRS_Packet_History_All_HistoryTest.java | 2 +- .../comm/DanaRS_Packet_History_BasalTest.java | 2 +- ...naRS_Packet_History_Blood_GlucoseTest.java | 2 +- .../comm/DanaRS_Packet_History_BolusTest.java | 2 +- ...anaRS_Packet_History_CarbohydrateTest.java | 2 +- .../comm/DanaRS_Packet_History_DailyTest.java | 2 +- .../comm/DanaRS_Packet_History_PrimeTest.java | 2 +- .../DanaRS_Packet_History_RefillTest.java | 2 +- .../DanaRS_Packet_History_SuspendTest.java | 2 +- .../DanaRS_Packet_History_TemporaryTest.java | 2 +- 15 files changed, 148 insertions(+), 19 deletions(-) create mode 100644 app/src/test/java/info/nightscout/androidaps/plugins/IobCobCalculatorPlugin/IobCobCalculatorPluginTest.java 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 83b4e62cf0..64837a60cc 100644 --- a/app/src/main/java/info/nightscout/androidaps/db/BgReading.java +++ b/app/src/main/java/info/nightscout/androidaps/db/BgReading.java @@ -162,6 +162,16 @@ public class BgReading implements DataPointWithLabelInterface { _id = other._id; } + public BgReading date(long date) { + this.date = date; + return this; + } + + public BgReading value(double value) { + this.value = value; + return this; + } + // ------------------ DataPointWithLabelInterface ------------------ @Override public double getX() { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/IobCobCalculator/IobCobCalculatorPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/IobCobCalculator/IobCobCalculatorPlugin.java index 0b238eddb3..3e0ccbba7f 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/IobCobCalculator/IobCobCalculatorPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/IobCobCalculator/IobCobCalculatorPlugin.java @@ -102,6 +102,10 @@ public class IobCobCalculatorPlugin extends PluginBase { return bgReadings; } + public void setBgReadings(List bgReadings) { + this.bgReadings = bgReadings; + } + public List getBucketedData() { return bucketed_data; } @@ -149,7 +153,7 @@ public class IobCobCalculatorPlugin extends PluginBase { log.debug("BG data loaded. Size: " + bgReadings.size() + " Start date: " + DateUtil.dateAndTimeString(start) + " End date: " + DateUtil.dateAndTimeString(now)); } - private boolean isAbout5minData() { + public boolean isAbout5minData() { synchronized (dataLock) { if (bgReadings == null || bgReadings.size() < 3) { return true; @@ -160,10 +164,10 @@ public class IobCobCalculatorPlugin extends PluginBase { long lastbgTime = bgReadings.get(i - 1).date; long diff = lastbgTime - bgTime; totalDiff += diff; - if (diff > 30 * 1000 && diff < 270 * 1000) { // 0:30 - 4:30 - log.debug("Interval detection: values: " + bgReadings.size() + " diff: " + (diff / 1000) + "sec is5minData: " + false); + if (diff > T.secs(30).msecs() && diff < T.secs(270).msecs()) { // 0:30 - 4:30 if (L.isEnabled(L.AUTOSENS)) - return false; + log.debug("Interval detection: values: " + bgReadings.size() + " diff: " + (diff / 1000) + "sec is5minData: " + false); + return false; } } double intervals = totalDiff / (5 * 60 * 1000d); @@ -175,7 +179,7 @@ public class IobCobCalculatorPlugin extends PluginBase { } } - void createBucketedData() { + public void createBucketedData() { if (isAbout5minData()) createBucketedData5min(); else @@ -299,9 +303,9 @@ public class IobCobCalculatorPlugin extends PluginBase { } // Normalize bucketed data - for (int i = bucketed_data.size() - 2; i > 0 ; i--) { + for (int i = bucketed_data.size() - 2; i > 0; i--) { BgReading current = bucketed_data.get(i); - BgReading previous = bucketed_data.get(i+1); + BgReading previous = bucketed_data.get(i + 1); long msecDiff = current.date - previous.date; long adjusted = (msecDiff - T.mins(5).msecs()) / 1000; log.debug("Adjusting bucketed data time. Current: " + DateUtil.toISOString(current.date) + " to: " + DateUtil.toISOString(previous.date + T.mins(5).msecs()) + " by " + adjusted + " sec"); diff --git a/app/src/test/java/info/AAPSMocker.java b/app/src/test/java/info/AAPSMocker.java index b44fa732d1..e52d61169b 100644 --- a/app/src/test/java/info/AAPSMocker.java +++ b/app/src/test/java/info/AAPSMocker.java @@ -148,9 +148,10 @@ public class AAPSMocker { when(MainApp.instance().getApplicationContext()).thenReturn(context); } - public static void mockDatabaseHelper() { + public static DatabaseHelper mockDatabaseHelper() { DatabaseHelper databaseHelper = mock(DatabaseHelper.class); when(MainApp.getDbHelper()).thenReturn(databaseHelper); + return databaseHelper; } public static void mockCommandQueue() { diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/IobCobCalculatorPlugin/IobCobCalculatorPluginTest.java b/app/src/test/java/info/nightscout/androidaps/plugins/IobCobCalculatorPlugin/IobCobCalculatorPluginTest.java new file mode 100644 index 0000000000..063cc2efca --- /dev/null +++ b/app/src/test/java/info/nightscout/androidaps/plugins/IobCobCalculatorPlugin/IobCobCalculatorPluginTest.java @@ -0,0 +1,114 @@ +package info.nightscout.androidaps.plugins.IobCobCalculatorPlugin; + +import android.content.Context; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; + +import java.util.ArrayList; +import java.util.List; + +import info.AAPSMocker; +import info.nightscout.androidaps.MainApp; +import info.nightscout.androidaps.db.BgReading; +import info.nightscout.androidaps.db.DatabaseHelper; +import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; +import info.nightscout.androidaps.plugins.IobCobCalculator.IobCobCalculatorPlugin; +import info.nightscout.utils.SP; +import info.nightscout.utils.T; + +@RunWith(PowerMockRunner.class) +@PrepareForTest({MainApp.class, ConfigBuilderPlugin.class, SP.class, Context.class}) +public class IobCobCalculatorPluginTest { + + IobCobCalculatorPlugin iobCobCalculatorPlugin = IobCobCalculatorPlugin.getPlugin(); + + @Test + public void isAbout5minDataTest() { + List bgReadingList = new ArrayList<>(); + + // Super data should not be touched + bgReadingList.clear(); + bgReadingList.add(new BgReading().date(T.mins(20).msecs()).value(100)); + bgReadingList.add(new BgReading().date(T.mins(15).msecs()).value(100)); + bgReadingList.add(new BgReading().date(T.mins(10).msecs()).value(100)); + bgReadingList.add(new BgReading().date(T.mins(5).msecs()).value(100)); + + iobCobCalculatorPlugin.setBgReadings(bgReadingList); + + Assert.assertEquals(true, iobCobCalculatorPlugin.isAbout5minData()); + + // too much shifted data should return false + bgReadingList.clear(); + bgReadingList.add(new BgReading().date(T.mins(20).msecs()).value(100)); + bgReadingList.add(new BgReading().date(T.mins(15).msecs()).value(100)); + bgReadingList.add(new BgReading().date(T.mins(9).msecs()).value(100)); + bgReadingList.add(new BgReading().date(T.mins(5).msecs()).value(100)); + + iobCobCalculatorPlugin.setBgReadings(bgReadingList); + + Assert.assertEquals(false, iobCobCalculatorPlugin.isAbout5minData()); + + // too much shifted and missing data should return false + bgReadingList.clear(); + bgReadingList.add(new BgReading().date(T.mins(20).msecs()).value(100)); + bgReadingList.add(new BgReading().date(T.mins(9).msecs()).value(100)); + bgReadingList.add(new BgReading().date(T.mins(5).msecs()).value(100)); + + iobCobCalculatorPlugin.setBgReadings(bgReadingList); + + Assert.assertEquals(false, iobCobCalculatorPlugin.isAbout5minData()); + + // slighly shifted data should return true + bgReadingList.clear(); + bgReadingList.add(new BgReading().date(T.mins(20).msecs()).value(100)); + bgReadingList.add(new BgReading().date(T.mins(15).msecs()).value(100)); + bgReadingList.add(new BgReading().date(T.mins(10).msecs() - T.secs(10).msecs()).value(100)); + bgReadingList.add(new BgReading().date(T.mins(5).msecs()).value(100)); + + iobCobCalculatorPlugin.setBgReadings(bgReadingList); + + Assert.assertEquals(true, iobCobCalculatorPlugin.isAbout5minData()); + + // slighly shifted and missing data should return true + bgReadingList.clear(); + bgReadingList.add(new BgReading().date(T.mins(20).msecs()).value(100)); + bgReadingList.add(new BgReading().date(T.mins(10).msecs() - T.secs(10).msecs()).value(100)); + bgReadingList.add(new BgReading().date(T.mins(5).msecs()).value(100)); + + iobCobCalculatorPlugin.setBgReadings(bgReadingList); + + Assert.assertEquals(true, iobCobCalculatorPlugin.isAbout5minData()); + + } + + @Test + public void createBucketedData5minTest() { + List bgReadingList = new ArrayList<>(); + + // Super data should not be touched + bgReadingList.clear(); + bgReadingList.add(new BgReading().date(T.mins(20).msecs()).value(100)); + bgReadingList.add(new BgReading().date(T.mins(15).msecs()).value(100)); + bgReadingList.add(new BgReading().date(T.mins(10).msecs()).value(100)); + bgReadingList.add(new BgReading().date(T.mins(5).msecs()).value(100)); + + iobCobCalculatorPlugin.setBgReadings(bgReadingList); + iobCobCalculatorPlugin.createBucketedData(); + + Assert.assertEquals(true, iobCobCalculatorPlugin.isAbout5minData()); + Assert.assertEquals(bgReadingList.get(0).date, iobCobCalculatorPlugin.getBucketedData().get(0).date); + Assert.assertEquals(bgReadingList.get(3).date, iobCobCalculatorPlugin.getBucketedData().get(3).date); + Assert.assertEquals(bgReadingList.size(), iobCobCalculatorPlugin.getBucketedData().size()); + + } + + @Before + public void doMock() { + AAPSMocker.mockMainApp(); + } +} diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_History_AlarmTest.java b/app/src/test/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_History_AlarmTest.java index d710f82d6b..a058a8dd85 100644 --- a/app/src/test/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_History_AlarmTest.java +++ b/app/src/test/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_History_AlarmTest.java @@ -29,7 +29,7 @@ public class DanaRS_Packet_History_AlarmTest { AAPSMocker.mockApplicationContext(); AAPSMocker.mockSP(); AAPSMocker.mockL(); - DanaRS_Packet_History_Alarm packet = new DanaRS_Packet_History_Alarm(new Date(System.currentTimeMillis())); + DanaRS_Packet_History_Alarm packet = new DanaRS_Packet_History_Alarm(System.currentTimeMillis()); assertEquals("REVIEW__ALARM", packet.getFriendlyName()); } diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_History_All_HistoryTest.java b/app/src/test/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_History_All_HistoryTest.java index e151ad929e..d93a58b281 100644 --- a/app/src/test/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_History_All_HistoryTest.java +++ b/app/src/test/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_History_All_HistoryTest.java @@ -29,7 +29,7 @@ public class DanaRS_Packet_History_All_HistoryTest { AAPSMocker.mockApplicationContext(); AAPSMocker.mockSP(); AAPSMocker.mockL(); - DanaRS_Packet_History_All_History packet = new DanaRS_Packet_History_All_History(new Date(System.currentTimeMillis())); + DanaRS_Packet_History_All_History packet = new DanaRS_Packet_History_All_History(System.currentTimeMillis()); assertEquals("REVIEW__ALL_HISTORY", packet.getFriendlyName()); } diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_History_BasalTest.java b/app/src/test/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_History_BasalTest.java index 5788c6af82..86673cc507 100644 --- a/app/src/test/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_History_BasalTest.java +++ b/app/src/test/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_History_BasalTest.java @@ -29,7 +29,7 @@ public class DanaRS_Packet_History_BasalTest { AAPSMocker.mockApplicationContext(); AAPSMocker.mockSP(); AAPSMocker.mockL(); - DanaRS_Packet_History_Basal packet = new DanaRS_Packet_History_Basal(new Date(System.currentTimeMillis())); + DanaRS_Packet_History_Basal packet = new DanaRS_Packet_History_Basal(System.currentTimeMillis()); assertEquals("REVIEW__BASAL", packet.getFriendlyName()); } diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_History_Blood_GlucoseTest.java b/app/src/test/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_History_Blood_GlucoseTest.java index 3c6898f4a3..04ed0e173f 100644 --- a/app/src/test/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_History_Blood_GlucoseTest.java +++ b/app/src/test/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_History_Blood_GlucoseTest.java @@ -29,7 +29,7 @@ public class DanaRS_Packet_History_Blood_GlucoseTest { AAPSMocker.mockApplicationContext(); AAPSMocker.mockSP(); AAPSMocker.mockL(); - DanaRS_Packet_History_Blood_Glucose packet = new DanaRS_Packet_History_Blood_Glucose(new Date(System.currentTimeMillis())); + DanaRS_Packet_History_Blood_Glucose packet = new DanaRS_Packet_History_Blood_Glucose(System.currentTimeMillis()); assertEquals("REVIEW__BLOOD_GLUCOSE", packet.getFriendlyName()); } diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_History_BolusTest.java b/app/src/test/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_History_BolusTest.java index 442be028a5..1a4951f6dc 100644 --- a/app/src/test/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_History_BolusTest.java +++ b/app/src/test/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_History_BolusTest.java @@ -29,7 +29,7 @@ public class DanaRS_Packet_History_BolusTest { AAPSMocker.mockApplicationContext(); AAPSMocker.mockSP(); AAPSMocker.mockL(); - DanaRS_Packet_History_Bolus packet = new DanaRS_Packet_History_Bolus(new Date(System.currentTimeMillis())); + DanaRS_Packet_History_Bolus packet = new DanaRS_Packet_History_Bolus(System.currentTimeMillis()); assertEquals("REVIEW__BOLUS", packet.getFriendlyName()); } diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_History_CarbohydrateTest.java b/app/src/test/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_History_CarbohydrateTest.java index 31db3dd27f..ff1b041953 100644 --- a/app/src/test/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_History_CarbohydrateTest.java +++ b/app/src/test/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_History_CarbohydrateTest.java @@ -29,7 +29,7 @@ public class DanaRS_Packet_History_CarbohydrateTest { AAPSMocker.mockApplicationContext(); AAPSMocker.mockSP(); AAPSMocker.mockL(); - DanaRS_Packet_History_Carbohydrate packet = new DanaRS_Packet_History_Carbohydrate(new Date(System.currentTimeMillis())); + DanaRS_Packet_History_Carbohydrate packet = new DanaRS_Packet_History_Carbohydrate(System.currentTimeMillis()); assertEquals("REVIEW__CARBOHYDRATE", packet.getFriendlyName()); } diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_History_DailyTest.java b/app/src/test/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_History_DailyTest.java index b3ab86078a..8ba3df73b1 100644 --- a/app/src/test/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_History_DailyTest.java +++ b/app/src/test/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_History_DailyTest.java @@ -29,7 +29,7 @@ public class DanaRS_Packet_History_DailyTest { AAPSMocker.mockApplicationContext(); AAPSMocker.mockSP(); AAPSMocker.mockL(); - DanaRS_Packet_History_Daily packet = new DanaRS_Packet_History_Daily(new Date(System.currentTimeMillis())); + DanaRS_Packet_History_Daily packet = new DanaRS_Packet_History_Daily(System.currentTimeMillis()); assertEquals("REVIEW__DAILY", packet.getFriendlyName()); } diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_History_PrimeTest.java b/app/src/test/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_History_PrimeTest.java index 1e97ccb678..3f923d67cb 100644 --- a/app/src/test/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_History_PrimeTest.java +++ b/app/src/test/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_History_PrimeTest.java @@ -29,7 +29,7 @@ public class DanaRS_Packet_History_PrimeTest { AAPSMocker.mockApplicationContext(); AAPSMocker.mockSP(); AAPSMocker.mockL(); - DanaRS_Packet_History_Prime packet = new DanaRS_Packet_History_Prime(new Date(System.currentTimeMillis())); + DanaRS_Packet_History_Prime packet = new DanaRS_Packet_History_Prime(System.currentTimeMillis()); assertEquals("REVIEW__PRIME", packet.getFriendlyName()); } diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_History_RefillTest.java b/app/src/test/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_History_RefillTest.java index 0d6a74461e..d2b0c0e453 100644 --- a/app/src/test/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_History_RefillTest.java +++ b/app/src/test/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_History_RefillTest.java @@ -29,7 +29,7 @@ public class DanaRS_Packet_History_RefillTest { AAPSMocker.mockApplicationContext(); AAPSMocker.mockSP(); AAPSMocker.mockL(); - DanaRS_Packet_History_Refill packet = new DanaRS_Packet_History_Refill(new Date(System.currentTimeMillis())); + DanaRS_Packet_History_Refill packet = new DanaRS_Packet_History_Refill(System.currentTimeMillis()); assertEquals("REVIEW__REFILL", packet.getFriendlyName()); } diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_History_SuspendTest.java b/app/src/test/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_History_SuspendTest.java index 7b8b6b9817..665b20a27d 100644 --- a/app/src/test/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_History_SuspendTest.java +++ b/app/src/test/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_History_SuspendTest.java @@ -29,7 +29,7 @@ public class DanaRS_Packet_History_SuspendTest { AAPSMocker.mockApplicationContext(); AAPSMocker.mockSP(); AAPSMocker.mockL(); - DanaRS_Packet_History_Suspend packet = new DanaRS_Packet_History_Suspend(new Date(System.currentTimeMillis())); + DanaRS_Packet_History_Suspend packet = new DanaRS_Packet_History_Suspend(System.currentTimeMillis()); assertEquals("REVIEW__SUSPEND", packet.getFriendlyName()); } diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_History_TemporaryTest.java b/app/src/test/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_History_TemporaryTest.java index dbb67e3a7d..cd0fe15e7a 100644 --- a/app/src/test/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_History_TemporaryTest.java +++ b/app/src/test/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_History_TemporaryTest.java @@ -29,7 +29,7 @@ public class DanaRS_Packet_History_TemporaryTest { AAPSMocker.mockApplicationContext(); AAPSMocker.mockSP(); AAPSMocker.mockL(); - DanaRS_Packet_History_Temporary packet = new DanaRS_Packet_History_Temporary(new Date(System.currentTimeMillis())); + DanaRS_Packet_History_Temporary packet = new DanaRS_Packet_History_Temporary(System.currentTimeMillis()); assertEquals("REVIEW__TEMPORARY", packet.getFriendlyName()); }