From a382fc89eaf1e38af7a78c0a631b04743e919abd Mon Sep 17 00:00:00 2001 From: davidoskky Date: Mon, 26 Sep 2022 23:11:26 +0200 Subject: [PATCH] Test item caching --- .../android/fragments/ArticleFragment.kt | 1 + .../repository/RepositoryImpl.kt | 5 +- .../repository/RepositoryTest.kt | 76 +++++++++++++++++++ 3 files changed, 81 insertions(+), 1 deletion(-) diff --git a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/fragments/ArticleFragment.kt b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/fragments/ArticleFragment.kt index 7e24dea..6cf09b0 100644 --- a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/fragments/ArticleFragment.kt +++ b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/fragments/ArticleFragment.kt @@ -269,6 +269,7 @@ class ArticleFragment : Fragment(), DIAware { private fun getContentFromMercury(customTabsIntent: CustomTabsIntent) { if (repository.isNetworkAvailable()) { binding.progressBar.visibility = View.VISIBLE + // TODO: The api should be accessed through the repository val parser = MercuryApi() parser.parseUrl(url).enqueue( diff --git a/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/repository/RepositoryImpl.kt b/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/repository/RepositoryImpl.kt index 22d9a7f..9acb07e 100644 --- a/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/repository/RepositoryImpl.kt +++ b/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/repository/RepositoryImpl.kt @@ -378,6 +378,7 @@ class Repository(private val api: SelfossApi, private val appSettingsService: Ap api.refreshLoginInformation() } + // TODO: This should be private suspend fun updateApiVersion() { val apiMajorVersion = appSettingsService.getApiVersion() @@ -389,6 +390,8 @@ class Repository(private val api: SelfossApi, private val appSettingsService: Ap } } + // TODO: This should be private (since all api calls are made through the repository + // no other entity needs to know about the connectivity status) fun isNetworkAvailable() = isConnectionAvailable.value && !offlineOverride private fun getDBActions(): List = @@ -440,7 +443,7 @@ class Repository(private val api: SelfossApi, private val appSettingsService: Ap db.itemsQueries.updateItem(item.datetime, item.title.getHtmlDecoded(), item.content, item.unread, item.starred, item.thumbnail, item.icon, item.link, item.sourcetitle, item.tags.joinToString(","), item.id.toString()) - suspend fun tryToCacheItemsAndGetNewOnes(): List? { + suspend fun tryToCacheItemsAndGetNewOnes(): List { try { val newItems = getMaxItemsForBackground(ItemType.UNREAD) val allItems = getMaxItemsForBackground(ItemType.ALL) diff --git a/shared/src/commonTest/kotlin/bou/amine/apps/readerforselfossv2/repository/RepositoryTest.kt b/shared/src/commonTest/kotlin/bou/amine/apps/readerforselfossv2/repository/RepositoryTest.kt index dc762a2..cc498a2 100644 --- a/shared/src/commonTest/kotlin/bou/amine/apps/readerforselfossv2/repository/RepositoryTest.kt +++ b/shared/src/commonTest/kotlin/bou/amine/apps/readerforselfossv2/repository/RepositoryTest.kt @@ -692,6 +692,82 @@ class RepositoryTest() { coVerify(exactly = 1) { api.refreshLoginInformation() } coVerify(exactly = 1) {appSettingsService.refreshLoginInformation("https://test.com/selfoss/", "login", "password")} } + + @Test + fun `cache items`() { + coEvery { api.getItems(any(), any(), any(), any(), any(), any(), any()) } returns + SelfossModel.StatusAndData(success = true, data = generateTestApiItem()) + + val repository = Repository(api, appSettingsService, connectivityStatus, db) + repository.tagFilter = SelfossModel.Tag("Tag", "read", 0) + repository.sourceFilter = SelfossModel.Source( + 1, + "First source", + listOf("Test", "second"), + "spouts\\rss\\fulltextrss", + "", + "d8c92cdb1ef119ea85c4b9205c879ca7.png" + ) + repository.searchFilter = "search" + var items = emptyList() + runBlocking { + items = repository.tryToCacheItemsAndGetNewOnes() + } + + coVerify(exactly = 3) { api.getItems(any(), 0, null, null, null, null, 200) } + assertSame(3, items.size) + } + + @Test + fun `cache items but response fails`() { + coEvery { api.getItems(any(), any(), any(), any(), any(), any(), any()) } returns + SelfossModel.StatusAndData(success = false, data = generateTestApiItem()) + + val repository = Repository(api, appSettingsService, connectivityStatus, db) + repository.tagFilter = SelfossModel.Tag("Tag", "read", 0) + repository.sourceFilter = SelfossModel.Source( + 1, + "First source", + listOf("Test", "second"), + "spouts\\rss\\fulltextrss", + "", + "d8c92cdb1ef119ea85c4b9205c879ca7.png" + ) + repository.searchFilter = "search" + var items = emptyList() + runBlocking { + items = repository.tryToCacheItemsAndGetNewOnes() + } + + coVerify(exactly = 3) { api.getItems(any(), 0, null, null, null, null, 200) } + assertSame(0, items.size) + } + + @Test + fun `cache items without connection`() { + coEvery { api.getItems(any(), any(), any(), any(), any(), any(), any()) } returns + SelfossModel.StatusAndData(success = false, data = generateTestApiItem()) + every { connectivityStatus.isNetworkConnected } returns MutableStateFlow(false) + + val repository = Repository(api, appSettingsService, connectivityStatus, db) + repository.tagFilter = SelfossModel.Tag("Tag", "read", 0) + repository.sourceFilter = SelfossModel.Source( + 1, + "First source", + listOf("Test", "second"), + "spouts\\rss\\fulltextrss", + "", + "d8c92cdb1ef119ea85c4b9205c879ca7.png" + ) + repository.searchFilter = "search" + var items = emptyList() + runBlocking { + items = repository.tryToCacheItemsAndGetNewOnes() + } + + coVerify(exactly = 0) { api.getItems(any(), 0, null, null, null, null, 200) } + assertSame(emptyList(), items) + } } fun generateTestDBItems(item : FakeItemParameters = FakeItemParameters()) : List {