Rewrites TriggerHeartRateTest with matchers

Issue #2745
This commit is contained in:
Ryan Haining 2023-10-04 22:38:00 -07:00
parent 570e3adea5
commit e259d8e45e

View file

@ -3,19 +3,20 @@ package app.aaps.plugins.automation.triggers
import app.aaps.database.entities.HeartRate import app.aaps.database.entities.HeartRate
import app.aaps.plugins.automation.R import app.aaps.plugins.automation.R
import app.aaps.plugins.automation.elements.Comparator import app.aaps.plugins.automation.elements.Comparator
import com.google.common.truth.Truth.assertThat
import io.reactivex.rxjava3.core.Single import io.reactivex.rxjava3.core.Single
import org.json.JSONObject import org.json.JSONObject
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test import org.junit.jupiter.api.Test
import org.mockito.Mockito.verify import org.mockito.Mockito.verify
import org.mockito.Mockito.verifyNoMoreInteractions import org.mockito.Mockito.verifyNoMoreInteractions
import org.mockito.Mockito.`when` import org.mockito.Mockito.`when`
import org.skyscreamer.jsonassert.JSONAssert
class TriggerHeartRateTest : TriggerTestBase() { class TriggerHeartRateTest : TriggerTestBase() {
@Test @Test
fun friendlyName() { fun friendlyName() {
Assertions.assertEquals(R.string.triggerHeartRate, TriggerHeartRate(injector).friendlyName()) assertThat(TriggerHeartRate(injector).friendlyName()).isEqualTo(R.string.triggerHeartRate)
} }
@Test @Test
@ -23,7 +24,7 @@ class TriggerHeartRateTest : TriggerTestBase() {
val t = TriggerHeartRate(injector) val t = TriggerHeartRate(injector)
`when`(rh.gs(Comparator.Compare.IS_EQUAL_OR_GREATER.stringRes)).thenReturn(">") `when`(rh.gs(Comparator.Compare.IS_EQUAL_OR_GREATER.stringRes)).thenReturn(">")
`when`(rh.gs(R.string.triggerHeartRateDesc, ">", 80.0)).thenReturn("test") `when`(rh.gs(R.string.triggerHeartRateDesc, ">", 80.0)).thenReturn("test")
Assertions.assertEquals("test", t.friendlyDescription()) assertThat(t.friendlyDescription()).isEqualTo("test")
} }
@Test @Test
@ -33,16 +34,16 @@ class TriggerHeartRateTest : TriggerTestBase() {
comparator.value = Comparator.Compare.IS_GREATER comparator.value = Comparator.Compare.IS_GREATER
} }
val dup = t.duplicate() as TriggerHeartRate val dup = t.duplicate() as TriggerHeartRate
Assertions.assertNotSame(t, dup) assertThat(dup).isNotSameInstanceAs(t)
Assertions.assertEquals(100.0, dup.heartRate.value, 0.01) assertThat(dup.heartRate.value).isWithin(0.01).of(100.0)
Assertions.assertEquals(Comparator.Compare.IS_GREATER, dup.comparator.value) assertThat(dup.comparator.value).isEqualTo(Comparator.Compare.IS_GREATER)
} }
@Test @Test
fun shouldRunNotAvailable() { fun shouldRunNotAvailable() {
val t = TriggerHeartRate(injector).apply { comparator.value = Comparator.Compare.IS_NOT_AVAILABLE } val t = TriggerHeartRate(injector).apply { comparator.value = Comparator.Compare.IS_NOT_AVAILABLE }
Assertions.assertTrue(t.shouldRun()) assertThat(t.shouldRun()).isTrue()
verifyNoMoreInteractions(repository) verifyNoMoreInteractions(repository)
} }
@ -53,7 +54,7 @@ class TriggerHeartRateTest : TriggerTestBase() {
comparator.value = Comparator.Compare.IS_GREATER comparator.value = Comparator.Compare.IS_GREATER
} }
`when`(repository.getHeartRatesFromTime(now - t.averageHeartRateDurationMillis)).thenReturn(Single.just(emptyList())) `when`(repository.getHeartRatesFromTime(now - t.averageHeartRateDurationMillis)).thenReturn(Single.just(emptyList()))
Assertions.assertFalse(t.shouldRun()) assertThat(t.shouldRun()).isFalse()
verify(repository).getHeartRatesFromTime(now - t.averageHeartRateDurationMillis) verify(repository).getHeartRatesFromTime(now - t.averageHeartRateDurationMillis)
verifyNoMoreInteractions(repository) verifyNoMoreInteractions(repository)
} }
@ -69,7 +70,7 @@ class TriggerHeartRateTest : TriggerTestBase() {
HeartRate(duration = 300_000, timestamp = now, beatsPerMinute = 60.0, device = "test"), HeartRate(duration = 300_000, timestamp = now, beatsPerMinute = 60.0, device = "test"),
) )
`when`(repository.getHeartRatesFromTime(now - t.averageHeartRateDurationMillis)).thenReturn(Single.just(hrs)) `when`(repository.getHeartRatesFromTime(now - t.averageHeartRateDurationMillis)).thenReturn(Single.just(hrs))
Assertions.assertFalse(t.shouldRun()) assertThat(t.shouldRun()).isFalse()
verify(repository).getHeartRatesFromTime(now - t.averageHeartRateDurationMillis) verify(repository).getHeartRatesFromTime(now - t.averageHeartRateDurationMillis)
verifyNoMoreInteractions(repository) verifyNoMoreInteractions(repository)
} }
@ -84,7 +85,7 @@ class TriggerHeartRateTest : TriggerTestBase() {
HeartRate(duration = 300_000, timestamp = now, beatsPerMinute = 120.0, device = "test"), HeartRate(duration = 300_000, timestamp = now, beatsPerMinute = 120.0, device = "test"),
) )
`when`(repository.getHeartRatesFromTime(now - t.averageHeartRateDurationMillis)).thenReturn(Single.just(hrs)) `when`(repository.getHeartRatesFromTime(now - t.averageHeartRateDurationMillis)).thenReturn(Single.just(hrs))
Assertions.assertTrue(t.shouldRun()) assertThat(t.shouldRun()).isTrue()
verify(repository).getHeartRatesFromTime(now - t.averageHeartRateDurationMillis) verify(repository).getHeartRatesFromTime(now - t.averageHeartRateDurationMillis)
verifyNoMoreInteractions(repository) verifyNoMoreInteractions(repository)
} }
@ -95,9 +96,9 @@ class TriggerHeartRateTest : TriggerTestBase() {
heartRate.value = 100.0 heartRate.value = 100.0
comparator.value = Comparator.Compare.IS_GREATER comparator.value = Comparator.Compare.IS_GREATER
} }
Assertions.assertEquals(Comparator.Compare.IS_GREATER, t.comparator.value) assertThat(t.comparator.value).isEqualTo(Comparator.Compare.IS_GREATER)
Assertions.assertEquals("""{"data":{"comparator":"IS_GREATER","heartRate":100},"type":"TriggerHeartRate"}""".trimMargin(), t.toJSON()) JSONAssert.assertEquals("""{"data":{"comparator":"IS_GREATER","heartRate":100},"type":"TriggerHeartRate"}""", t.toJSON(), true)
} }
@Test @Test
@ -107,7 +108,7 @@ class TriggerHeartRateTest : TriggerTestBase() {
"""{"data":{"comparator":"IS_GREATER","heartRate":100},"type":"TriggerHeartRate"}""" """{"data":{"comparator":"IS_GREATER","heartRate":100},"type":"TriggerHeartRate"}"""
) )
) as TriggerHeartRate ) as TriggerHeartRate
Assertions.assertEquals(Comparator.Compare.IS_GREATER, t.comparator.value) assertThat(t.comparator.value).isEqualTo(Comparator.Compare.IS_GREATER)
Assertions.assertEquals(100.0, t.heartRate.value, 0.01) assertThat(t.heartRate.value).isWithin(0.01).of(100.0)
} }
} }