Skip to content

Commit

Permalink
Added unit test for new DB query
Browse files Browse the repository at this point in the history
  • Loading branch information
borichellow committed Dec 17, 2024
1 parent 9e38f7d commit b4b66eb
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit b4b66eb

Please sign in to comment.