Rewrites TriggerIobTest with matchers

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

View file

@ -5,11 +5,11 @@ 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 com.google.common.truth.Truth.assertThat
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.ArgumentMatchers import org.mockito.ArgumentMatchers
import org.mockito.Mockito.`when` import org.mockito.Mockito.`when`
import org.skyscreamer.jsonassert.JSONAssert
class TriggerIobTest : TriggerTestBase() { class TriggerIobTest : TriggerTestBase() {
@ -20,43 +20,43 @@ class TriggerIobTest : TriggerTestBase() {
@Test fun shouldRunTest() { @Test fun shouldRunTest() {
`when`(iobCobCalculator.calculateFromTreatmentsAndTemps(ArgumentMatchers.anyLong(), anyObject())).thenReturn(generateIobRecordData()) `when`(iobCobCalculator.calculateFromTreatmentsAndTemps(ArgumentMatchers.anyLong(), anyObject())).thenReturn(generateIobRecordData())
var t: TriggerIob = TriggerIob(injector).setValue(1.1).comparator(Comparator.Compare.IS_EQUAL) var t: TriggerIob = TriggerIob(injector).setValue(1.1).comparator(Comparator.Compare.IS_EQUAL)
Assertions.assertFalse(t.shouldRun()) assertThat(t.shouldRun()).isFalse()
t = TriggerIob(injector).setValue(1.0).comparator(Comparator.Compare.IS_EQUAL) t = TriggerIob(injector).setValue(1.0).comparator(Comparator.Compare.IS_EQUAL)
Assertions.assertTrue(t.shouldRun()) assertThat(t.shouldRun()).isTrue()
t = TriggerIob(injector).setValue(0.8).comparator(Comparator.Compare.IS_GREATER) t = TriggerIob(injector).setValue(0.8).comparator(Comparator.Compare.IS_GREATER)
Assertions.assertTrue(t.shouldRun()) assertThat(t.shouldRun()).isTrue()
t = TriggerIob(injector).setValue(0.8).comparator(Comparator.Compare.IS_EQUAL_OR_GREATER) t = TriggerIob(injector).setValue(0.8).comparator(Comparator.Compare.IS_EQUAL_OR_GREATER)
Assertions.assertTrue(t.shouldRun()) assertThat(t.shouldRun()).isTrue()
t = TriggerIob(injector).setValue(0.9).comparator(Comparator.Compare.IS_EQUAL_OR_GREATER) t = TriggerIob(injector).setValue(0.9).comparator(Comparator.Compare.IS_EQUAL_OR_GREATER)
Assertions.assertTrue(t.shouldRun()) assertThat(t.shouldRun()).isTrue()
t = TriggerIob(injector).setValue(1.2).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER) t = TriggerIob(injector).setValue(1.2).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER)
Assertions.assertTrue(t.shouldRun()) assertThat(t.shouldRun()).isTrue()
t = TriggerIob(injector).setValue(1.1).comparator(Comparator.Compare.IS_EQUAL) t = TriggerIob(injector).setValue(1.1).comparator(Comparator.Compare.IS_EQUAL)
Assertions.assertFalse(t.shouldRun()) assertThat(t.shouldRun()).isFalse()
t = TriggerIob(injector).setValue(1.0).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER) t = TriggerIob(injector).setValue(1.0).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER)
Assertions.assertTrue(t.shouldRun()) assertThat(t.shouldRun()).isTrue()
t = TriggerIob(injector).setValue(0.9).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER) t = TriggerIob(injector).setValue(0.9).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER)
Assertions.assertFalse(t.shouldRun()) assertThat(t.shouldRun()).isFalse()
} }
@Test fun copyConstructorTest() { @Test fun copyConstructorTest() {
val t: TriggerIob = TriggerIob(injector).setValue(213.0).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER) val t: TriggerIob = TriggerIob(injector).setValue(213.0).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER)
Assertions.assertEquals(213.0, t.insulin.value, 0.01) assertThat(t.insulin.value).isWithin(0.01).of(213.0)
Assertions.assertEquals(Comparator.Compare.IS_EQUAL_OR_LESSER, t.comparator.value) assertThat(t.comparator.value).isEqualTo(Comparator.Compare.IS_EQUAL_OR_LESSER)
} }
private var bgJson = "{\"data\":{\"comparator\":\"IS_EQUAL\",\"insulin\":4.1},\"type\":\"TriggerIob\"}" private var bgJson = "{\"data\":{\"comparator\":\"IS_EQUAL\",\"insulin\":4.1},\"type\":\"TriggerIob\"}"
@Test fun toJSONTest() { @Test fun toJSONTest() {
val t: TriggerIob = TriggerIob(injector).setValue(4.1).comparator(Comparator.Compare.IS_EQUAL) val t: TriggerIob = TriggerIob(injector).setValue(4.1).comparator(Comparator.Compare.IS_EQUAL)
Assertions.assertEquals(bgJson, t.toJSON()) JSONAssert.assertEquals(bgJson, t.toJSON(), true)
} }
@Test @Test
fun fromJSONTest() { fun fromJSONTest() {
val t: TriggerIob = TriggerIob(injector).setValue(4.1).comparator(Comparator.Compare.IS_EQUAL) val t: TriggerIob = TriggerIob(injector).setValue(4.1).comparator(Comparator.Compare.IS_EQUAL)
val t2 = TriggerDummy(injector).instantiate(JSONObject(t.toJSON())) as TriggerIob val t2 = TriggerDummy(injector).instantiate(JSONObject(t.toJSON())) as TriggerIob
Assertions.assertEquals(Comparator.Compare.IS_EQUAL, t2.comparator.value) assertThat(t2.comparator.value).isEqualTo(Comparator.Compare.IS_EQUAL)
Assertions.assertEquals(4.1, t2.insulin.value, 0.01) assertThat(t2.insulin.value).isWithin(0.01).of(4.1)
} }
@Test fun iconTest() { @Test fun iconTest() {
@ -68,4 +68,4 @@ class TriggerIobTest : TriggerTestBase() {
iobTotal.iob = 1.0 iobTotal.iob = 1.0
return iobTotal return iobTotal
} }
} }