Skip to content

Commit

Permalink
Code coverage improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanarodr committed May 5, 2024
1 parent e1b7d31 commit b962f73
Show file tree
Hide file tree
Showing 21 changed files with 218 additions and 47 deletions.
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
package br.com.stonks.common.di

import android.content.res.AssetManager
import com.google.gson.Gson
import org.koin.android.ext.koin.androidApplication
import org.koin.dsl.module

val commonModule = module {

factory {
Gson()
}

factory<AssetManager> {
androidApplication().assets
}
}
33 changes: 33 additions & 0 deletions common/src/test/kotlin/br/com/stonks/common/di/CommonModuleTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package br.com.stonks.common.di

import android.app.Application
import android.content.Context
import io.mockk.mockk
import io.mockk.mockkClass
import org.junit.Rule
import org.junit.Test
import org.koin.android.ext.koin.androidContext
import org.koin.dsl.koinApplication
import org.koin.test.KoinTest
import org.koin.test.check.checkModules
import org.koin.test.mock.MockProviderRule

class CommonModuleTest : KoinTest {

@get:Rule
val mockProvider = MockProviderRule.create { clazz ->
mockkClass(clazz)
}

@Test
fun `given common di module then check dependency injections`() {
koinApplication {
androidContext(mockk(relaxed = true))
modules(commonModule)
checkModules {
withInstance<Application>()
withInstance<Context>()
}
}
}
}
2 changes: 2 additions & 0 deletions design-system/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ android {

dependencies {
implementation(projects.common)

testImplementation(libs.bundles.test.jvm)
}
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ fun PieChartLayout(
Box(
modifier = Modifier
.fillMaxWidth(FractionToken.level7)
.aspectRatio(1f),
.aspectRatio(FractionToken.level10),
contentAlignment = Alignment.Center,
) {
data.dataProgress.forEach {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ object ColorToken {
val Grayscale100 = Color(0xFFDCDCDC)
val Grayscale200 = Color(0xFFA6A6A6)
val Grayscale300 = Color(0xFF717171)
val Grayscale400 = Color(0xFF1A1A1A)

/* highlight */
val HighlightPurple = Color(0xFF8F0F4E)
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import androidx.compose.ui.unit.dp

object SpacingToken {
val none = 0.dp
val xxs = 2.dp
val xs = 4.dp
val sm = 8.dp
val md = 12.dp
Expand Down
1 change: 1 addition & 0 deletions feature/home/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ dependencies {
implementation(libs.retrofit.core)

testImplementation(libs.bundles.test.jvm)
testImplementation(libs.androidx.arch.testing)
androidTestImplementation(libs.bundles.test.jvm)
androidTestImplementation(libs.bundles.test.android)
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package br.com.stonks.feature.home.di

import br.com.stonks.common.states.ViewModelState
import br.com.stonks.feature.home.domain.mapper.DailyTransactionMapper
import br.com.stonks.feature.home.domain.mapper.WalletMapper
import br.com.stonks.feature.home.domain.mapper.DailyTransactionResponseToModelMapper
import br.com.stonks.feature.home.domain.mapper.WalletResponseToModelMapper
import br.com.stonks.feature.home.domain.usecase.DailyTransactionUseCase
import br.com.stonks.feature.home.domain.usecase.HomeContentUseCase
import br.com.stonks.feature.home.domain.usecase.WalletUseCase
import br.com.stonks.feature.home.repository.HomeRepository
import br.com.stonks.feature.home.repository.HomeRepositoryImpl
import br.com.stonks.feature.home.repository.remote.HomeApiService
import br.com.stonks.feature.home.repository.remote.HomeRemoteDataSource
import br.com.stonks.feature.home.ui.mapper.HomeUiMapper
import br.com.stonks.feature.home.domain.mapper.HomeModelToUiMapper
import br.com.stonks.feature.home.ui.viewmodel.HOME_VM_QUALIFIER
import br.com.stonks.feature.home.ui.viewmodel.HomeViewModel
import br.com.stonks.infrastructure.network.provider.NetworkServiceProvider
Expand Down Expand Up @@ -40,28 +40,28 @@ val homeModule = module {
}

factory {
WalletMapper()
WalletResponseToModelMapper()
}

factory {
DailyTransactionMapper()
DailyTransactionResponseToModelMapper()
}

factory {
HomeUiMapper()
HomeModelToUiMapper()
}

factory {
WalletUseCase(
homeRepository = get(),
walletMapper = get(),
walletModelMapper = get(),
)
}

factory {
DailyTransactionUseCase(
homeRepository = get(),
dailyTransactionMapper = get(),
dailyTransactionModelMapper = get(),
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import br.com.stonks.feature.home.domain.types.TransactionType
import br.com.stonks.feature.home.repository.remote.response.DailyTransactionResponse
import br.com.stonks.feature.home.repository.remote.response.TransactionResponse

internal class DailyTransactionMapper : Mapper<List<DailyTransactionResponse>, List<DailyTransactionModel>> {
internal class DailyTransactionResponseToModelMapper :
Mapper<List<DailyTransactionResponse>, List<DailyTransactionModel>> {

override fun mapper(input: List<DailyTransactionResponse>): List<DailyTransactionModel> {
return input.map(::mapperDailyGroup)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package br.com.stonks.feature.home.ui.mapper
package br.com.stonks.feature.home.domain.mapper

import br.com.stonks.common.formatters.DatePattern
import br.com.stonks.common.formatters.formatTo
Expand All @@ -17,7 +17,7 @@ import br.com.stonks.feature.home.ui.model.TransactionUiModel
import br.com.stonks.feature.home.utils.getColor
import br.com.stonks.feature.home.utils.getIcon

internal class HomeUiMapper : Mapper<HomeContentModel, HomeUiModel> {
internal class HomeModelToUiMapper : Mapper<HomeContentModel, HomeUiModel> {

override fun mapper(input: HomeContentModel) = HomeUiModel(
totalAssets = input.wallet.totalAssets,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import br.com.stonks.feature.home.domain.types.PortfolioType
import br.com.stonks.feature.home.repository.remote.response.PortfolioResponse
import br.com.stonks.feature.home.repository.remote.response.WalletResponse

internal class WalletMapper : Mapper<WalletResponse, WalletModel> {
internal class WalletResponseToModelMapper : Mapper<WalletResponse, WalletModel> {

override fun mapper(input: WalletResponse) = WalletModel(
availableBalance = input.availableBalance,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
package br.com.stonks.feature.home.domain.usecase

import br.com.stonks.common.utils.asFlow
import br.com.stonks.feature.home.domain.mapper.DailyTransactionMapper
import br.com.stonks.feature.home.domain.mapper.DailyTransactionResponseToModelMapper
import br.com.stonks.feature.home.domain.model.DailyTransactionModel
import br.com.stonks.feature.home.repository.HomeRepository
import kotlinx.coroutines.flow.Flow

internal class DailyTransactionUseCase(
private val homeRepository: HomeRepository,
private val dailyTransactionMapper: DailyTransactionMapper,
private val dailyTransactionModelMapper: DailyTransactionResponseToModelMapper,
) {

suspend operator fun invoke(): Flow<List<DailyTransactionModel>> {
return homeRepository.getTransactions().mapCatching {
dailyTransactionMapper.mapper(it)
dailyTransactionModelMapper.mapper(it)
}.asFlow()
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
package br.com.stonks.feature.home.domain.usecase

import br.com.stonks.common.utils.asFlow
import br.com.stonks.feature.home.domain.mapper.WalletMapper
import br.com.stonks.feature.home.domain.mapper.WalletResponseToModelMapper
import br.com.stonks.feature.home.domain.model.WalletModel
import br.com.stonks.feature.home.repository.HomeRepository
import kotlinx.coroutines.flow.Flow

internal class WalletUseCase(
private val homeRepository: HomeRepository,
private val walletMapper: WalletMapper,
private val walletModelMapper: WalletResponseToModelMapper,
) {

suspend operator fun invoke(): Flow<WalletModel> {
return homeRepository.getWallet().mapCatching {
walletMapper.mapper(it)
walletModelMapper.mapper(it)
}.asFlow()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package br.com.stonks.feature.home.ui.viewmodel
import androidx.lifecycle.viewModelScope
import br.com.stonks.common.states.ViewModelState
import br.com.stonks.feature.home.domain.usecase.HomeContentUseCase
import br.com.stonks.feature.home.ui.mapper.HomeUiMapper
import br.com.stonks.feature.home.domain.mapper.HomeModelToUiMapper
import br.com.stonks.feature.home.ui.states.HomeUiEvent
import br.com.stonks.feature.home.ui.states.HomeUiState
import kotlinx.coroutines.flow.MutableStateFlow
Expand All @@ -17,7 +17,7 @@ internal val HOME_VM_QUALIFIER: String = HomeViewModel::class.java.simpleName

internal class HomeViewModel(
private val homeContentUseCase: HomeContentUseCase,
private val homeUiMapper: HomeUiMapper,
private val homeUiMapper: HomeModelToUiMapper,
) : ViewModelState<HomeUiState, HomeUiEvent>() {

init {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package br.com.stonks.feature.home.domain.domain.usecase

import br.com.stonks.common.exception.StonksApiException
import br.com.stonks.feature.home.domain.mapper.DailyTransactionMapper
import br.com.stonks.feature.home.domain.mapper.DailyTransactionResponseToModelMapper
import br.com.stonks.feature.home.domain.usecase.DailyTransactionUseCase
import br.com.stonks.feature.home.repository.HomeRepository
import br.com.stonks.feature.home.repository.remote.response.DailyTransactionResponse
Expand All @@ -22,7 +22,7 @@ class DailyTransactionUseCaseTest {

private val fakeResponse = mockk<List<DailyTransactionResponse>>()
private val homeRepositoryMock = mockk<HomeRepository>()
private val dailyTransactionMapperMock = mockk<DailyTransactionMapper>()
private val dailyTransactionMapperMock = mockk<DailyTransactionResponseToModelMapper>()
private val sut = DailyTransactionUseCase(homeRepositoryMock, dailyTransactionMapperMock)

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package br.com.stonks.feature.home.domain.domain.usecase

import app.cash.turbine.test
import br.com.stonks.feature.home.domain.model.DailyTransactionModel
import br.com.stonks.feature.home.domain.model.HomeContentModel
import br.com.stonks.feature.home.domain.model.WalletModel
import br.com.stonks.feature.home.domain.usecase.DailyTransactionUseCase
import br.com.stonks.feature.home.domain.usecase.HomeContentUseCase
import br.com.stonks.feature.home.domain.usecase.WalletUseCase
import io.mockk.coEvery
import io.mockk.mockk
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.test.runTest
import org.junit.Assert.assertEquals
import org.junit.Test
import java.util.Date

class HomeContentUseCaseTest {

private val currentDate = Date()
private val walletUseCaseMock = mockk<WalletUseCase>()
private val dailyTransactionUseCaseMock = mockk<DailyTransactionUseCase>()
private val sut = HomeContentUseCase(walletUseCaseMock, dailyTransactionUseCaseMock)

@Test
fun `given use case when fetch data with success then return valid result`() = runTest {
coEvery { walletUseCaseMock() } returns flowOf(fakeWallet)
coEvery { dailyTransactionUseCaseMock() } returns flowOf(fakeTransaction)

sut.fetchData().test {
assertEquals(fakeHomeContent(), awaitItem())
awaitComplete()
}
}

private fun fakeHomeContent() = HomeContentModel(
wallet = fakeWallet,
dailyTransactions = fakeTransaction,
)

private val fakeWallet = WalletModel(
availableBalance = 1.0,
investedBalance = 1.0,
totalAssets = 2.0,
portfolio = emptyList(),
)

private val fakeTransaction = listOf(
DailyTransactionModel(
date = currentDate,
dailyBalance = 1.0,
transactions = emptyList(),
)
)
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package br.com.stonks.feature.home.domain.domain.usecase

import br.com.stonks.common.exception.StonksApiException
import br.com.stonks.feature.home.domain.mapper.WalletMapper
import br.com.stonks.feature.home.domain.mapper.WalletResponseToModelMapper
import br.com.stonks.feature.home.domain.usecase.WalletUseCase
import br.com.stonks.feature.home.repository.HomeRepository
import br.com.stonks.feature.home.repository.remote.response.WalletResponse
Expand All @@ -22,7 +22,7 @@ class WalletUseCaseTest {

private val fakeResponse = mockk<WalletResponse>()
private val homeRepositoryMock = mockk<HomeRepository>()
private val walletMapperMock = mockk<WalletMapper>()
private val walletMapperMock = mockk<WalletResponseToModelMapper>()
private val sut = WalletUseCase(homeRepositoryMock, walletMapperMock)

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import org.junit.Test
import java.util.Date
import kotlin.test.assertEquals

class DailyTransactionMapperTest {
class DailyTransactionResponseToModelMapperTest {

private val transactionDate = Date()
private val sut = DailyTransactionMapper()
private val sut = DailyTransactionResponseToModelMapper()

@Test
fun `given response data when call mapper then convert to model`() {
Expand Down
Loading

0 comments on commit b962f73

Please sign in to comment.