From d311c2cdebae84981ec2433aab716bd1d274a579 Mon Sep 17 00:00:00 2001 From: davidoskky Date: Wed, 28 Sep 2022 18:45:21 +0200 Subject: [PATCH] Fix sources tests --- .../repository/RepositoryImpl.kt | 10 +- .../repository/RepositoryTest.kt | 151 ++++++++++++------ 2 files changed, 108 insertions(+), 53 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 5009e38..fdba511 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 @@ -171,15 +171,17 @@ class Repository(private val api: SelfossApi, private val appSettingsService: Ap } } - suspend fun getSources(): ArrayList? { + suspend fun getSources(): ArrayList { return if (isNetworkAvailable()) { val apiSources = api.sources() - if (apiSources.success && apiSources.data != null && (appSettingsService.isItemCachingEnabled() || !appSettingsService.isUpdateSourcesEnabled())) { + if (apiSources.success && apiSources.data != null && (appSettingsService.isItemCachingEnabled() || appSettingsService.isUpdateSourcesEnabled())) { resetDBSourcesWithData(apiSources.data) } - apiSources.data - } else { + apiSources.data ?: ArrayList() + } else if (appSettingsService.isItemCachingEnabled() || appSettingsService.isUpdateSourcesEnabled()) { ArrayList(getDBSources().map { it.toView() }) + } else { + ArrayList() } } 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 84425af..46fbc06 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 @@ -17,7 +17,7 @@ import kotlin.test.* class RepositoryTest() { private val connectivityStatus = mockk() - private val db = mockk() + private val db = mockk(relaxed = true) private val appSettingsService = mockk() private val api = mockk() @@ -466,8 +466,11 @@ class RepositoryTest() { fun `get sources`() { val sources = arrayListOf(SelfossModel.Source(1, "First source", listOf("Test", "second"),"spouts\\rss\\fulltextrss", "", "d8c92cdb1ef119ea85c4b9205c879ca7.png"), SelfossModel.Source(2, "Second source", listOf("second"),"spouts\\rss\\fulltextrss", "", "b3aa8a664d08eb15d6ff1db2fa83e0d9.png")) + val sourcesDB = listOf(SOURCE("1", "First DB source", "Test,second","spouts\\rss\\fulltextrss", "", "d8c92cdb1ef119ea85c4b9205c879ca7.png"), + SOURCE("2", "Second source", "second","spouts\\rss\\fulltextrss", "", "b3aa8a664d08eb15d6ff1db2fa83e0d9.png")) coEvery { api.sources() } returns SelfossModel.StatusAndData(success = true, data = sources) + every { db.sourcesQueries.sources().executeAsList() } returns sourcesDB val repository = Repository(api, appSettingsService, connectivityStatus, db) var testSources: List? = null runBlocking { @@ -475,57 +478,14 @@ class RepositoryTest() { } assertSame(sources, testSources) + assertNotEquals(sourcesDB.map { it.toView() }, testSources) coVerify(exactly = 1) { api.sources() } - verify(exactly = 0) { db.sourcesQueries.sources().executeAsList() } - } - - @Test - fun `get sources without connection`() { - val sources = arrayListOf(SelfossModel.Source(1, "First source", listOf("Test", "second"),"spouts\\rss\\fulltextrss", "", "d8c92cdb1ef119ea85c4b9205c879ca7.png"), - SelfossModel.Source(2, "Second source", listOf("second"),"spouts\\rss\\fulltextrss", "", "b3aa8a664d08eb15d6ff1db2fa83e0d9.png")) - val sourcesDB = listOf(SOURCE("1", "First source", "Test,second","spouts\\rss\\fulltextrss", "", "d8c92cdb1ef119ea85c4b9205c879ca7.png"), - SOURCE("2", "Second source", "second","spouts\\rss\\fulltextrss", "", "b3aa8a664d08eb15d6ff1db2fa83e0d9.png")) - - every { connectivityStatus.isNetworkConnected } returns MutableStateFlow(false) - coEvery { api.sources() } returns SelfossModel.StatusAndData(success = true, data = sources) - every { db.sourcesQueries.sources().executeAsList() } returns sourcesDB - val repository = Repository(api, appSettingsService, connectivityStatus, db) - var testSources: List? = null - runBlocking { - testSources = repository.getSources() - } - - assertContentEquals(sources, testSources) - coVerify(exactly = 0) { api.sources() } - verify(atLeast = 1) { db.sourcesQueries.sources().executeAsList() } - } - - @Test - fun `get sources without connection and items caching disabled`() { - val sources = arrayListOf(SelfossModel.Source(1, "First source", listOf("Test", "second"),"spouts\\rss\\fulltextrss", "", "d8c92cdb1ef119ea85c4b9205c879ca7.png"), - SelfossModel.Source(2, "Second source", listOf("second"),"spouts\\rss\\fulltextrss", "", "b3aa8a664d08eb15d6ff1db2fa83e0d9.png")) - val sourcesDB = listOf(SOURCE("1", "First source", "Test,second","spouts\\rss\\fulltextrss", "", "d8c92cdb1ef119ea85c4b9205c879ca7.png"), - SOURCE("2", "Second source", "second","spouts\\rss\\fulltextrss", "", "b3aa8a664d08eb15d6ff1db2fa83e0d9.png")) - - every { connectivityStatus.isNetworkConnected } returns MutableStateFlow(false) - every { appSettingsService.isItemCachingEnabled() } returns false - coEvery { api.sources() } returns SelfossModel.StatusAndData(success = true, data = sources) - every { db.sourcesQueries.sources().executeAsList() } returns sourcesDB - val repository = Repository(api, appSettingsService, connectivityStatus, db) - var testSources: List? = null - runBlocking { - testSources = repository.getSources() - } - - assertSame(null, testSources) - coVerify(exactly = 0) { api.sources() } - verify(exactly = 0) { db.sourcesQueries.sources().executeAsList() } } @Test fun `get sources with sources update disabled`() { val sources = arrayListOf(SelfossModel.Source(1, "First source", listOf("Test", "second"),"spouts\\rss\\fulltextrss", "", "d8c92cdb1ef119ea85c4b9205c879ca7.png"), - SelfossModel.Source(2, "Second source", listOf("second"),"spouts\\rss\\fulltextrss", "", "b3aa8a664d08eb15d6ff1db2fa83e0d9.png")) + SelfossModel.Source(2, "Second DB source", listOf("second"),"spouts\\rss\\fulltextrss", "", "b3aa8a664d08eb15d6ff1db2fa83e0d9.png")) val sourcesDB = listOf(SOURCE("1", "First source", "Test,second","spouts\\rss\\fulltextrss", "", "d8c92cdb1ef119ea85c4b9205c879ca7.png"), SOURCE("2", "Second source", "second","spouts\\rss\\fulltextrss", "", "b3aa8a664d08eb15d6ff1db2fa83e0d9.png")) @@ -536,10 +496,13 @@ class RepositoryTest() { var testSources: List? = null runBlocking { testSources = repository.getSources() + // Sources will be fetched from the database on the second call, thus testSources != sources + testSources = repository.getSources() } - assertContentEquals(sources, testSources) - coVerify(exactly = 0) { api.sources() } + assertNotSame(sources, testSources) + assertContentEquals(sourcesDB.map { it.toView() }, testSources) + coVerify(exactly = 1) { api.sources() } verify(atLeast = 1) { db.sourcesQueries.sources().executeAsList() } } @@ -560,7 +523,97 @@ class RepositoryTest() { testSources = repository.getSources() } - assertSame(null, testSources) + assertSame(sources, testSources) + coVerify(exactly = 1) { api.sources() } + verify(exactly = 0) { db.sourcesQueries.sources().executeAsList() } + } + + @Test + fun `get sources without connection`() { + val sources = arrayListOf(SelfossModel.Source(1, "First source", listOf("Test", "second"),"spouts\\rss\\fulltextrss", "", "d8c92cdb1ef119ea85c4b9205c879ca7.png"), + SelfossModel.Source(2, "Second source", listOf("second"),"spouts\\rss\\fulltextrss", "", "b3aa8a664d08eb15d6ff1db2fa83e0d9.png")) + val sourcesDB = listOf(SOURCE("1", "First DB source", "Test,second","spouts\\rss\\fulltextrss", "", "d8c92cdb1ef119ea85c4b9205c879ca7.png"), + SOURCE("2", "Second source", "second","spouts\\rss\\fulltextrss", "", "b3aa8a664d08eb15d6ff1db2fa83e0d9.png")) + + every { connectivityStatus.isNetworkConnected } returns MutableStateFlow(false) + coEvery { api.sources() } returns SelfossModel.StatusAndData(success = true, data = sources) + every { db.sourcesQueries.sources().executeAsList() } returns sourcesDB + val repository = Repository(api, appSettingsService, connectivityStatus, db) + var testSources: List? = null + runBlocking { + testSources = repository.getSources() + } + + assertContentEquals(sourcesDB.map { it.toView() }, testSources) + coVerify(exactly = 0) { api.sources() } + verify(atLeast = 1) { db.sourcesQueries.sources().executeAsList() } + } + + @Test + fun `get sources without connection and items caching disabled`() { + val sources = arrayListOf(SelfossModel.Source(1, "First source", listOf("Test", "second"),"spouts\\rss\\fulltextrss", "", "d8c92cdb1ef119ea85c4b9205c879ca7.png"), + SelfossModel.Source(2, "Second source", listOf("second"),"spouts\\rss\\fulltextrss", "", "b3aa8a664d08eb15d6ff1db2fa83e0d9.png")) + val sourcesDB = listOf(SOURCE("1", "First DB source", "Test,second","spouts\\rss\\fulltextrss", "", "d8c92cdb1ef119ea85c4b9205c879ca7.png"), + SOURCE("2", "Second source", "second","spouts\\rss\\fulltextrss", "", "b3aa8a664d08eb15d6ff1db2fa83e0d9.png")) + + every { connectivityStatus.isNetworkConnected } returns MutableStateFlow(false) + every { appSettingsService.isItemCachingEnabled() } returns false + every { appSettingsService.isUpdateSourcesEnabled() } returns true + coEvery { api.sources() } returns SelfossModel.StatusAndData(success = true, data = sources) + every { db.sourcesQueries.sources().executeAsList() } returns sourcesDB + val repository = Repository(api, appSettingsService, connectivityStatus, db) + var testSources: List? = null + runBlocking { + testSources = repository.getSources() + } + + assertContentEquals(sourcesDB.map { it.toView() }, testSources) + coVerify(exactly = 0) { api.sources() } + verify(atLeast = 1) { db.sourcesQueries.sources().executeAsList() } + } + + @Test + fun `get sources without connection and sources update disabled`() { + val sources = arrayListOf(SelfossModel.Source(1, "First source", listOf("Test", "second"),"spouts\\rss\\fulltextrss", "", "d8c92cdb1ef119ea85c4b9205c879ca7.png"), + SelfossModel.Source(2, "Second source", listOf("second"),"spouts\\rss\\fulltextrss", "", "b3aa8a664d08eb15d6ff1db2fa83e0d9.png")) + val sourcesDB = listOf(SOURCE("1", "First DB source", "Test,second","spouts\\rss\\fulltextrss", "", "d8c92cdb1ef119ea85c4b9205c879ca7.png"), + SOURCE("2", "Second source", "second","spouts\\rss\\fulltextrss", "", "b3aa8a664d08eb15d6ff1db2fa83e0d9.png")) + + every { connectivityStatus.isNetworkConnected } returns MutableStateFlow(false) + every { appSettingsService.isItemCachingEnabled() } returns true + every { appSettingsService.isUpdateSourcesEnabled() } returns false + coEvery { api.sources() } returns SelfossModel.StatusAndData(success = true, data = sources) + every { db.sourcesQueries.sources().executeAsList() } returns sourcesDB + val repository = Repository(api, appSettingsService, connectivityStatus, db) + var testSources: List? = null + runBlocking { + testSources = repository.getSources() + } + + assertContentEquals(sourcesDB.map { it.toView() }, testSources) + coVerify(exactly = 0) { api.sources() } + verify(atLeast = 1) { db.sourcesQueries.sources().executeAsList() } + } + + @Test + fun `get sources without connection and items caching and sources update disabled`() { + val sources = arrayListOf(SelfossModel.Source(1, "First source", listOf("Test", "second"),"spouts\\rss\\fulltextrss", "", "d8c92cdb1ef119ea85c4b9205c879ca7.png"), + SelfossModel.Source(2, "Second source", listOf("second"),"spouts\\rss\\fulltextrss", "", "b3aa8a664d08eb15d6ff1db2fa83e0d9.png")) + val sourcesDB = listOf(SOURCE("1", "First DB source", "Test,second","spouts\\rss\\fulltextrss", "", "d8c92cdb1ef119ea85c4b9205c879ca7.png"), + SOURCE("2", "Second source", "second","spouts\\rss\\fulltextrss", "", "b3aa8a664d08eb15d6ff1db2fa83e0d9.png")) + + every { connectivityStatus.isNetworkConnected } returns MutableStateFlow(false) + every { appSettingsService.isItemCachingEnabled() } returns false + every { appSettingsService.isUpdateSourcesEnabled() } returns false + coEvery { api.sources() } returns SelfossModel.StatusAndData(success = true, data = sources) + every { db.sourcesQueries.sources().executeAsList() } returns sourcesDB + val repository = Repository(api, appSettingsService, connectivityStatus, db) + var testSources: List? = null + runBlocking { + testSources = repository.getSources() + } + + assertContentEquals(ArrayList(), testSources) coVerify(exactly = 0) { api.sources() } verify(exactly = 0) { db.sourcesQueries.sources().executeAsList() } }