From ef994460c1cb5d22ff37d2cba300351c2444c69a Mon Sep 17 00:00:00 2001 From: davidoskky Date: Sun, 18 Sep 2022 18:14:14 +0200 Subject: [PATCH] get sources tests --- .../repository/RepositoryImpl.kt | 1 + .../repository/RepositoryTest.kt | 110 +++++++++++++++++- 2 files changed, 107 insertions(+), 4 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 0dc55d9..f1add44 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 @@ -148,6 +148,7 @@ class Repository(private val api: SelfossApi, private val appSettingsService: Ap } } + // TODO: Add tests suspend fun getSpouts(): Map? { return if (isNetworkAvailable()) { api.spouts() 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 8e0e5c3..eff73b9 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 @@ -2,20 +2,19 @@ package bou.amine.apps.readerforselfossv2.repository import bou.amine.apps.readerforselfossv2.dao.ITEM import bou.amine.apps.readerforselfossv2.dao.ReaderForSelfossDB +import bou.amine.apps.readerforselfossv2.dao.SOURCE import bou.amine.apps.readerforselfossv2.dao.TAG import bou.amine.apps.readerforselfossv2.model.SelfossModel import bou.amine.apps.readerforselfossv2.rest.SelfossApi import bou.amine.apps.readerforselfossv2.service.AppSettingsService import bou.amine.apps.readerforselfossv2.utils.ItemType import bou.amine.apps.readerforselfossv2.utils.toEntity +import bou.amine.apps.readerforselfossv2.utils.toView import com.github.ln_12.library.ConnectivityStatus import io.mockk.* import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.runBlocking -import kotlin.test.BeforeTest -import kotlin.test.Test -import kotlin.test.assertNotSame -import kotlin.test.assertSame +import kotlin.test.* class RepositoryTest() { private val connectivityStatus = mockk() @@ -397,6 +396,109 @@ class RepositoryTest() { 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"), + SelfossModel.Source(2, "Second source", listOf("second"),"spouts\\rss\\fulltextrss", "", "b3aa8a664d08eb15d6ff1db2fa83e0d9.png")) + + coEvery { api.sources() } returns sources + val repository = Repository(api, appSettingsService, connectivityStatus, db) + var testSources: List? = null + runBlocking { + testSources = repository.getSources() + } + + 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 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 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 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")) + 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 { appSettingsService.isUpdateSourcesEnabled() } returns false + coEvery { api.sources() } returns 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 with sources update 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 { appSettingsService.isUpdateSourcesEnabled() } returns false + every { appSettingsService.isItemCachingEnabled() } returns false + coEvery { api.sources() } returns 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() } + } } fun generateTestDBItems(item : FakeItemParameters = FakeItemParameters()) : List {