Repository Unit Tests #50

Merged
AmineB merged 38 commits from davidoskky/ReaderForSelfoss-multiplatform:repository_tests into master 2022-09-30 11:31:55 +00:00
2 changed files with 9 additions and 17 deletions
Showing only changes of commit 2968aee309 - Show all commits

View File

@ -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
}

View File

@ -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

In reloadBadges(), there is a missing success = true in the else statement.

In `reloadBadges()`, there is a missing `success = true` in the else statement.
val itemParameter = FakeItemParameters()
itemParameter.starred = true
itemParameter.unread = false
var success = false
AmineB marked this conversation as resolved
Review

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.
Review

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
Review

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.
Review

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.

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.
Review

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.

> 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.
Review

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.

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.
Review

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.
Review

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.
Review

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)