Rewrites TriggerBgTest with matchers

Issue #2745
This commit is contained in:
Ryan Haining 2023-10-04 21:59:51 -07:00
parent f51206e417
commit 88cd20c4d6

View file

@ -6,10 +6,11 @@ import app.aaps.database.entities.GlucoseValue
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 com.google.common.truth.Truth.assertThat
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.Mockito.`when` import org.mockito.Mockito.`when`
import org.skyscreamer.jsonassert.JSONAssert
class TriggerBgTest : TriggerTestBase() { class TriggerBgTest : TriggerTestBase() {
@ -22,37 +23,37 @@ class TriggerBgTest : TriggerTestBase() {
fun shouldRunTest() { fun shouldRunTest() {
`when`(autosensDataStore.getBucketedDataTableCopy()).thenReturn(generateOneCurrentRecordBgData()) `when`(autosensDataStore.getBucketedDataTableCopy()).thenReturn(generateOneCurrentRecordBgData())
var t: TriggerBg = TriggerBg(injector).setUnits(GlucoseUnit.MMOL).setValue(4.1).comparator(Comparator.Compare.IS_EQUAL) var t: TriggerBg = TriggerBg(injector).setUnits(GlucoseUnit.MMOL).setValue(4.1).comparator(Comparator.Compare.IS_EQUAL)
Assertions.assertFalse(t.shouldRun()) assertThat(t.shouldRun()).isFalse()
t = TriggerBg(injector).setUnits(GlucoseUnit.MGDL).setValue(214.0).comparator(Comparator.Compare.IS_EQUAL) t = TriggerBg(injector).setUnits(GlucoseUnit.MGDL).setValue(214.0).comparator(Comparator.Compare.IS_EQUAL)
Assertions.assertTrue(t.shouldRun()) assertThat(t.shouldRun()).isTrue()
t = TriggerBg(injector).setUnits(GlucoseUnit.MGDL).setValue(214.0).comparator(Comparator.Compare.IS_EQUAL_OR_GREATER) t = TriggerBg(injector).setUnits(GlucoseUnit.MGDL).setValue(214.0).comparator(Comparator.Compare.IS_EQUAL_OR_GREATER)
Assertions.assertTrue(t.shouldRun()) assertThat(t.shouldRun()).isTrue()
t = TriggerBg(injector).setUnits(GlucoseUnit.MGDL).setValue(214.0).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER) t = TriggerBg(injector).setUnits(GlucoseUnit.MGDL).setValue(214.0).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER)
Assertions.assertTrue(t.shouldRun()) assertThat(t.shouldRun()).isTrue()
t = TriggerBg(injector).setUnits(GlucoseUnit.MGDL).setValue(215.0).comparator(Comparator.Compare.IS_EQUAL) t = TriggerBg(injector).setUnits(GlucoseUnit.MGDL).setValue(215.0).comparator(Comparator.Compare.IS_EQUAL)
Assertions.assertFalse(t.shouldRun()) assertThat(t.shouldRun()).isFalse()
t = TriggerBg(injector).setUnits(GlucoseUnit.MGDL).setValue(215.0).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER) t = TriggerBg(injector).setUnits(GlucoseUnit.MGDL).setValue(215.0).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER)
Assertions.assertTrue(t.shouldRun()) assertThat(t.shouldRun()).isTrue()
t = TriggerBg(injector).setUnits(GlucoseUnit.MGDL).setValue(215.0).comparator(Comparator.Compare.IS_EQUAL_OR_GREATER) t = TriggerBg(injector).setUnits(GlucoseUnit.MGDL).setValue(215.0).comparator(Comparator.Compare.IS_EQUAL_OR_GREATER)
Assertions.assertFalse(t.shouldRun()) assertThat(t.shouldRun()).isFalse()
t = TriggerBg(injector).setUnits(GlucoseUnit.MGDL).setValue(213.0).comparator(Comparator.Compare.IS_EQUAL_OR_GREATER) t = TriggerBg(injector).setUnits(GlucoseUnit.MGDL).setValue(213.0).comparator(Comparator.Compare.IS_EQUAL_OR_GREATER)
Assertions.assertTrue(t.shouldRun()) assertThat(t.shouldRun()).isTrue()
t = TriggerBg(injector).setUnits(GlucoseUnit.MGDL).setValue(213.0).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER) t = TriggerBg(injector).setUnits(GlucoseUnit.MGDL).setValue(213.0).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER)
Assertions.assertFalse(t.shouldRun()) assertThat(t.shouldRun()).isFalse()
`when`(autosensDataStore.getBucketedDataTableCopy()).thenReturn(ArrayList()) `when`(autosensDataStore.getBucketedDataTableCopy()).thenReturn(ArrayList())
t = TriggerBg(injector).setUnits(GlucoseUnit.MGDL).setValue(213.0).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER) t = TriggerBg(injector).setUnits(GlucoseUnit.MGDL).setValue(213.0).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER)
Assertions.assertFalse(t.shouldRun()) assertThat(t.shouldRun()).isFalse()
t = TriggerBg(injector).comparator(Comparator.Compare.IS_NOT_AVAILABLE) t = TriggerBg(injector).comparator(Comparator.Compare.IS_NOT_AVAILABLE)
Assertions.assertTrue(t.shouldRun()) assertThat(t.shouldRun()).isTrue()
} }
@Test @Test
fun copyConstructorTest() { fun copyConstructorTest() {
val t: TriggerBg = TriggerBg(injector).setUnits(GlucoseUnit.MGDL).setValue(213.0).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER) val t: TriggerBg = TriggerBg(injector).setUnits(GlucoseUnit.MGDL).setValue(213.0).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER)
val t1 = t.duplicate() as TriggerBg val t1 = t.duplicate() as TriggerBg
Assertions.assertEquals(213.0, t1.bg.value, 0.01) assertThat(t1.bg.value).isWithin(0.01).of(213.0)
Assertions.assertEquals(GlucoseUnit.MGDL, t1.bg.units) assertThat(t1.bg.units).isEqualTo(GlucoseUnit.MGDL)
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\",\"bg\":4.1,\"units\":\"mmol\"},\"type\":\"TriggerBg\"}" private var bgJson = "{\"data\":{\"comparator\":\"IS_EQUAL\",\"bg\":4.1,\"units\":\"mmol\"},\"type\":\"TriggerBg\"}"
@ -60,16 +61,16 @@ class TriggerBgTest : TriggerTestBase() {
@Test @Test
fun toJSONTest() { fun toJSONTest() {
val t: TriggerBg = TriggerBg(injector).setUnits(GlucoseUnit.MMOL).setValue(4.1).comparator(Comparator.Compare.IS_EQUAL) val t: TriggerBg = TriggerBg(injector).setUnits(GlucoseUnit.MMOL).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: TriggerBg = TriggerBg(injector).setUnits(GlucoseUnit.MMOL).setValue(4.1).comparator(Comparator.Compare.IS_EQUAL) val t: TriggerBg = TriggerBg(injector).setUnits(GlucoseUnit.MMOL).setValue(4.1).comparator(Comparator.Compare.IS_EQUAL)
val t2 = TriggerDummy(injector).instantiate(JSONObject(t.toJSON())) as TriggerBg val t2 = TriggerDummy(injector).instantiate(JSONObject(t.toJSON())) as TriggerBg
Assertions.assertEquals(Comparator.Compare.IS_EQUAL, t2.comparator.value) assertThat(t2.comparator.value).isEqualTo(Comparator.Compare.IS_EQUAL)
Assertions.assertEquals(4.1, t2.bg.value, 0.01) assertThat(t2.bg.value).isWithin(0.01).of(4.1)
Assertions.assertEquals(GlucoseUnit.MMOL, t2.bg.units) assertThat(t2.bg.units).isEqualTo(GlucoseUnit.MMOL)
} }
@Test @Test