Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package app.aaps.plugins.automation.triggers

import app.aaps.core.data.iob.InMemoryGlucoseValue
import app.aaps.core.data.model.GlucoseUnit
import app.aaps.core.data.model.SourceSensor
import app.aaps.core.data.model.TrendArrow
import app.aaps.plugins.automation.elements.Comparator
import app.aaps.plugins.automation.elements.InputBg
import com.google.common.truth.Truth.assertThat
import kotlinx.coroutines.test.runTest
import org.junit.jupiter.api.Test
import org.mockito.kotlin.whenever

/**
* A new glucose or temp target condition starts at the lowest threshold the editor offers, not at 0.0.
* The editor shows 0.0 as an empty field, and a condition saved untouched compared against 0.
*/
class NewGlucoseConditionTest : TriggerTestBase() {

@Test
fun `a new condition starts at the lowest threshold of the profile units`() {
whenever(profileFunction.getUnits()).thenReturn(GlucoseUnit.MGDL)
assertThat(TriggerBg(triggerDeps).bg.value).isEqualTo(InputBg.MGDL_MIN)
assertThat(TriggerTempTargetValue(triggerDeps).ttValue.value).isEqualTo(InputBg.MGDL_MIN)

whenever(profileFunction.getUnits()).thenReturn(GlucoseUnit.MMOL)
assertThat(TriggerBg(triggerDeps).bg.value).isEqualTo(InputBg.MMOL_MIN)
assertThat(TriggerTempTargetValue(triggerDeps).ttValue.value).isEqualTo(InputBg.MMOL_MIN)
}

@Test
fun `an untouched new above condition does not fire for a low glucose`() = runTest {
whenever(profileFunction.getUnits()).thenReturn(GlucoseUnit.MGDL)
whenever(autosensDataStore.getBucketedDataTableCopy()).thenReturn(
mutableListOf(InMemoryGlucoseValue(value = 40.0, timestamp = now - 1, trendArrow = TrendArrow.FLAT, sourceSensor = SourceSensor.UNKNOWN))
)

// Before, "above" an untouched field compared 40 mg/dL against 0 and fired.
assertThat(TriggerBg(triggerDeps).comparator(Comparator.Compare.IS_GREATER).shouldRun()).isFalse()
}

@Test
fun `a stored threshold is still read as stored`() {
whenever(profileFunction.getUnits()).thenReturn(GlucoseUnit.MGDL)
val restored = TriggerBg(triggerDeps).fromJSON("""{"bg":0,"comparator":"IS_GREATER","units":"mg/dl"}""") as TriggerBg
assertThat(restored.bg.value).isEqualTo(0.0)
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package app.aaps.plugins.automation.elements

import app.aaps.core.data.format.NumberFormat
import app.aaps.core.data.model.GlucoseUnit
import app.aaps.core.interfaces.profile.ProfileFunction

Expand All @@ -9,9 +8,6 @@ class InputBg(profileFunction: ProfileFunction) {
var units = GlucoseUnit.MGDL
var value = 0.0
var minValue = 0.0
private var maxValue = 0.0
private var step = 0.0
private var decimalFormat: NumberFormat? = null

constructor(profileFunction: ProfileFunction, value: Double, units: GlucoseUnit) : this(profileFunction) {
setUnits(units)
Expand All @@ -20,6 +16,9 @@ class InputBg(profileFunction: ProfileFunction) {

init {
setUnits(profileFunction.getUnits())
// Start inside the editor's range. 0.0 shows as an empty field, and a condition saved untouched
// then compared against 0: "below" never fired and "above" always did.
value = minValue
}

fun setValue(value: Double): InputBg {
Expand All @@ -28,17 +27,7 @@ class InputBg(profileFunction: ProfileFunction) {
}

fun setUnits(units: GlucoseUnit): InputBg {
if (units == GlucoseUnit.MMOL) {
minValue = MMOL_MIN
maxValue = MMOL_MAX
step = 0.1
decimalFormat = NumberFormat.DECIMAL_1
} else {
minValue = MGDL_MIN
maxValue = MGDL_MAX
step = 1.0
decimalFormat = NumberFormat.INTEGER
}
minValue = if (units == GlucoseUnit.MMOL) MMOL_MIN else MGDL_MIN
this.units = units
return this
}
Expand Down
Loading