TherapyEventExtensionKtTest

This commit is contained in:
Milos Kozak 2023-09-03 11:28:05 +02:00
parent d47e826ecc
commit f17b37e8df
3 changed files with 51 additions and 9 deletions

View file

@ -2,13 +2,14 @@ package info.nightscout.core.extensions
import info.nightscout.database.entities.TherapyEvent
import info.nightscout.interfaces.GlucoseUnit
import info.nightscout.shared.utils.DateUtil
fun TherapyEvent.isOlderThan(hours: Double): Boolean {
return getHoursFromStart() > hours
fun TherapyEvent.isOlderThan(hours: Double, dateUtil: DateUtil): Boolean {
return getHoursFromStart(dateUtil) > hours
}
fun TherapyEvent.getHoursFromStart(): Double {
return (System.currentTimeMillis() - timestamp) / (60 * 60 * 1000.0)
fun TherapyEvent.getHoursFromStart(dateUtil: DateUtil): Double {
return (dateUtil.now() - timestamp) / (60 * 60 * 1000.0)
}
fun TherapyEvent.GlucoseUnit.Companion.fromConstant(units: GlucoseUnit): TherapyEvent.GlucoseUnit =

View file

@ -5,11 +5,13 @@ import info.nightscout.core.extensions.isOlderThan
import info.nightscout.database.entities.TherapyEvent
import info.nightscout.interfaces.pump.WarnColors
import info.nightscout.shared.interfaces.ResourceHelper
import info.nightscout.shared.utils.DateUtil
import javax.inject.Inject
import javax.inject.Singleton
@Singleton
class WarnColorsImpl @Inject constructor(val rh: ResourceHelper): WarnColors {
@Singleton class WarnColorsImpl @Inject constructor(
private val rh: ResourceHelper, private val dateUtil: DateUtil
) : WarnColors {
override fun setColor(view: TextView?, value: Double, warnLevel: Double, urgentLevel: Double) {
view?.setTextColor(
@ -39,9 +41,9 @@ class WarnColorsImpl @Inject constructor(val rh: ResourceHelper): WarnColors {
view?.setTextColor(
rh.gac(
view.context, when {
therapyEvent.isOlderThan(urgentThreshold) -> info.nightscout.core.ui.R.attr.lowColor
therapyEvent.isOlderThan(warnThreshold) -> info.nightscout.core.ui.R.attr.highColor
else -> info.nightscout.core.ui.R.attr.defaultTextColor
therapyEvent.isOlderThan(urgentThreshold, dateUtil) -> info.nightscout.core.ui.R.attr.lowColor
therapyEvent.isOlderThan(warnThreshold, dateUtil) -> info.nightscout.core.ui.R.attr.highColor
else -> info.nightscout.core.ui.R.attr.defaultTextColor
}
)
)

View file

@ -0,0 +1,39 @@
package info.nightscout.core.extensions
import info.nightscout.androidaps.TestBaseWithProfile
import info.nightscout.database.entities.TherapyEvent
import info.nightscout.database.entities.embedments.InterfaceIDs
import info.nightscout.shared.utils.T
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test
import org.mockito.Mockito
class TherapyEventExtensionKtTest : TestBaseWithProfile() {
private val now = 1000000L
@Test
fun isOlderThan() {
val therapyEvent = TherapyEvent(
timestamp = now,
isValid = true,
type = TherapyEvent.Type.ANNOUNCEMENT,
note = "c",
enteredBy = "dddd",
glucose = 101.0,
glucoseType = TherapyEvent.MeterType.FINGER,
glucoseUnit = TherapyEvent.GlucoseUnit.MGDL,
duration = 3600000,
interfaceIDs_backing = InterfaceIDs(
nightscoutId = "nightscoutId",
pumpId = 11000,
pumpType = InterfaceIDs.PumpType.DANA_I,
pumpSerial = "b"
)
)
Mockito.`when`(dateUtil.now()).thenReturn(now + T.mins(30).msecs())
Assertions.assertFalse(therapyEvent.isOlderThan(1.0, dateUtil))
Mockito.`when`(dateUtil.now()).thenReturn(now + T.hours(2).msecs())
Assertions.assertTrue(therapyEvent.isOlderThan(1.0, dateUtil))
}
}