1st ProfileSwitch must be with zero duration, ProfileIntervalsTest

This commit is contained in:
Milos Kozak 2018-03-27 14:19:52 +02:00
parent 0a87b0ede7
commit 1b9b20cd41
5 changed files with 141 additions and 17 deletions

View file

@ -38,8 +38,10 @@ public class ProfileIntervals<T extends Interval> {
}
public synchronized void add(T newInterval) {
rawData.put(newInterval.start(), newInterval);
merge();
if (newInterval.isValid()) {
rawData.put(newInterval.start(), newInterval);
merge();
}
}
public synchronized void add(List<T> list) {
@ -64,10 +66,12 @@ public class ProfileIntervals<T extends Interval> {
public synchronized Interval getValueToTime(long time) {
int index = binarySearch(time);
if (index >= 0) return rawData.valueAt(index);
// if we request data older than first record, use oldest instead
if (rawData.size() > 0) {
//log.debug("Requested profile for time: " + DateUtil.dateAndTimeString(time) + ". Providing oldest record: " + rawData.valueAt(0).toString());
return rawData.valueAt(0);
// if we request data older than first record, use oldest with zero duration instead
for (index = 0; index < rawData.size(); index++) {
if (rawData.valueAt(index).durationInMsec() == 0) {
//log.debug("Requested profile for time: " + DateUtil.dateAndTimeString(time) + ". Providing oldest record: " + rawData.valueAt(0).toString());
return rawData.valueAt(index);
}
}
return null;
}

View file

@ -61,6 +61,31 @@ public class ProfileSwitch implements Interval, DataPointWithLabelInterface {
private Profile profile = null;
public ProfileSwitch date(long date) {
this.date = date;
return this;
}
public ProfileSwitch profileName(String profileName) {
this.profileName = profileName;
return this;
}
public ProfileSwitch profile(Profile profile) {
this.profile = profile;
return this;
}
public ProfileSwitch source(int source) {
this.source = source;
return this;
}
public ProfileSwitch duration(int duration) {
this.durationInMinutes = duration;
return this;
}
@Nullable
public Profile getProfileObject() {
if (profile == null)

View file

@ -51,7 +51,6 @@ import info.nightscout.androidaps.db.ProfileSwitch;
import info.nightscout.androidaps.db.Source;
import info.nightscout.androidaps.db.TempTarget;
import info.nightscout.androidaps.events.EventNewBasalProfile;
import info.nightscout.androidaps.interfaces.Constraint;
import info.nightscout.androidaps.plugins.Careportal.OptionsToShow;
import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin;
import info.nightscout.androidaps.plugins.Overview.Dialogs.ErrorHelperActivity;
@ -349,19 +348,19 @@ public class NewNSTreatmentDialog extends DialogFragment implements View.OnClick
editTimeshift = (NumberPicker) view.findViewById(R.id.careportal_newnstreatment_timeshift);
editTimeshift.setParams(0d, (double) Constants.CPP_MIN_TIMESHIFT, (double) Constants.CPP_MAX_TIMESHIFT, 1d, new DecimalFormat("0"), false);
ProfileSwitch ps = MainApp.getConfigBuilder().getProfileSwitchFromHistory(System.currentTimeMillis());
ProfileSwitch ps = MainApp.getConfigBuilder().getProfileSwitchFromHistory(DateUtil.now());
if (ps != null && ps.isCPP) {
final int percentage = ps.percentage;
final int timeshift = ps.timeshift;
reuseButton.setText(reuseButton.getText() + " " + percentage + "% " + timeshift + "h");
reuseButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
editPercentage.setValue((double) percentage);
editTimeshift.setValue((double) timeshift);
}
reuseButton.setOnClickListener(v -> {
editPercentage.setValue((double) percentage);
editTimeshift.setValue((double) timeshift);
});
}
if (ps == null) {
options.duration = false;
}
showOrHide((ViewGroup) view.findViewById(R.id.careportal_newnstreatment_eventtime_layout), options.date);
showOrHide((ViewGroup) view.findViewById(R.id.careportal_newnstreatment_bg_layout), options.bg);

View file

@ -65,6 +65,7 @@ public class AAPSMocker {
when(MainApp.gs(R.string.delta)).thenReturn("Delta");
when(MainApp.gs(R.string.short_avgdelta)).thenReturn("Short avg. delta");
when(MainApp.gs(R.string.long_avgdelta)).thenReturn("Long avg. delta");
when(MainApp.gs(R.string.zerovalueinprofile)).thenReturn("Invalid profile: %s");
}
public static MainApp mockMainApp() {
@ -107,9 +108,11 @@ public class AAPSMocker {
when(MainApp.getDbHelper()).thenReturn(databaseHelper);
}
public static Profile getValidProfile() throws JSONException {
if (profile == null)
profile = new Profile(new JSONObject(validProfile), Constants.MGDL);
public static Profile getValidProfile() {
try {
if (profile == null)
profile = new Profile(new JSONObject(validProfile), Constants.MGDL);
} catch (JSONException e) {}
return profile;
}
}

View file

@ -0,0 +1,93 @@
package info.nightscout.androidaps.data;
import com.squareup.otto.Bus;
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.ProfileSwitch;
import info.nightscout.utils.DateUtil;
import info.nightscout.utils.T;
/**
* Created by mike on 26.03.2018.
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest({MainApp.class, Bus.class})
public class ProfileIntervalsTest {
private final long startDate = DateUtil.now();
ProfileIntervals<ProfileSwitch> list = new ProfileIntervals<>();
@Test
public void doTests() {
// create one 10h interval and test value in and out
list.add(new ProfileSwitch().date(startDate).duration((int) T.hours(10).mins()).profileName("1").profile(AAPSMocker.getValidProfile()));
// for older date first record should be returned only if has zero duration
Assert.assertEquals(null, list.getValueToTime(startDate - T.secs(1).msecs()));
Assert.assertEquals("1", ((ProfileSwitch) list.getValueToTime(startDate)).profileName);
Assert.assertEquals(null, list.getValueToTime(startDate + T.hours(10).msecs() + 1));
list.reset();
list.add(new ProfileSwitch().date(startDate).profileName("1").profile(AAPSMocker.getValidProfile()));
Assert.assertEquals("1", ((ProfileSwitch) list.getValueToTime(startDate - T.secs(1).msecs())).profileName);
Assert.assertEquals("1", ((ProfileSwitch) list.getValueToTime(startDate)).profileName);
Assert.assertEquals("1", ((ProfileSwitch) list.getValueToTime(startDate + T.hours(10).msecs() + 1)).profileName);
// switch to different profile after 5h
list.add(new ProfileSwitch().date(startDate + T.hours(5).msecs()).duration(0).profileName("2").profile(AAPSMocker.getValidProfile()));
Assert.assertEquals("1", ((ProfileSwitch) list.getValueToTime(startDate - T.secs(1).msecs())).profileName);
Assert.assertEquals("1", ((ProfileSwitch) list.getValueToTime(startDate + T.hours(5).msecs() - 1)).profileName);
Assert.assertEquals("2", ((ProfileSwitch) list.getValueToTime(startDate + T.hours(5).msecs() + 1)).profileName);
// insert 1h interval inside
list.add(new ProfileSwitch().date(startDate + T.hours(6).msecs()).duration((int) T.hours(1).mins()).profileName("3").profile(AAPSMocker.getValidProfile()));
Assert.assertEquals("2", ((ProfileSwitch) list.getValueToTime(startDate + T.hours(6).msecs() - 1)).profileName);
Assert.assertEquals("3", ((ProfileSwitch) list.getValueToTime(startDate + T.hours(6).msecs() + 1)).profileName);
Assert.assertEquals("2", ((ProfileSwitch) list.getValueToTime(startDate + T.hours(7).msecs() + 1)).profileName);
}
@Test
public void testCopyConstructor() {
list.reset();
list.add(new ProfileSwitch().date(startDate).duration((int) T.hours(10).mins()).profileName("4").profile(AAPSMocker.getValidProfile()));
ProfileIntervals<ProfileSwitch> list2 = new ProfileIntervals<>(list);
Assert.assertEquals(1, list2.getList().size());
}
@Test
public void invalidProfilesShouldNotBeReturned() {
list.reset();
list.add(new ProfileSwitch().date(startDate + T.hours(1).msecs()).profileName("6"));
Assert.assertEquals(null, list.get(0));
}
@Test
public void testReversingArrays() {
List<ProfileSwitch> someList = new ArrayList<>();
someList.add(new ProfileSwitch().date(startDate).duration((int) T.hours(3).mins()).profileName("5").profile(AAPSMocker.getValidProfile()));
someList.add(new ProfileSwitch().date(startDate + T.hours(1).msecs()).duration((int) T.hours(1).mins()).profileName("6").profile(AAPSMocker.getValidProfile()));
list.reset();
list.add(someList);
Assert.assertEquals(startDate, list.get(0).date);
Assert.assertEquals(startDate + T.hours(1).msecs(), list.getReversed(0).date);
Assert.assertEquals(startDate + T.hours(1).msecs(), list.getReversedList().get(0).date);
}
@Before
public void prepareMock() {
AAPSMocker.mockMainApp();
AAPSMocker.mockStrings();
AAPSMocker.mockBus();
}
}