Merge pull request #2805 from ryanhaining/assertthat_app

Rewrites app/ tests with assertThat matchers
This commit is contained in:
Milos Kozak 2023-09-20 13:26:04 +02:00 committed by GitHub
commit 63e7d7406b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 61 additions and 60 deletions

View file

@ -2,6 +2,7 @@ package info.nightscout.plugins.aps.loop
import android.app.NotificationManager
import android.content.Context
import com.google.common.truth.Truth.assertThat
import dagger.android.AndroidInjector
import dagger.android.HasAndroidInjector
import info.nightscout.core.utils.fabric.FabricPrivacy
@ -25,7 +26,6 @@ import info.nightscout.shared.interfaces.ResourceHelper
import info.nightscout.shared.sharedPreferences.SP
import info.nightscout.shared.utils.DateUtil
import info.nightscout.sharedtests.TestBase
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.mockito.Mock
@ -74,28 +74,28 @@ class LoopPluginTest : TestBase() {
`when`(sp.getString(info.nightscout.core.utils.R.string.key_aps_mode, ApsMode.OPEN.name)).thenReturn(ApsMode.CLOSED.name)
val pumpDescription = PumpDescription()
`when`(virtualPumpPlugin.pumpDescription).thenReturn(pumpDescription)
Assertions.assertEquals(LoopFragment::class.java.name, loopPlugin.pluginDescription.fragmentClass)
Assertions.assertEquals(PluginType.LOOP, loopPlugin.getType())
Assertions.assertEquals("Loop", loopPlugin.name)
Assertions.assertEquals("LOOP", loopPlugin.nameShort)
Assertions.assertEquals(true, loopPlugin.hasFragment())
Assertions.assertEquals(true, loopPlugin.showInList(PluginType.LOOP))
Assertions.assertEquals(info.nightscout.plugins.aps.R.xml.pref_loop.toLong(), loopPlugin.preferencesId.toLong())
assertThat(loopPlugin.pluginDescription.fragmentClass).isEqualTo(LoopFragment::class.java.name)
assertThat(loopPlugin.getType()).isEqualTo(PluginType.LOOP)
assertThat(loopPlugin.name).isEqualTo("Loop")
assertThat(loopPlugin.nameShort).isEqualTo("LOOP")
assertThat(loopPlugin.hasFragment()).isTrue()
assertThat(loopPlugin.showInList(PluginType.LOOP)).isTrue()
assertThat(loopPlugin.preferencesId.toLong()).isEqualTo(info.nightscout.plugins.aps.R.xml.pref_loop.toLong())
// Plugin is disabled by default
Assertions.assertEquals(false, loopPlugin.isEnabled())
assertThat(loopPlugin.isEnabled()).isFalse()
loopPlugin.setPluginEnabled(PluginType.LOOP, true)
Assertions.assertEquals(true, loopPlugin.isEnabled())
assertThat(loopPlugin.isEnabled()).isTrue()
// No temp basal capable pump should disable plugin
virtualPumpPlugin.pumpDescription.isTempBasalCapable = false
Assertions.assertEquals(false, loopPlugin.isEnabled())
assertThat(loopPlugin.isEnabled()).isFalse()
virtualPumpPlugin.pumpDescription.isTempBasalCapable = true
// Fragment is hidden by default
Assertions.assertEquals(false, loopPlugin.isFragmentVisible())
assertThat(loopPlugin.isFragmentVisible()).isFalse()
loopPlugin.setFragmentVisible(PluginType.LOOP, true)
Assertions.assertEquals(true, loopPlugin.isFragmentVisible())
assertThat(loopPlugin.isFragmentVisible()).isTrue()
}
/* *********** not working
@ -118,7 +118,7 @@ class LoopPluginTest : TestBase() {
MockedLoopPlugin mockedLoopPlugin = new MockedLoopPlugin();
Treatment t = new Treatment();
bus.post(new EventTreatmentChange(t));
Assertions.assertEquals(true, mockedLoopPlugin.invokeCalled);
assertThat(mockedLoopPlugin.invokeCalled).isTrue();
}
*/
}

View file

