Skip to content

Commit

Permalink
Added unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanarodr committed May 6, 2024
1 parent ba694e0 commit f8d1f40
Show file tree
Hide file tree
Showing 5 changed files with 336 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,35 +14,47 @@ import br.com.stonks.feature.home.ui.model.DailyTransactionUiModel
import br.com.stonks.feature.home.ui.model.HomeUiModel
import br.com.stonks.feature.home.ui.model.PortfolioUiModel
import br.com.stonks.feature.home.ui.model.TransactionUiModel
import io.mockk.every
import io.mockk.mockk
import org.junit.Assert.assertEquals
import org.junit.Test
import java.util.Date

class HomeModelToUiMapperTest {

private val currentDate = Date(2024, 4, 3)
private val sut = HomeModelToUiMapper()
private val walletModelToChartMapper = mockk<WalletModelToChartMapper>()
private val sut = HomeModelToUiMapper(walletModelToChartMapper)

@Test
fun `given model when call mapper then convert to ui model`() {
every {
walletModelToChartMapper.mapper(any())
} returns createPieChartData()

val expected = createResultData()
val result = sut.mapper(createInputData())

assertEquals(expected, result)
}

private fun createResultData() = HomeUiModel(
totalAssets = 1.0,
portfolioChart = PieChartData(
private fun createPieChartData() = listOf(
PieChartData(
title = "Todos os produtos",
value = 1.0,
progress = 1f,
dataProgress = listOf(
PieChartDataProgress(
progress = 0.5f,
progressColor = ColorToken.HighlightOrange,
),
),
),
)

private fun createResultData() = HomeUiModel(
totalAssets = 1.0,
portfolioChart = createPieChartData(),
portfolio = listOf(
PortfolioUiModel(
tagColor = ColorToken.HighlightOrange,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package br.com.stonks.feature.stocks.domain.types

import org.junit.Assert
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.Parameterized

@RunWith(Parameterized::class)
internal class StockAlertTypeTest(
private val input: String,
private val expected: StockAlertType,
) {

companion object {
@JvmStatic
@Parameterized.Parameters
fun arguments(): List<Array<Any>> = listOf(
arrayOf("unknown", StockAlertType.UNKNOWN),
arrayOf("high", StockAlertType.HIGH_PRICE),
arrayOf("low", StockAlertType.LOW_PRICE),
)
}

@Test
fun `given type string when convert to enum return a mapped type`() {
Assert.assertEquals(expected, StockAlertType.fromString(input))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package br.com.stonks.feature.stocks.domain.types

import org.junit.Assert.assertEquals
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.Parameterized

@RunWith(Parameterized::class)
internal class StockStatusTypeTest(
private val input: String,
private val expected: StockStatusType,
) {

companion object {
@JvmStatic
@Parameterized.Parameters
fun arguments(): List<Array<Any>> = listOf(
arrayOf("unknown", StockStatusType.UNKNOWN),
arrayOf("available", StockStatusType.AVAILABLE),
arrayOf("unavailable", StockStatusType.UNAVAILABLE),
)
}

@Test
fun `given type string when convert to enum return a mapped type`() {
assertEquals(expected, StockStatusType.fromString(input))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
package br.com.stonks.feature.stocks.domain.usecase

import br.com.stonks.common.db.DefaultPrimaryKey
import br.com.stonks.feature.stocks.domain.mapper.StockAlertModelToResponseMapper
import br.com.stonks.feature.stocks.domain.mapper.StockAlertResponseToModelMapper
import br.com.stonks.feature.stocks.domain.model.StockAlertModel
import br.com.stonks.feature.stocks.domain.types.StockAlertType
import br.com.stonks.feature.stocks.domain.types.StockStatusType
import br.com.stonks.feature.stocks.repository.StockRepository
import br.com.stonks.feature.stocks.repository.remote.response.StockAlertResponse
import br.com.stonks.testing.rules.CoroutinesTestRule
import io.mockk.coEvery
import io.mockk.coVerify
import io.mockk.coVerifyOrder
import io.mockk.every
import io.mockk.mockk
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.runTest
import org.junit.Rule
import org.junit.Test

@OptIn(ExperimentalCoroutinesApi::class)
class StockAlertUseCaseTest {

@get:Rule
var coroutinesTestRule = CoroutinesTestRule()

private val stockRepositoryMock = mockk<StockRepository>()
private val stockPriceComparatorUseCaseMock = mockk<StockPriceComparatorUseCase>()
private val stockAlertModelMapperMock = mockk<StockAlertResponseToModelMapper>()
private val stockAlertResponseMapperMock = mockk<StockAlertModelToResponseMapper>()

private val sut = StockAlertUseCase(
stockRepository = stockRepositoryMock,
stockPriceComparatorUseCase = stockPriceComparatorUseCaseMock,
stockAlertModelMapper = stockAlertModelMapperMock,
stockAlertResponseMapper = stockAlertResponseMapperMock,
)

@Test
fun `given use case when call local alerts then check price comparator and mapper to model`() = runTest {
val fakeResponse = createFakeResponse()

coEvery {
stockRepositoryMock.listStockAlerts()
} returns Result.success(listOf(fakeResponse))

coEvery {
stockPriceComparatorUseCaseMock.checkStockPrice(fakeResponse)
} returns StockAlertType.HIGH_PRICE

sut.listStockAlerts()

coVerifyOrder {
stockPriceComparatorUseCaseMock.checkStockPrice(
fakeResponse
)
stockAlertModelMapperMock.mapper(
createFakeResponse(trigger = StockAlertType.HIGH_PRICE.status)
)
}
}

@Test
fun `given save alert when use default primary key then call insert method`() = runTest {
val fakeAlert = createFakeAlert()
val fakeResponse = createFakeResponse()

every {
stockAlertResponseMapperMock.mapper(fakeAlert)
} returns fakeResponse

coEvery {
stockRepositoryMock.insertStockAlert(fakeResponse)
} returns mockk()

sut.saveStockAlert(fakeAlert)

coVerify(exactly = 1) {
stockRepositoryMock.insertStockAlert(fakeResponse)
}

coVerify(exactly = 0) {
stockRepositoryMock.updateStockAlert(any())
}
}

@Test
fun `given save alert when use generated key then call update method`() = runTest {
val fakeAlert = createFakeAlert(id = 5L)
val fakeResponse = createFakeResponse(id = 5L)

every {
stockAlertResponseMapperMock.mapper(fakeAlert)
} returns fakeResponse

coEvery {
stockRepositoryMock.updateStockAlert(fakeResponse)
} returns Result.success(mockk())

sut.saveStockAlert(fakeAlert)

coVerify(exactly = 1) {
stockRepositoryMock.updateStockAlert(fakeResponse)
}

coVerify(exactly = 0) {
stockRepositoryMock.insertStockAlert(any())
}
}

@Test
fun `given use case when call delete stock then call delete method`() = runTest {
val alertId = 1L

coEvery {
stockRepositoryMock.deleteStockAlert(alertId)
} returns mockk()

sut.deleteStockAlert(alertId)

coVerify {
stockRepositoryMock.deleteStockAlert(alertId)
}
}

private fun createFakeAlert(
id: Long = DefaultPrimaryKey,
) = StockAlertModel(
id = id,
ticket = "lorem",
alertValue = 1.0,
status = StockStatusType.AVAILABLE,
notificationTrigger = StockAlertType.HIGH_PRICE,
)

private fun createFakeResponse(
id: Long = DefaultPrimaryKey,
trigger: String = "unknown",
) = StockAlertResponse(
id = id,
stockTicket = "lorem",
alertValue = 5.0,
status = "available",
notificationTrigger = trigger,
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package br.com.stonks.feature.stocks.repository

import br.com.stonks.feature.stocks.domain.mapper.StockAlertEntityToResponseMapper
import br.com.stonks.feature.stocks.domain.mapper.StockAlertResponseToEntityMapper
import br.com.stonks.feature.stocks.repository.local.StockAlertEntity
import br.com.stonks.feature.stocks.repository.local.StockLocalDataSource
import br.com.stonks.feature.stocks.repository.remote.StockRemoteDataSource
import br.com.stonks.feature.stocks.repository.remote.response.StockAlertResponse
import io.mockk.coEvery
import io.mockk.coVerify
import io.mockk.coVerifyOrder
import io.mockk.mockk
import kotlinx.coroutines.test.runTest
import org.junit.Test

class StockRepositoryImplTest {

private val fakeEntity = mockk<StockAlertEntity>()
private val fakeResponse = mockk<StockAlertResponse>()
private val stockRemoteDataSourceMock = mockk<StockRemoteDataSource>(relaxed = true)
private val stockLocalDataSourceMock = mockk<StockLocalDataSource>(relaxed = true)
private val stockAlertResponseMapperMock = mockk<StockAlertEntityToResponseMapper>(relaxed = true)
private val stockAlertEntityMapperMock = mockk<StockAlertResponseToEntityMapper>(relaxed = true)

private val sut = StockRepositoryImpl(
stockRemoteDataSource = stockRemoteDataSourceMock,
stockLocalDataSource = stockLocalDataSourceMock,
stockAlertResponseMapper = stockAlertResponseMapperMock,
stockAlertEntityMapper = stockAlertEntityMapperMock,
)

@Test
fun `given repository when get remote alerts then call remote data source`() = runTest {
sut.getRemoteStockAlerts()

coVerify { stockRemoteDataSourceMock.getStockAlerts() }
}

@Test
fun `given repository when get local alerts then call local data source`() = runTest {
coEvery {
stockLocalDataSourceMock.listStockAlerts()
} returns Result.success(listOf(fakeEntity))

sut.listStockAlerts()

coVerifyOrder {
stockLocalDataSourceMock.listStockAlerts()
stockAlertResponseMapperMock.mapper(fakeEntity)
}
}

@Test
fun `given repository when get alert then call local data source`() = runTest {
val alertId = 1L

coEvery {
stockLocalDataSourceMock.getStockAlert(alertId)
} returns Result.success(fakeEntity)

sut.getStockAlert(alertId)

coVerifyOrder {
stockLocalDataSourceMock.getStockAlert(alertId)
stockAlertResponseMapperMock.mapper(fakeEntity)
}
}

@Test
fun `given repository when insert alert then call local data source`() = runTest {
coEvery {
stockAlertEntityMapperMock.mapper(fakeResponse)
} returns fakeEntity

coEvery {
stockLocalDataSourceMock.insertStockAlert(fakeEntity)
} returns mockk()

sut.insertStockAlert(fakeResponse)

coVerify {
stockLocalDataSourceMock.insertStockAlert(fakeEntity)
}
}

@Test
fun `given repository when update alert then call local data source`() = runTest {
coEvery {
stockAlertEntityMapperMock.mapper(fakeResponse)
} returns fakeEntity

coEvery {
stockLocalDataSourceMock.updateStockAlert(fakeEntity)
} returns mockk()

sut.updateStockAlert(fakeResponse)

coVerify {
stockLocalDataSourceMock.updateStockAlert(fakeEntity)
}
}

@Test
fun `given repository when delete alert then call local data source`() = runTest {
val alertId = 1L

coEvery {
stockLocalDataSourceMock.deleteStockAlert(alertId)
} returns mockk()

sut.deleteStockAlert(alertId)

coVerify {
stockLocalDataSourceMock.deleteStockAlert(alertId)
}
}
}

0 comments on commit f8d1f40

Please sign in to comment.