From 1c145e125041ce2046af20c1dc49cf891773a2f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Zag=C3=B3rski?= Date: Thu, 14 Nov 2024 10:03:47 +0100 Subject: [PATCH] fix: Properly updating draft message [WPB-14271] (#3104) --- .../wire/kalium/persistence/MessageDrafts.sq | 4 +- .../dao/message/draft/MessageDraftDAOTest.kt | 110 +++++++++++++++++- 2 files changed, 111 insertions(+), 3 deletions(-) diff --git a/persistence/src/commonMain/db_user/com/wire/kalium/persistence/MessageDrafts.sq b/persistence/src/commonMain/db_user/com/wire/kalium/persistence/MessageDrafts.sq index f5f40c82474..1ec7f1e3097 100644 --- a/persistence/src/commonMain/db_user/com/wire/kalium/persistence/MessageDrafts.sq +++ b/persistence/src/commonMain/db_user/com/wire/kalium/persistence/MessageDrafts.sq @@ -28,8 +28,8 @@ ON CONFLICT(conversation_id) DO UPDATE SET mention_list = excluded.mention_list WHERE -- execute the update only if any of the fields changed MessageDraft.text != excluded.text - OR MessageDraft.edit_message_id != excluded.edit_message_id - OR MessageDraft.quoted_message_id != excluded.quoted_message_id + OR MessageDraft.edit_message_id IS NOT excluded.edit_message_id + OR MessageDraft.quoted_message_id IS NOT excluded.quoted_message_id OR MessageDraft.mention_list != excluded.mention_list; getDraft: diff --git a/persistence/src/commonTest/kotlin/com/wire/kalium/persistence/dao/message/draft/MessageDraftDAOTest.kt b/persistence/src/commonTest/kotlin/com/wire/kalium/persistence/dao/message/draft/MessageDraftDAOTest.kt index f5247aac31a..5484ed6a4de 100644 --- a/persistence/src/commonTest/kotlin/com/wire/kalium/persistence/dao/message/draft/MessageDraftDAOTest.kt +++ b/persistence/src/commonTest/kotlin/com/wire/kalium/persistence/dao/message/draft/MessageDraftDAOTest.kt @@ -33,6 +33,8 @@ import kotlinx.coroutines.test.runTest import kotlin.test.BeforeTest import kotlin.test.Test import kotlin.test.assertEquals +import kotlin.test.assertNotNull +import kotlin.test.assertNull @Suppress("LargeClass") class MessageDraftDAOTest : BaseDatabaseTest() { @@ -69,7 +71,7 @@ class MessageDraftDAOTest : BaseDatabaseTest() { } @Test - fun givenAlreadyExistingMessageDraft_whenUpserting_thenItShouldBeProperlyUpdatedInDb() = runTest { + fun givenAlreadyExistingMessageDraft_whenUpsertingTextChange_thenItShouldBeProperlyUpdatedInDb() = runTest { // Given insertInitialData() messageDraftDAO.upsertMessageDraft(MESSAGE_DRAFT.copy(text = "@John I need")) @@ -82,6 +84,98 @@ class MessageDraftDAOTest : BaseDatabaseTest() { assertEquals(MESSAGE_DRAFT, result) } + @Test + fun givenAlreadyExistingMessageDraft_whenUpsertingDifferentQuotedMessageId_thenItShouldBeProperlyUpdatedInDb() = runTest { + // given + val newQuotedMessageId = "newQuotedMessageId" + insertInitialData() + messageDraftDAO.upsertMessageDraft(MESSAGE_DRAFT) + + // when + messageDraftDAO.upsertMessageDraft(MESSAGE_DRAFT.copy(quotedMessageId = newQuotedMessageId)) + + // then + val result = messageDraftDAO.getMessageDraft(conversationEntity1.id) + assertNotNull(result) + assertEquals(newQuotedMessageId, result.quotedMessageId) + } + + @Test + fun givenAlreadyExistingMessageDraft_whenUpsertingNullQuotedMessageId_thenItShouldBeProperlyUpdatedInDb() = runTest { + // given + insertInitialData() + messageDraftDAO.upsertMessageDraft(MESSAGE_DRAFT) + + // when + messageDraftDAO.upsertMessageDraft(MESSAGE_DRAFT.copy(quotedMessageId = null)) + + // then + val result = messageDraftDAO.getMessageDraft(conversationEntity1.id) + assertNotNull(result) + assertNull(result.quotedMessageId) + } + + @Test + fun givenAlreadyExistingMessageDraftWithoutQuotedMessageId_whenUpsertingQuotedMessageId_thenItShouldBeProperlyUpdatedInDb() = runTest{ + // given + insertInitialData() + messageDraftDAO.upsertMessageDraft(MESSAGE_DRAFT.copy(quotedMessageId = null)) + + // when + messageDraftDAO.upsertMessageDraft(MESSAGE_DRAFT) + + // then + val result = messageDraftDAO.getMessageDraft(conversationEntity1.id) + assertNotNull(result) + assertEquals(MESSAGE_DRAFT.quotedMessageId, result.quotedMessageId) + } + + @Test + fun givenAlreadyExistingMessageDraft_whenUpsertingDifferentEditMessageId_thenItShouldBeProperlyUpdatedInDb() = runTest { + // given + val newEditMessageId = "newEditMessageId" + insertInitialData() + messageDraftDAO.upsertMessageDraft(MESSAGE_DRAFT) + + // when + messageDraftDAO.upsertMessageDraft(MESSAGE_DRAFT.copy(editMessageId = newEditMessageId)) + + // then + val result = messageDraftDAO.getMessageDraft(conversationEntity1.id) + assertNotNull(result) + assertEquals(newEditMessageId, result.editMessageId) + } + + @Test + fun givenAlreadyExistingMessageDraft_whenUpsertingNullEditMessageId_thenItShouldBeProperlyUpdatedInDb() = runTest { + // given + insertInitialData() + messageDraftDAO.upsertMessageDraft(MESSAGE_DRAFT) + + // when + messageDraftDAO.upsertMessageDraft(MESSAGE_DRAFT.copy(editMessageId = null)) + + // then + val result = messageDraftDAO.getMessageDraft(conversationEntity1.id) + assertNotNull(result) + assertNull(result.editMessageId) + } + + @Test + fun givenAlreadyExistingMessageDraft_whenUpsertingEmptyMentionList_thenItShouldBeProperlyUpdatedInDb() = runTest { + // given + insertInitialData() + messageDraftDAO.upsertMessageDraft(MESSAGE_DRAFT) + + // when + messageDraftDAO.upsertMessageDraft(MESSAGE_DRAFT.copy(selectedMentionList = emptyList())) + + // then + val result = messageDraftDAO.getMessageDraft(conversationEntity1.id) + assertNotNull(result) + assertEquals(emptyList(), result.selectedMentionList) + } + @Test fun givenAlreadyExistingMessageDraft_whenDeletingIt_thenItShouldBeProperlyRemovedInDb() = runTest { // Given @@ -207,6 +301,13 @@ class MessageDraftDAOTest : BaseDatabaseTest() { senderUserId = userEntity1.id ) ) + messageDAO.insertOrIgnoreMessage( + newRegularMessageEntity( + id = "newEditMessageId", + conversationId = conversationEntity1.id, + senderUserId = userEntity1.id + ) + ) messageDAO.insertOrIgnoreMessage( newRegularMessageEntity( id = "quotedMessageId", @@ -214,6 +315,13 @@ class MessageDraftDAOTest : BaseDatabaseTest() { senderUserId = userEntity1.id ) ) + messageDAO.insertOrIgnoreMessage( + newRegularMessageEntity( + id = "newQuotedMessageId", + conversationId = conversationEntity1.id, + senderUserId = userEntity1.id + ) + ) } companion object {