From 2252858e65c2b41f31481640db765ecbdcb5a48f Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Tue, 10 Jan 2023 18:25:53 +0100 Subject: [PATCH] NSCv3: tests --- .../sync/nsclientV3/workers/LoadBgWorker.kt | 23 +++++++++---------- .../nsclientV3/workers/LoadBgWorkerTest.kt | 11 +++++++-- 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/plugins/sync/src/main/java/info/nightscout/plugins/sync/nsclientV3/workers/LoadBgWorker.kt b/plugins/sync/src/main/java/info/nightscout/plugins/sync/nsclientV3/workers/LoadBgWorker.kt index 5d89e23419..507ae73e36 100644 --- a/plugins/sync/src/main/java/info/nightscout/plugins/sync/nsclientV3/workers/LoadBgWorker.kt +++ b/plugins/sync/src/main/java/info/nightscout/plugins/sync/nsclientV3/workers/LoadBgWorker.kt @@ -84,18 +84,17 @@ class LoadBgWorker( .then(OneTimeWorkRequest.Builder(LoadTreatmentsWorker::class.java).build()) .enqueue() } - } - else { - // End first load - if (isFirstLoad) { - nsClientV3Plugin.lastLoadedSrvModified.collections.entries = lastLoaded - nsClientV3Plugin.storeLastLoadedSrvModified() - } - rxBus.send(EventNSClientNewLog("RCV END", "No new SGVs from ${dateUtil.dateAndTimeAndSecondsString(lastLoaded)}")) - WorkManager.getInstance(context) - .beginUniqueWork( - NSClientV3Plugin.JOB_NAME, - ExistingWorkPolicy.APPEND_OR_REPLACE, + } else { + // End first load + if (isFirstLoad) { + nsClientV3Plugin.lastLoadedSrvModified.collections.entries = lastLoaded + nsClientV3Plugin.storeLastLoadedSrvModified() + } + rxBus.send(EventNSClientNewLog("RCV END", "No new SGVs from ${dateUtil.dateAndTimeAndSecondsString(lastLoaded)}")) + WorkManager.getInstance(context) + .beginUniqueWork( + NSClientV3Plugin.JOB_NAME, + ExistingWorkPolicy.APPEND_OR_REPLACE, OneTimeWorkRequest.Builder(StoreDataForDbImpl.StoreBgWorker::class.java).build() ) .then(OneTimeWorkRequest.Builder(LoadTreatmentsWorker::class.java).build()) diff --git a/plugins/sync/src/test/java/info/nightscout/plugins/sync/nsclientV3/workers/LoadBgWorkerTest.kt b/plugins/sync/src/test/java/info/nightscout/plugins/sync/nsclientV3/workers/LoadBgWorkerTest.kt index 6b0af34c16..b85a9e2ac0 100644 --- a/plugins/sync/src/test/java/info/nightscout/plugins/sync/nsclientV3/workers/LoadBgWorkerTest.kt +++ b/plugins/sync/src/test/java/info/nightscout/plugins/sync/nsclientV3/workers/LoadBgWorkerTest.kt @@ -52,6 +52,8 @@ internal class LoadBgWorkerTest : TestBase() { private lateinit var nsClientReceiverDelegate: NsClientReceiverDelegate private lateinit var sut: LoadBgWorker + private val now = 1000000000L + private val injector = HasAndroidInjector { AndroidInjector { if (it is LoadBgWorker) { @@ -72,6 +74,7 @@ internal class LoadBgWorkerTest : TestBase() { fun setUp() { Mockito.`when`(context.applicationContext).thenReturn(context) Mockito.`when`(context.androidInjector()).thenReturn(injector.androidInjector()) + Mockito.`when`(dateUtil.now()).thenReturn(now) nsClientReceiverDelegate = NsClientReceiverDelegate(rxBus, rh, sp, receiverStatusStore) nsClientV3Plugin = NSClientV3Plugin( injector, aapsLogger, aapsSchedulers, rxBus, rh, context, fabricPrivacy, sp, nsClientReceiverDelegate, config, dateUtil, uiInteraction, dataSyncSelector, @@ -89,12 +92,16 @@ internal class LoadBgWorkerTest : TestBase() { } @Test - fun doWork() = runTest { + fun testThereAreNewerDataFirstLoadEmptyReturn() = runTest { initWorkManager() nsClientV3Plugin.nsAndroidClient = nsAndroidClient + nsClientV3Plugin.lastLoadedSrvModified.collections.entries = 0L // first load + nsClientV3Plugin.firstLoadContinueTimestamp.collections.entries = now - 1000 sut = TestListenableWorkerBuilder(context).build() 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) } } \ No newline at end of file