NSCv3: tests

This commit is contained in:
Milos Kozak 2023-01-10 18:25:53 +01:00
parent 9b8632239c
commit 2252858e65
2 changed files with 20 additions and 14 deletions

View file

@ -84,18 +84,17 @@ class LoadBgWorker(
.then(OneTimeWorkRequest.Builder(LoadTreatmentsWorker::class.java).build()) .then(OneTimeWorkRequest.Builder(LoadTreatmentsWorker::class.java).build())
.enqueue() .enqueue()
} }
} } else {
else { // End first load
// End first load if (isFirstLoad) {
if (isFirstLoad) { nsClientV3Plugin.lastLoadedSrvModified.collections.entries = lastLoaded
nsClientV3Plugin.lastLoadedSrvModified.collections.entries = lastLoaded nsClientV3Plugin.storeLastLoadedSrvModified()
nsClientV3Plugin.storeLastLoadedSrvModified() }
} rxBus.send(EventNSClientNewLog("RCV END", "No new SGVs from ${dateUtil.dateAndTimeAndSecondsString(lastLoaded)}"))
rxBus.send(EventNSClientNewLog("RCV END", "No new SGVs from ${dateUtil.dateAndTimeAndSecondsString(lastLoaded)}")) WorkManager.getInstance(context)
WorkManager.getInstance(context) .beginUniqueWork(
.beginUniqueWork( NSClientV3Plugin.JOB_NAME,
NSClientV3Plugin.JOB_NAME, ExistingWorkPolicy.APPEND_OR_REPLACE,
ExistingWorkPolicy.APPEND_OR_REPLACE,
OneTimeWorkRequest.Builder(StoreDataForDbImpl.StoreBgWorker::class.java).build() OneTimeWorkRequest.Builder(StoreDataForDbImpl.StoreBgWorker::class.java).build()
) )
.then(OneTimeWorkRequest.Builder(LoadTreatmentsWorker::class.java).build()) .then(OneTimeWorkRequest.Builder(LoadTreatmentsWorker::class.java).build())

View file

@ -52,6 +52,8 @@ internal class LoadBgWorkerTest : TestBase() {
private lateinit var nsClientReceiverDelegate: NsClientReceiverDelegate private lateinit var nsClientReceiverDelegate: NsClientReceiverDelegate
private lateinit var sut: LoadBgWorker private lateinit var sut: LoadBgWorker
private val now = 1000000000L
private val injector = HasAndroidInjector { private val injector = HasAndroidInjector {
AndroidInjector { AndroidInjector {
if (it is LoadBgWorker) { if (it is LoadBgWorker) {
@ -72,6 +74,7 @@ internal class LoadBgWorkerTest : TestBase() {
fun setUp() { fun setUp() {
Mockito.`when`(context.applicationContext).thenReturn(context) Mockito.`when`(context.applicationContext).thenReturn(context)
Mockito.`when`(context.androidInjector()).thenReturn(injector.androidInjector()) Mockito.`when`(context.androidInjector()).thenReturn(injector.androidInjector())
Mockito.`when`(dateUtil.now()).thenReturn(now)
nsClientReceiverDelegate = NsClientReceiverDelegate(rxBus, rh, sp, receiverStatusStore) nsClientReceiverDelegate = NsClientReceiverDelegate(rxBus, rh, sp, receiverStatusStore)
nsClientV3Plugin = NSClientV3Plugin( nsClientV3Plugin = NSClientV3Plugin(
injector, aapsLogger, aapsSchedulers, rxBus, rh, context, fabricPrivacy, sp, nsClientReceiverDelegate, config, dateUtil, uiInteraction, dataSyncSelector, injector, aapsLogger, aapsSchedulers, rxBus, rh, context, fabricPrivacy, sp, nsClientReceiverDelegate, config, dateUtil, uiInteraction, dataSyncSelector,
@ -89,12 +92,16 @@ internal class LoadBgWorkerTest : TestBase() {
} }
@Test @Test
fun doWork() = runTest { fun testThereAreNewerDataFirstLoadEmptyReturn() = runTest {
initWorkManager() initWorkManager()
nsClientV3Plugin.nsAndroidClient = nsAndroidClient nsClientV3Plugin.nsAndroidClient = nsAndroidClient
nsClientV3Plugin.lastLoadedSrvModified.collections.entries = 0L // first load
nsClientV3Plugin.firstLoadContinueTimestamp.collections.entries = now - 1000
sut = TestListenableWorkerBuilder<LoadBgWorker>(context).build() sut = TestListenableWorkerBuilder<LoadBgWorker>(context).build()
Mockito.`when`(nsAndroidClient.getSgvsNewerThan(anyLong(), anyLong())).thenReturn(NSAndroidClient.ReadResponse(200, 0, emptyList())) Mockito.`when`(nsAndroidClient.getSgvsNewerThan(anyLong(), anyLong())).thenReturn(NSAndroidClient.ReadResponse(200, 0, emptyList()))
var result = sut.doWorkAndLog()
val result = sut.doWorkAndLog()
Assertions.assertEquals(now - 1000, nsClientV3Plugin.lastLoadedSrvModified.collections.entries)
Assertions.assertTrue(result is ListenableWorker.Result.Success) Assertions.assertTrue(result is ListenableWorker.Result.Success)
} }
} }