@ -1,6 +1,7 @@
package info.nightscout.plugins.general.maintenance
import android.content.Context
import com.google.common.truth.Truth.assertThat
import dagger.android.HasAndroidInjector
import info.nightscout.configuration.maintenance.MaintenancePlugin
import info.nightscout.interfaces.Config
@ -10,7 +11,6 @@ import info.nightscout.interfaces.nsclient.NSSettingsStatus
import info.nightscout.shared.interfaces.ResourceHelper
import info.nightscout.shared.sharedPreferences.SP
import info.nightscout.sharedtests.TestBase
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.mockito.Mock
@ -40,11 +40,12 @@ class MaintenancePluginTest : TestBase() {
@Test fun logFilesTest() {
var logs = sut.getLogFiles(2)
Assertions.assertEquals(2, logs.size)
Assertions.assertEquals("AndroidAPS.log", logs[0].name)
Assertions.assertEquals("AndroidAPS.2018-01-03_01-01-00.1.zip", logs[1].name)
assertThat(logs.map { it.name }).containsExactly(
"AndroidAPS.log",
"AndroidAPS.2018-01-03_01-01-00.1.zip",
).inOrder()
logs = sut.getLogFiles(10)
Assertions.assertEquals(4, logs.size)
assertThat(logs).hasSize(4)
}
@Test
@ -53,7 +54,7 @@ class MaintenancePluginTest : TestBase() {
val name = "AndroidAPS.log.zip"
var zipFile = File("build/$name")
zipFile = sut.zipLogs(zipFile, logs)
Assertions.assertTrue(zipFile.exists())
Assertions.assertTrue(zipFile.isFile)
assertThat(zipFile.exists()).isTrue()
assertThat(zipFile.isFile).isTrue()
}
}

View file

@ -1,5 +1,6 @@
package info.nightscout.plugins.sensitivity
import com.google.common.truth.Truth.assertThat
import dagger.android.AndroidInjector
import dagger.android.HasAndroidInjector
import info.nightscout.interfaces.aps.AutosensDataStore
@ -12,7 +13,6 @@ import info.nightscout.shared.interfaces.ResourceHelper
import info.nightscout.shared.sharedPreferences.SP
import info.nightscout.sharedtests.TestBase
import org.json.JSONObject
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test
import org.mockito.Mock
@ -45,12 +45,12 @@ class AbstractSensitivityPluginTest : TestBase() {
fun fillResultTest() {
val sut = SensitivityTestClass(pluginDescription, aapsLogger, rh, sp)
var ar = sut.fillResult(1.0, 1.0, "1", "1.2", "1", 12, 0.7, 1.2)
Assertions.assertEquals(1.0, ar.ratio, 0.01)
assertThat(ar.ratio).isWithin(0.01).of(1.0)
ar = sut.fillResult(1.2, 1.0, "1", "1.2", "1", 40, 0.7, 1.2)
Assertions.assertEquals(1.16, ar.ratio, 0.01)
assertThat(ar.ratio).isWithin(0.01).of(1.16)
ar = sut.fillResult(1.2, 1.0, "1", "1.2", "1", 50, 0.7, 1.2)
Assertions.assertEquals(1.2, ar.ratio, 0.01)
assertThat(ar.ratio).isWithin(0.01).of(1.2)
ar = sut.fillResult(1.2, 1.0, "1", "1.2", "1", 50, 0.7, 1.1)
Assertions.assertEquals(1.1, ar.ratio, 0.01)
assertThat(ar.ratio).isWithin(0.01).of(1.1)
}
}

View file

