Skip to content

Commit

Permalink
Adding SyncExchangeRatesActTest
Browse files Browse the repository at this point in the history
  • Loading branch information
philipplackner committed Aug 5, 2023
1 parent e65612e commit 31d7c32
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.ivy.core.domain.action.exchange

import com.ivy.core.persistence.dao.exchange.ExchangeRateDao
import com.ivy.core.persistence.entity.exchange.ExchangeRateEntity
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.map

class ExchangeRateDaoFake: ExchangeRateDao {

private val rates = MutableStateFlow<List<ExchangeRateEntity>>(emptyList())

override suspend fun save(values: List<ExchangeRateEntity>) {
rates.value = values
}

override fun findAllByBaseCurrency(baseCurrency: String): Flow<List<ExchangeRateEntity>> {
return rates
.map {
it.filter { it.baseCurrency.uppercase() == baseCurrency.uppercase() }
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.ivy.core.domain.action.exchange

import com.ivy.data.CurrencyCode
import com.ivy.data.ExchangeRatesMap
import com.ivy.data.exchange.ExchangeProvider
import com.ivy.exchange.RemoteExchangeProvider

class RemoteExchangeProviderFake: RemoteExchangeProvider {

var ratesMap = mapOf(
"USD" to mapOf(
"EUR" to 0.91,
"AUD" to 1.49,
"CAD" to -3.0,
),
"EUR" to mapOf(
"EUR" to 1.08,
"AUD" to 1.62,
"CAD" to 1.43,
)
)

override suspend fun fetchExchangeRates(baseCurrency: CurrencyCode): RemoteExchangeProvider.Result {
return RemoteExchangeProvider.Result(
ratesMap = ratesMap[baseCurrency] as ExchangeRatesMap,
provider = ExchangeProvider.Fawazahmed0
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.ivy.core.domain.action.exchange

import assertk.assertThat
import assertk.assertions.isNotNull
import assertk.assertions.isNull
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.runBlocking
import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test

internal class SyncExchangeRatesActTest {

private lateinit var syncExchangeRatesAct: SyncExchangeRatesAct
private lateinit var exchangeProviderFake: RemoteExchangeProviderFake
private lateinit var exchangeRateDaoFake: ExchangeRateDaoFake

@BeforeEach
fun setUp() {
exchangeProviderFake = RemoteExchangeProviderFake()
exchangeRateDaoFake = ExchangeRateDaoFake()
syncExchangeRatesAct = SyncExchangeRatesAct(
exchangeProvider = exchangeProviderFake,
exchangeRateDao = exchangeRateDaoFake
)
}

@Test
fun `Test sync exchange rates, negative values ignored`() = runBlocking {
syncExchangeRatesAct("USD")

val usdRates = exchangeRateDaoFake
.findAllByBaseCurrency("USD")
.first { it.isNotEmpty() }
val cadRate = usdRates.find { it.currency == "CAD" }

assertThat(cadRate).isNull()
}

@Test
fun `Test sync exchange rates, valid values saved`() = runBlocking<Unit> {
syncExchangeRatesAct("USD")

val usdRates = exchangeRateDaoFake
.findAllByBaseCurrency("USD")
.first { it.isNotEmpty() }
val eurRate = usdRates.find { it.currency == "EUR" }
val audRate = usdRates.find { it.currency == "AUD" }

assertThat(eurRate).isNotNull()
assertThat(audRate).isNotNull()
}
}

0 comments on commit 31d7c32

Please sign in to comment.