Skip to content

Commit

Permalink
fix: Properly updating draft message [WPB-14271] (#3104)
Browse files Browse the repository at this point in the history
  • Loading branch information
m-zagorski authored and github-actions[bot] committed Nov 14, 2024
1 parent f883583 commit 1c145e1
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 3 deletions.
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

0 comments on commit 1c145e1

Please sign in to comment.