fix tests

This commit is contained in:
Milos Kozak 2021-09-29 23:18:43 +02:00
parent 6a8d3e25ee
commit 82980285de

View file

@ -3,36 +3,25 @@ package info.nightscout.androidaps.plugins.pump.omnipod.dash.util
import info.nightscout.androidaps.interfaces.Profile
import info.nightscout.androidaps.interfaces.Profile.ProfileValue
import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.definition.BasalProgram
import org.junit.Assert
import org.junit.Assert.assertEquals
import org.junit.Rule
import org.junit.Test
import org.junit.rules.ExpectedException
import org.mockito.Mockito
import org.powermock.api.mockito.PowerMockito
import org.mockito.Mockito.`when`
class FunctionsTest {
@Rule
@JvmField var thrown = ExpectedException.none()
@Test fun validProfile() {
val profile = Mockito.mock(Profile::class.java)
val value1 = Mockito.mock(ProfileValue::class.java)
value1.timeAsSeconds = 0
value1.value = 0.5
val value2 = Mockito.mock(ProfileValue::class.java)
value2.timeAsSeconds = 18000
value2.value = 1.0
val value3 = Mockito.mock(ProfileValue::class.java)
value3.timeAsSeconds = 50400
value3.value = 3.05
PowerMockito.`when`(profile.getBasalValues()).thenReturn(
`when`(profile.getBasalValues()).thenReturn(
arrayOf(
value1,
value2,
value3
ProfileValue(0, 0.5),
ProfileValue(18000, 1.0),
ProfileValue(50400, 3.05)
)
)
val basalProgram: BasalProgram = mapProfileToBasalProgram(profile)
val entries: List<BasalProgram.Segment> = basalProgram.segments
assertEquals(3, entries.size)
@ -51,73 +40,72 @@ class FunctionsTest {
}
@Test fun invalidProfileZeroEntries() {
thrown.expect(IllegalArgumentException::class.java)
thrown.expectMessage("Basal values should contain values")
val profile = Mockito.mock(Profile::class.java)
PowerMockito.`when`(profile.getBasalValues()).thenReturn(emptyArray())
`when`(profile.getBasalValues()).thenReturn(emptyArray())
Assert.assertThrows(
"Basal values should contain values",
java.lang.IllegalArgumentException::class.java
) {
mapProfileToBasalProgram(profile)
}
}
@Test fun invalidProfileNonZeroOffset() {
thrown.expect(IllegalArgumentException::class.java)
thrown.expectMessage("First basal segment start time should be 0")
val profile = Mockito.mock(Profile::class.java)
val value = Mockito.mock(ProfileValue::class.java)
value.timeAsSeconds = 1800
value.value = 0.5
PowerMockito.`when`(profile.getBasalValues()).thenReturn(
arrayOf(
value
)
`when`(profile.getBasalValues()).thenReturn(
arrayOf(ProfileValue(1800, 0.5))
)
Assert.assertThrows(
"First basal segment start time should be 0",
java.lang.IllegalArgumentException::class.java
) {
mapProfileToBasalProgram(profile)
}
}
@Test fun invalidProfileMoreThan24Hours() {
thrown.expect(IllegalArgumentException::class.java)
thrown.expectMessage("Basal segment start time can not be greater than 86400")
val profile = Mockito.mock(Profile::class.java)
val value1 = Mockito.mock(ProfileValue::class.java)
value1.timeAsSeconds = 0
value1.value = 0.5
val value2 = Mockito.mock(ProfileValue::class.java)
value2.timeAsSeconds = 86400
value2.value = 0.5
PowerMockito.`when`(profile.getBasalValues()).thenReturn(
`when`(profile.getBasalValues()).thenReturn(
arrayOf(
value1,
value2
ProfileValue(0, 0.5),
ProfileValue(86400, 0.5)
)
)
Assert.assertThrows(
"Basal segment start time can not be greater than 86400",
java.lang.IllegalArgumentException::class.java
) {
mapProfileToBasalProgram(profile)
}
}
@Test fun invalidProfileNegativeOffset() {
thrown.expect(IllegalArgumentException::class.java)
thrown.expectMessage("Basal segment start time can not be less than 0")
val profile = Mockito.mock(Profile::class.java)
val value = Mockito.mock(ProfileValue::class.java)
value.timeAsSeconds = -1
value.value = 0.5
PowerMockito.`when`(profile.getBasalValues()).thenReturn(
arrayOf(
value
)
`when`(profile.getBasalValues()).thenReturn(
arrayOf(ProfileValue(-1, 0.5))
)
Assert.assertThrows("Basal segment start time can not be less than 0", IllegalArgumentException::class.java) {
mapProfileToBasalProgram(profile)
}
}
@Test fun roundsToSupportedPrecision() {
val profile = Mockito.mock(Profile::class.java)
val value = Mockito.mock(ProfileValue::class.java)
value.timeAsSeconds = 0
value.value = 0.04
PowerMockito.`when`(profile.getBasalValues()).thenReturn(
`when`(profile.getBasalValues()).thenReturn(
arrayOf(
value
ProfileValue(0, 0.04)
)
)
val basalProgram: BasalProgram = mapProfileToBasalProgram(profile)
val basalProgramElement: BasalProgram.Segment = basalProgram.segments[0]
assertEquals(5, basalProgramElement.basalRateInHundredthUnitsPerHour)