Rewrites IobTotalTest with matchers

Issue #2745
This commit is contained in:
Ryan Haining 2023-09-20 22:14:01 -07:00
parent 62c5c53304
commit cfb003af01

View file

@ -1,6 +1,7 @@
package info.nightscout.core.data
import android.content.Context
import com.google.common.truth.Truth.assertThat
import info.nightscout.core.iob.combine
import info.nightscout.core.iob.copy
import info.nightscout.core.iob.determineBasalJson
@ -11,7 +12,6 @@ import info.nightscout.interfaces.iob.IobTotal
import info.nightscout.shared.utils.DateUtil
import info.nightscout.shared.utils.DateUtilImpl
import info.nightscout.sharedtests.TestBase
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.mockito.Mock
@ -34,7 +34,7 @@ class IobTotalTest : TestBase() {
val a = IobTotal(now)
a.iob = 10.0
val b = a.copy()
Assertions.assertEquals(a.iob, b.iob, 0.01)
assertThat(b.iob).isWithin(0.01).of(a.iob)
}
@Test fun plusTest() {
@ -48,14 +48,14 @@ class IobTotalTest : TestBase() {
a.netInsulin = 10.0
a.extendedBolusInsulin = 10.0
a.plus(a.copy())
Assertions.assertEquals(20.0, a.iob, 0.01)
Assertions.assertEquals(20.0, a.activity, 0.01)
Assertions.assertEquals(20.0, a.bolussnooze, 0.01)
Assertions.assertEquals(20.0, a.basaliob, 0.01)
Assertions.assertEquals(20.0, a.netbasalinsulin, 0.01)
Assertions.assertEquals(20.0, a.hightempinsulin, 0.01)
Assertions.assertEquals(20.0, a.netInsulin, 0.01)
Assertions.assertEquals(20.0, a.extendedBolusInsulin, 0.01)
assertThat(a.iob).isWithin(0.01).of(20.0)
assertThat(a.activity).isWithin(0.01).of(20.0)
assertThat(a.bolussnooze).isWithin(0.01).of(20.0)
assertThat(a.basaliob).isWithin(0.01).of(20.0)
assertThat(a.netbasalinsulin).isWithin(0.01).of(20.0)
assertThat(a.hightempinsulin).isWithin(0.01).of(20.0)
assertThat(a.netInsulin).isWithin(0.01).of(20.0)
assertThat(a.extendedBolusInsulin).isWithin(0.01).of(20.0)
}
@Test fun combineTest() {
@ -70,15 +70,15 @@ class IobTotalTest : TestBase() {
a.extendedBolusInsulin = 17.0
val b = a.copy()
val c = IobTotal.combine(a, b)
Assertions.assertEquals(a.time.toDouble(), c.time.toDouble(), 0.01)
Assertions.assertEquals(23.0, c.iob, 0.01)
Assertions.assertEquals(22.0, c.activity, 0.01)
Assertions.assertEquals(12.0, c.bolussnooze, 0.01)
Assertions.assertEquals(26.0, c.basaliob, 0.01)
Assertions.assertEquals(28.0, c.netbasalinsulin, 0.01)
Assertions.assertEquals(30.0, c.hightempinsulin, 0.01)
Assertions.assertEquals(32.0, c.netInsulin, 0.01)
Assertions.assertEquals(34.0, c.extendedBolusInsulin, 0.01)
assertThat(c.time.toDouble()).isWithin(0.01).of(a.time.toDouble())
assertThat(c.iob).isWithin(0.01).of(23.0)
assertThat(c.activity).isWithin(0.01).of(22.0)
assertThat(c.bolussnooze).isWithin(0.01).of(12.0)
assertThat(c.basaliob).isWithin(0.01).of(26.0)
assertThat(c.netbasalinsulin).isWithin(0.01).of(28.0)
assertThat(c.hightempinsulin).isWithin(0.01).of(30.0)
assertThat(c.netInsulin).isWithin(0.01).of(32.0)
assertThat(c.extendedBolusInsulin).isWithin(0.01).of(34.0)
}
@Test fun roundTest() {
@ -92,14 +92,14 @@ class IobTotalTest : TestBase() {
a.netInsulin = 1.1111111111111
a.extendedBolusInsulin = 1.1111111111111
a.round()
Assertions.assertEquals(1.111, a.iob, 0.00001)
Assertions.assertEquals(1.1111, a.activity, 0.00001)
Assertions.assertEquals(1.1111, a.bolussnooze, 0.00001)
Assertions.assertEquals(1.111, a.basaliob, 0.00001)
Assertions.assertEquals(1.111, a.netbasalinsulin, 0.00001)
Assertions.assertEquals(1.111, a.hightempinsulin, 0.00001)
Assertions.assertEquals(1.111, a.netInsulin, 0.00001)
Assertions.assertEquals(1.111, a.extendedBolusInsulin, 0.00001)
assertThat(a.iob).isWithin(0.00001).of(1.111)
assertThat(a.activity).isWithin(0.00001).of(1.1111)
assertThat(a.bolussnooze).isWithin(0.00001).of(1.1111)
assertThat(a.basaliob).isWithin(0.00001).of(1.111)
assertThat(a.netbasalinsulin).isWithin(0.00001).of(1.111)
assertThat(a.hightempinsulin).isWithin(0.00001).of(1.111)
assertThat(a.netInsulin).isWithin(0.00001).of(1.111)
assertThat(a.extendedBolusInsulin).isWithin(0.00001).of(1.111)
}
@Test fun jsonTest() {
@ -112,15 +112,11 @@ class IobTotalTest : TestBase() {
a.hightempinsulin = 15.0
a.netInsulin = 16.0
a.extendedBolusInsulin = 17.0
try {
val j = a.json(dateUtil)
Assertions.assertEquals(a.iob, j.getDouble("iob"), 0.0000001)
Assertions.assertEquals(a.basaliob, j.getDouble("basaliob"), 0.0000001)
Assertions.assertEquals(a.activity, j.getDouble("activity"), 0.0000001)
Assertions.assertEquals(now, dateUtil.fromISODateString(j.getString("time")))
} catch (e: Exception) {
Assertions.fail("Exception: " + e.message)
}
val j = a.json(dateUtil)
assertThat(j.getDouble("iob")).isWithin(0.0000001).of(a.iob)
assertThat(j.getDouble("basaliob")).isWithin(0.0000001).of(a.basaliob)
assertThat(j.getDouble("activity")).isWithin(0.0000001).of(a.activity)
assertThat(dateUtil.fromISODateString(j.getString("time"))).isEqualTo(now)
}
@Test fun determineBasalJsonTest() {
@ -134,17 +130,13 @@ class IobTotalTest : TestBase() {
a.netInsulin = 16.0
a.extendedBolusInsulin = 17.0
a.iobWithZeroTemp = IobTotal(now)
try {
val j = a.determineBasalJson(dateUtil)
Assertions.assertEquals(a.iob, j.getDouble("iob"), 0.0000001)
Assertions.assertEquals(a.basaliob, j.getDouble("basaliob"), 0.0000001)
Assertions.assertEquals(a.bolussnooze, j.getDouble("bolussnooze"), 0.0000001)
Assertions.assertEquals(a.activity, j.getDouble("activity"), 0.0000001)
Assertions.assertEquals(0, j.getLong("lastBolusTime"))
Assertions.assertEquals(now, dateUtil.fromISODateString(j.getString("time")))
Assertions.assertNotNull(j.getJSONObject("iobWithZeroTemp"))
} catch (e: Exception) {
Assertions.fail("Exception: " + e.message)
}
val j = a.determineBasalJson(dateUtil)
assertThat(j.getDouble("iob")).isWithin(0.0000001).of(a.iob)
assertThat(j.getDouble("basaliob")).isWithin(0.0000001).of(a.basaliob)
assertThat(j.getDouble("bolussnooze")).isWithin(0.0000001).of(a.bolussnooze)
assertThat(j.getDouble("activity")).isWithin(0.0000001).of(a.activity)
assertThat(j.getLong("lastBolusTime")).isEqualTo(0)
assertThat(dateUtil.fromISODateString(j.getString("time"))).isEqualTo(now)
assertThat(j.getJSONObject("iobWithZeroTemp")).isNotNull()
}
}
}