Merge pull request #1293 from MilosKozak/rscomm

Rscomm test update
This commit is contained in:
Milos Kozak 2018-08-02 16:34:32 +02:00 committed by GitHub
commit fe532598c4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 59 additions and 44 deletions

View file

@ -19,11 +19,13 @@ import info.nightscout.androidaps.data.ConstraintChecker;
import info.nightscout.androidaps.data.Profile;
import info.nightscout.androidaps.data.ProfileStore;
import info.nightscout.androidaps.db.DatabaseHelper;
import info.nightscout.androidaps.logging.L;
import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin;
import info.nightscout.androidaps.plugins.Treatments.TreatmentService;
import info.nightscout.androidaps.queue.CommandQueue;
import info.nightscout.utils.SP;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyLong;
@ -122,6 +124,11 @@ public class AAPSMocker {
when(SP.getInt(anyInt(), anyInt())).thenReturn(0);
}
public static void mockL() {
PowerMockito.mockStatic(L.class);
when(L.isEnabled(any())).thenReturn(true);
}
public static void mockApplicationContext() {
Context context = mock(Context.class);
when(MainApp.instance().getApplicationContext()).thenReturn(context);

View file

@ -1,8 +1,6 @@
package info.nightscout.androidaps.plugins.PumpDanaRS.comm;
import android.content.Context;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
@ -10,77 +8,87 @@ import org.powermock.modules.junit4.PowerMockRunner;
import info.AAPSMocker;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin;
import info.nightscout.androidaps.logging.L;
import info.nightscout.utils.SP;
import info.nightscout.utils.ToastUtils;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.when;
/**
* Created by mike on 20.11.2017.
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest({MainApp.class, SP.class})
public class DanaRS_Packet_APS_Basal_Set_Temporary_BasalTest extends DanaRS_Packet_APS_Basal_Set_Temporary_Basal {
@PrepareForTest({MainApp.class, SP.class, L.class})
public class DanaRS_Packet_APS_Basal_Set_Temporary_BasalTest {
@Test
public void runTest() {
AAPSMocker.mockMainApp();
AAPSMocker.mockApplicationContext();
AAPSMocker.mockSP();
AAPSMocker.mockL();
// under 100% should last 30 min
setParams(0);
assertEquals(0, temporaryBasalRatio);
assertEquals(PARAM30MIN, temporaryBasalDuration);
DanaRS_Packet_APS_Basal_Set_Temporary_Basal packet = new DanaRS_Packet_APS_Basal_Set_Temporary_Basal();
packet.setParams(0);
assertEquals(0, packet.temporaryBasalRatio);
assertEquals(packet.PARAM30MIN, packet.temporaryBasalDuration);
//constructor with param
packet = new DanaRS_Packet_APS_Basal_Set_Temporary_Basal(10);
assertEquals(10, packet.temporaryBasalRatio);
assertEquals(packet.PARAM30MIN, packet.temporaryBasalDuration);
packet = new DanaRS_Packet_APS_Basal_Set_Temporary_Basal(20, true, false);
assertEquals(20, packet.temporaryBasalRatio);
assertEquals(packet.PARAM15MIN, packet.temporaryBasalDuration);
// over 100% should last 15 min
setParams(150);
assertEquals(150, temporaryBasalRatio);
assertEquals(PARAM15MIN, temporaryBasalDuration);
packet.setParams(150);
assertEquals(150, packet.temporaryBasalRatio);
assertEquals(packet.PARAM15MIN, packet.temporaryBasalDuration);
// test low hard limit
setParams(-1);
assertEquals(0, temporaryBasalRatio);
assertEquals(PARAM30MIN, temporaryBasalDuration);
packet.setParams(-1);
assertEquals(0, packet.temporaryBasalRatio);
assertEquals(packet.PARAM30MIN, packet.temporaryBasalDuration);
// test high hard limit
setParams(550);
assertEquals(500, temporaryBasalRatio);
assertEquals(PARAM15MIN, temporaryBasalDuration);
packet.setParams(550);
assertEquals(500, packet.temporaryBasalRatio);
assertEquals(packet.PARAM15MIN, packet.temporaryBasalDuration);
// test setting 15 min
setParams(50, true, false);
assertEquals(50, temporaryBasalRatio);
assertEquals(PARAM15MIN, temporaryBasalDuration);
packet.setParams(50, true, false);
assertEquals(50, packet.temporaryBasalRatio);
assertEquals(packet.PARAM15MIN, packet.temporaryBasalDuration);
// test setting 30 min
setParams(50, false, true);
assertEquals(50, temporaryBasalRatio);
assertEquals(PARAM30MIN, temporaryBasalDuration);
packet.setParams(50, false, true);
assertEquals(50, packet.temporaryBasalRatio);
assertEquals(packet.PARAM30MIN, packet.temporaryBasalDuration);
// over 200% set always 15 min
setParams(250, false, true);
assertEquals(250, temporaryBasalRatio);
assertEquals(PARAM15MIN, temporaryBasalDuration);
packet.setParams(250, false, true);
assertEquals(250, packet.temporaryBasalRatio);
assertEquals(packet.PARAM15MIN, packet.temporaryBasalDuration);
// test low hard limit
setParams(-1, false, true);
assertEquals(0, temporaryBasalRatio);
assertEquals(PARAM30MIN, temporaryBasalDuration);
packet.setParams(-1, false, true);
assertEquals(0, packet.temporaryBasalRatio);
assertEquals(packet.PARAM30MIN, packet.temporaryBasalDuration);
// test high hard limit
setParams(550, false,true);
assertEquals(500, temporaryBasalRatio);
assertEquals(PARAM15MIN, temporaryBasalDuration);
packet.setParams(550, false, true);
assertEquals(500, packet.temporaryBasalRatio);
assertEquals(packet.PARAM15MIN, packet.temporaryBasalDuration);
// test message generation
setParams(260, true, false);
byte[] generatedCode = getRequestParams();
assertEquals(3 , generatedCode.length);
assertEquals((byte)4 , generatedCode[0]);
assertEquals((byte)1 , generatedCode[1]);
assertEquals((byte)PARAM15MIN, generatedCode[2]);
packet.setParams(260, true, false);
byte[] generatedCode = packet.getRequestParams();
assertEquals(3, generatedCode.length);
assertEquals((byte) 4, generatedCode[0]);
assertEquals((byte) 1, generatedCode[1]);
assertEquals((byte) packet.PARAM15MIN, generatedCode[2]);
// test message decoding
handleMessage(new byte[]{(byte) 0, (byte) 0, (byte) 0});
assertEquals(false, failed);
handleMessage(new byte[]{(byte) 0, (byte) 0, (byte) 1});
assertEquals(true, failed);
packet.handleMessage(new byte[]{(byte) 0, (byte) 0, (byte) 0});
assertEquals(false, packet.failed);
packet.handleMessage(new byte[]{(byte) 0, (byte) 0, (byte) 1});
assertEquals(true, packet.failed);
assertEquals("BASAL__APS_SET_TEMPORARY_BASAL", getFriendlyName());
assertEquals("BASAL__APS_SET_TEMPORARY_BASAL", packet.getFriendlyName());
}
}