fix bucketed data, add some tests, fix failing tests

This commit is contained in:
Milos Kozak 2018-08-17 16:41:12 +02:00
parent 74f2866fb5
commit 94b1fa3552
15 changed files with 148 additions and 19 deletions

View file

@ -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() {

View file

@ -102,6 +102,10 @@ public class IobCobCalculatorPlugin extends PluginBase {
return bgReadings;
}
public void setBgReadings(List<BgReading> bgReadings) {
this.bgReadings = bgReadings;
}
public List<BgReading> 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");

View file

@ -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() {

View file

@ -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<BgReading> 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<BgReading> 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();
}
}

View file

@ -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());
}

View file

@ -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());
}

View file

@ -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());
}

View file

@ -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());
}

View file

@ -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());
}

View file

@ -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());
}

View file

@ -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());
}

View file

@ -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());
}

View file

@ -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());
}

View file

@ -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());
}

View file

@ -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());
}