Repository Unit Tests #50
@ -132,12 +132,13 @@ class Repository(private val api: SelfossApi, private val appSettingsService: Ap
|
||||
badgeStarred = response.data.starred
|
||||
success = true
|
||||
}
|
||||
} else {
|
||||
} else if (appSettingsService.isItemCachingEnabled()) {
|
||||
// TODO: do this differently, because it's not efficient
|
||||
val dbItems = getDBItems()
|
||||
badgeUnread = dbItems.filter { item -> item.unread }.size
|
||||
badgeStarred = dbItems.filter { item -> item.starred }.size
|
||||
badgeAll = items.size
|
||||
badgeAll = dbItems.size
|
||||
success = true
|
||||
}
|
||||
return success
|
||||
}
|
||||
|
@ -31,7 +31,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 { appSettingsService.isUpdateSourcesEnabled() } returns false
|
||||
|
||||
every { connectivityStatus.isNetworkConnected } returns MutableStateFlow(true)
|
||||
|
||||
@ -258,24 +258,19 @@ class RepositoryTest() {
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `Reload badges with items caching`() {
|
||||
fun `Reload badges without connection`() {
|
||||
every { connectivityStatus.isNetworkConnected } returns MutableStateFlow(false)
|
||||
every { appSettingsService.isItemCachingEnabled() } returns true
|
||||
every { db.itemsQueries.items().executeAsList() } returns generateTestDBItems()
|
||||
AmineB marked this conversation as resolved
Outdated
|
||||
|
||||
val itemParameter = FakeItemParameters()
|
||||
itemParameter.starred = true
|
||||
itemParameter.unread = false
|
||||
|
||||
var success = false
|
||||
AmineB marked this conversation as resolved
AmineB
commented
Be cause of the line 40 of the setup function you have an unread/starred item in your DB. Be cause of the line 40 of the setup function you have an unread/starred item in your DB.
davidoskky
commented
Right, I definitely have to define these things within the functions to avoid these kinds of errors. Right, I definitely have to define these things within the functions to avoid these kinds of errors.
|
||||
|
||||
val repository = Repository(api, appSettingsService, connectivityStatus, db)
|
||||
repository.items = ArrayList(generateTestApiItem(itemParameter))
|
||||
runBlocking {
|
||||
success = repository.reloadBadges()
|
||||
}
|
||||
|
||||
assertSame(true, success)
|
||||
assertTrue(success)
|
||||
assertSame(1, repository.badgeAll)
|
||||
assertSame(1, repository.badgeUnread)
|
||||
assertSame(1, repository.badgeStarred)
|
||||
@ -284,23 +279,19 @@ class RepositoryTest() {
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `Reload badges without items caching`() {
|
||||
fun `Reload badges without connection and items caching disabled`() {
|
||||
every { connectivityStatus.isNetworkConnected } returns MutableStateFlow(false)
|
||||
every { appSettingsService.isItemCachingEnabled() } returns false
|
||||
|
||||
val itemParameter = FakeItemParameters()
|
||||
itemParameter.starred = true
|
||||
itemParameter.unread = false
|
||||
every { appSettingsService.isUpdateSourcesEnabled() } returns true
|
||||
|
||||
var success = false
|
||||
|
||||
val repository = Repository(api, appSettingsService, connectivityStatus, db)
|
||||
repository.items = ArrayList(generateTestApiItem(itemParameter))
|
||||
runBlocking {
|
||||
AmineB marked this conversation as resolved
AmineB
commented
Be cause of the line 40 of the Be cause of the line 40 of the `setup` function you have an unread/starred item in your DB.
davidoskky
commented
Yes, this is intended. Yes, this is intended.
Since items caching is disabled I would expect the function not to access items in the database.
Maybe it's better if I define that item within this function to avoid ambiguity.
AmineB
commented
I answerd this in another comment. The DB should be empty when the setting is disabled, so this should never happen. > Since items caching is disabled I would expect the function not to access items in the database.
I answerd this in another comment.
The DB should be empty when the setting is disabled, so this should never happen.
davidoskky
commented
Even if the DB is empty, there is no reason to call it if the setting is disabled. Even if the DB is empty, there is no reason to call it if the setting is disabled.
I placed something in the DB just to test that it was not called.
AmineB
commented
I find it useless to test if caching is enabled to check if there is something in the DB. I find it useless to test if caching is enabled to check if there is something in the DB.
davidoskky
commented
It should be quicker and prevents errors in case some problems with DB clearing happens. It should be quicker and prevents errors in case some problems with DB clearing happens.
AmineB
commented
Still, db cleaning should be done on setting change. Still, db cleaning should be done on setting change.
|
||||
success = repository.reloadBadges()
|
||||
}
|
||||
|
||||
assertSame(false, success)
|
||||
assertFalse(success)
|
||||
assertSame(0, repository.badgeAll)
|
||||
assertSame(0, repository.badgeUnread)
|
||||
assertSame(0, repository.badgeStarred)
|
||||
|
In
reloadBadges()
, there is a missingsuccess = true
in the else statement.