Skip to content

Commit

Permalink
test: improve contracts when asserting either result (#2364)
Browse files Browse the repository at this point in the history
* refactor: improve contracts when asserting either result

* chore: remove duplicated lines

* chore: cleanup imports
  • Loading branch information
vitorhugods authored Jan 14, 2024
1 parent e9ab9c6 commit 07d38f1
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ import io.mockative.mock
import io.mockative.once
import io.mockative.thenDoNothing
import io.mockative.verify
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.test.runTest
import okio.Buffer
Expand Down Expand Up @@ -253,7 +252,7 @@ class AssetRepositoryTest {

// Then
with(arrangement) {
assertTrue(result is Either.Right)
result.shouldSucceed()
val expectedPath = fakeKaliumFileSystem.providePersistentAssetPath("${assetKey.value}.${assetName.fileExtension()}")
val realPath = result.value
assertEquals(expectedPath, realPath)
Expand Down Expand Up @@ -300,7 +299,7 @@ class AssetRepositoryTest {

// Then
with(arrangement) {
assertTrue(result is Either.Left)
result.shouldFail()
assertIs<StorageFailure.DataNotFound>(result.value)
verify(assetDAO).suspendFunction(assetDAO::getAssetByKey)
.with(eq(assetKey.value))
Expand Down Expand Up @@ -346,7 +345,7 @@ class AssetRepositoryTest {

// Then
with(arrangement) {
assertTrue(result is Either.Right)
result.shouldSucceed()
assertEquals(assetPath, result.value)
verify(assetDAO).suspendFunction(assetDAO::getAssetByKey)
.with(eq(assetKey.value))
Expand Down Expand Up @@ -397,7 +396,7 @@ class AssetRepositoryTest {

// Then
with(arrangement) {
assertTrue(result is Either.Left)
result.shouldFail()
assertTrue(result.value is EncryptionFailure.WrongAssetHash)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ import io.mockative.*
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.runTest
import kotlin.test.Test
import kotlin.test.assertTrue

@ExperimentalCoroutinesApi
class EnrollE2EICertificateUseCaseTest {
Expand All @@ -52,7 +51,6 @@ class EnrollE2EICertificateUseCaseTest {

// then
result.shouldFail()
assertTrue(result is Either.Left)

verify(arrangement.e2EIRepository)
.function(arrangement.e2EIRepository::loadACMEDirectories)
Expand Down Expand Up @@ -130,7 +128,6 @@ class EnrollE2EICertificateUseCaseTest {

// then
result.shouldFail()
assertTrue(result is Either.Left)

verify(arrangement.e2EIRepository)
.function(arrangement.e2EIRepository::loadACMEDirectories)
Expand Down Expand Up @@ -210,7 +207,6 @@ class EnrollE2EICertificateUseCaseTest {

// then
result.shouldFail()
assertTrue(result is Either.Left)

verify(arrangement.e2EIRepository)
.function(arrangement.e2EIRepository::loadACMEDirectories)
Expand Down Expand Up @@ -292,7 +288,6 @@ class EnrollE2EICertificateUseCaseTest {

// then
result.shouldFail()
assertTrue(result is Either.Left)

verify(arrangement.e2EIRepository)
.function(arrangement.e2EIRepository::loadACMEDirectories)
Expand Down Expand Up @@ -376,7 +371,6 @@ class EnrollE2EICertificateUseCaseTest {

// then
result.shouldFail()
assertTrue(result is Either.Left)

verify(arrangement.e2EIRepository)
.function(arrangement.e2EIRepository::loadACMEDirectories)
Expand Down Expand Up @@ -462,7 +456,6 @@ class EnrollE2EICertificateUseCaseTest {

// then
result.shouldFail()
assertTrue(result is Either.Left)

verify(arrangement.e2EIRepository)
.function(arrangement.e2EIRepository::getWireNonce)
Expand Down Expand Up @@ -529,7 +522,6 @@ class EnrollE2EICertificateUseCaseTest {

// then
result.shouldFail()
assertTrue(result is Either.Left)

verify(arrangement.e2EIRepository)
.function(arrangement.e2EIRepository::getWireNonce)
Expand Down Expand Up @@ -599,7 +591,6 @@ class EnrollE2EICertificateUseCaseTest {

// then
result.shouldFail()
assertTrue(result is Either.Left)

verify(arrangement.e2EIRepository)
.function(arrangement.e2EIRepository::getWireNonce)
Expand Down Expand Up @@ -670,7 +661,6 @@ class EnrollE2EICertificateUseCaseTest {

// then
result.shouldFail()
assertTrue(result is Either.Left)

verify(arrangement.e2EIRepository)
.function(arrangement.e2EIRepository::getWireNonce)
Expand Down Expand Up @@ -743,7 +733,6 @@ class EnrollE2EICertificateUseCaseTest {

// then
result.shouldFail()
assertTrue(result is Either.Left)

verify(arrangement.e2EIRepository)
.function(arrangement.e2EIRepository::getWireNonce)
Expand Down Expand Up @@ -811,7 +800,6 @@ class EnrollE2EICertificateUseCaseTest {

// then
result.shouldFail()
assertTrue(result is Either.Left)

verify(arrangement.e2EIRepository)
.function(arrangement.e2EIRepository::getWireNonce)
Expand Down Expand Up @@ -878,7 +866,6 @@ class EnrollE2EICertificateUseCaseTest {

// then
result.shouldFail()
assertTrue(result is Either.Left)

verify(arrangement.e2EIRepository)
.function(arrangement.e2EIRepository::getWireNonce)
Expand Down Expand Up @@ -940,7 +927,6 @@ class EnrollE2EICertificateUseCaseTest {

// then
result.shouldFail()
assertTrue(result is Either.Left)

verify(arrangement.e2EIRepository)
.function(arrangement.e2EIRepository::getWireNonce)
Expand Down Expand Up @@ -1007,7 +993,6 @@ class EnrollE2EICertificateUseCaseTest {

// then
result.shouldFail()
assertTrue(result is Either.Left)

verify(arrangement.e2EIRepository)
.function(arrangement.e2EIRepository::getWireNonce)
Expand Down Expand Up @@ -1079,7 +1064,6 @@ class EnrollE2EICertificateUseCaseTest {

// then
result.shouldSucceed()
assertTrue(result is Either.Right)

verify(arrangement.e2EIRepository)
.function(arrangement.e2EIRepository::getWireNonce)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import com.wire.kalium.logic.data.user.UserId
import com.wire.kalium.logic.framework.TestClient
import com.wire.kalium.logic.framework.TestConversation
import com.wire.kalium.logic.framework.TestUser
import com.wire.kalium.logic.functional.Either
import com.wire.kalium.logic.util.shouldSucceed
import com.wire.kalium.persistence.dao.MigrationDAO
import com.wire.kalium.protobuf.encodeToByteArray
import com.wire.kalium.protobuf.messages.GenericMessage
Expand All @@ -32,13 +32,10 @@ import io.mockative.Mock
import io.mockative.any
import io.mockative.given
import io.mockative.mock
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.TestScope
import kotlinx.coroutines.test.runTest
import kotlin.test.Test
import kotlin.test.assertTrue

@OptIn(ExperimentalCoroutinesApi::class)
class PersistMigratedMessagesUseCaseTest {

@Test
Expand All @@ -52,7 +49,7 @@ class PersistMigratedMessagesUseCaseTest {
val result = persistMigratedMessages(listOf(arrangement.fakeMigratedMessage()), TestScope())

// Then
assertTrue(result is Either.Right)
result.shouldSucceed()
}

private class Arrangement {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import com.wire.kalium.logic.framework.TestClient
import com.wire.kalium.logic.framework.TestConversation
import com.wire.kalium.logic.framework.TestUser
import com.wire.kalium.logic.functional.Either
import com.wire.kalium.logic.util.shouldFail
import com.wire.kalium.logic.util.shouldSucceed
import com.wire.kalium.persistence.dao.message.MessageEntity
import io.mockative.Mock
import io.mockative.any
Expand All @@ -39,14 +41,11 @@ import io.mockative.given
import io.mockative.mock
import io.mockative.once
import io.mockative.verify
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.test.runTest
import kotlin.test.Test
import kotlin.test.assertTrue

@OptIn(ExperimentalCoroutinesApi::class)
class SendEditTextMessageUseCaseTest {

@Test
Expand All @@ -67,7 +66,7 @@ class SendEditTextMessageUseCaseTest {
val result = sendEditTextMessage(TestConversation.ID, originalMessageId, editedMessageText, listOf(), editedMessageId)

// Then
assertTrue(result is Either.Right)
result.shouldSucceed()
verify(arrangement.messageRepository)
.suspendFunction(arrangement.messageRepository::updateTextMessage)
.with(any(), any(), eq(originalMessageId), any())
Expand Down Expand Up @@ -104,7 +103,7 @@ class SendEditTextMessageUseCaseTest {
val result = sendEditTextMessage(TestConversation.ID, originalMessageId, editedMessageText, listOf(), editedMessageId)

// Then
assertTrue(result is Either.Left)
result.shouldFail()
verify(arrangement.messageRepository)
.suspendFunction(arrangement.messageRepository::updateTextMessage)
.with(any(), any(), eq(originalMessageId), any())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ import com.wire.kalium.logic.framework.TestUser
import com.wire.kalium.logic.functional.Either
import com.wire.kalium.logic.util.arrangement.ObserveSelfDeletionTimerSettingsForConversationUseCaseArrangement
import com.wire.kalium.logic.util.arrangement.ObserveSelfDeletionTimerSettingsForConversationUseCaseArrangementImpl
import com.wire.kalium.logic.util.shouldFail
import com.wire.kalium.logic.util.shouldSucceed
import io.mockative.Mock
import io.mockative.any
import io.mockative.classOf
Expand All @@ -48,10 +50,8 @@ import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.test.runTest
import kotlin.test.Test
import kotlin.test.assertIs
import kotlin.test.assertTrue
import kotlin.time.Duration

@OptIn(ExperimentalCoroutinesApi::class)
class SendKnockUserCaseTest {

@Test
Expand All @@ -71,7 +71,7 @@ class SendKnockUserCaseTest {
val result = sendKnockUseCase.invoke(conversationId, false)

// Then
assertTrue(result is Either.Right)
result.shouldSucceed()
verify(arrangement.messageSender)
.suspendFunction(arrangement.messageSender::sendMessage)
.with(any(), any())
Expand Down Expand Up @@ -99,7 +99,7 @@ class SendKnockUserCaseTest {
val result = sendKnockUseCase.invoke(conversationId, false)

// Then
assertTrue(result is Either.Left)
result.shouldFail()
verify(arrangement.messageSender)
.suspendFunction(arrangement.messageSender::sendMessage)
.with(any(), any())
Expand Down Expand Up @@ -128,7 +128,7 @@ class SendKnockUserCaseTest {
val result = sendKnockUseCase.invoke(conversationId, false)

// Then
assertTrue(result is Either.Right)
result.shouldSucceed()
verify(arrangement.messageSender)
.suspendFunction(arrangement.messageSender::sendMessage)
.with(matching {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ import com.wire.kalium.logic.framework.TestClient
import com.wire.kalium.logic.framework.TestConversation
import com.wire.kalium.logic.framework.TestUser
import com.wire.kalium.logic.functional.Either
import com.wire.kalium.logic.util.shouldFail
import com.wire.kalium.logic.util.shouldSucceed
import io.mockative.Mock
import io.mockative.any
import io.mockative.classOf
Expand All @@ -40,15 +42,12 @@ import io.mockative.mock
import io.mockative.once
import io.mockative.verify
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.test.runTest
import kotlin.test.Test
import kotlin.test.assertTrue

@OptIn(ExperimentalCoroutinesApi::class)
class SendTextMessageCaseTest {

@Test
Expand All @@ -67,7 +66,7 @@ class SendTextMessageCaseTest {
val result = sendTextMessage(TestConversation.ID, "some-text")

// Then
assertTrue(result is Either.Right)
result.shouldSucceed()

verify(arrangement.userPropertyRepository)
.suspendFunction(arrangement.userPropertyRepository::getReadReceiptsStatus)
Expand Down Expand Up @@ -102,7 +101,7 @@ class SendTextMessageCaseTest {
val result = sendTextMessage(TestConversation.ID, "some-text")

// Then
assertTrue(result is Either.Left)
result.shouldFail()

verify(arrangement.userPropertyRepository)
.suspendFunction(arrangement.userPropertyRepository::getReadReceiptsStatus)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,30 @@ package com.wire.kalium.logic.util

import com.wire.kalium.logic.functional.Either
import com.wire.kalium.logic.functional.fold
import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.contract
import kotlin.test.fail

inline infix fun <L, R> Either<L, R>.shouldSucceed(crossinline successAssertion: (R) -> Unit) =
@OptIn(ExperimentalContracts::class)
inline infix fun <L, R> Either<L, R>.shouldSucceed(crossinline successAssertion: (R) -> Unit) {
contract { returns() implies (this@shouldSucceed is Either.Right<R>) }
this.fold({ fail("Expected a Right value but got Left") }) { successAssertion(it) }
}

fun <L, R> Either<L, R>.shouldSucceed() = shouldSucceed { }
@OptIn(ExperimentalContracts::class)
fun <L, R> Either<L, R>.shouldSucceed() {
contract { returns() implies (this@shouldSucceed is Either.Right<R>) }
shouldSucceed { }
}

inline infix fun <L, R> Either<L, R>.shouldFail(crossinline failAssertion: (L) -> Unit): Unit =
@OptIn(ExperimentalContracts::class)
inline infix fun <L, R> Either<L, R>.shouldFail(crossinline failAssertion: (L) -> Unit) {
contract { returns() implies (this@shouldFail is Either.Left<L>) }
this.fold({ failAssertion(it) }) { fail("Expected a Left value but got Right") }
}

fun <L, R> Either<L, R>.shouldFail(): Unit = shouldFail { }
@OptIn(ExperimentalContracts::class)
fun <L, R> Either<L, R>.shouldFail() {
contract { returns() implies (this@shouldFail is Either.Left<L>) }
shouldFail { }
}

0 comments on commit 07d38f1

Please sign in to comment.