Rewrites SafetyPluginTest with matchers

Issue #2745
This commit is contained in:
Ryan Haining 2023-09-08 19:13:43 -07:00
parent 9ea67288a1
commit 75ff796510

View file

@ -1,5 +1,6 @@
package info.nightscout.plugins.safety package info.nightscout.plugins.safety
import com.google.common.truth.Truth.assertThat
import dagger.android.AndroidInjector import dagger.android.AndroidInjector
import dagger.android.HasAndroidInjector import dagger.android.HasAndroidInjector
import info.nightscout.database.impl.AppRepository import info.nightscout.database.impl.AppRepository
@ -21,7 +22,6 @@ import info.nightscout.plugins.constraints.safety.SafetyPlugin
import info.nightscout.pump.virtual.VirtualPumpPlugin import info.nightscout.pump.virtual.VirtualPumpPlugin
import info.nightscout.sharedtests.TestBaseWithProfile import info.nightscout.sharedtests.TestBaseWithProfile
import info.nightscout.source.GlimpPlugin import info.nightscout.source.GlimpPlugin
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.BeforeEach import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test import org.junit.jupiter.api.Test
import org.mockito.Mock import org.mockito.Mock
@ -88,8 +88,8 @@ class SafetyPluginTest : TestBaseWithProfile() {
pumpDescription.isTempBasalCapable = false pumpDescription.isTempBasalCapable = false
var c = Constraint(true) var c = Constraint(true)
c = safetyPlugin.isLoopInvocationAllowed(c) c = safetyPlugin.isLoopInvocationAllowed(c)
Assertions.assertEquals("Safety: Pump is not temp basal capable", c.getReasons(aapsLogger)) assertThat(c.getReasons(aapsLogger)).isEqualTo("Safety: Pump is not temp basal capable")
Assertions.assertEquals(false, c.value()) assertThat(c.value()).isFalse()
} }
@Test @Test
@ -98,8 +98,8 @@ class SafetyPluginTest : TestBaseWithProfile() {
`when`(config.isEngineeringModeOrRelease()).thenReturn(false) `when`(config.isEngineeringModeOrRelease()).thenReturn(false)
var c = Constraint(true) var c = Constraint(true)
c = safetyPlugin.isClosedLoopAllowed(c) c = safetyPlugin.isClosedLoopAllowed(c)
Assertions.assertTrue(c.getReasons(aapsLogger).contains("Running dev version. Closed loop is disabled.")) assertThat(c.getReasons(aapsLogger)).contains("Running dev version. Closed loop is disabled.")
Assertions.assertEquals(false, c.value()) assertThat(c.value()).isFalse()
} }
@Test @Test
@ -107,8 +107,8 @@ class SafetyPluginTest : TestBaseWithProfile() {
`when`(sp.getString(info.nightscout.core.utils.R.string.key_aps_mode, ApsMode.OPEN.name)).thenReturn(ApsMode.OPEN.name) `when`(sp.getString(info.nightscout.core.utils.R.string.key_aps_mode, ApsMode.OPEN.name)).thenReturn(ApsMode.OPEN.name)
var c = Constraint(true) var c = Constraint(true)
c = safetyPlugin.isClosedLoopAllowed(c) c = safetyPlugin.isClosedLoopAllowed(c)
Assertions.assertTrue(c.getReasons(aapsLogger).contains("Closed loop mode disabled in preferences")) assertThat(c.getReasons(aapsLogger)).contains("Closed loop mode disabled in preferences")
Assertions.assertEquals(false, c.value()) assertThat(c.value()).isFalse()
} }
@Test @Test
@ -117,8 +117,8 @@ class SafetyPluginTest : TestBaseWithProfile() {
`when`(constraintChecker.isClosedLoopAllowed(anyObject())).thenReturn(Constraint(true)) `when`(constraintChecker.isClosedLoopAllowed(anyObject())).thenReturn(Constraint(true))
var c = Constraint(true) var c = Constraint(true)
c = openAPSSMBPlugin.isSMBModeEnabled(c) c = openAPSSMBPlugin.isSMBModeEnabled(c)
Assertions.assertTrue(c.getReasons(aapsLogger).contains("SMB disabled in preferences")) assertThat(c.getReasons(aapsLogger)).contains("SMB disabled in preferences")
Assertions.assertEquals(false, c.value()) assertThat(c.value()).isFalse()
} }
@Test @Test
@ -127,8 +127,8 @@ class SafetyPluginTest : TestBaseWithProfile() {
`when`(constraintChecker.isClosedLoopAllowed(anyObject())).thenReturn(Constraint(false)) `when`(constraintChecker.isClosedLoopAllowed(anyObject())).thenReturn(Constraint(false))
var c = Constraint(true) var c = Constraint(true)
c = safetyPlugin.isSMBModeEnabled(c) c = safetyPlugin.isSMBModeEnabled(c)
Assertions.assertTrue(c.getReasons(aapsLogger).contains("SMB not allowed in open loop mode")) assertThat(c.getReasons(aapsLogger)).contains("SMB not allowed in open loop mode")
Assertions.assertEquals(false, c.value()) assertThat(c.value()).isFalse()
} }
@Test @Test
@ -136,8 +136,8 @@ class SafetyPluginTest : TestBaseWithProfile() {
`when`(activePlugin.activeBgSource).thenReturn(glimpPlugin) `when`(activePlugin.activeBgSource).thenReturn(glimpPlugin)
var c = Constraint(true) var c = Constraint(true)
c = safetyPlugin.isAdvancedFilteringEnabled(c) c = safetyPlugin.isAdvancedFilteringEnabled(c)
Assertions.assertEquals("Safety: SMB always and after carbs disabled because active BG source doesn\\'t support advanced filtering", c.getReasons(aapsLogger)) assertThat(c.getReasons(aapsLogger)).isEqualTo("Safety: SMB always and after carbs disabled because active BG source doesn\\'t support advanced filtering")
Assertions.assertEquals(false, c.value()) assertThat(c.value()).isFalse()
} }
@Test @Test
@ -148,13 +148,12 @@ class SafetyPluginTest : TestBaseWithProfile() {
`when`(sp.getString(info.nightscout.core.utils.R.string.key_age, "")).thenReturn("child") `when`(sp.getString(info.nightscout.core.utils.R.string.key_age, "")).thenReturn("child")
val c = Constraint(Constants.REALLYHIGHBASALRATE) val c = Constraint(Constants.REALLYHIGHBASALRATE)
safetyPlugin.applyBasalConstraints(c, validProfile) safetyPlugin.applyBasalConstraints(c, validProfile)
Assertions.assertEquals(2.0, c.value(), 0.01) assertThat(c.value()).isWithin(0.01).of(2.0)
Assertions.assertEquals( assertThat(c.getReasons(aapsLogger)).isEqualTo(
""" """
Safety: Limiting max basal rate to 2.00 U/h because of hard limit Safety: Limiting max basal rate to 2.00 U/h because of hard limit
""".trimIndent(), c.getReasons(aapsLogger) """.trimIndent())
) assertThat(c.getMostLimitedReasons(aapsLogger)).isEqualTo("Safety: Limiting max basal rate to 2.00 U/h because of hard limit")
Assertions.assertEquals("Safety: Limiting max basal rate to 2.00 U/h because of hard limit", c.getMostLimitedReasons(aapsLogger))
} }
@Test @Test
@ -162,8 +161,9 @@ class SafetyPluginTest : TestBaseWithProfile() {
`when`(sp.getString(info.nightscout.core.utils.R.string.key_age, "")).thenReturn("child") `when`(sp.getString(info.nightscout.core.utils.R.string.key_age, "")).thenReturn("child")
val d = Constraint(-0.5) val d = Constraint(-0.5)
safetyPlugin.applyBasalConstraints(d, validProfile) safetyPlugin.applyBasalConstraints(d, validProfile)
Assertions.assertEquals(0.0, d.value(), 0.01) assertThat(d.value()).isWithin(0.01).of(0.0)
Assertions.assertEquals("Safety: Limiting max basal rate to 0.00 U/h because of it must be positive value", d.getReasons(aapsLogger)) assertThat(d.getReasons(aapsLogger)).isEqualTo(
"Safety: Limiting max basal rate to 0.00 U/h because of it must be positive value")
} }
@Test @Test
@ -175,16 +175,17 @@ class SafetyPluginTest : TestBaseWithProfile() {
`when`(sp.getString(info.nightscout.core.utils.R.string.key_age, "")).thenReturn("child") `when`(sp.getString(info.nightscout.core.utils.R.string.key_age, "")).thenReturn("child")
val i = Constraint(Constants.REALLYHIGHPERCENTBASALRATE) val i = Constraint(Constants.REALLYHIGHPERCENTBASALRATE)
safetyPlugin.applyBasalPercentConstraints(i, validProfile) safetyPlugin.applyBasalPercentConstraints(i, validProfile)
Assertions.assertEquals(200, i.value()) assertThat(i.value()).isEqualTo(200)
Assertions.assertEquals( assertThat(i.getReasons(aapsLogger)).isEqualTo(
""" """
Safety: Percent rate 1111111% recalculated to 11111.11 U/h with current basal 1.00 U/h Safety: Percent rate 1111111% recalculated to 11111.11 U/h with current basal 1.00 U/h
Safety: Limiting max basal rate to 2.00 U/h because of hard limit Safety: Limiting max basal rate to 2.00 U/h because of hard limit
Safety: Limiting max percent rate to 200% because of pump limit Safety: Limiting max percent rate to 200% because of pump limit
Safety: Limiting max basal rate to 500.00 U/h because of pump limit Safety: Limiting max basal rate to 500.00 U/h because of pump limit
""".trimIndent(), i.getReasons(aapsLogger) """.trimIndent()
) )
Assertions.assertEquals("Safety: Limiting max percent rate to 200% because of pump limit", i.getMostLimitedReasons(aapsLogger)) assertThat(i.getMostLimitedReasons(aapsLogger)).isEqualTo(
"Safety: Limiting max percent rate to 200% because of pump limit")
} }
@Test @Test
@ -197,15 +198,14 @@ Safety: Limiting max basal rate to 500.00 U/h because of pump limit
openAPSSMBPlugin.setPluginEnabled(PluginType.APS, true) openAPSSMBPlugin.setPluginEnabled(PluginType.APS, true)
val i = Constraint(Constants.REALLYHIGHBASALRATE) val i = Constraint(Constants.REALLYHIGHBASALRATE)
openAPSSMBPlugin.applyBasalConstraints(i, validProfile) openAPSSMBPlugin.applyBasalConstraints(i, validProfile)
Assertions.assertEquals(1.0, i.value(), 0.01) assertThat(i.value()).isWithin(0.01).of(1.0)
Assertions.assertEquals( assertThat(i.getReasons(aapsLogger)).isEqualTo(
""" """
OpenAPSSMB: Limiting max basal rate to 1.00 U/h because of max value in preferences OpenAPSSMB: Limiting max basal rate to 1.00 U/h because of max value in preferences
OpenAPSSMB: Limiting max basal rate to 4.00 U/h because of max basal multiplier OpenAPSSMB: Limiting max basal rate to 4.00 U/h because of max basal multiplier
OpenAPSSMB: Limiting max basal rate to 3.00 U/h because of max daily basal multiplier OpenAPSSMB: Limiting max basal rate to 3.00 U/h because of max daily basal multiplier
""".trimIndent(), i.getReasons(aapsLogger) """.trimIndent())
) assertThat(i.getMostLimitedReasons(aapsLogger)).isEqualTo("OpenAPSSMB: Limiting max basal rate to 1.00 U/h because of max value in preferences")
Assertions.assertEquals("OpenAPSSMB: Limiting max basal rate to 1.00 U/h because of max value in preferences", i.getMostLimitedReasons(aapsLogger))
} }
@Test @Test
@ -213,15 +213,14 @@ Safety: Limiting max basal rate to 500.00 U/h because of pump limit
`when`(sp.getString(info.nightscout.core.utils.R.string.key_age, "")).thenReturn("child") `when`(sp.getString(info.nightscout.core.utils.R.string.key_age, "")).thenReturn("child")
val i = Constraint(-22) val i = Constraint(-22)
safetyPlugin.applyBasalPercentConstraints(i, validProfile) safetyPlugin.applyBasalPercentConstraints(i, validProfile)
Assertions.assertEquals(0, i.value()) assertThat(i.value()).isEqualTo(0)
Assertions.assertEquals( assertThat(i.getReasons(aapsLogger)).isEqualTo(
""" """
Safety: Percent rate -22% recalculated to -0.22 U/h with current basal 1.00 U/h Safety: Percent rate -22% recalculated to -0.22 U/h with current basal 1.00 U/h
Safety: Limiting max basal rate to 0.00 U/h because of it must be positive value Safety: Limiting max basal rate to 0.00 U/h because of it must be positive value
Safety: Limiting max percent rate to 0% because of pump limit Safety: Limiting max percent rate to 0% because of pump limit
""".trimIndent(), i.getReasons(aapsLogger) """.trimIndent())
) assertThat(i.getMostLimitedReasons(aapsLogger)).isEqualTo("Safety: Limiting max percent rate to 0% because of pump limit")
Assertions.assertEquals("Safety: Limiting max percent rate to 0% because of pump limit", i.getMostLimitedReasons(aapsLogger))
} }
@Test @Test
@ -230,14 +229,13 @@ Safety: Limiting max basal rate to 500.00 U/h because of pump limit
`when`(sp.getString(info.nightscout.core.utils.R.string.key_age, "")).thenReturn("child") `when`(sp.getString(info.nightscout.core.utils.R.string.key_age, "")).thenReturn("child")
var d = Constraint(Constants.REALLYHIGHBOLUS) var d = Constraint(Constants.REALLYHIGHBOLUS)
d = safetyPlugin.applyBolusConstraints(d) d = safetyPlugin.applyBolusConstraints(d)
Assertions.assertEquals(3.0, d.value(), 0.01) assertThat(d.value()).isWithin(0.01).of(3.0)
Assertions.assertEquals( assertThat(d.getReasons(aapsLogger)).isEqualTo(
""" """
Safety: Limiting bolus to 3.0 U because of max value in preferences Safety: Limiting bolus to 3.0 U because of max value in preferences
Safety: Limiting bolus to 5.0 U because of hard limit Safety: Limiting bolus to 5.0 U because of hard limit
""".trimIndent(), d.getReasons(aapsLogger) """.trimIndent())
) assertThat(d.getMostLimitedReasons(aapsLogger)).isEqualTo("Safety: Limiting bolus to 3.0 U because of max value in preferences")
Assertions.assertEquals("Safety: Limiting bolus to 3.0 U because of max value in preferences", d.getMostLimitedReasons(aapsLogger))
} }
@Test @Test
@ -246,9 +244,9 @@ Safety: Limiting max basal rate to 500.00 U/h because of pump limit
`when`(sp.getString(info.nightscout.core.utils.R.string.key_age, "")).thenReturn("child") `when`(sp.getString(info.nightscout.core.utils.R.string.key_age, "")).thenReturn("child")
var d = Constraint(-22.0) var d = Constraint(-22.0)
d = safetyPlugin.applyBolusConstraints(d) d = safetyPlugin.applyBolusConstraints(d)
Assertions.assertEquals(0.0, d.value(), 0.01) assertThat(d.value()).isWithin(0.01).of(0.0)
Assertions.assertEquals("Safety: Limiting bolus to 0.0 U because of it must be positive value", d.getReasons(aapsLogger)) assertThat(d.getReasons(aapsLogger)).isEqualTo("Safety: Limiting bolus to 0.0 U because of it must be positive value")
Assertions.assertEquals("Safety: Limiting bolus to 0.0 U because of it must be positive value", d.getMostLimitedReasons(aapsLogger)) assertThat(d.getMostLimitedReasons(aapsLogger)).isEqualTo("Safety: Limiting bolus to 0.0 U because of it must be positive value")
} }
@Test @Test
@ -259,13 +257,13 @@ Safety: Limiting max basal rate to 500.00 U/h because of pump limit
// Negative carbs not allowed // Negative carbs not allowed
var i = Constraint(-22) var i = Constraint(-22)
safetyPlugin.applyCarbsConstraints(i) safetyPlugin.applyCarbsConstraints(i)
Assertions.assertEquals(0, i.value()) assertThat(i.value()).isEqualTo(0)
Assertions.assertEquals("Safety: Limiting carbs to 0 g because of it must be positive value", i.getReasons(aapsLogger)) assertThat(i.getReasons(aapsLogger)).isEqualTo("Safety: Limiting carbs to 0 g because of it must be positive value")
// Apply all limits // Apply all limits
i = safetyPlugin.applyCarbsConstraints(Constraint(Constants.REALLYHIGHCARBS)) i = safetyPlugin.applyCarbsConstraints(Constraint(Constants.REALLYHIGHCARBS))
Assertions.assertEquals(48, i.value()) assertThat(i.value()).isEqualTo(48)
Assertions.assertEquals("Safety: Limiting carbs to 48 g because of max value in preferences", i.getReasons(aapsLogger)) assertThat(i.getReasons(aapsLogger)).isEqualTo("Safety: Limiting carbs to 48 g because of max value in preferences")
} }
@Test @Test
@ -282,22 +280,22 @@ Safety: Limiting max basal rate to 500.00 U/h because of pump limit
// Apply all limits // Apply all limits
var d = Constraint(Constants.REALLYHIGHIOB) var d = Constraint(Constants.REALLYHIGHIOB)
d = safetyPlugin.applyMaxIOBConstraints(d) d = safetyPlugin.applyMaxIOBConstraints(d)
Assertions.assertEquals(HardLimits.MAX_IOB_LGS, d.value(), 0.01) assertThat(d.value()).isWithin(0.01).of(HardLimits.MAX_IOB_LGS)
Assertions.assertEquals("Safety: Limiting IOB to 0.0 U because of Low Glucose Suspend", d.getReasons(aapsLogger)) assertThat(d.getReasons(aapsLogger)).isEqualTo("Safety: Limiting IOB to 0.0 U because of Low Glucose Suspend")
Assertions.assertEquals("Safety: Limiting IOB to 0.0 U because of Low Glucose Suspend", d.getMostLimitedReasons(aapsLogger)) assertThat(d.getMostLimitedReasons(aapsLogger)).isEqualTo("Safety: Limiting IOB to 0.0 U because of Low Glucose Suspend")
// Apply all limits // Apply all limits
d = Constraint(Constants.REALLYHIGHIOB) d = Constraint(Constants.REALLYHIGHIOB)
val a = openAPSAMAPlugin.applyMaxIOBConstraints(d) val a = openAPSAMAPlugin.applyMaxIOBConstraints(d)
Assertions.assertEquals(1.5, a.value(), 0.01) assertThat(a.value()).isWithin(0.01).of(1.5)
Assertions.assertEquals("OpenAPSAMA: Limiting IOB to 1.5 U because of max value in preferences\nOpenAPSAMA: Limiting IOB to 7.0 U because of hard limit", d.getReasons(aapsLogger)) assertThat(d.getReasons(aapsLogger)).isEqualTo("OpenAPSAMA: Limiting IOB to 1.5 U because of max value in preferences\nOpenAPSAMA: Limiting IOB to 7.0 U because of hard limit")
Assertions.assertEquals("OpenAPSAMA: Limiting IOB to 1.5 U because of max value in preferences", d.getMostLimitedReasons(aapsLogger)) assertThat(d.getMostLimitedReasons(aapsLogger)).isEqualTo("OpenAPSAMA: Limiting IOB to 1.5 U because of max value in preferences")
// Apply all limits // Apply all limits
d = Constraint(Constants.REALLYHIGHIOB) d = Constraint(Constants.REALLYHIGHIOB)
val s = openAPSSMBPlugin.applyMaxIOBConstraints(d) val s = openAPSSMBPlugin.applyMaxIOBConstraints(d)
Assertions.assertEquals(3.0, s.value(), 0.01) assertThat(s.value()).isWithin(0.01).of(3.0)
Assertions.assertEquals("OpenAPSSMB: Limiting IOB to 3.0 U because of max value in preferences\nOpenAPSSMB: Limiting IOB to 22.0 U because of hard limit", d.getReasons(aapsLogger)) assertThat(d.getReasons(aapsLogger)).isEqualTo("OpenAPSSMB: Limiting IOB to 3.0 U because of max value in preferences\nOpenAPSSMB: Limiting IOB to 22.0 U because of hard limit")
Assertions.assertEquals("OpenAPSSMB: Limiting IOB to 3.0 U because of max value in preferences", d.getMostLimitedReasons(aapsLogger)) assertThat(d.getMostLimitedReasons(aapsLogger)).isEqualTo("OpenAPSSMB: Limiting IOB to 3.0 U because of max value in preferences")
} }
} }