Fix GetFoldersWithFeeds test

This commit is contained in:
Shinokuni 2024-02-18 19:04:10 +01:00
parent 425d20741b
commit ad13ba99e0
3 changed files with 28 additions and 14 deletions

View File

@ -10,7 +10,7 @@ import com.readrops.db.entities.Folder
import com.readrops.db.entities.Item import com.readrops.db.entities.Item
import com.readrops.db.entities.account.Account import com.readrops.db.entities.account.Account
import com.readrops.db.entities.account.AccountType import com.readrops.db.entities.account.AccountType
import kotlinx.coroutines.test.StandardTestDispatcher import kotlinx.coroutines.launch
import kotlinx.coroutines.test.runTest import kotlinx.coroutines.test.runTest
import org.joda.time.LocalDateTime import org.joda.time.LocalDateTime
import org.junit.Before import org.junit.Before
@ -31,34 +31,48 @@ class GetFoldersWithFeedsTest {
runTest { runTest {
account.id = database.newAccountDao().insert(account).toInt() account.id = database.newAccountDao().insert(account).toInt()
// inserting 3 folders
repeat(3) { time -> repeat(3) { time ->
database.newFolderDao().insert(Folder(name = "Folder $time", accountId = account.id)) database.newFolderDao()
.insert(Folder(name = "Folder $time", accountId = account.id))
} }
// inserting 2 feeds, not linked to any folder
repeat(2) { time -> repeat(2) { time ->
database.newFeedDao().insert(Feed(name = "Feed $time", accountId = account.id)) database.newFeedDao().insert(Feed(name = "Feed $time", accountId = account.id))
} }
// inserting 2 feeds linked to first folder (Folder 0)
repeat(2) { time -> repeat(2) { time ->
database.newFeedDao().insert(Feed(name = "Feed ${time+2}", folderId = 1, accountId = account.id)) database.newFeedDao()
.insert(Feed(name = "Feed ${time + 2}", folderId = 1, accountId = account.id))
} }
// inserting 3 items linked to first feed (Feed 0)
repeat(3) { time -> repeat(3) { time ->
database.newItemDao().insert(Item(title = "Item $time", feedId = 1, pubDate = LocalDateTime.now())) database.newItemDao()
.insert(Item(title = "Item $time", feedId = 1, pubDate = LocalDateTime.now()))
} }
} }
} }
@Test @Test
fun getFoldersWithFeedsTest() = runTest { fun getFoldersWithFeedsTest() = runTest {
getFoldersWithFeeds = GetFoldersWithFeeds(database, StandardTestDispatcher(testScheduler)) getFoldersWithFeeds = GetFoldersWithFeeds(database)
val foldersAndFeeds = getFoldersWithFeeds.get(account.id) val job = launch {
getFoldersWithFeeds.get(account.id)
.collect { foldersAndFeeds ->
assertTrue { foldersAndFeeds.size == 4 } assertTrue { foldersAndFeeds.size == 4 }
assertTrue { foldersAndFeeds.entries.first().value.size == 2 } assertTrue { foldersAndFeeds.entries.first().value.size == 2 }
assertTrue { foldersAndFeeds.entries.last().key == null } assertTrue { foldersAndFeeds.entries.last().key == null }
assertTrue { foldersAndFeeds[null]!!.size == 2 } assertTrue { foldersAndFeeds[null]!!.size == 2 }
assertTrue { foldersAndFeeds[null]!!.first().unreadCount == 3 } assertTrue { foldersAndFeeds[null]!!.first().unreadCount == 3 }
}
}
// for an unknown reason, the coroutine must be canceled to stop the test, and I don't really know why
job.cancel()
} }
} }

View File

@ -21,8 +21,8 @@ abstract class NewFeedDao : NewBaseDao<Feed> {
@Query("Select case When :feedUrl In (Select url from Feed Where account_id = :accountId) Then 1 else 0 end") @Query("Select case When :feedUrl In (Select url from Feed Where account_id = :accountId) Then 1 else 0 end")
abstract suspend fun feedExists(feedUrl: String, accountId: Int): Boolean abstract suspend fun feedExists(feedUrl: String, accountId: Int): Boolean
@Query("Select Feed.*, count(*) as unreadCount From Feed Inner Join Item On Feed.id = Item.feed_id " + @Query("Select Feed.*, count(*) as unreadCount From Feed Left Join Item On Feed.id = Item.feed_id " +
"Where Feed.folder_id is Null And Item.read = 0 And Feed.account_id = :accountId Group by Feed.id") "Where Feed.folder_id is Null And (Item.read = 0 OR Item.read is NULL) And Feed.account_id = :accountId Group by Feed.id")
abstract fun selectFeedsWithoutFolder(accountId: Int): Flow<List<FeedWithCount>> abstract fun selectFeedsWithoutFolder(accountId: Int): Flow<List<FeedWithCount>>
@Query("Update Feed set name = :feedName, url = :feedUrl, folder_id = :folderId Where id = :feedId") @Query("Update Feed set name = :feedName, url = :feedUrl, folder_id = :folderId Where id = :feedId")

View File

@ -12,7 +12,7 @@ abstract class NewFolderDao : NewBaseDao<Folder> {
@Query("Select Folder.id As folderId, Folder.name as folderName, Feed.id As feedId, Feed.name AS feedName, " + @Query("Select Folder.id As folderId, Folder.name as folderName, Feed.id As feedId, Feed.name AS feedName, " +
"Feed.icon_url As feedIcon, Feed.url as feedUrl, Feed.siteUrl as feedSiteUrl, count(*) As unreadCount, Folder.account_id as accountId " + "Feed.icon_url As feedIcon, Feed.url as feedUrl, Feed.siteUrl as feedSiteUrl, count(*) As unreadCount, Folder.account_id as accountId " +
"From Folder Left Join Feed On Folder.id = Feed.folder_id Left Join Item On Item.feed_id = Feed.id " + "From Folder Left Join Feed On Folder.id = Feed.folder_id Left Join Item On Item.feed_id = Feed.id " +
"Where Feed.folder_id is NULL OR Feed.folder_id is NOT NULL And Item.read = 0 " + "Where Feed.folder_id is NULL OR Feed.folder_id is NOT NULL And (Item.read = 0 OR Item.read is NULL) " +
"And Feed.account_id = :accountId Group By Feed.id, Folder.id Order By Folder.id") "And Feed.account_id = :accountId Group By Feed.id, Folder.id Order By Folder.id")
abstract fun selectFoldersAndFeeds(accountId: Int): Flow<List<FolderWithFeed>> abstract fun selectFoldersAndFeeds(accountId: Int): Flow<List<FolderWithFeed>>