From b4b66eb70a59f7f1c8178d75498d14a2cc4a0db8 Mon Sep 17 00:00:00 2001 From: Boris Safonov Date: Tue, 17 Dec 2024 21:45:48 +0200 Subject: [PATCH] Added unit test for new DB query --- .../GetSenderNameByMessageIdUseCase.kt | 4 + .../com/wire/kalium/persistence/Users.sq | 2 +- .../persistence/dao/message/MessageDAOTest.kt | 90 +++++++++++++++++++ 3 files changed, 95 insertions(+), 1 deletion(-) diff --git a/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/message/GetSenderNameByMessageIdUseCase.kt b/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/message/GetSenderNameByMessageIdUseCase.kt index 33b57989039..276798ea6d3 100644 --- a/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/message/GetSenderNameByMessageIdUseCase.kt +++ b/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/message/GetSenderNameByMessageIdUseCase.kt @@ -26,6 +26,10 @@ import com.wire.kalium.util.KaliumDispatcher import com.wire.kalium.util.KaliumDispatcherImpl import kotlinx.coroutines.withContext +/** + * Provides a way to get a name of user that sent a message + * using its [ConversationId] and message ID coordinates. + */ class GetSenderNameByMessageIdUseCase internal constructor( private val messageRepository: MessageRepository, private val dispatchers: KaliumDispatcher = KaliumDispatcherImpl diff --git a/persistence/src/commonMain/db_user/com/wire/kalium/persistence/Users.sq b/persistence/src/commonMain/db_user/com/wire/kalium/persistence/Users.sq index 06b200dc684..1a36f2801fa 100644 --- a/persistence/src/commonMain/db_user/com/wire/kalium/persistence/Users.sq +++ b/persistence/src/commonMain/db_user/com/wire/kalium/persistence/Users.sq @@ -279,4 +279,4 @@ UPDATE User SET team = ? WHERE qualified_id = ?; selectNameByMessageId: SELECT name FROM User -WHERE qualified_id = (SELECT Message.sender_user_id FROM Message WHERE Message.id = :messageId AND Message.conversation_id = :conversationId LIMIT 1); +WHERE qualified_id = (SELECT Message.sender_user_id FROM Message WHERE Message.id = :messageId AND Message.conversation_id = :conversationId); diff --git a/persistence/src/commonTest/kotlin/com/wire/kalium/persistence/dao/message/MessageDAOTest.kt b/persistence/src/commonTest/kotlin/com/wire/kalium/persistence/dao/message/MessageDAOTest.kt index da4724ba739..4c346bd2fe0 100644 --- a/persistence/src/commonTest/kotlin/com/wire/kalium/persistence/dao/message/MessageDAOTest.kt +++ b/persistence/src/commonTest/kotlin/com/wire/kalium/persistence/dao/message/MessageDAOTest.kt @@ -2344,6 +2344,96 @@ class MessageDAOTest : BaseDatabaseTest() { assertEquals(messages.size, assetStatuses.size) } + @Test + fun givenMessagesAndUsersAreInserted_whenGettingSenderNameByMessageId_thenOnlyRelevantNameReturned() = runTest { + insertInitialData() + + val userInQuestion = userDetailsEntity1 + val otherUser = userDetailsEntity2 + + val expectedMessages = listOf( + newRegularMessageEntity( + "1", + conversationId = conversationEntity1.id, + senderUserId = userInQuestion.id, + status = MessageEntity.Status.PENDING, + senderName = userInQuestion.name!!, + sender = userInQuestion + ), + newRegularMessageEntity( + "2", + conversationId = conversationEntity1.id, + senderUserId = otherUser.id, + status = MessageEntity.Status.PENDING, + senderName = otherUser.name!!, + sender = otherUser + ) + ) + messageDAO.insertOrIgnoreMessages(expectedMessages) + + val result = messageDAO.getSenderNameById("1", conversationEntity1.id) + + assertEquals(userDetailsEntity1.name, result) + } + + @Test + fun givenMessagesAreInserted_whenGettingSenderNameByMessageId_thenOnlyRelevantNameReturned() = runTest { + insertInitialData() + + val expectedMessages = listOf( + newRegularMessageEntity( + "1", + conversationId = conversationEntity1.id, + senderUserId = userDetailsEntity1.id, + status = MessageEntity.Status.PENDING, + senderName = userDetailsEntity1.name!!, + sender = userDetailsEntity1 + ), + newRegularMessageEntity( + "2", + conversationId = conversationEntity1.id, + senderUserId = userDetailsEntity2.id, + status = MessageEntity.Status.PENDING, + senderName = userDetailsEntity2.name!!, + sender = userDetailsEntity2 + ) + ) + messageDAO.insertOrIgnoreMessages(expectedMessages) + + val result = messageDAO.getSenderNameById("1", conversationEntity1.id) + + assertEquals(userDetailsEntity1.name, result) + } + + @Test + fun givenMessagesAreButNoUserInserted_whenGettingSenderNameByMessageId_thenNullNameReturned() = runTest { + insertInitialData() + + val expectedMessages = listOf( + newRegularMessageEntity( + "1", + conversationId = conversationEntity1.id, + senderUserId = userDetailsEntity1.id.copy(value = "absolutely_another_value"), + status = MessageEntity.Status.PENDING, + senderName = "s", + sender = userDetailsEntity1.copy(name = "s", id = userDetailsEntity1.id.copy(value = "absolutely_another_value")) + ), + newRegularMessageEntity( + "2", + conversationId = conversationEntity1.id, + senderUserId = userDetailsEntity2.id, + status = MessageEntity.Status.PENDING, + senderName = userDetailsEntity2.name!!, + sender = userDetailsEntity2 + ) + ) + messageDAO.insertOrIgnoreMessages(expectedMessages) + + val result = messageDAO.getSenderNameById("1", conversationEntity1.id) + + assertEquals(null, result) + } + private suspend fun insertInitialData() { userDAO.upsertUsers(listOf(userEntity1, userEntity2)) conversationDAO.insertConversation(