Merge pull request #385 from nightscout/adrian/tt

mocking with argThat
This commit is contained in:
Milos Kozak 2021-03-01 19:01:16 +01:00 committed by GitHub
commit 56aaa0a90b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 39 additions and 5 deletions

View file

@ -5,6 +5,7 @@ import info.nightscout.androidaps.utils.rx.AapsSchedulers
import info.nightscout.androidaps.utils.rx.TestAapsSchedulers
import org.junit.Before
import org.junit.Rule
import org.mockito.ArgumentMatcher
import org.mockito.Mockito
import org.mockito.junit.MockitoJUnit
import org.mockito.junit.MockitoRule
@ -35,6 +36,17 @@ open class TestBase {
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")
fun <T> uninitialized(): T = null as T
}

View file

@ -13,6 +13,7 @@ import org.junit.Assert
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.ArgumentMatcher
import org.mockito.Mockito
import org.mockito.Mockito.`when`
import org.powermock.modules.junit4.PowerMockRunner
@ -45,15 +46,36 @@ class ActionStartTempTargetTest : ActionsTestBase() {
}
@Test fun doActionTest() {
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 {
// insert all inserted TTs
add(expectedTarget)
}
val updated = mutableListOf<TemporaryTarget>().apply {
// add(TemporaryTarget(id = 0, version = 0, dateCreated = 0, isValid = false, referenceId = null, interfaceIDs_backing = null, timestamp = 0, utcOffset = 0, reason =, highTarget = 0.0, lowTarget = 0.0, duration = 0))
// insert all updated TTs
// TODO insert all updated TTs
}
`when`(
repository.runTransactionForResult(anyObject<Transaction<InsertTemporaryTargetAndCancelCurrentTransaction.TransactionResult>>())
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)

View file

@ -4,7 +4,7 @@ import info.nightscout.androidaps.database.entities.TemporaryTarget
import info.nightscout.androidaps.database.interfaces.end
class InsertTemporaryTargetAndCancelCurrentTransaction(
private val temporaryTarget: TemporaryTarget
val temporaryTarget: TemporaryTarget
) : Transaction<InsertTemporaryTargetAndCancelCurrentTransaction.TransactionResult>() {
constructor(timestamp: Long, duration: Long, reason: TemporaryTarget.Reason, lowTarget: Double, highTarget: Double) :