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