From 219cae5d7491ddb395d1afaa7462d4595b9ce5de Mon Sep 17 00:00:00 2001 From: davidoskky Date: Wed, 28 Sep 2022 18:22:06 +0200 Subject: [PATCH] Fix tags tests --- .../repository/RepositoryImpl.kt | 6 ++-- .../repository/RepositoryTest.kt | 35 +++++++++++++++++-- 2 files changed, 36 insertions(+), 5 deletions(-) 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 f235771..5009e38 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 @@ -146,12 +146,14 @@ class Repository(private val api: SelfossApi, private val appSettingsService: Ap suspend fun getTags(): List { return if (isNetworkAvailable()) { val apiTags = api.tags() - if (apiTags.success && apiTags.data != null && (appSettingsService.isItemCachingEnabled() || !appSettingsService.isUpdateSourcesEnabled())) { + if (apiTags.success && apiTags.data != null && (appSettingsService.isItemCachingEnabled() || appSettingsService.isUpdateSourcesEnabled())) { resetDBTagsWithData(apiTags.data) } apiTags.data ?: emptyList() - } else { + } else if (appSettingsService.isItemCachingEnabled() || appSettingsService.isUpdateSourcesEnabled()) { getDBTags().map { it.toView() } + } else { + emptyList() } } 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 094def3..84425af 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 @@ -303,8 +303,11 @@ class RepositoryTest() { fun `Get tags`() { val tags = listOf(SelfossModel.Tag("test", "red", 6), SelfossModel.Tag("second", "yellow", 0)) + val tagsDB = listOf(TAG("test_DB", "red", 6), + TAG("second_DB", "yellow", 0)) coEvery { api.tags() } returns SelfossModel.StatusAndData(success = true, data = tags) + coEvery { db.tagsQueries.tags().executeAsList() } returns tagsDB val repository = Repository(api, appSettingsService, connectivityStatus, db) var testTags: List? = null @@ -313,7 +316,8 @@ class RepositoryTest() { } assertSame(tags, testTags) - verify(exactly = 0) { db.tagsQueries.tags().executeAsList() } + assertNotSame(tagsDB.map { it.toView() }, testTags) + coVerify(exactly = 1) { api.tags() } } @Test @@ -331,6 +335,7 @@ class RepositoryTest() { var testTags: List = emptyList() runBlocking { testTags = repository.getTags() + // Tags will be fetched from the database on the second call, thus testTags != tags testTags = repository.getTags() } @@ -358,8 +363,8 @@ class RepositoryTest() { testTags = repository.getTags() } - assertSame(emptyList(), testTags) - coVerify(exactly = 0) { api.tags() } + assertSame(tags, testTags) + coVerify(exactly = 1) { api.tags() } verify(exactly = 0) { db.tagsQueries.tags().executeAsList() } } @@ -433,6 +438,30 @@ class RepositoryTest() { verify(atLeast = 1) { db.tagsQueries.tags().executeAsList() } } + @Test + fun `Get tags without connection and sources update and items caching disabled`() { + val tags = listOf(SelfossModel.Tag("test", "red", 6), + SelfossModel.Tag("second", "yellow", 0)) + val tagsDB = listOf(TAG("test_DB", "red", 6), + TAG("second_DB", "yellow", 0)) + + coEvery { api.tags() } returns SelfossModel.StatusAndData(success = true, data = tags) + coEvery { db.tagsQueries.tags().executeAsList() } returns tagsDB + every { connectivityStatus.isNetworkConnected } returns MutableStateFlow(false) + every { appSettingsService.isUpdateSourcesEnabled() } returns false + every { appSettingsService.isItemCachingEnabled() } returns false + + val repository = Repository(api, appSettingsService, connectivityStatus, db) + var testTags: List = emptyList() + runBlocking { + testTags = repository.getTags() + } + + assertSame(emptyList(), testTags) + coVerify(exactly = 0) { api.tags() } + verify(exactly = 0) { db.tagsQueries.tags().executeAsList() } + } + @Test fun `get sources`() { val sources = arrayListOf(SelfossModel.Source(1, "First source", listOf("Test", "second"),"spouts\\rss\\fulltextrss", "", "d8c92cdb1ef119ea85c4b9205c879ca7.png"),