Skip to content

Commit

Permalink
Merge branch 'fake-api-removal'
Browse files Browse the repository at this point in the history
  • Loading branch information
shorthouse committed Feb 18, 2024
2 parents fc3879d + 54efb49 commit 406b484
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 356 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,28 @@ import com.google.common.truth.Truth.assertThat
import dev.shorthouse.coinwatch.MainDispatcherRule
import dev.shorthouse.coinwatch.common.Result
import dev.shorthouse.coinwatch.data.mapper.CoinChartMapper
import dev.shorthouse.coinwatch.data.source.remote.FakeCoinApi
import dev.shorthouse.coinwatch.data.source.remote.FakeCoinNetworkDataSource
import dev.shorthouse.coinwatch.data.source.remote.CoinNetworkDataSource
import dev.shorthouse.coinwatch.data.source.remote.model.CoinChartApiModel
import dev.shorthouse.coinwatch.data.source.remote.model.CoinChartData
import dev.shorthouse.coinwatch.data.source.remote.model.PastPrice
import dev.shorthouse.coinwatch.data.userPreferences.Currency
import dev.shorthouse.coinwatch.model.CoinChart
import dev.shorthouse.coinwatch.model.Percentage
import dev.shorthouse.coinwatch.model.Price
import java.math.BigDecimal
import io.mockk.MockKAnnotations
import io.mockk.coEvery
import io.mockk.impl.annotations.MockK
import io.mockk.unmockkAll
import kotlinx.collections.immutable.persistentListOf
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.test.runTest
import okhttp3.ResponseBody.Companion.toResponseBody
import org.junit.After
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import retrofit2.Response
import java.math.BigDecimal

class CoinChartRepositoryTest {

Expand All @@ -27,17 +35,25 @@ class CoinChartRepositoryTest {
// Class under test
private lateinit var coinChartRepository: CoinChartRepository

@MockK
private lateinit var coinNetworkDataSource: CoinNetworkDataSource

@Before
fun setup() {
MockKAnnotations.init(this)

coinChartRepository = CoinChartRepositoryImpl(
coinNetworkDataSource = FakeCoinNetworkDataSource(
coinApi = FakeCoinApi()
),
coinNetworkDataSource = coinNetworkDataSource,
coinChartMapper = CoinChartMapper(),
ioDispatcher = mainDispatcherRule.testDispatcher
)
}

@After
fun tearDown() {
unmockkAll()
}

@Test
fun `When coin chart data is valid should return success`() = runTest {
// Arrange
Expand All @@ -59,6 +75,27 @@ class CoinChartRepositoryTest {
)
)

coEvery {
coinNetworkDataSource.getCoinChart(
coinId = coinId,
chartPeriod = chartPeriod,
currency = currency
)
} returns Response.success(
CoinChartApiModel(
coinChartData = CoinChartData(
priceChangePercentage = "-0.97",
pastPrices = listOf(
PastPrice(amount = "20000.20"),
PastPrice(amount = "30000.47"),
PastPrice(amount = null),
PastPrice(amount = "25000.89"),
PastPrice(amount = "27000.44")
)
)
)
)

// Act
val result = coinChartRepository.getCoinChart(
coinId = coinId,
Expand All @@ -75,7 +112,7 @@ class CoinChartRepositoryTest {
fun `When coin chart data has null values should populate these with default values and return success`() =
runTest {
// Arrange
val coinId = "nullValues"
val coinId = "Qwsogvtv82FCd"
val chartPeriod = "1d"
val currency = Currency.USD

Expand All @@ -84,6 +121,21 @@ class CoinChartRepositoryTest {
val expectedMaxPriceAmount = BigDecimal.ZERO
val expectedPeriodPriceChangePercentageAmount = BigDecimal.ZERO

coEvery {
coinNetworkDataSource.getCoinChart(
coinId = coinId,
chartPeriod = chartPeriod,
currency = currency
)
} returns Response.success(
CoinChartApiModel(
coinChartData = CoinChartData(
priceChangePercentage = null,
pastPrices = null
)
)
)

// Act
val result = coinChartRepository.getCoinChart(
coinId = coinId,
Expand All @@ -106,7 +158,7 @@ class CoinChartRepositoryTest {
fun `When coin chart has null prices should ignore these prices and return success`() =
runTest {
// Arrange
val coinId = "nullPrices"
val coinId = "Qwsogvtv82FCd"
val chartPeriod = "1d"
val currency = Currency.USD

Expand All @@ -115,6 +167,27 @@ class CoinChartRepositoryTest {
BigDecimal("30000.47")
)

coEvery {
coinNetworkDataSource.getCoinChart(
coinId = coinId,
chartPeriod = chartPeriod,
currency = currency
)
} returns Response.success(
CoinChartApiModel(
coinChartData = CoinChartData(
priceChangePercentage = null,
pastPrices = listOf(
PastPrice(amount = "30000.47"),
null,
PastPrice(amount = null),
PastPrice(amount = "25000.89"),
null
)
)
)
)

// Act
val result = coinChartRepository.getCoinChart(
coinId = coinId,
Expand All @@ -138,6 +211,17 @@ class CoinChartRepositoryTest {
message = "Unable to fetch coin chart"
)

coEvery {
coinNetworkDataSource.getCoinChart(
coinId = coinId,
chartPeriod = chartPeriod,
currency = currency
)
} returns Response.error(
404,
"Chart not found".toResponseBody(null)
)

// Act
val result = coinChartRepository.getCoinChart(
coinId = coinId,
Expand Down

This file was deleted.

Loading

0 comments on commit 406b484

Please sign in to comment.