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

Update test #120

Merged
merged 6 commits into from
Aug 16, 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
1 change: 1 addition & 0 deletions daraja/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ kotlin {
implementation(libs.kotlinX.coroutines.test)
implementation(libs.mockative)
implementation(libs.ktor.mock)
implementation(libs.assertK)
}

androidMain.dependencies {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package com.vickbt.darajakmp.network

import assertk.assertThat
import assertk.assertions.isEqualTo
import com.vickbt.darajakmp.network.models.DarajaToken
import com.vickbt.darajakmp.network.models.DarajaTransactionRequest
import com.vickbt.darajakmp.network.models.DarajaTransactionResponse
Expand All @@ -29,8 +31,6 @@ import kotlinx.coroutines.test.runTest
import kotlin.test.AfterTest
import kotlin.test.BeforeTest
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNotNull
import kotlin.test.assertNull

class DarajaApiServiceTest {
Expand Down Expand Up @@ -99,10 +99,7 @@ class DarajaApiServiceTest {
val actualResult = darajaApiService.fetchAccessToken()

// then
assertEquals(
expected = DarajaResult.Success(darajaToken),
actual = actualResult,
)
assertThat(actualResult).isEqualTo(DarajaResult.Success(darajaToken))
}

@Test
Expand All @@ -116,8 +113,7 @@ class DarajaApiServiceTest {
// then
val cachedToken = mockInMemoryCache.get(1)

assertNotNull(cachedToken)
assertEquals(expected = darajaToken, actual = cachedToken)
assertThat(darajaToken).isEqualTo(cachedToken)
}

@Test
Expand All @@ -140,8 +136,8 @@ class DarajaApiServiceTest {
)

// then
assertEquals(expected = expectedResult, actual = actualResult)
assertNotNull(mockInMemoryCache.get(1))
assertThat(expectedResult).isEqualTo(actualResult)
assertThat(mockInMemoryCache.get(1)).isEqualTo(darajaToken)
}

@Test
Expand All @@ -162,6 +158,6 @@ class DarajaApiServiceTest {
),
)

assertEquals(expected = expectedResult, actual = actualResult)
assertThat(expectedResult).isEqualTo(actualResult)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,18 @@

package com.vickbt.darajakmp.utils

import assertk.all
import assertk.assertThat
import assertk.assertions.hasMessage
import assertk.assertions.isEqualTo
import assertk.assertions.isNotNull
import assertk.assertions.isNull
import assertk.assertions.isSameInstanceAs
import assertk.assertions.prop
import com.vickbt.darajakmp.network.models.DarajaException
import com.vickbt.darajakmp.utils.DarajaResult.Success
import kotlinx.coroutines.test.runTest
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNotNull
import kotlin.test.assertNull

class DarajaResultTest {
Expand All @@ -31,94 +39,110 @@ class DarajaResultTest {
)

@Test
fun darajaResult_getOrNull_returns_data_on_success() {
val result = DarajaResult.Success(data = "Success").getOrNull()
fun `darajaResult getOrNull returns data on success`() =
runTest {
val result = Success(data = "Success").getOrNull()

assertNotNull(result)
assertEquals(expected = "Success", actual = result)
}
assertThat(result).isEqualTo("Success")
}

@Test
fun darajaResult_getOrNull_returns_null_on_error() {
val result = DarajaResult.Failure(exception = darajaException).getOrNull()
fun `darajaResult getOrNull returns null on error`() =
runTest {
val result = DarajaResult.Failure(exception = darajaException).getOrNull()

assertNull(result)
assertEquals(expected = null, actual = result)
}
assertThat(result).isEqualTo(null)
}

@Test
fun darajaResult_throwOnFailure_returns_exception_on_error() {
val result = DarajaResult.Failure(exception = darajaException).throwOnFailure()

assertNotNull(result)
assertEquals(expected = darajaException, actual = result)
}
fun `darajaResult throwOnFailure returns exception on error`() =
runTest {
val result = DarajaResult.Failure(exception = darajaException).throwOnFailure()

assertThat(result).all {
hasMessage(darajaException.errorMessage)
isSameInstanceAs(darajaException)
}
}

@Test
fun darajaResult_getOrThrow_returns_data_on_success() {
val result = DarajaResult.Success("Success").getOrThrow()
fun `darajaResult getOrThrow returns data on success`() =
runTest {
val result = Success("Success").getOrThrow()

assertNotNull(result)
assertEquals(expected = "Success", actual = result)
}
assertThat(result).isEqualTo("Success")
}

@Test
fun darajaResult_onSuccess_returns_data_on_success() {
val result = DarajaResult.Success(data = "Success")

result.onSuccess {
assertNotNull(it)
assertEquals(expected = it, actual = "Success")
fun `darajaResult onSuccess returns data on success`() =
runTest {
val result = Success(data = "Success")

result.onSuccess {
assertThat(it).isEqualTo("Success")
}.onFailure {
assertThat(it).all {
prop(DarajaException::errorMessage).isNotNull()
isSameInstanceAs(DarajaException::errorMessage)
}
}
}
}

@Test
fun darajaResult_onSuccess_returns_null_on_error() {
val result = DarajaResult.Failure(darajaException)

result.onSuccess {
assertNull(it)
fun `darajaResult onSuccess returns null on error`() =
runTest {
val result = DarajaResult.Failure(darajaException)

result.onSuccess {
assertNull(it)
}.onFailure {
assertThat(it.errorMessage).isEqualTo("Invalid Authentication passed")
}
}
}

@Test
fun darajaResult_onFailure_returns_null_on_success() {
val result = DarajaResult.Success(data = "Success")
fun `darajaResult onFailure returns null on success`() =
runTest {
val result = Success(data = "Success")

result.onFailure {
assertNull(it)
result.onFailure {
assertThat(it.errorMessage).isEqualTo("Invalid Authentication passed")
}
}
}

@Test
fun darajaResult_onFailure_returns_exception_on_error() {
val result = DarajaResult.Failure(darajaException)

result.onFailure {
assertNotNull(it)
assertEquals(expected = it, actual = darajaException)
fun `darajaResult onFailure returns exception on error`() =
runTest {
val result = DarajaResult.Failure(darajaException)

result.onFailure {
assertThat(it).all {
prop(DarajaException::errorMessage).isEqualTo("Invalid Authentication passed")
}
}
}
}

@Test
fun darajaResult_onSuccess_onFailure_on_success() {
val result = DarajaResult.Success(data = "Success")

result.onSuccess {
assertNotNull(it)
assertEquals(expected = it, actual = "Success")
}.onFailure {
assertNull(it)
fun `darajaResult onSuccess does not trigger onFailure on success`() =
runTest {
val result = Success(data = "Success")

result.onSuccess {
assertThat(it).isEqualTo("Success")
}.onFailure {
assertThat(it).isNull()
}
}
}

@Test
fun darajaResult_onSuccess_onFailure_on_error() {
val result = DarajaResult.Failure(darajaException)

result.onFailure {
assertNotNull(it)
assertEquals(expected = it, actual = darajaException)
fun `darajaResult onSuccess returns onFailure on error`() =
runTest {
val result = DarajaResult.Failure(darajaException)

result.onFailure {
assertThat(it).all {
prop(DarajaException::errorMessage).isEqualTo("Invalid Authentication passed")
}
}
}
}
}
Loading