Add tests

This commit is contained in:
Florian Renaud 2023-05-03 17:35:14 +02:00
parent f99ea2f297
commit 73473bc2d2
2 changed files with 33 additions and 13 deletions

View file

@ -47,18 +47,7 @@ internal class AllMatrixUsersDataSourceTest {
val dataSource = AllMatrixUsersDataSource(matrixClient)
val results = dataSource.search("test")
Truth.assertThat(results).containsExactly(
MatrixUser(
userId = A_USER_ID,
displayName = A_USER_NAME,
avatarUrl = AN_AVATAR_URL
),
MatrixUser(
userId = A_USER_ID_2,
displayName = A_USER_NAME,
avatarUrl = AN_AVATAR_URL
),
)
Truth.assertThat(results).containsExactly(aMatrixUserProfile(), aMatrixUserProfile(userId = A_USER_ID_2))
}
@Test
@ -74,6 +63,32 @@ internal class AllMatrixUsersDataSourceTest {
Truth.assertThat(results).isEmpty()
}
@Test
fun `get profile - returns user on success`() = runTest {
val matrixClient = FakeMatrixClient()
matrixClient.givenGetProfileResult(
userId = A_USER_ID,
result = Result.success(aMatrixUserProfile())
)
val dataSource = AllMatrixUsersDataSource(matrixClient)
val result = dataSource.getProfile(A_USER_ID)
Truth.assertThat(result).isEqualTo(aMatrixUserProfile())
}
@Test
fun `get profile - returns null on error`() = runTest {
val matrixClient = FakeMatrixClient()
matrixClient.givenGetProfileResult(
userId = A_USER_ID,
result = Result.failure(Throwable("Ruhroh"))
)
val dataSource = AllMatrixUsersDataSource(matrixClient)
val result = dataSource.getProfile(A_USER_ID)
Truth.assertThat(result).isNull()
}
private fun aMatrixUserProfile(
userId: UserId = A_USER_ID,
displayName: String = A_USER_NAME,

View file

@ -58,6 +58,7 @@ class FakeMatrixClient(
private var logoutFailure: Throwable? = null
private val getRoomResults = mutableMapOf<RoomId, MatrixRoom>()
private val searchUserResults = mutableMapOf<String, Result<MatrixSearchUserResults>>()
private val getProfileResults = mutableMapOf<UserId, Result<MatrixUser>>()
override fun getRoom(roomId: RoomId): MatrixRoom? {
return getRoomResults[roomId]
@ -87,7 +88,7 @@ class FakeMatrixClient(
}
override suspend fun getProfile(userId: UserId): Result<MatrixUser> {
return Result.success(MatrixUser(userId))
return getProfileResults[userId] ?: Result.failure(IllegalStateException("No profile found for $userId"))
}
override fun startSync() = Unit
@ -174,4 +175,8 @@ class FakeMatrixClient(
fun givenSearchUsersResult(searchTerm: String, result: Result<MatrixSearchUserResults>) {
searchUserResults[searchTerm] = result
}
fun givenGetProfileResult(userId: UserId, result: Result<MatrixUser>) {
getProfileResults[userId] = result
}
}