Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Properly updating draft message [WPB-14271] #3104

Merged
merged 1 commit into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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"))
Expand All @@ -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
Expand Down Expand Up @@ -207,13 +301,27 @@ class MessageDraftDAOTest : BaseDatabaseTest() {
senderUserId = userEntity1.id
)
)
messageDAO.insertOrIgnoreMessage(
newRegularMessageEntity(
id = "newEditMessageId",
conversationId = conversationEntity1.id,
senderUserId = userEntity1.id
)
)
messageDAO.insertOrIgnoreMessage(
newRegularMessageEntity(
id = "quotedMessageId",
conversationId = conversationEntity1.id,
senderUserId = userEntity1.id
)
)
messageDAO.insertOrIgnoreMessage(
newRegularMessageEntity(
id = "newQuotedMessageId",
conversationId = conversationEntity1.id,
senderUserId = userEntity1.id
)
)
}

companion object {
Expand Down
Loading