Fix tags tests

This commit is contained in:
davidoskky 2022-09-28 18:22:06 +02:00
parent 2968aee309
commit 219cae5d74
2 changed files with 36 additions and 5 deletions

View File

@ -146,12 +146,14 @@ class Repository(private val api: SelfossApi, private val appSettingsService: Ap
suspend fun getTags(): List<SelfossModel.Tag> { suspend fun getTags(): List<SelfossModel.Tag> {
return if (isNetworkAvailable()) { return if (isNetworkAvailable()) {
val apiTags = api.tags() 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) resetDBTagsWithData(apiTags.data)
} }
apiTags.data ?: emptyList() apiTags.data ?: emptyList()
} else { } else if (appSettingsService.isItemCachingEnabled() || appSettingsService.isUpdateSourcesEnabled()) {
getDBTags().map { it.toView() } getDBTags().map { it.toView() }
} else {
emptyList()
} }
} }

View File

@ -303,8 +303,11 @@ class RepositoryTest() {
fun `Get tags`() { fun `Get tags`() {
val tags = listOf(SelfossModel.Tag("test", "red", 6), val tags = listOf(SelfossModel.Tag("test", "red", 6),
SelfossModel.Tag("second", "yellow", 0)) 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 { api.tags() } returns SelfossModel.StatusAndData(success = true, data = tags)
coEvery { db.tagsQueries.tags().executeAsList() } returns tagsDB
val repository = Repository(api, appSettingsService, connectivityStatus, db) val repository = Repository(api, appSettingsService, connectivityStatus, db)
var testTags: List<SelfossModel.Tag>? = null var testTags: List<SelfossModel.Tag>? = null
@ -313,7 +316,8 @@ class RepositoryTest() {
} }
assertSame(tags, testTags) assertSame(tags, testTags)
verify(exactly = 0) { db.tagsQueries.tags().executeAsList() } assertNotSame(tagsDB.map { it.toView() }, testTags)
coVerify(exactly = 1) { api.tags() }
} }
@Test @Test
@ -331,6 +335,7 @@ class RepositoryTest() {
var testTags: List<SelfossModel.Tag> = emptyList() var testTags: List<SelfossModel.Tag> = emptyList()
runBlocking { runBlocking {
testTags = repository.getTags() testTags = repository.getTags()
// Tags will be fetched from the database on the second call, thus testTags != tags
testTags = repository.getTags() testTags = repository.getTags()
} }
@ -358,8 +363,8 @@ class RepositoryTest() {
testTags = repository.getTags() testTags = repository.getTags()
} }
assertSame(emptyList(), testTags) assertSame(tags, testTags)
coVerify(exactly = 0) { api.tags() } coVerify(exactly = 1) { api.tags() }
verify(exactly = 0) { db.tagsQueries.tags().executeAsList() } verify(exactly = 0) { db.tagsQueries.tags().executeAsList() }
} }
@ -433,6 +438,30 @@ class RepositoryTest() {
verify(atLeast = 1) { db.tagsQueries.tags().executeAsList() } 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<SelfossModel.Tag> = emptyList()
runBlocking {
testTags = repository.getTags()
}
assertSame(emptyList(), testTags)
coVerify(exactly = 0) { api.tags() }
verify(exactly = 0) { db.tagsQueries.tags().executeAsList() }
}
@Test @Test
fun `get sources`() { fun `get sources`() {
val sources = arrayListOf(SelfossModel.Source(1, "First source", listOf("Test", "second"),"spouts\\rss\\fulltextrss", "", "d8c92cdb1ef119ea85c4b9205c879ca7.png"), val sources = arrayListOf(SelfossModel.Source(1, "First source", listOf("Test", "second"),"spouts\\rss\\fulltextrss", "", "d8c92cdb1ef119ea85c4b9205c879ca7.png"),