@ -1,5 +1,6 @@
package info.nightscout.pump
import com.google.common.truth.Truth.assertThat
import dagger.android.AndroidInjector
import dagger.android.HasAndroidInjector
import info.nightscout.core.pump.toHtml
@ -8,7 +9,6 @@ import info.nightscout.plugins.aps.loop.extensions.json
import info.nightscout.pump.virtual.extensions.toText
import info.nightscout.sharedtests.TestBaseWithProfile
import org.json.JSONObject
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.mockito.Mockito.`when`
@ -42,132 +42,132 @@ class PumpEnactResultTest : TestBaseWithProfile() {
val per = PumpEnactResult(injector)
per.success(true)
Assertions.assertEquals(true, per.success)
assertThat(per.success).isTrue()
}
@Test fun enactedTest() {
val per = PumpEnactResult(injector)
per.enacted(true)
Assertions.assertEquals(true, per.enacted)
assertThat(per.enacted).isTrue()
}
@Test fun commentTest() {
val per = PumpEnactResult(injector)
per.comment("SomeComment")
Assertions.assertEquals("SomeComment", per.comment)
assertThat(per.comment).isEqualTo("SomeComment")
}
@Test fun durationTest() {
val per = PumpEnactResult(injector)
per.duration(10)
Assertions.assertEquals(10, per.duration.toLong())
assertThat(per.duration.toLong()).isEqualTo(10L)
}
@Test fun absoluteTest() {
val per = PumpEnactResult(injector)
per.absolute(11.0)
Assertions.assertEquals(11.0, per.absolute, 0.01)
assertThat(per.absolute).isWithin(0.01).of(11.0)
}
@Test fun percentTest() {
val per = PumpEnactResult(injector)
per.percent(10)
Assertions.assertEquals(10, per.percent)
assertThat(per.percent).isEqualTo(10)
}
@Test fun isPercentTest() {
val per = PumpEnactResult(injector)
per.isPercent(true)
Assertions.assertEquals(true, per.isPercent)
assertThat(per.isPercent).isTrue()
}
@Test fun isTempCancelTest() {
val per = PumpEnactResult(injector)
per.isTempCancel(true)
Assertions.assertEquals(true, per.isTempCancel)
assertThat(per.isTempCancel).isTrue()
}
@Test fun bolusDeliveredTest() {
val per = PumpEnactResult(injector)
per.bolusDelivered(11.0)
Assertions.assertEquals(11.0, per.bolusDelivered, 0.01)
assertThat(per.bolusDelivered).isWithin(0.01).of(11.0)
}
@Test fun queuedTest() {
val per = PumpEnactResult(injector)
per.queued(true)
Assertions.assertEquals(true, per.queued)
assertThat(per.queued).isTrue()
}
@Test fun toStringTest() {
var per = PumpEnactResult(injector).enacted(true).bolusDelivered(10.0).comment("AAA")
Assertions.assertEquals(
assertThat(per.toText(rh)).isEqualTo(
"""
Success: false
Enacted: true
Comment: AAA
Insulin: 10.0 U
""".trimIndent(), per.toText(rh)
""".trimIndent()
)
per = PumpEnactResult(injector).enacted(true).isTempCancel(true).comment("AAA")
Assertions.assertEquals(
assertThat(per.toText(rh)).isEqualTo(
"""
Success: false
Enacted: true
Comment: AAA
Cancel temp basal
""".trimIndent(), per.toText(rh)
""".trimIndent()
)
per = PumpEnactResult(injector).enacted(true).isPercent(true).percent(90).duration(20).comment("AAA")
Assertions.assertEquals(
assertThat(per.toText(rh)).isEqualTo(
"""
Success: false
Enacted: true
Comment: AAA
Duration: 20 min
Percent: 90%
""".trimIndent(), per.toText(rh)
""".trimIndent()
)
per = PumpEnactResult(injector).enacted(true).isPercent(false).absolute(1.0).duration(30).comment("AAA")
Assertions.assertEquals(
assertThat(per.toText(rh)).isEqualTo(
"""
Success: false
Enacted: true
Comment: AAA
Duration: 30 min
Absolute: 1.0 U/h
""".trimIndent(), per.toText(rh)
""".trimIndent()
)
per = PumpEnactResult(injector).enacted(false).comment("AAA")
Assertions.assertEquals(
assertThat(per.toText(rh)).isEqualTo(
"""
Success: false
Comment: AAA
""".trimIndent(), per.toText(rh)
""".trimIndent()
)
}
@Test fun toHtmlTest() {
var per: PumpEnactResult = PumpEnactResult(injector).enacted(true).bolusDelivered(10.0).comment("AAA")
Assertions.assertEquals("<b>Success</b>: false<br><b>Enacted</b>: true<br><b>Comment</b>: AAA<br><b>SMB</b>: 10.0 U", per.toHtml(rh, decimalFormatter))
assertThat(per.toHtml(rh, decimalFormatter)).isEqualTo("<b>Success</b>: false<br><b>Enacted</b>: true<br><b>Comment</b>: AAA<br><b>SMB</b>: 10.0 U")
per = PumpEnactResult(injector).enacted(true).isTempCancel(true).comment("AAA")
Assertions.assertEquals("<b>Success</b>: false<br><b>Enacted</b>: true<br><b>Comment</b>: AAA<br>Cancel temp basal", per.toHtml(rh, decimalFormatter))
assertThat(per.toHtml(rh, decimalFormatter)).isEqualTo("<b>Success</b>: false<br><b>Enacted</b>: true<br><b>Comment</b>: AAA<br>Cancel temp basal")
per = PumpEnactResult(injector).enacted(true).isPercent(true).percent(90).duration(20).comment("AAA")
Assertions.assertEquals("<b>Success</b>: false<br><b>Enacted</b>: true<br><b>Comment</b>: AAA<br><b>Duration</b>: 20 min<br><b>Percent</b>: 90%", per.toHtml(rh, decimalFormatter))
assertThat(per.toHtml(rh, decimalFormatter)).isEqualTo("<b>Success</b>: false<br><b>Enacted</b>: true<br><b>Comment</b>: AAA<br><b>Duration</b>: 20 min<br><b>Percent</b>: 90%")
per = PumpEnactResult(injector).enacted(true).isPercent(false).absolute(1.0).duration(30).comment("AAA")
Assertions.assertEquals("<b>Success</b>: false<br><b>Enacted</b>: true<br><b>Comment</b>: AAA<br><b>Duration</b>: 30 min<br><b>Absolute</b>: 1.00 U/h", per.toHtml(rh, decimalFormatter))
assertThat(per.toHtml(rh, decimalFormatter)).isEqualTo("<b>Success</b>: false<br><b>Enacted</b>: true<br><b>Comment</b>: AAA<br><b>Duration</b>: 30 min<br><b>Absolute</b>: 1.00 U/h")
per = PumpEnactResult(injector).enacted(false).comment("AAA")
Assertions.assertEquals("<b>Success</b>: false<br><b>Comment</b>: AAA", per.toHtml(rh, decimalFormatter))
assertThat(per.toHtml(rh, decimalFormatter)).isEqualTo("<b>Success</b>: false<br><b>Comment</b>: AAA")
}
@Test fun jsonTest() {
@ -175,15 +175,15 @@ class PumpEnactResultTest : TestBaseWithProfile() {
var per: PumpEnactResult = PumpEnactResult(injector).enacted(true).bolusDelivered(10.0).comment("AAA")
o = per.json(validProfile.getBasal())
JSONAssert.assertEquals("{\"smb\":10}", o, false)
JSONAssert.assertEquals("""{"smb":10}""", o, false)
per = PumpEnactResult(injector).enacted(true).isTempCancel(true).comment("AAA")
o = per.json(validProfile.getBasal())
JSONAssert.assertEquals("{\"rate\":0,\"duration\":0}", o, false)
JSONAssert.assertEquals("""{"rate":0,"duration":0}""", o, false)
per = PumpEnactResult(injector).enacted(true).isPercent(true).percent(90).duration(20).comment("AAA")
o = per.json(validProfile.getBasal())
JSONAssert.assertEquals("{\"rate\":0.9,\"duration\":20}", o, false)
JSONAssert.assertEquals("""{"rate":0.9,"duration":20}""", o, false)
per = PumpEnactResult(injector).enacted(true).isPercent(false).absolute(1.0).duration(30).comment("AAA")
o = per.json(validProfile.getBasal())
JSONAssert.assertEquals("{\"rate\":1,\"duration\":30}", o, false)
JSONAssert.assertEquals("""{"rate":1,"duration":30}""", o, false)
}
}