From ad13ba99e026a933f9bbb44ec67037226489ed1d Mon Sep 17 00:00:00 2001 From: Shinokuni Date: Sun, 18 Feb 2024 19:04:10 +0100 Subject: [PATCH] Fix GetFoldersWithFeeds test --- .../app/compose/GetFoldersWithFeedsTest.kt | 36 +++++++++++++------ .../com/readrops/db/dao/newdao/NewFeedDao.kt | 4 +-- .../readrops/db/dao/newdao/NewFolderDao.kt | 2 +- 3 files changed, 28 insertions(+), 14 deletions(-) diff --git a/appcompose/src/androidTest/java/com/readrops/app/compose/GetFoldersWithFeedsTest.kt b/appcompose/src/androidTest/java/com/readrops/app/compose/GetFoldersWithFeedsTest.kt index b0b5ac2d..0cf78a58 100644 --- a/appcompose/src/androidTest/java/com/readrops/app/compose/GetFoldersWithFeedsTest.kt +++ b/appcompose/src/androidTest/java/com/readrops/app/compose/GetFoldersWithFeedsTest.kt @@ -10,7 +10,7 @@ import com.readrops.db.entities.Folder import com.readrops.db.entities.Item import com.readrops.db.entities.account.Account import com.readrops.db.entities.account.AccountType -import kotlinx.coroutines.test.StandardTestDispatcher +import kotlinx.coroutines.launch import kotlinx.coroutines.test.runTest import org.joda.time.LocalDateTime import org.junit.Before @@ -31,34 +31,48 @@ class GetFoldersWithFeedsTest { runTest { account.id = database.newAccountDao().insert(account).toInt() + // inserting 3 folders 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 -> database.newFeedDao().insert(Feed(name = "Feed $time", accountId = account.id)) } + // inserting 2 feeds linked to first folder (Folder 0) 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 -> - 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 fun getFoldersWithFeedsTest() = runTest { - getFoldersWithFeeds = GetFoldersWithFeeds(database, StandardTestDispatcher(testScheduler)) - val foldersAndFeeds = getFoldersWithFeeds.get(account.id) + getFoldersWithFeeds = GetFoldersWithFeeds(database) + val job = launch { + getFoldersWithFeeds.get(account.id) + .collect { foldersAndFeeds -> - assertTrue { foldersAndFeeds.size == 4 } - assertTrue { foldersAndFeeds.entries.first().value.size == 2 } - assertTrue { foldersAndFeeds.entries.last().key == null } - assertTrue { foldersAndFeeds[null]!!.size == 2 } - assertTrue { foldersAndFeeds[null]!!.first().unreadCount == 3 } + assertTrue { foldersAndFeeds.size == 4 } + assertTrue { foldersAndFeeds.entries.first().value.size == 2 } + assertTrue { foldersAndFeeds.entries.last().key == null } + assertTrue { foldersAndFeeds[null]!!.size == 2 } + 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() } } \ No newline at end of file diff --git a/db/src/main/java/com/readrops/db/dao/newdao/NewFeedDao.kt b/db/src/main/java/com/readrops/db/dao/newdao/NewFeedDao.kt index bb2fb36f..f5aa95cb 100644 --- a/db/src/main/java/com/readrops/db/dao/newdao/NewFeedDao.kt +++ b/db/src/main/java/com/readrops/db/dao/newdao/NewFeedDao.kt @@ -21,8 +21,8 @@ abstract class NewFeedDao : NewBaseDao { @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 - @Query("Select Feed.*, count(*) as unreadCount From Feed Inner 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") + @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 OR Item.read is NULL) And Feed.account_id = :accountId Group by Feed.id") abstract fun selectFeedsWithoutFolder(accountId: Int): Flow> @Query("Update Feed set name = :feedName, url = :feedUrl, folder_id = :folderId Where id = :feedId") diff --git a/db/src/main/java/com/readrops/db/dao/newdao/NewFolderDao.kt b/db/src/main/java/com/readrops/db/dao/newdao/NewFolderDao.kt index 44a0211d..4968b6a5 100644 --- a/db/src/main/java/com/readrops/db/dao/newdao/NewFolderDao.kt +++ b/db/src/main/java/com/readrops/db/dao/newdao/NewFolderDao.kt @@ -12,7 +12,7 @@ abstract class NewFolderDao : NewBaseDao { @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 " + "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") abstract fun selectFoldersAndFeeds(accountId: Int): Flow>