forked from philipplackner/IvyWallet-PL_Version
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e65612e
commit 31d7c32
Showing
3 changed files
with
106 additions
and
0 deletions.
There are no files selected for viewing
24 changes: 24 additions & 0 deletions
24
core/domain/src/test/java/com/ivy/core/domain/action/exchange/ExchangeRateDaoFake.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() } | ||
} | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
core/domain/src/test/java/com/ivy/core/domain/action/exchange/RemoteExchangeProviderFake.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
core/domain/src/test/java/com/ivy/core/domain/action/exchange/SyncExchangeRatesActTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |