CalculateCurrencyUseCaseTest.kt 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package com.mrozon.currencyconverter.domain
  2. import androidx.test.core.app.ApplicationProvider
  3. import com.mrozon.currencyconverter.CoroutineTestRule
  4. import com.mrozon.currencyconverter.data.db.CurrencyDao
  5. import com.mrozon.currencyconverter.data.db.ValuteDb
  6. import com.mrozon.currencyconverter.domain.dto.CurrencyMapper
  7. import com.mrozon.currencyconverter.domain.model.Currency
  8. import kotlinx.coroutines.ExperimentalCoroutinesApi
  9. import kotlinx.coroutines.flow.first
  10. import kotlinx.coroutines.flow.flowOf
  11. import org.junit.Assert.assertEquals
  12. import org.junit.Before
  13. import org.junit.Rule
  14. import org.junit.Test
  15. import org.junit.runner.RunWith
  16. import org.mockito.Mock
  17. import org.mockito.Mockito
  18. import org.mockito.junit.MockitoJUnit
  19. import org.mockito.junit.MockitoRule
  20. import org.robolectric.RobolectricTestRunner
  21. import org.robolectric.annotation.Config
  22. @ExperimentalCoroutinesApi
  23. @RunWith(RobolectricTestRunner::class)
  24. @Config(sdk = [29])
  25. class CalculateCurrencyUseCaseTest {
  26. @get:Rule
  27. val mockitoRule: MockitoRule = MockitoJUnit.rule()
  28. @get:Rule
  29. var coroutinesTestRule = CoroutineTestRule()
  30. @Mock
  31. lateinit var dao: CurrencyDao
  32. lateinit var useCase: CalculateCurrencyUseCase
  33. @Before
  34. fun setUp() {
  35. useCase = CalculateCurrencyUseCase(dao, CurrencyMapper(), ApplicationProvider.getApplicationContext())
  36. }
  37. @Test
  38. fun loadCurrencies_success() = coroutinesTestRule.runBlockingTest {
  39. val valute1 = ValuteDb(
  40. charCode = "HKD",
  41. value = 93.0223,
  42. id = "R01200",
  43. name = "Гонконгских долларов"
  44. )
  45. val valute2 = ValuteDb(
  46. charCode = "HKS",
  47. value = 93.0223,
  48. id = "R01201",
  49. name = "Гонконгских долларов"
  50. )
  51. val valute3 = ValuteDb(
  52. charCode = "HKY",
  53. value = 93.0223,
  54. id = "R01203",
  55. name = "Гонконгских долларов"
  56. )
  57. Mockito.`when`(dao.getValutes()).thenReturn(
  58. flowOf(listOf(valute1, valute2, valute3))
  59. )
  60. val expected = listOf(
  61. Currency(charCode="RUB", name="Российский рубль", value=1.0),
  62. Currency(charCode="HKD", name="Гонконгских долларов", value=93.0223),
  63. Currency(charCode="HKS", name="Гонконгских долларов", value=93.0223),
  64. Currency(charCode="HKY", name="Гонконгских долларов", value=93.0223)
  65. )
  66. val actual = useCase.loadCurrencies()
  67. assertEquals(
  68. expected,
  69. actual.first()
  70. )
  71. }
  72. @Test
  73. fun loadCurrencies_load_empty_list() = coroutinesTestRule.runBlockingTest {
  74. Mockito.`when`(dao.getValutes()).thenReturn(
  75. flowOf(listOf())
  76. )
  77. val expected = listOf(
  78. Currency(charCode="RUB", name="Российский рубль", value=1.0),
  79. )
  80. val actual = useCase.loadCurrencies()
  81. assertEquals(
  82. expected,
  83. actual.first()
  84. )
  85. }
  86. @Test
  87. fun convertCurrency_change_rub2eur() {
  88. val from = Currency(charCode = "RUB", name = "Российский рубль", value = 1.0)
  89. val to = Currency(charCode = "EUR", name = "Евро", value = 85.9943)
  90. val expected = 0.01162
  91. val actual = useCase.convertCurrency(from, to, 1.0)
  92. assertEquals(
  93. expected,
  94. actual,
  95. 0.0001
  96. )
  97. }
  98. @Test
  99. fun convertCurrency_change_eur2rub() {
  100. val from = Currency(charCode = "EUR", name = "Евро", value = 85.9943)
  101. val to = Currency(charCode = "RUB", name = "Российский рубль", value = 1.0)
  102. val expected = 429.9715
  103. val actual = useCase.convertCurrency(from, to, 5.0)
  104. assertEquals(
  105. expected,
  106. actual,
  107. 0.0001
  108. )
  109. }
  110. @Test
  111. fun convertCurrency_change_eur2usd() {
  112. val from = Currency(charCode = "EUR", name = "Евро", value = 85.9943)
  113. val to = Currency(charCode = "USD", name = "Доллар США", value = 72.5048)
  114. val expected = 5.9302
  115. val actual = useCase.convertCurrency(from, to, 5.0)
  116. assertEquals(
  117. expected,
  118. actual,
  119. 0.0001
  120. )
  121. }
  122. }