From 758708e18d44b1bf7a0adc1db61148ed16bf28d0 Mon Sep 17 00:00:00 2001 From: davidoskky Date: Sat, 17 Sep 2022 22:04:24 +0200 Subject: [PATCH] Tags tests --- .../repository/RepositoryImpl.kt | 6 +- .../repository/RepositoryTest.kt | 116 ++++++++++++++++++ 2 files changed, 120 insertions(+), 2 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 e5dbc14..0dc55d9 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 @@ -380,11 +380,13 @@ class Repository(private val api: SelfossApi, private val appSettingsService: Ap private fun deleteDBAction(action: ACTION) = db.actionsQueries.deleteAction(action.id) + // TODO: This function should be private fun getDBTags(): List = db.tagsQueries.tags().executeAsList() + // TODO: This function should be private fun getDBSources(): List = db.sourcesQueries.sources().executeAsList() - fun resetDBTagsWithData(tagEntities: List) { + private fun resetDBTagsWithData(tagEntities: List) { db.tagsQueries.deleteAllTags() db.tagsQueries.transaction { @@ -394,7 +396,7 @@ class Repository(private val api: SelfossApi, private val appSettingsService: Ap } } - fun resetDBSourcesWithData(sources: List) { + private fun resetDBSourcesWithData(sources: List) { db.sourcesQueries.deleteAllSources() db.sourcesQueries.transaction { 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 a78a858..8e0e5c3 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,16 +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.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 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 class RepositoryTest() { @@ -30,6 +33,7 @@ class RepositoryTest() { every { appSettingsService.getApiVersion() } returns 4 every { appSettingsService.getBaseUrl() } returns "https://test.com/selfoss/" every { appSettingsService.isItemCachingEnabled() } returns false + every { appSettingsService.isUpdateSourcesEnabled() } returns true every { connectivityStatus.isNetworkConnected } returns MutableStateFlow(true) @@ -37,6 +41,9 @@ class RepositoryTest() { coEvery { api.stats() } returns SelfossModel.Stats(NUMBER_ARTICLES, NUMBER_UNREAD, NUMBER_STARRED) every { db.itemsQueries.items().executeAsList() } returns generateTestDBItems() + every { db.tagsQueries.deleteAllTags() } returns Unit + every { db.tagsQueries.transaction(any(), any()) } returns Unit + every { db.tagsQueries.insertTag(any()) } returns Unit } @Test @@ -281,6 +288,115 @@ class RepositoryTest() { coVerify(exactly = 0) { api.stats() } verify(exactly = 0) { db.itemsQueries.items().executeAsList()} } + + @Test + fun `Get tags`() { + val tags = listOf(SelfossModel.Tag("test", "red", 6), + SelfossModel.Tag("second", "yellow", 0)) + + coEvery { api.tags() } returns tags + + val repository = Repository(api, appSettingsService, connectivityStatus, db) + var testTags: List? = null + runBlocking { + testTags = repository.getTags() + } + + assertSame(tags, testTags) + verify(exactly = 0) { db.tagsQueries.tags().executeAsList() } + } + + @Test + fun `Get tags with sources update 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 tags + coEvery { db.tagsQueries.tags().executeAsList() } returns tagsDB + every { appSettingsService.isUpdateSourcesEnabled() } returns false + + val repository = Repository(api, appSettingsService, connectivityStatus, db) + var testTags: List? = null + runBlocking { + testTags = repository.getTags() + } + + assertNotSame(tags, testTags) + assertSame(tagsDB.first().name, testTags?.first()?.tag) + coVerify(exactly = 0) { api.tags() } + verify(atLeast = 1) { db.tagsQueries.tags().executeAsList() } + } + + @Test + fun `Get tags with sources update and db 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 tags + coEvery { db.tagsQueries.tags().executeAsList() } returns tagsDB + every { appSettingsService.isUpdateSourcesEnabled() } returns false + every { appSettingsService.isItemCachingEnabled() } returns false + + val repository = Repository(api, appSettingsService, connectivityStatus, db) + var testTags: List? = null + runBlocking { + testTags = repository.getTags() + } + + assertSame(null, testTags) + coVerify(exactly = 0) { api.tags() } + verify(exactly = 0) { db.tagsQueries.tags().executeAsList() } + } + + @Test + fun `Get tags without connection`() { + 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 tags + coEvery { db.tagsQueries.tags().executeAsList() } returns tagsDB + every { connectivityStatus.isNetworkConnected } returns MutableStateFlow(false) + + val repository = Repository(api, appSettingsService, connectivityStatus, db) + var testTags: List? = null + runBlocking { + testTags = repository.getTags() + } + + assertNotSame(tags, testTags) + assertSame(tagsDB.first().name, testTags?.first()?.tag) + coVerify(exactly = 0) { api.tags() } + verify(atLeast = 1) { db.tagsQueries.tags().executeAsList() } + } + + @Test + fun `Get tags without connection 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 tags + coEvery { db.tagsQueries.tags().executeAsList() } returns tagsDB + every { connectivityStatus.isNetworkConnected } returns MutableStateFlow(false) + every { appSettingsService.isItemCachingEnabled() } returns false + + val repository = Repository(api, appSettingsService, connectivityStatus, db) + var testTags: List? = null + runBlocking { + testTags = repository.getTags() + } + + assertSame(null, testTags) + coVerify(exactly = 0) { api.tags() } + verify(exactly = 0) { db.tagsQueries.tags().executeAsList() } + } } fun generateTestDBItems(item : FakeItemParameters = FakeItemParameters()) : List {