DataSyncWorkerTest

This commit is contained in:
Milos Kozak 2023-01-02 14:48:31 +01:00
parent ff1d3f1237
commit ddba45622c
3 changed files with 66 additions and 0 deletions

View file

@ -26,6 +26,7 @@ dependencies {
implementation project(':core:utils')
implementation project(':core:validators')
testImplementation "androidx.work:work-testing:$work_version"
testImplementation project(':implementation')
// NSClient, Tidepool

View file

@ -2,11 +2,13 @@ package info.nightscout.plugins.sync.nsclientV3.workers
import android.content.Context
import androidx.work.WorkerParameters
import info.nightscout.androidaps.annotations.OpenForTesting
import info.nightscout.core.utils.worker.LoggingWorker
import info.nightscout.interfaces.plugin.ActivePlugin
import info.nightscout.interfaces.sync.DataSyncSelector
import javax.inject.Inject
@OpenForTesting
class DataSyncWorker(
context: Context, params: WorkerParameters
) : LoggingWorker(context, params) {

View file

@ -0,0 +1,63 @@
package info.nightscout.plugins.sync.nsclientV3.workers
import android.content.Context
import androidx.work.ListenableWorker.Result.Success
import androidx.work.testing.TestListenableWorkerBuilder
import dagger.android.AndroidInjector
import dagger.android.HasAndroidInjector
import info.nightscout.androidaps.TestBase
import info.nightscout.core.utils.fabric.FabricPrivacy
import info.nightscout.interfaces.plugin.ActivePlugin
import info.nightscout.interfaces.sync.DataSyncSelector
import info.nightscout.interfaces.sync.NsClient
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.mockito.Mock
import org.mockito.Mockito
import org.mockito.Mockito.`when`
internal class DataSyncWorkerTest : TestBase() {
abstract class ContextWithInjector : Context(), HasAndroidInjector
@Mock lateinit var fabricPrivacy: FabricPrivacy
@Mock lateinit var dataSyncSelector: DataSyncSelector
@Mock lateinit var context: ContextWithInjector
@Mock lateinit var activePlugin: ActivePlugin
@Mock lateinit var nsClient: NsClient
private lateinit var sut: DataSyncWorker
private val injector = HasAndroidInjector {
AndroidInjector {
if (it is DataSyncWorker) {
it.aapsLogger = aapsLogger
it.fabricPrivacy = fabricPrivacy
it.dataSyncSelector = dataSyncSelector
it.activePlugin = activePlugin
}
}
}
@BeforeEach
fun prepare() {
`when`(context.applicationContext).thenReturn(context)
`when`(context.androidInjector()).thenReturn(injector.androidInjector())
`when`(activePlugin.activeNsClient).thenReturn(nsClient)
}
@Test
fun doWorkAndLog() {
sut = TestListenableWorkerBuilder<DataSyncWorker>(context).build()
`when`(nsClient.hasWritePermission).thenReturn(false)
sut.doWorkAndLog()
Mockito.verify(dataSyncSelector, Mockito.times(0)).doUpload()
`when`(nsClient.hasWritePermission).thenReturn(true)
val result = sut.doWorkAndLog()
Mockito.verify(dataSyncSelector, Mockito.times(1)).doUpload()
Assertions.assertTrue(result is Success)
}
}