mocking with argThat
This commit is contained in:
parent
3b5b945fea
commit
fc7f63d219
3 changed files with 52 additions and 1 deletions
|
@ -5,6 +5,7 @@ import info.nightscout.androidaps.utils.rx.AapsSchedulers
|
||||||
import info.nightscout.androidaps.utils.rx.TestAapsSchedulers
|
import info.nightscout.androidaps.utils.rx.TestAapsSchedulers
|
||||||
import org.junit.Before
|
import org.junit.Before
|
||||||
import org.junit.Rule
|
import org.junit.Rule
|
||||||
|
import org.mockito.ArgumentMatcher
|
||||||
import org.mockito.Mockito
|
import org.mockito.Mockito
|
||||||
import org.mockito.junit.MockitoJUnit
|
import org.mockito.junit.MockitoJUnit
|
||||||
import org.mockito.junit.MockitoRule
|
import org.mockito.junit.MockitoRule
|
||||||
|
@ -35,6 +36,17 @@ open class TestBase {
|
||||||
return uninitialized()
|
return uninitialized()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun <T> argThatKotlin(matcher: ArgumentMatcher<T>): T {
|
||||||
|
Mockito.argThat(matcher)
|
||||||
|
return uninitialized()
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
fun <T> eqObject(expected: T): T {
|
||||||
|
Mockito.eq<T>(expected)
|
||||||
|
return uninitialized()
|
||||||
|
}
|
||||||
|
|
||||||
@Suppress("Unchecked_Cast")
|
@Suppress("Unchecked_Cast")
|
||||||
fun <T> uninitialized(): T = null as T
|
fun <T> uninitialized(): T = null as T
|
||||||
}
|
}
|
|
@ -2,13 +2,17 @@ package info.nightscout.androidaps.plugins.general.automation.actions
|
||||||
|
|
||||||
import info.nightscout.androidaps.Constants
|
import info.nightscout.androidaps.Constants
|
||||||
import info.nightscout.androidaps.automation.R
|
import info.nightscout.androidaps.automation.R
|
||||||
|
import info.nightscout.androidaps.database.entities.TemporaryTarget
|
||||||
|
import info.nightscout.androidaps.database.transactions.InsertTemporaryTargetAndCancelCurrentTransaction
|
||||||
import info.nightscout.androidaps.plugins.general.automation.elements.InputDuration
|
import info.nightscout.androidaps.plugins.general.automation.elements.InputDuration
|
||||||
import info.nightscout.androidaps.plugins.general.automation.elements.InputTempTarget
|
import info.nightscout.androidaps.plugins.general.automation.elements.InputTempTarget
|
||||||
import info.nightscout.androidaps.queue.Callback
|
import info.nightscout.androidaps.queue.Callback
|
||||||
|
import io.reactivex.Single
|
||||||
import org.junit.Assert
|
import org.junit.Assert
|
||||||
import org.junit.Before
|
import org.junit.Before
|
||||||
import org.junit.Test
|
import org.junit.Test
|
||||||
import org.junit.runner.RunWith
|
import org.junit.runner.RunWith
|
||||||
|
import org.mockito.ArgumentMatcher
|
||||||
import org.mockito.Mockito.`when`
|
import org.mockito.Mockito.`when`
|
||||||
import org.powermock.modules.junit4.PowerMockRunner
|
import org.powermock.modules.junit4.PowerMockRunner
|
||||||
|
|
||||||
|
@ -41,6 +45,41 @@ class ActionStartTempTargetTest : ActionsTestBase() {
|
||||||
|
|
||||||
@Test fun doActionTest() {
|
@Test fun doActionTest() {
|
||||||
//`when`(activePlugin.activeTreatments).thenReturn(treatmentsInterface)
|
//`when`(activePlugin.activeTreatments).thenReturn(treatmentsInterface)
|
||||||
|
|
||||||
|
val expectedTarget = TemporaryTarget(
|
||||||
|
id = 0,
|
||||||
|
version = 0,
|
||||||
|
dateCreated = -1,
|
||||||
|
isValid = true,
|
||||||
|
referenceId = null,
|
||||||
|
interfaceIDs_backing = null,
|
||||||
|
timestamp = 0,
|
||||||
|
utcOffset = 0,
|
||||||
|
reason = TemporaryTarget.Reason.AUTOMATION,
|
||||||
|
highTarget = 110.0,
|
||||||
|
lowTarget = 110.0,
|
||||||
|
duration = 1800000
|
||||||
|
)
|
||||||
|
|
||||||
|
val inserted = mutableListOf<TemporaryTarget>().apply {
|
||||||
|
add(expectedTarget)
|
||||||
|
}
|
||||||
|
|
||||||
|
val updated = mutableListOf<TemporaryTarget>().apply {
|
||||||
|
// TODO insert all updated TTs
|
||||||
|
}
|
||||||
|
|
||||||
|
`when`(
|
||||||
|
repository.runTransactionForResult(argThatKotlin<InsertTemporaryTargetAndCancelCurrentTransaction> {
|
||||||
|
it.temporaryTarget
|
||||||
|
.copy(timestamp = expectedTarget.timestamp, utcOffset = expectedTarget.utcOffset) // those can be different
|
||||||
|
.contentEqualsTo(expectedTarget)
|
||||||
|
})
|
||||||
|
).thenReturn(Single.just(InsertTemporaryTargetAndCancelCurrentTransaction.TransactionResult().apply {
|
||||||
|
inserted.addAll(inserted)
|
||||||
|
updated.addAll(updated)
|
||||||
|
}))
|
||||||
|
|
||||||
sut.doAction(object : Callback() {
|
sut.doAction(object : Callback() {
|
||||||
override fun run() {
|
override fun run() {
|
||||||
Assert.assertTrue(result.success)
|
Assert.assertTrue(result.success)
|
||||||
|
|
|
@ -4,7 +4,7 @@ import info.nightscout.androidaps.database.entities.TemporaryTarget
|
||||||
import info.nightscout.androidaps.database.interfaces.end
|
import info.nightscout.androidaps.database.interfaces.end
|
||||||
|
|
||||||
class InsertTemporaryTargetAndCancelCurrentTransaction(
|
class InsertTemporaryTargetAndCancelCurrentTransaction(
|
||||||
private val temporaryTarget: TemporaryTarget
|
val temporaryTarget: TemporaryTarget
|
||||||
) : Transaction<InsertTemporaryTargetAndCancelCurrentTransaction.TransactionResult>() {
|
) : Transaction<InsertTemporaryTargetAndCancelCurrentTransaction.TransactionResult>() {
|
||||||
|
|
||||||
constructor(timestamp: Long, duration: Long, reason: TemporaryTarget.Reason, lowTarget: Double, highTarget: Double) :
|
constructor(timestamp: Long, duration: Long, reason: TemporaryTarget.Reason, lowTarget: Double, highTarget: Double) :
|
||||||
|
|
Loading…
Reference in a new issue