From b2aa09890c5b446a63db6b4b633c1c502d376813 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Mon, 28 Nov 2022 10:46:53 +0100 Subject: [PATCH] fix tests --- .../androidaps/pump/PumpEnactResultTest.kt | 2 +- .../androidaps/data/DetailedBolusInfoTest.kt | 10 +++--- core/ui/src/main/res/values/strings.xml | 1 + core/utils/src/main/res/values/keys.xml | 1 + .../automation/actions/ActionsTestBase.kt | 17 ++++++++++ .../automation/triggers/TriggerTestBase.kt | 4 +-- .../triggers/TriggerWifiSsidTest.kt | 5 ++- .../nightscout/plugins/aps/loop/LoopPlugin.kt | 29 +---------------- .../extensions/PumpEnactResultExtension.kt | 32 +++++++++++++++++++ plugins/main/src/main/res/values/strings.xml | 2 -- .../nsclient/NsClientReceiverDelegateTest.kt | 6 ++-- .../plugins/sync/tidepool/comm/SessionTest.kt | 0 12 files changed, 64 insertions(+), 45 deletions(-) create mode 100644 plugins/main/src/main/java/info/nightscout/plugins/aps/loop/extensions/PumpEnactResultExtension.kt rename plugins/{main => sync}/src/test/java/info/nightscout/plugins/sync/nsclient/NsClientReceiverDelegateTest.kt (96%) rename plugins/{main => sync}/src/test/java/info/nightscout/plugins/sync/tidepool/comm/SessionTest.kt (100%) diff --git a/app/src/test/java/info/nightscout/androidaps/pump/PumpEnactResultTest.kt b/app/src/test/java/info/nightscout/androidaps/pump/PumpEnactResultTest.kt index f8e9402252..caa34d8832 100644 --- a/app/src/test/java/info/nightscout/androidaps/pump/PumpEnactResultTest.kt +++ b/app/src/test/java/info/nightscout/androidaps/pump/PumpEnactResultTest.kt @@ -6,8 +6,8 @@ import info.nightscout.androidaps.TestBaseWithProfile import info.nightscout.core.main.R import info.nightscout.core.pump.toHtml import info.nightscout.interfaces.pump.PumpEnactResult +import info.nightscout.plugins.aps.loop.extensions.json import info.nightscout.plugins.extensions.toText -import info.nightscout.plugins.sync.nsShared.extensions.json import info.nightscout.plugins.sync.nsShared.extensions.log import org.json.JSONObject import org.junit.Assert diff --git a/core/core-main/src/test/java/info/nightscout/androidaps/data/DetailedBolusInfoTest.kt b/core/core-main/src/test/java/info/nightscout/androidaps/data/DetailedBolusInfoTest.kt index 079a8b9fa6..8a64de27cf 100644 --- a/core/core-main/src/test/java/info/nightscout/androidaps/data/DetailedBolusInfoTest.kt +++ b/core/core-main/src/test/java/info/nightscout/androidaps/data/DetailedBolusInfoTest.kt @@ -68,9 +68,9 @@ class DetailedBolusInfoTest : TestBase() { detailedBolusInfo.insulin = 7.0 val bolus = detailedBolusInfo.createBolus() - Assert.assertEquals(1000L, bolus?.timestamp) - Assert.assertEquals(Bolus.Type.SMB, bolus?.type) - Assert.assertEquals(7.0, bolus?.amount) + Assert.assertEquals(1000L, bolus.timestamp) + Assert.assertEquals(Bolus.Type.SMB, bolus.type) + Assert.assertEquals(7.0, bolus.amount, 0.01) } @Test @@ -80,8 +80,8 @@ class DetailedBolusInfoTest : TestBase() { detailedBolusInfo.carbs = 6.0 val carbs = detailedBolusInfo.createCarbs() - Assert.assertEquals(1000L, carbs?.timestamp) - Assert.assertEquals(6.0, carbs?.amount) + Assert.assertEquals(1000L, carbs.timestamp) + Assert.assertEquals(6.0, carbs.amount, 0.01) } private fun createBolusCalculatorResult(): BolusCalculatorResult = diff --git a/core/ui/src/main/res/values/strings.xml b/core/ui/src/main/res/values/strings.xml index 382a18cade..f87cd8051e 100644 --- a/core/ui/src/main/res/values/strings.xml +++ b/core/ui/src/main/res/values/strings.xml @@ -33,6 +33,7 @@ Result Settings Status lights + Upload BG data to NS Battery diff --git a/core/utils/src/main/res/values/keys.xml b/core/utils/src/main/res/values/keys.xml index 051a932926..244df0330d 100644 --- a/core/utils/src/main/res/values/keys.xml +++ b/core/utils/src/main/res/values/keys.xml @@ -131,5 +131,6 @@ snoozedTo ns_receive_cgm ns_create_announcements_from_carbs_req + dexcomg5_nsupload \ No newline at end of file diff --git a/plugins/automation/src/test/java/info/nightscout/automation/actions/ActionsTestBase.kt b/plugins/automation/src/test/java/info/nightscout/automation/actions/ActionsTestBase.kt index 41778314f1..44fc8a3fd6 100644 --- a/plugins/automation/src/test/java/info/nightscout/automation/actions/ActionsTestBase.kt +++ b/plugins/automation/src/test/java/info/nightscout/automation/actions/ActionsTestBase.kt @@ -7,22 +7,29 @@ import info.nightscout.androidaps.TestBaseWithProfile import info.nightscout.androidaps.TestPumpPlugin import info.nightscout.interfaces.logging.UserEntryLogger import info.nightscout.automation.triggers.Trigger +import info.nightscout.database.entities.DeviceStatus import info.nightscout.database.entities.OfflineEvent import info.nightscout.interfaces.ConfigBuilder import info.nightscout.interfaces.GlucoseUnit import info.nightscout.interfaces.aps.Loop +import info.nightscout.interfaces.configBuilder.RunningConfiguration import info.nightscout.interfaces.constraints.Constraint +import info.nightscout.interfaces.iob.IobCobCalculator import info.nightscout.interfaces.plugin.ActivePlugin import info.nightscout.interfaces.plugin.PluginBase import info.nightscout.interfaces.plugin.PluginDescription import info.nightscout.interfaces.profile.Profile +import info.nightscout.interfaces.profile.ProfileFunction import info.nightscout.interfaces.profile.ProfileSource +import info.nightscout.interfaces.pump.Pump import info.nightscout.interfaces.pump.PumpEnactResult import info.nightscout.interfaces.queue.CommandQueue +import info.nightscout.interfaces.receivers.ReceiverStatusStore import info.nightscout.interfaces.smsCommunicator.SmsCommunicator import info.nightscout.rx.logging.AAPSLogger import info.nightscout.shared.interfaces.ResourceHelper import info.nightscout.shared.sharedPreferences.SP +import info.nightscout.shared.utils.DateUtil import org.junit.Before import org.mockito.Mock import org.mockito.Mockito.`when` @@ -57,6 +64,16 @@ ActionsTestBase : TestBaseWithProfile() { override fun goToZeroTemp(durationInMinutes: Int, profile: Profile, reason: OfflineEvent.Reason) {} override fun suspendLoop(durationInMinutes: Int) {} override fun disableCarbSuggestions(durationMinutes: Int) {} + override fun buildDeviceStatus( + dateUtil: DateUtil, + loop: Loop, + iobCobCalculatorPlugin: IobCobCalculator, + profileFunction: ProfileFunction, + pump: Pump, + receiverStatusStore: ReceiverStatusStore, + runningConfiguration: RunningConfiguration, + version: String + ): DeviceStatus? = null } @Mock lateinit var sp: SP diff --git a/plugins/automation/src/test/java/info/nightscout/automation/triggers/TriggerTestBase.kt b/plugins/automation/src/test/java/info/nightscout/automation/triggers/TriggerTestBase.kt index 9f99359547..5ee0773fc1 100644 --- a/plugins/automation/src/test/java/info/nightscout/automation/triggers/TriggerTestBase.kt +++ b/plugins/automation/src/test/java/info/nightscout/automation/triggers/TriggerTestBase.kt @@ -28,14 +28,12 @@ open class TriggerTestBase : TestBaseWithProfile() { @Mock lateinit var autosensDataStore: AutosensDataStore @Mock lateinit var context: Context @Mock lateinit var automationPlugin: AutomationPlugin - - lateinit var receiverStatusStore: ReceiverStatusStore + @Mock lateinit var receiverStatusStore: ReceiverStatusStore private val pluginDescription = PluginDescription() lateinit var testPumpPlugin: TestPumpPlugin @Before fun prepareMock1() { - receiverStatusStore = ReceiverStatusStore(context, rxBus) testPumpPlugin = TestPumpPlugin(pluginDescription, aapsLogger, rh, injector) `when`(activePlugin.activePump).thenReturn(testPumpPlugin) `when`(iobCobCalculator.ads).thenReturn(autosensDataStore) diff --git a/plugins/automation/src/test/java/info/nightscout/automation/triggers/TriggerWifiSsidTest.kt b/plugins/automation/src/test/java/info/nightscout/automation/triggers/TriggerWifiSsidTest.kt index ad114e9432..61acd6a1ee 100644 --- a/plugins/automation/src/test/java/info/nightscout/automation/triggers/TriggerWifiSsidTest.kt +++ b/plugins/automation/src/test/java/info/nightscout/automation/triggers/TriggerWifiSsidTest.kt @@ -14,14 +14,13 @@ import org.mockito.Mockito.`when` class TriggerWifiSsidTest : TriggerTestBase() { var now = 1514766900000L - @Before fun mock() { `when`(dateUtil.now()).thenReturn(now) } @Test fun shouldRunTest() { val e = EventNetworkChange() - receiverStatusStore.lastNetworkEvent = e + `when`(receiverStatusStore.lastNetworkEvent).thenReturn(e) var t: TriggerWifiSsid = TriggerWifiSsid(injector).setValue("aSSID").comparator(Comparator.Compare.IS_EQUAL) e.wifiConnected = false Assert.assertFalse(t.shouldRun()) @@ -36,7 +35,7 @@ class TriggerWifiSsidTest : TriggerTestBase() { Assert.assertTrue(t.shouldRun()) // no network data - receiverStatusStore.lastNetworkEvent = null + `when`(receiverStatusStore.lastNetworkEvent).thenReturn(null) Assert.assertFalse(t.shouldRun()) } diff --git a/plugins/main/src/main/java/info/nightscout/plugins/aps/loop/LoopPlugin.kt b/plugins/main/src/main/java/info/nightscout/plugins/aps/loop/LoopPlugin.kt index 11f18d6b8a..ee87e48f92 100644 --- a/plugins/main/src/main/java/info/nightscout/plugins/aps/loop/LoopPlugin.kt +++ b/plugins/main/src/main/java/info/nightscout/plugins/aps/loop/LoopPlugin.kt @@ -56,11 +56,11 @@ import info.nightscout.interfaces.queue.CommandQueue import info.nightscout.interfaces.receivers.ReceiverStatusStore import info.nightscout.interfaces.ui.ActivityNames import info.nightscout.interfaces.utils.HardLimits -import info.nightscout.interfaces.utils.Round import info.nightscout.plugins.R import info.nightscout.plugins.aps.loop.events.EventLoopSetLastRunGui import info.nightscout.plugins.aps.loop.events.EventLoopUpdateGui import info.nightscout.plugins.aps.loop.events.EventNewOpenLoopNotification +import info.nightscout.plugins.aps.loop.extensions.json import info.nightscout.plugins.pump.virtual.VirtualPumpPlugin import info.nightscout.rx.AapsSchedulers import info.nightscout.rx.bus.RxBus @@ -782,33 +782,6 @@ class LoopPlugin @Inject constructor( configuration = runningConfiguration.configuration().toString() ) } - - fun PumpEnactResult.json(baseBasal: Double): JSONObject { - val result = JSONObject() - when { - bolusDelivered > 0 -> { - result.put("smb", bolusDelivered) - } - - isTempCancel -> { - result.put("rate", 0) - result.put("duration", 0) - } - - isPercent -> { - // Nightscout is expecting absolute value - val abs = Round.roundTo(baseBasal * percent / 100, 0.01) - result.put("rate", abs) - result.put("duration", duration) - } - - else -> { - result.put("rate", absolute) - result.put("duration", duration) - } - } - return result - } companion object { private const val CHANNEL_ID = "AAPS-OpenLoop" diff --git a/plugins/main/src/main/java/info/nightscout/plugins/aps/loop/extensions/PumpEnactResultExtension.kt b/plugins/main/src/main/java/info/nightscout/plugins/aps/loop/extensions/PumpEnactResultExtension.kt new file mode 100644 index 0000000000..f6953be0ae --- /dev/null +++ b/plugins/main/src/main/java/info/nightscout/plugins/aps/loop/extensions/PumpEnactResultExtension.kt @@ -0,0 +1,32 @@ +package info.nightscout.plugins.aps.loop.extensions + +import info.nightscout.interfaces.pump.PumpEnactResult +import info.nightscout.interfaces.utils.Round +import org.json.JSONObject + +fun PumpEnactResult.json(baseBasal: Double): JSONObject { + val result = JSONObject() + when { + bolusDelivered > 0 -> { + result.put("smb", bolusDelivered) + } + + isTempCancel -> { + result.put("rate", 0) + result.put("duration", 0) + } + + isPercent -> { + // Nightscout is expecting absolute value + val abs = Round.roundTo(baseBasal * percent / 100, 0.01) + result.put("rate", abs) + result.put("duration", duration) + } + + else -> { + result.put("rate", absolute) + result.put("duration", duration) + } + } + return result +} diff --git a/plugins/main/src/main/res/values/strings.xml b/plugins/main/src/main/res/values/strings.xml index 0db241998a..2d32a5e10c 100644 --- a/plugins/main/src/main/res/values/strings.xml +++ b/plugins/main/src/main/res/values/strings.xml @@ -411,7 +411,6 @@ Upload status to NS - dexcomg5_nsupload dexcom_lognssensorchange last_processed_glunovo_timestamp last_processed_intelligo_timestamp @@ -449,7 +448,6 @@ Send BG data to xDrip+ In xDrip+ select 640g/Eversense data source - Upload BG data to NS BG upload settings Log sensor change to NS Create event \"Sensor Change\" in NS automatically on sensor start diff --git a/plugins/main/src/test/java/info/nightscout/plugins/sync/nsclient/NsClientReceiverDelegateTest.kt b/plugins/sync/src/test/java/info/nightscout/plugins/sync/nsclient/NsClientReceiverDelegateTest.kt similarity index 96% rename from plugins/main/src/test/java/info/nightscout/plugins/sync/nsclient/NsClientReceiverDelegateTest.kt rename to plugins/sync/src/test/java/info/nightscout/plugins/sync/nsclient/NsClientReceiverDelegateTest.kt index 3d087f1192..62226f82e0 100644 --- a/plugins/main/src/test/java/info/nightscout/plugins/sync/nsclient/NsClientReceiverDelegateTest.kt +++ b/plugins/sync/src/test/java/info/nightscout/plugins/sync/nsclient/NsClientReceiverDelegateTest.kt @@ -3,7 +3,7 @@ package info.nightscout.plugins.sync.nsclient import android.content.Context import info.nightscout.androidaps.TestBase import info.nightscout.interfaces.receivers.ReceiverStatusStore -import info.nightscout.plugins.R +import info.nightscout.plugins.sync.R import info.nightscout.rx.bus.RxBus import info.nightscout.rx.events.EventChargingState import info.nightscout.rx.events.EventNetworkChange @@ -22,13 +22,13 @@ class NsClientReceiverDelegateTest : TestBase() { @Mock lateinit var rh: ResourceHelper val rxBus = RxBus(aapsSchedulers, aapsLogger) - private lateinit var receiverStatusStore: ReceiverStatusStore + @Mock private lateinit var receiverStatusStore: ReceiverStatusStore private lateinit var sut: NsClientReceiverDelegate @Before fun prepare() { - receiverStatusStore = ReceiverStatusStore(context, rxBus) + //receiverStatusStore = ReceiverStatusStore(context, rxBus) sut = NsClientReceiverDelegate(rxBus, rh, sp, receiverStatusStore) } diff --git a/plugins/main/src/test/java/info/nightscout/plugins/sync/tidepool/comm/SessionTest.kt b/plugins/sync/src/test/java/info/nightscout/plugins/sync/tidepool/comm/SessionTest.kt similarity index 100% rename from plugins/main/src/test/java/info/nightscout/plugins/sync/tidepool/comm/SessionTest.kt rename to plugins/sync/src/test/java/info/nightscout/plugins/sync/tidepool/comm/SessionTest.kt