Rewrites ProfileTest with matchers

Issue #2745
This commit is contained in:
Ryan Haining 2023-09-20 22:28:04 -07:00
parent a46e396815
commit 1448eb9394

View file

@ -1,6 +1,7 @@
package info.nightscout.core.data package info.nightscout.core.data
import android.content.Context import android.content.Context
import com.google.common.truth.Truth.assertThat
import dagger.android.AndroidInjector import dagger.android.AndroidInjector
import info.nightscout.core.extensions.pureProfileFromJson import info.nightscout.core.extensions.pureProfileFromJson
import info.nightscout.core.profile.ProfileSealed import info.nightscout.core.profile.ProfileSealed
@ -15,7 +16,6 @@ import info.nightscout.sharedtests.HardLimitsMock
import info.nightscout.sharedtests.TestBase import info.nightscout.sharedtests.TestBase
import info.nightscout.sharedtests.TestPumpPlugin import info.nightscout.sharedtests.TestPumpPlugin
import org.json.JSONObject import org.json.JSONObject
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
@ -71,77 +71,77 @@ class ProfileTest : TestBase() {
// Test valid profile // Test valid profile
var p = ProfileSealed.Pure(pureProfileFromJson(JSONObject(okProfile), dateUtil)!!) var p = ProfileSealed.Pure(pureProfileFromJson(JSONObject(okProfile), dateUtil)!!)
Assertions.assertEquals(true, p.isValid("Test", testPumpPlugin, config, rh, rxBus, hardLimits, false).isValid) assertThat(p.isValid("Test", testPumpPlugin, config, rh, rxBus, hardLimits, false).isValid).isTrue()
// Assertions.assertEquals(true, p.log().contains("NS units: mmol")) // assertThat(p.log()).contains("NS units: mmol")
// JSONAssertions.assertEquals(JSONObject(okProfile), p.toPureNsJson(dateUtil), false) // JSONAssertions.assertEquals(JSONObject(okProfile), p.toPureNsJson(dateUtil), false)
Assertions.assertEquals(5.0, p.dia, 0.01) assertThat(p.dia).isWithin(0.01).of(5.0)
// Assertions.assertEquals(TimeZone.getTimeZone("UTC"), p.timeZone) // assertThat(p.timeZone).isEqualTo(TimeZone.getTimeZone("UTC"))
Assertions.assertEquals("00:30", dateUtil.formatHHMM(30 * 60)) assertThat(dateUtil.formatHHMM(30 * 60)).isEqualTo("00:30")
val c = Calendar.getInstance() val c = Calendar.getInstance()
c[Calendar.HOUR_OF_DAY] = 1 c[Calendar.HOUR_OF_DAY] = 1
c[Calendar.MINUTE] = 0 c[Calendar.MINUTE] = 0
c[Calendar.SECOND] = 0 c[Calendar.SECOND] = 0
c[Calendar.MILLISECOND] = 0 c[Calendar.MILLISECOND] = 0
Assertions.assertEquals(108.0, p.getIsfMgdl(c.timeInMillis), 0.01) assertThat(p.getIsfMgdl(c.timeInMillis)).isWithin(0.01).of(108.0)
c[Calendar.HOUR_OF_DAY] = 2 c[Calendar.HOUR_OF_DAY] = 2
Assertions.assertEquals(111.6, p.getIsfMgdl(c.timeInMillis), 0.01) assertThat(p.getIsfMgdl(c.timeInMillis)).isWithin(0.01).of(111.6)
// Assertions.assertEquals(110.0, p.getIsfTimeFromMidnight(2 * 60 * 60), 0.01) // assertThat(p.getIsfTimeFromMidnight(2 * 60 * 60)).isWithin(0.01).of(110.0)
Assertions.assertEquals( assertThat(p.getIsfList(rh, dateUtil).replace(".", ",")).isEqualTo(
""" """
00:00 6,0 mmol/U 00:00 6,0 mmol/U
02:00 6,2 mmol/U 02:00 6,2 mmol/U
""".trimIndent(), p.getIsfList(rh, dateUtil).replace(".", ",") """.trimIndent()
) )
Assertions.assertEquals(30.0, p.getIc(c.timeInMillis), 0.01) assertThat(p.getIc(c.timeInMillis)).isWithin(0.01).of(30.0)
Assertions.assertEquals(30.0, p.getIcTimeFromMidnight(2 * 60 * 60), 0.01) assertThat(p.getIcTimeFromMidnight(2 * 60 * 60)).isWithin(0.01).of(30.0)
Assertions.assertEquals("00:00 30,0 g/U", p.getIcList(rh, dateUtil).replace(".", ",")) assertThat(p.getIcList(rh, dateUtil).replace(".", ",")).isEqualTo("00:00 30,0 g/U")
Assertions.assertEquals(0.1, p.getBasal(c.timeInMillis), 0.01) assertThat(p.getBasal(c.timeInMillis)).isWithin(0.01).of(0.1)
Assertions.assertEquals(0.1, p.getBasalTimeFromMidnight(2 * 60 * 60), 0.01) assertThat(p.getBasalTimeFromMidnight(2 * 60 * 60)).isWithin(0.01).of(0.1)
Assertions.assertEquals("00:00 0,10 U/h", p.getBasalList(rh, dateUtil).replace(".", ",")) assertThat(p.getBasalList(rh, dateUtil).replace(".", ",")).isEqualTo("00:00 0,10 U/h")
Assertions.assertEquals(0.1, p.getBasalValues()[0].value, 0.01) assertThat(p.getBasalValues()[0].value).isWithin(0.01).of(0.1)
Assertions.assertEquals(0.1, p.getMaxDailyBasal(), 0.01) assertThat(p.getMaxDailyBasal()).isWithin(0.01).of(0.1)
Assertions.assertEquals(2.4, p.percentageBasalSum(), 0.01) assertThat(p.percentageBasalSum()).isWithin(0.01).of(2.4)
Assertions.assertEquals(2.4, p.baseBasalSum(), 0.01) assertThat(p.baseBasalSum()).isWithin(0.01).of(2.4)
// Assertions.assertEquals(81.0, p.getTargetMgdl(2 * 60 * 60), 0.01) // assertThat( p.getTargetMgdl(2 * 60 * 60)).isWithin(0.01).of(81.0)
Assertions.assertEquals(90.0, p.getTargetLowMgdl(c.timeInMillis), 0.01) assertThat( p.getTargetLowMgdl(c.timeInMillis)).isWithin(0.01).of(90.0)
// Assertions.assertEquals(4.0, p.getTargetLowTimeFromMidnight(2 * 60 * 60), 0.01) // assertThat( p.getTargetLowTimeFromMidnight(2 * 60 * 60)).isWithin(0.01).of(4.0)
Assertions.assertEquals(90.0, p.getTargetHighMgdl(c.timeInMillis), 0.01) assertThat( p.getTargetHighMgdl(c.timeInMillis)).isWithin(0.01).of(90.0)
// Assertions.assertEquals(5.0, p.getTargetHighTimeFromMidnight(2 * 60 * 60), 0.01) // assertThat( p.getTargetHighTimeFromMidnight(2 * 60 * 60)).isWithin(0.01).of(5.0)
Assertions.assertEquals("00:00 5,0 - 5,0 mmol", p.getTargetList(rh, dateUtil).replace(".", ",")) assertThat(p.getTargetList(rh, dateUtil).replace(".", ",")).isEqualTo("00:00 5,0 - 5,0 mmol")
Assertions.assertEquals(100, p.percentage) assertThat(p.percentage).isEqualTo(100)
Assertions.assertEquals(0, p.timeshift) assertThat(p.timeshift).isEqualTo(0)
//Test basal profile below limit //Test basal profile below limit
p = ProfileSealed.Pure(pureProfileFromJson(JSONObject(belowLimitValidProfile), dateUtil)!!) p = ProfileSealed.Pure(pureProfileFromJson(JSONObject(belowLimitValidProfile), dateUtil)!!)
p.isValid("Test", testPumpPlugin, config, rh, rxBus, hardLimits, false) p.isValid("Test", testPumpPlugin, config, rh, rxBus, hardLimits, false)
// Test profile w/o units // Test profile w/o units
Assertions.assertNull(pureProfileFromJson(JSONObject(noUnitsValidProfile), dateUtil)) assertThat(pureProfileFromJson(JSONObject(noUnitsValidProfile), dateUtil)).isNull()
//Test profile not starting at midnight //Test profile not starting at midnight
p = ProfileSealed.Pure(pureProfileFromJson(JSONObject(notStartingAtZeroValidProfile), dateUtil)!!) p = ProfileSealed.Pure(pureProfileFromJson(JSONObject(notStartingAtZeroValidProfile), dateUtil)!!)
Assertions.assertEquals(30.0, p.getIc(0), 0.01) assertThat(p.getIc(0)).isWithin(0.01).of(30.0)
// Test wrong profile // Test wrong profile
Assertions.assertNull(pureProfileFromJson(JSONObject(wrongProfile), dateUtil)) assertThat(pureProfileFromJson(JSONObject(wrongProfile), dateUtil)).isNull()
// Test percentage functionality // Test percentage functionality
p = ProfileSealed.Pure(pureProfileFromJson(JSONObject(okProfile), dateUtil)!!) p = ProfileSealed.Pure(pureProfileFromJson(JSONObject(okProfile), dateUtil)!!)
p.pct = 50 p.pct = 50
Assertions.assertEquals(0.05, p.getBasal(c.timeInMillis), 0.01) assertThat(p.getBasal(c.timeInMillis)).isWithin(0.01).of(0.05)
Assertions.assertEquals(1.2, p.percentageBasalSum(), 0.01) assertThat(p.percentageBasalSum()).isWithin(0.01).of(1.2)
Assertions.assertEquals(60.0, p.getIc(c.timeInMillis), 0.01) assertThat(p.getIc(c.timeInMillis)).isWithin(0.01).of(60.0)
Assertions.assertEquals(223.2, p.getIsfMgdl(c.timeInMillis), 0.01) assertThat(p.getIsfMgdl(c.timeInMillis)).isWithin(0.01).of(223.2)
// Test timeshift functionality // Test timeshift functionality
p = ProfileSealed.Pure(pureProfileFromJson(JSONObject(okProfile), dateUtil)!!) p = ProfileSealed.Pure(pureProfileFromJson(JSONObject(okProfile), dateUtil)!!)
p.ts = 1 p.ts = 1
Assertions.assertEquals( assertThat(p.getIsfList(rh, dateUtil).replace(',', '.')).isEqualTo(
""" """
00:00 6.2 mmol/U 00:00 6.2 mmol/U
01:00 6.0 mmol/U 01:00 6.0 mmol/U
03:00 6.2 mmol/U 03:00 6.2 mmol/U
""".trimIndent(), p.getIsfList(rh, dateUtil).replace(',', '.') """.trimIndent()
) )
// Test hour alignment // Test hour alignment
@ -149,4 +149,4 @@ class ProfileTest : TestBase() {
p = ProfileSealed.Pure(pureProfileFromJson(JSONObject(notAlignedBasalValidProfile), dateUtil)!!) p = ProfileSealed.Pure(pureProfileFromJson(JSONObject(notAlignedBasalValidProfile), dateUtil)!!)
p.isValid("Test", testPumpPlugin, config, rh, rxBus, hardLimits, false) p.isValid("Test", testPumpPlugin, config, rh, rxBus, hardLimits, false)
} }
} }