From 2c3313ca5472d52c4d79eb178f23bb305c42fd72 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Thu, 19 Mar 2020 20:57:56 +0100 Subject: [PATCH] PumpEnactResultTest --- .../androidaps/data/PumpEnactResultTest.java | 182 ---------------- .../androidaps/data/PumpEnactResultTest.kt | 197 ++++++++++++++++++ 2 files changed, 197 insertions(+), 182 deletions(-) delete mode 100644 app/src/test/java/info/nightscout/androidaps/data/PumpEnactResultTest.java create mode 100644 app/src/test/java/info/nightscout/androidaps/data/PumpEnactResultTest.kt diff --git a/app/src/test/java/info/nightscout/androidaps/data/PumpEnactResultTest.java b/app/src/test/java/info/nightscout/androidaps/data/PumpEnactResultTest.java deleted file mode 100644 index 7ce14c2281..0000000000 --- a/app/src/test/java/info/nightscout/androidaps/data/PumpEnactResultTest.java +++ /dev/null @@ -1,182 +0,0 @@ -package info.nightscout.androidaps.data; - -import android.text.Html; -import android.text.Spanned; - -import org.json.JSONObject; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.invocation.InvocationOnMock; -import org.mockito.stubbing.Answer; -import org.powermock.api.mockito.PowerMockito; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; -import org.skyscreamer.jsonassert.JSONAssert; - -import info.AAPSMocker; -import info.nightscout.androidaps.MainApp; - -import static org.mockito.ArgumentMatchers.anyString; -import static org.powermock.api.mockito.PowerMockito.when; - -/** - * Created by mike on 29.03.2018. - */ - -@RunWith(PowerMockRunner.class) -@PrepareForTest({MainApp.class, Html.class}) -public class PumpEnactResultTest { - - PumpEnactResult per = new PumpEnactResult(); - - @Test - public void successTest() throws Exception { - per.success(true); - Assert.assertEquals(true, per.success); - } - - @Test - public void enactedTest() throws Exception { - per.enacted(true); - Assert.assertEquals(true, per.enacted); - } - - @Test - public void commentTest() throws Exception { - per.comment("SomeComment"); - Assert.assertEquals("SomeComment", per.comment); - } - - @Test - public void durationTest() throws Exception { - per.duration(10); - Assert.assertEquals(10, per.duration); - } - - @Test - public void absoluteTest() throws Exception { - per.absolute(11d); - Assert.assertEquals(11d, per.absolute, 0.01d); - } - - @Test - public void percentTest() throws Exception { - per.percent(10); - Assert.assertEquals((int) 10, per.percent); - } - - @Test - public void isPercentTest() throws Exception { - per.isPercent(true); - Assert.assertEquals(true, per.isPercent); - } - - @Test - public void isTempCancelTest() throws Exception { - per.isTempCancel(true); - Assert.assertEquals(true, per.isTempCancel); - } - - @Test - public void bolusDeliveredTest() throws Exception { - per.bolusDelivered(11d); - Assert.assertEquals(11d, per.bolusDelivered, 0.01d); - } - - @Test - public void carbsDeliveredTest() throws Exception { - per.carbsDelivered(11d); - Assert.assertEquals(11d, per.carbsDelivered, 0.01d); - } - - @Test - public void queuedTest() throws Exception { - per.queued(true); - Assert.assertEquals(true, per.queued); - } - - @Test - public void logTest() throws Exception { - per = new PumpEnactResult(); - Assert.assertEquals("Success: false Enacted: false Comment: Duration: -1 Absolute: -1.0 Percent: -1 IsPercent: false IsTempCancel: false bolusDelivered: 0.0 carbsDelivered: 0.0 Queued: false", per.log()); - } - - @Test - public void toStringTest() throws Exception { - per = new PumpEnactResult().enacted(true).bolusDelivered(10).comment("AAA"); - Assert.assertEquals("Success: false\n" + - "Enacted: true\n" + - "Comment: AAA\n" + - "Insulin: 10.0 U", per.toString()); - - per = new PumpEnactResult().enacted(true).isTempCancel(true).comment("AAA"); - Assert.assertEquals("Success: false\n" + - "Enacted: true\n" + - "Comment: AAA\n" + - "Cancel temp basal", per.toString()); - - per = new PumpEnactResult().enacted(true).isPercent(true).percent(90).duration(20).comment("AAA"); - Assert.assertEquals("Success: false\n" + - "Enacted: true\n" + - "Comment: AAA\n" + - "Duration: 20 min\n" + - "Percent: 90%", per.toString()); - - per = new PumpEnactResult().enacted(true).isPercent(false).absolute(1).duration(30).comment("AAA"); - Assert.assertEquals("Success: false\n" + - "Enacted: true\n" + - "Comment: AAA\n" + - "Duration: 30 min\n" + - "Absolute: 1.0 U/h", per.toString()); - - per = new PumpEnactResult().enacted(false).comment("AAA"); - Assert.assertEquals("Success: false\n" + - "Comment: AAA", per.toString()); - } - - @Test - public void toHtmlTest() throws Exception { - per = new PumpEnactResult().enacted(true).bolusDelivered(10).comment("AAA"); - Assert.assertEquals("Success: false
Enacted: true
Comment: AAA
SMB: 10.0 U", per.toHtml()); - - per = new PumpEnactResult().enacted(true).isTempCancel(true).comment("AAA"); - Assert.assertEquals("Success: false
Enacted: true
Comment: AAA
Cancel temp basal", per.toHtml()); - - per = new PumpEnactResult().enacted(true).isPercent(true).percent(90).duration(20).comment("AAA"); - Assert.assertEquals("Success: false
Enacted: true
Comment: AAA
Duration: 20 min
Percent: 90%", per.toHtml()); - - per = new PumpEnactResult().enacted(true).isPercent(false).absolute(1).duration(30).comment("AAA"); - Assert.assertEquals("Success: false
Enacted: true
Comment: AAA
Duration: 30 min
Absolute: 1.00 U/h", per.toHtml()); - - per = new PumpEnactResult().enacted(false).comment("AAA"); - Assert.assertEquals("Success: false
Comment: AAA", per.toHtml()); - } - - @Test - public void jsonTest() throws Exception { - JSONObject o; - per = new PumpEnactResult().enacted(true).bolusDelivered(10).comment("AAA"); - o = per.json(AAPSMocker.getValidProfile()); - JSONAssert.assertEquals("{\"smb\":10}", o, false); - - per = new PumpEnactResult().enacted(true).isTempCancel(true).comment("AAA"); - o = per.json(AAPSMocker.getValidProfile()); - JSONAssert.assertEquals("{\"rate\":0,\"duration\":0}", o, false); - - per = new PumpEnactResult().enacted(true).isPercent(true).percent(90).duration(20).comment("AAA"); - o = per.json(AAPSMocker.getValidProfile()); - JSONAssert.assertEquals("{\"rate\":0.9,\"duration\":20}", o, false); - - per = new PumpEnactResult().enacted(true).isPercent(false).absolute(1).duration(30).comment("AAA"); - o = per.json(AAPSMocker.getValidProfile()); - JSONAssert.assertEquals("{\"rate\":1,\"duration\":30}", o, false); - } - - @Before - public void prepareMock() { - AAPSMocker.mockMainApp(); - AAPSMocker.mockStrings(); - } -} diff --git a/app/src/test/java/info/nightscout/androidaps/data/PumpEnactResultTest.kt b/app/src/test/java/info/nightscout/androidaps/data/PumpEnactResultTest.kt new file mode 100644 index 0000000000..e15db553c2 --- /dev/null +++ b/app/src/test/java/info/nightscout/androidaps/data/PumpEnactResultTest.kt @@ -0,0 +1,197 @@ +package info.nightscout.androidaps.data + +import android.text.Html +import dagger.android.AndroidInjector +import dagger.android.HasAndroidInjector +import info.nightscout.androidaps.MainApp +import info.nightscout.androidaps.R +import info.nightscout.androidaps.TestBaseWithProfile +import org.json.JSONObject +import org.junit.Assert +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.mockito.Mockito.`when` +import org.powermock.core.classloader.annotations.PrepareForTest +import org.powermock.modules.junit4.PowerMockRunner +import org.skyscreamer.jsonassert.JSONAssert + +@RunWith(PowerMockRunner::class) +@PrepareForTest(MainApp::class, Html::class) +class PumpEnactResultTest : TestBaseWithProfile() { + + val injector = HasAndroidInjector { + AndroidInjector { + if (it is PumpEnactResult) { + it.aapsLogger = aapsLogger + it.resourceHelper = resourceHelper + } + } + } + + @Before + fun mock() { + `when`(resourceHelper.gs(R.string.success)).thenReturn("Success") + `when`(resourceHelper.gs(R.string.enacted)).thenReturn("Enacted") + `when`(resourceHelper.gs(R.string.comment)).thenReturn("Comment") + `when`(resourceHelper.gs(R.string.configbuilder_insulin)).thenReturn("Insulin") + `when`(resourceHelper.gs(R.string.smb_shortname)).thenReturn("SMB") + `when`(resourceHelper.gs(R.string.insulin_unit_shortname)).thenReturn("U") + `when`(resourceHelper.gs(R.string.canceltemp)).thenReturn("Cancel temp basal") + `when`(resourceHelper.gs(R.string.duration)).thenReturn("Duration") + `when`(resourceHelper.gs(R.string.percent)).thenReturn("Percent") + `when`(resourceHelper.gs(R.string.absolute)).thenReturn("Absolute") + } + + @Test fun successTest() { + val per = PumpEnactResult(injector) + + per.success(true) + Assert.assertEquals(true, per.success) + } + + @Test fun enactedTest() { + val per = PumpEnactResult(injector) + + per.enacted(true) + Assert.assertEquals(true, per.enacted) + } + + @Test fun commentTest() { + val per = PumpEnactResult(injector) + + per.comment("SomeComment") + Assert.assertEquals("SomeComment", per.comment) + } + + @Test fun durationTest() { + val per = PumpEnactResult(injector) + + per.duration(10) + Assert.assertEquals(10, per.duration.toLong()) + } + + @Test fun absoluteTest() { + val per = PumpEnactResult(injector) + + per.absolute(11.0) + Assert.assertEquals(11.0, per.absolute, 0.01) + } + + @Test fun percentTest() { + val per = PumpEnactResult(injector) + + per.percent(10) + Assert.assertEquals(10, per.percent) + } + + @Test fun isPercentTest() { + val per = PumpEnactResult(injector) + + per.isPercent(true) + Assert.assertEquals(true, per.isPercent) + } + + @Test fun isTempCancelTest() { + val per = PumpEnactResult(injector) + + per.isTempCancel(true) + Assert.assertEquals(true, per.isTempCancel) + } + + @Test fun bolusDeliveredTest() { + val per = PumpEnactResult(injector) + + per.bolusDelivered(11.0) + Assert.assertEquals(11.0, per.bolusDelivered, 0.01) + } + + @Test fun carbsDeliveredTest() { + val per = PumpEnactResult(injector) + + per.carbsDelivered(11.0) + Assert.assertEquals(11.0, per.carbsDelivered, 0.01) + } + + @Test fun queuedTest() { + val per = PumpEnactResult(injector) + + per.queued(true) + Assert.assertEquals(true, per.queued) + } + + @Test fun logTest() { + val per = PumpEnactResult(injector) + + Assert.assertEquals("Success: false Enacted: false Comment: Duration: -1 Absolute: -1.0 Percent: -1 IsPercent: false IsTempCancel: false bolusDelivered: 0.0 carbsDelivered: 0.0 Queued: false", per.log()) + } + + @Test fun toStringTest() { + var per = PumpEnactResult(injector).enacted(true).bolusDelivered(10.0).comment("AAA") + Assert.assertEquals(""" + Success: false + Enacted: true + Comment: AAA + Insulin: 10.0 U + """.trimIndent(), per.toString()) + per = PumpEnactResult(injector).enacted(true).isTempCancel(true).comment("AAA") + Assert.assertEquals(""" + Success: false + Enacted: true + Comment: AAA + Cancel temp basal + """.trimIndent(), per.toString()) + per = PumpEnactResult(injector).enacted(true).isPercent(true).percent(90).duration(20).comment("AAA") + Assert.assertEquals(""" + Success: false + Enacted: true + Comment: AAA + Duration: 20 min + Percent: 90% + """.trimIndent(), per.toString()) + per = PumpEnactResult(injector).enacted(true).isPercent(false).absolute(1.0).duration(30).comment("AAA") + Assert.assertEquals(""" + Success: false + Enacted: true + Comment: AAA + Duration: 30 min + Absolute: 1.0 U/h + """.trimIndent(), per.toString()) + per = PumpEnactResult(injector).enacted(false).comment("AAA") + Assert.assertEquals(""" + Success: false + Comment: AAA + """.trimIndent(), per.toString()) + } + + @Test fun toHtmlTest() { + + var per: PumpEnactResult = PumpEnactResult(injector).enacted(true).bolusDelivered(10.0).comment("AAA") + Assert.assertEquals("Success: false
Enacted: true
Comment: AAA
SMB: 10.0 U", per.toHtml()) + per = PumpEnactResult(injector).enacted(true).isTempCancel(true).comment("AAA") + Assert.assertEquals("Success: false
Enacted: true
Comment: AAA
Cancel temp basal", per.toHtml()) + per = PumpEnactResult(injector).enacted(true).isPercent(true).percent(90).duration(20).comment("AAA") + Assert.assertEquals("Success: false
Enacted: true
Comment: AAA
Duration: 20 min
Percent: 90%", per.toHtml()) + per = PumpEnactResult(injector).enacted(true).isPercent(false).absolute(1.0).duration(30).comment("AAA") + Assert.assertEquals("Success: false
Enacted: true
Comment: AAA
Duration: 30 min
Absolute: 1.00 U/h", per.toHtml()) + per = PumpEnactResult(injector).enacted(false).comment("AAA") + Assert.assertEquals("Success: false
Comment: AAA", per.toHtml()) + } + + @Test fun jsonTest() { + var o: JSONObject? + + var per: PumpEnactResult = PumpEnactResult(injector).enacted(true).bolusDelivered(10.0).comment("AAA") + o = per.json(validProfile) + JSONAssert.assertEquals("{\"smb\":10}", o, false) + per = PumpEnactResult(injector).enacted(true).isTempCancel(true).comment("AAA") + o = per.json(validProfile) + 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) + 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) + JSONAssert.assertEquals("{\"rate\":1,\"duration\":30}", o, false) + } +} \ No newline at end of file