Skip to content

Commit

Permalink
Merge pull request #234 from ishanvaghani/unittest/usecases
Browse files Browse the repository at this point in the history
Unittest/usecases
  • Loading branch information
hieuwu authored Dec 27, 2023
2 parents a2a628a + 565a64e commit 1c03f41
Show file tree
Hide file tree
Showing 10 changed files with 415 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,20 @@ class UpdateCartItemUseCaseImpl @Inject constructor(
) : UpdateCartItemUseCase {
override suspend fun updateLineItem(lineItemModel: LineItemModel) {
withContext(ioDispatcher) {
productRepository.updateLineItemQuantityById(
lineItemModel.quantity!!,
lineItemModel.id!!
)
if (lineItemModel.quantity != null && lineItemModel.id != null) {
productRepository.updateLineItemQuantityById(
lineItemModel.quantity!!,
lineItemModel.id!!
)
}
}
}

override suspend fun removeLineItem(lineItemModel: LineItemModel) {
withContext(ioDispatcher) {
productRepository.removeLineItemById(lineItemModel.id!!)
if (lineItemModel.id != null) {
productRepository.removeLineItemById(lineItemModel.id!!)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.hieuwu.groceriesstore.domain.usecases.impl

import com.hieuwu.groceriesstore.data.repository.ProductRepository
import com.hieuwu.groceriesstore.domain.models.ProductModel
import com.hieuwu.groceriesstore.domain.usecases.GetProductDetailUseCase
import junit.framework.TestCase
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.runBlocking
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mock
import org.mockito.junit.MockitoJUnitRunner
import org.mockito.kotlin.whenever

@RunWith(MockitoJUnitRunner::class)
class GetProductDetailUseCaseImplTest {

@Mock
lateinit var mockedProductRepository: ProductRepository

private lateinit var testee: GetProductDetailUseCase

@Before
fun setup() {
testee = GetProductDetailUseCaseImpl(productRepository = mockedProductRepository)
}

@Test
fun productDetailsAvailable_whenExecuted_thenReturnCorrectValue() {
val input = "1"
val mockProduct = ProductModel("1")

whenever(mockedProductRepository.getProductById(input)).thenReturn(flow {
emit(mockProduct)
})

runBlocking {
val result = testee.getProductDetail(input)
result.collect {
TestCase.assertEquals(mockProduct, it)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.hieuwu.groceriesstore.domain.usecases.impl

import com.hieuwu.groceriesstore.data.repository.ProductRepository
import com.hieuwu.groceriesstore.domain.models.ProductModel
import com.hieuwu.groceriesstore.domain.usecases.GetProductsByCategoryUseCase
import junit.framework.TestCase
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.runBlocking
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mock
import org.mockito.junit.MockitoJUnitRunner
import org.mockito.kotlin.whenever

@RunWith(MockitoJUnitRunner::class)
class GetProductsByCategoryUseCaseImplTest {

@Mock
lateinit var mockedProductRepository: ProductRepository

private lateinit var testee: GetProductsByCategoryUseCaseImpl

@Before
fun setup() {
testee = GetProductsByCategoryUseCaseImpl(productRepository = mockedProductRepository)
}

@Test
fun productsAvailable_whenExecute_thenReturnCorrectValue() {
val input = GetProductsByCategoryUseCase.Input("20")
val mockedProducts = listOf(ProductModel("1"), ProductModel("2"))

whenever(mockedProductRepository.getAllProductsByCategory(input.categoryId)).thenReturn(flow {
emit(mockedProducts)
})

runBlocking {
val result = testee.execute(input)
result.result.collect {
TestCase.assertTrue(it.isNotEmpty())
TestCase.assertEquals(mockedProducts[0], it[0])
TestCase.assertEquals(mockedProducts[0].id, it[0].id)
}
}
}

@Test
fun productsNotAvailable_whenExecute_thenReturnCorrectValue() {
val input = GetProductsByCategoryUseCase.Input("20")
val mockedProducts = listOf<ProductModel>()

whenever(mockedProductRepository.getAllProductsByCategory(input.categoryId)).thenReturn(flow {
emit(mockedProducts)
})

runBlocking {
val result = testee.execute(input)
result.result.collect {
TestCase.assertTrue(it.isEmpty())
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.hieuwu.groceriesstore.domain.usecases.impl

import com.hieuwu.groceriesstore.data.repository.CategoryRepository
import com.hieuwu.groceriesstore.data.repository.ProductRepository
import com.hieuwu.groceriesstore.data.repository.RecipeRepository
import com.hieuwu.groceriesstore.domain.usecases.RefreshAppDataUseCase
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runBlocking
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mock
import org.mockito.junit.MockitoJUnitRunner
import org.mockito.kotlin.verify

@RunWith(MockitoJUnitRunner::class)
class RefreshAppDataUseCaseImplTest {

@Mock
lateinit var mockedProductRepository: ProductRepository

@Mock
lateinit var mockedCategoryRepository: CategoryRepository

@Mock
lateinit var mockedRecipeRepository: RecipeRepository

private lateinit var testee: RefreshAppDataUseCase

@Before
fun setup() {
testee = RefreshAppDataUseCaseImpl(
productRepository = mockedProductRepository,
categoryRepository = mockedCategoryRepository,
recipeRepository = mockedRecipeRepository,
ioDispatcher = Dispatchers.IO
)
}

@Test
fun whenExecute_thenCallRepositories() {
runBlocking {
testee.execute(Unit)

verify(mockedProductRepository).refreshDatabase()
verify(mockedCategoryRepository).refreshDatabase()
verify(mockedRecipeRepository).refreshDatabase()
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.hieuwu.groceriesstore.domain.usecases.impl

import com.hieuwu.groceriesstore.data.repository.ProductRepository
import com.hieuwu.groceriesstore.domain.models.ProductModel
import com.hieuwu.groceriesstore.domain.usecases.SearchProductUseCase
import junit.framework.TestCase
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.runBlocking
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mock
import org.mockito.junit.MockitoJUnitRunner
import org.mockito.kotlin.whenever

@RunWith(MockitoJUnitRunner::class)
class SearchProductUseCaseImplTest {

@Mock
lateinit var mockedProductRepository: ProductRepository

private lateinit var testee: SearchProductUseCase

@Before
fun setup() {
testee = SearchProductUseCaseImpl(
productRepository = mockedProductRepository,
ioDispatcher = Dispatchers.IO
)
}

@Test
fun productsAvailable_whenExecute_thenReturnCorrectValue() {
val input = SearchProductUseCase.Input("abc")
val mockProducts = listOf(ProductModel("1"), ProductModel("2"))

whenever(mockedProductRepository.searchProductsListByName(input.name)).thenReturn(flow {
emit(mockProducts)
})

runBlocking {
val result = testee.execute(input)
result.result.collect {
TestCase.assertTrue(it.isNotEmpty())
TestCase.assertEquals(mockProducts[0], it[0])
TestCase.assertEquals(mockProducts[1].id, it[1].id)
}
}
}

@Test
fun productsNotAvailable_whenExecute_thenReturnCorrectValue() {
val input = SearchProductUseCase.Input("abc")
val mockProducts = listOf<ProductModel>()

whenever(mockedProductRepository.searchProductsListByName(input.name)).thenReturn(flow {
emit(mockProducts)
})

runBlocking {
val result = testee.execute(input)
result.result.collect {
TestCase.assertTrue(it.isEmpty())
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import junit.framework.TestCase.assertEquals
import kotlinx.coroutines.Dispatchers
import org.mockito.Mock
import org.mockito.junit.MockitoJUnitRunner
import org.mockito.kotlin.whenever
Expand All @@ -23,7 +24,8 @@ class SignInUseCaseImplTest {
@Before
fun setUp() {
testee = SignInUseCaseImpl(
userRepository = mockedUserRepository
userRepository = mockedUserRepository,
ioDispatcher = Dispatchers.IO
)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.hieuwu.groceriesstore.domain.usecases.impl

import com.hieuwu.groceriesstore.data.repository.UserRepository
import com.hieuwu.groceriesstore.domain.usecases.SignOutUseCase
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runBlocking
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mock
import org.mockito.junit.MockitoJUnitRunner
import org.mockito.kotlin.verify

@RunWith(MockitoJUnitRunner::class)
class SignOutUseCaseImplTest {

@Mock
lateinit var mockedUserRepository: UserRepository
private lateinit var testee: SignOutUseCase

@Before
fun setup() {
testee = SignOutUseCaseImpl(
userRepository = mockedUserRepository,
ioDispatcher = Dispatchers.IO
)
}

@Test
fun whenExecute_thenCallUserRepository() {
val input = SignOutUseCase.Input()

runBlocking {
testee.execute(input)

verify(mockedUserRepository).clearUser()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.hieuwu.groceriesstore.domain.usecases.impl
import com.hieuwu.groceriesstore.data.repository.OrderRepository
import com.hieuwu.groceriesstore.domain.models.OrderModel
import com.hieuwu.groceriesstore.domain.usecases.SubmitOrderUseCase
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runBlocking
import org.junit.Before
import org.junit.Test
Expand All @@ -18,13 +19,13 @@ class SubmitOrderUseCaseImplTest {

@Mock
lateinit var mockedOrderRepository: OrderRepository

private lateinit var testee: SubmitOrderUseCase

@Before
fun setUp() {
testee = SubmitOrderUseCaseImpl(
orderRepository = mockedOrderRepository
orderRepository = mockedOrderRepository,
ioDispatcher = Dispatchers.IO
)
}

Expand Down
Loading

0 comments on commit 1c03f41

Please sign in to comment.