diff --git a/.github/workflows/Build.yaml b/.github/workflows/Build.yaml index 94d36866..03958e19 100644 --- a/.github/workflows/Build.yaml +++ b/.github/workflows/Build.yaml @@ -79,7 +79,7 @@ jobs: with: slack_token: ${{ secrets.SLACK_TOKEN }} channel_id: ${{ secrets.SLACK_CHANNEL }} - file_path: '**/build/test-results/test*UnitTest/**.xml' + file_path: './app/build/test-results/test*UnitTest/**.xml' initial_comment: 'Unit Test Reports' - name: Upload lint reports @@ -87,7 +87,7 @@ jobs: with: slack_token: ${{ secrets.SLACK_TOKEN }} channel_id: ${{ secrets.SLACK_CHANNEL }} - file_path: '**/build/reports/lint-results-*.html' + file_path: './app/build/reports/lint-results-*.html' initial_comment: 'Lint Reports' screenshot_tests: @@ -257,5 +257,5 @@ jobs: with: slack_token: ${{ secrets.SLACK_TOKEN }} channel_id: ${{ secrets.SLACK_CHANNEL }} - file_path: '**/build/reports/jacoco/' + file_path: './app/build/reports/jacoco/' initial_comment: 'Local Coverage Reports' \ No newline at end of file diff --git a/core/data/src/main/kotlin/com/niyaj/data/data/repository/MarketListItemRepositoryImpl.kt b/core/data/src/main/kotlin/com/niyaj/data/data/repository/MarketListItemRepositoryImpl.kt index 488679b9..f5ef13b9 100644 --- a/core/data/src/main/kotlin/com/niyaj/data/data/repository/MarketListItemRepositoryImpl.kt +++ b/core/data/src/main/kotlin/com/niyaj/data/data/repository/MarketListItemRepositoryImpl.kt @@ -24,7 +24,7 @@ import com.niyaj.data.repository.MarketListItemRepository import com.niyaj.database.dao.MarketListWIthItemsDao import com.niyaj.model.MarketItemAndQuantity import com.niyaj.model.MarketListAndType -import com.niyaj.model.searchItems +import com.niyaj.model.searchMarketType import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.async import kotlinx.coroutines.flow.Flow @@ -47,7 +47,7 @@ class MarketListItemRepositoryImpl @Inject constructor( marketListDao .getAllMarketItemsById(listTypeId) .distinctUntilChanged() - .mapLatest { it.searchItems(searchText) } + .mapLatest { it.searchMarketType(searchText) } } } @@ -58,7 +58,7 @@ class MarketListItemRepositoryImpl @Inject constructor( return withContext(ioDispatcher) { marketListDao.getAllMarketItemsByIds(listTypeIds) .distinctUntilChanged() - .mapLatest { it.searchItems(searchText) } + .mapLatest { it.searchMarketType(searchText) } } } diff --git a/core/data/src/main/kotlin/com/niyaj/data/data/repository/MarketTypeRepositoryImpl.kt b/core/data/src/main/kotlin/com/niyaj/data/data/repository/MarketTypeRepositoryImpl.kt index 096fb630..edd921bc 100644 --- a/core/data/src/main/kotlin/com/niyaj/data/data/repository/MarketTypeRepositoryImpl.kt +++ b/core/data/src/main/kotlin/com/niyaj/data/data/repository/MarketTypeRepositoryImpl.kt @@ -26,7 +26,7 @@ import com.niyaj.database.dao.MarketTypeDao import com.niyaj.database.model.MarketTypeEntity import com.niyaj.database.model.asExternalModel import com.niyaj.model.MarketType -import com.niyaj.model.searchItems +import com.niyaj.model.searchMarketType import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.mapLatest @@ -42,7 +42,7 @@ class MarketTypeRepositoryImpl @Inject constructor( return withContext(ioDispatcher) { marketTypeDao.getAllMarketTypes() .mapLatest(List::asExternalModel) - .mapLatest { it.searchItems(searchText) } + .mapLatest { it.searchMarketType(searchText) } } } diff --git a/core/data/src/main/kotlin/com/niyaj/data/data/repository/ProductRepositoryImpl.kt b/core/data/src/main/kotlin/com/niyaj/data/data/repository/ProductRepositoryImpl.kt index 8310a527..299b71d0 100644 --- a/core/data/src/main/kotlin/com/niyaj/data/data/repository/ProductRepositoryImpl.kt +++ b/core/data/src/main/kotlin/com/niyaj/data/data/repository/ProductRepositoryImpl.kt @@ -29,7 +29,7 @@ import com.niyaj.model.Category import com.niyaj.model.Product import com.niyaj.model.ProductIdWithPrice import com.niyaj.model.ProductWiseOrder -import com.niyaj.model.filterProducts +import com.niyaj.model.searchProducts import kotlinx.collections.immutable.ImmutableList import kotlinx.collections.immutable.toImmutableList import kotlinx.coroutines.CoroutineDispatcher @@ -74,7 +74,7 @@ class ProductRepositoryImpl @Inject constructor( list } }.mapLatest { - it.filterProducts(searchText) + it.searchProducts(searchText) } } } diff --git a/core/domain/src/main/java/com/niyaj/domain/market/ValidateUnitValueUseCase.kt b/core/domain/src/main/java/com/niyaj/domain/market/ValidateUnitValueUseCase.kt index 20a2a382..9f7576f7 100644 --- a/core/domain/src/main/java/com/niyaj/domain/market/ValidateUnitValueUseCase.kt +++ b/core/domain/src/main/java/com/niyaj/domain/market/ValidateUnitValueUseCase.kt @@ -18,7 +18,6 @@ package com.niyaj.domain.market import com.niyaj.common.result.ValidationResult -import com.niyaj.common.tags.MeasureUnitTestTags import com.niyaj.common.tags.MeasureUnitTestTags.UNIT_VALUE_EMPTY_ERROR import com.niyaj.common.tags.MeasureUnitTestTags.UNIT_VALUE_INVALID import com.niyaj.common.utils.safeDouble @@ -32,7 +31,7 @@ class ValidateUnitValueUseCase @Inject constructor() { try { if (unitValue.safeDouble() <= 0) { - return ValidationResult(false, MeasureUnitTestTags.UNIT_VALUE_LESS_THAN_FIVE_ERROR) + return ValidationResult(false, UNIT_VALUE_INVALID) } } catch (e: Exception) { return ValidationResult(false, UNIT_VALUE_INVALID) diff --git a/core/domain/src/main/java/com/niyaj/domain/product/ValidateProductPriceUseCase.kt b/core/domain/src/main/java/com/niyaj/domain/product/ValidateProductPriceUseCase.kt index 410c6dcb..7865724f 100644 --- a/core/domain/src/main/java/com/niyaj/domain/product/ValidateProductPriceUseCase.kt +++ b/core/domain/src/main/java/com/niyaj/domain/product/ValidateProductPriceUseCase.kt @@ -23,7 +23,7 @@ import com.niyaj.common.tags.ProductTestTags.PRODUCT_PRICE_LENGTH_ERROR import javax.inject.Inject class ValidateProductPriceUseCase @Inject constructor() { - operator fun invoke(productPrice: Int, type: String? = null): ValidationResult { + operator fun invoke(productPrice: Int): ValidationResult { if (productPrice == 0) { return ValidationResult( successful = false, @@ -31,7 +31,7 @@ class ValidateProductPriceUseCase @Inject constructor() { ) } - if (type.isNullOrEmpty() && productPrice < 10) { + if (productPrice < 10) { return ValidationResult( successful = false, errorMessage = PRODUCT_PRICE_LENGTH_ERROR, diff --git a/core/domain/src/test/java/com/niyaj/domain/ValidateAbsentDateUseCaseTest.kt b/core/domain/src/test/java/com/niyaj/domain/absent/ValidateAbsentDateUseCaseTest.kt similarity index 97% rename from core/domain/src/test/java/com/niyaj/domain/ValidateAbsentDateUseCaseTest.kt rename to core/domain/src/test/java/com/niyaj/domain/absent/ValidateAbsentDateUseCaseTest.kt index e8008ad4..9c85816d 100644 --- a/core/domain/src/test/java/com/niyaj/domain/ValidateAbsentDateUseCaseTest.kt +++ b/core/domain/src/test/java/com/niyaj/domain/absent/ValidateAbsentDateUseCaseTest.kt @@ -15,13 +15,12 @@ * */ -package com.niyaj.domain +package com.niyaj.domain.absent import com.niyaj.common.tags.AbsentScreenTags.ABSENT_DATE_EMPTY import com.niyaj.common.tags.AbsentScreenTags.ABSENT_DATE_EXIST import com.niyaj.common.utils.getDateInMilliseconds import com.niyaj.common.utils.getStartTime -import com.niyaj.domain.absent.ValidateAbsentDateUseCase import com.niyaj.testing.repository.TestAbsentRepository import com.niyaj.testing.util.MainDispatcherRule import kotlinx.coroutines.test.UnconfinedTestDispatcher diff --git a/core/domain/src/test/java/com/niyaj/domain/ValidateAbsentEmployeeUseCaseTest.kt b/core/domain/src/test/java/com/niyaj/domain/absent/ValidateAbsentEmployeeUseCaseTest.kt similarity index 94% rename from core/domain/src/test/java/com/niyaj/domain/ValidateAbsentEmployeeUseCaseTest.kt rename to core/domain/src/test/java/com/niyaj/domain/absent/ValidateAbsentEmployeeUseCaseTest.kt index f0a90b23..cc24b145 100644 --- a/core/domain/src/test/java/com/niyaj/domain/ValidateAbsentEmployeeUseCaseTest.kt +++ b/core/domain/src/test/java/com/niyaj/domain/absent/ValidateAbsentEmployeeUseCaseTest.kt @@ -15,10 +15,9 @@ * */ -package com.niyaj.domain +package com.niyaj.domain.absent import com.niyaj.common.tags.AbsentScreenTags.ABSENT_EMPLOYEE_NAME_EMPTY -import com.niyaj.domain.absent.ValidateAbsentEmployeeUseCase import com.niyaj.testing.util.MainDispatcherRule import org.junit.Assert.assertEquals import org.junit.Assert.assertFalse diff --git a/core/domain/src/test/java/com/niyaj/domain/ValidateItemNameUseCaseTest.kt b/core/domain/src/test/java/com/niyaj/domain/addonitem/ValidateItemNameUseCaseTest.kt similarity index 97% rename from core/domain/src/test/java/com/niyaj/domain/ValidateItemNameUseCaseTest.kt rename to core/domain/src/test/java/com/niyaj/domain/addonitem/ValidateItemNameUseCaseTest.kt index 870f1812..45f2773b 100644 --- a/core/domain/src/test/java/com/niyaj/domain/ValidateItemNameUseCaseTest.kt +++ b/core/domain/src/test/java/com/niyaj/domain/addonitem/ValidateItemNameUseCaseTest.kt @@ -15,12 +15,11 @@ * */ -package com.niyaj.domain +package com.niyaj.domain.addonitem import com.niyaj.common.tags.AddOnTestTags import com.niyaj.common.tags.AddOnTestTags.ADDON_NAME_EMPTY_ERROR import com.niyaj.common.tags.AddOnTestTags.ADDON_WHITELIST_ITEM -import com.niyaj.domain.addonitem.ValidateItemNameUseCase import com.niyaj.testing.repository.TestAddOnItemRepository import com.niyaj.testing.util.MainDispatcherRule import kotlinx.coroutines.test.UnconfinedTestDispatcher diff --git a/core/domain/src/test/java/com/niyaj/domain/ValidateItemPriceUseCaseTest.kt b/core/domain/src/test/java/com/niyaj/domain/addonitem/ValidateItemPriceUseCaseTest.kt similarity index 95% rename from core/domain/src/test/java/com/niyaj/domain/ValidateItemPriceUseCaseTest.kt rename to core/domain/src/test/java/com/niyaj/domain/addonitem/ValidateItemPriceUseCaseTest.kt index f82de2b3..c8a2c822 100644 --- a/core/domain/src/test/java/com/niyaj/domain/ValidateItemPriceUseCaseTest.kt +++ b/core/domain/src/test/java/com/niyaj/domain/addonitem/ValidateItemPriceUseCaseTest.kt @@ -15,10 +15,9 @@ * */ -package com.niyaj.domain +package com.niyaj.domain.addonitem import com.niyaj.common.tags.AddOnTestTags -import com.niyaj.domain.addonitem.ValidateItemPriceUseCase import com.niyaj.testing.util.MainDispatcherRule import org.junit.Assert.assertEquals import org.junit.Assert.assertFalse diff --git a/core/domain/src/test/java/com/niyaj/domain/ValidateAddressNameUseCaseTest.kt b/core/domain/src/test/java/com/niyaj/domain/address/ValidateAddressNameUseCaseTest.kt similarity index 97% rename from core/domain/src/test/java/com/niyaj/domain/ValidateAddressNameUseCaseTest.kt rename to core/domain/src/test/java/com/niyaj/domain/address/ValidateAddressNameUseCaseTest.kt index 7da4e9a3..997699ff 100644 --- a/core/domain/src/test/java/com/niyaj/domain/ValidateAddressNameUseCaseTest.kt +++ b/core/domain/src/test/java/com/niyaj/domain/address/ValidateAddressNameUseCaseTest.kt @@ -15,10 +15,9 @@ * */ -package com.niyaj.domain +package com.niyaj.domain.address import com.niyaj.common.tags.AddressTestTags -import com.niyaj.domain.address.ValidateAddressNameUseCase import com.niyaj.testing.repository.TestAddressRepository import com.niyaj.testing.util.MainDispatcherRule import kotlinx.coroutines.test.UnconfinedTestDispatcher diff --git a/core/domain/src/test/java/com/niyaj/domain/ValidateAddressShortNameUseCaseTest.kt b/core/domain/src/test/java/com/niyaj/domain/address/ValidateAddressShortNameUseCaseTest.kt similarity index 95% rename from core/domain/src/test/java/com/niyaj/domain/ValidateAddressShortNameUseCaseTest.kt rename to core/domain/src/test/java/com/niyaj/domain/address/ValidateAddressShortNameUseCaseTest.kt index bae9adb2..f1b591e7 100644 --- a/core/domain/src/test/java/com/niyaj/domain/ValidateAddressShortNameUseCaseTest.kt +++ b/core/domain/src/test/java/com/niyaj/domain/address/ValidateAddressShortNameUseCaseTest.kt @@ -15,10 +15,9 @@ * */ -package com.niyaj.domain +package com.niyaj.domain.address import com.niyaj.common.tags.AddressTestTags -import com.niyaj.domain.address.ValidateAddressShortNameUseCase import com.niyaj.testing.util.MainDispatcherRule import org.junit.Assert.assertEquals import org.junit.Assert.assertFalse diff --git a/core/domain/src/test/java/com/niyaj/domain/cartorder/ValidateOrderAddressNameUseCaseTest.kt b/core/domain/src/test/java/com/niyaj/domain/cartorder/ValidateOrderAddressNameUseCaseTest.kt new file mode 100644 index 00000000..8764c040 --- /dev/null +++ b/core/domain/src/test/java/com/niyaj/domain/cartorder/ValidateOrderAddressNameUseCaseTest.kt @@ -0,0 +1,55 @@ +/* + * Copyright 2024 Sk Niyaj Ali + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package com.niyaj.domain.cartorder + +import com.niyaj.common.tags.CartOrderTestTags.ADDRESS_NAME_LENGTH_ERROR +import com.niyaj.common.tags.CartOrderTestTags.CART_ORDER_NAME_EMPTY_ERROR +import com.niyaj.testing.util.MainDispatcherRule +import org.junit.Rule +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertFalse +import kotlin.test.assertNull + +class ValidateOrderAddressNameUseCaseTest { + @get:Rule + val dispatcherRule = MainDispatcherRule() + val useCase = ValidateOrderAddressNameUseCase() + + @Test + fun `given empty address name when invoke then return error`() { + val result = useCase("") + assert(result.successful.not()) + assertEquals(CART_ORDER_NAME_EMPTY_ERROR, result.errorMessage) + } + + @Test + fun `given address name length less than 6 digit return error`() { + val result = useCase("Test") + + assertFalse(result.successful) + assertEquals(ADDRESS_NAME_LENGTH_ERROR, result.errorMessage) + } + + @Test + fun `given valid address name return success`() { + val result = useCase("Test Address") + assert(result.successful) + assertNull(result.errorMessage) + } +} diff --git a/core/domain/src/test/java/com/niyaj/domain/cartorder/ValidateOrderCustomerPhoneUseCaseTest.kt b/core/domain/src/test/java/com/niyaj/domain/cartorder/ValidateOrderCustomerPhoneUseCaseTest.kt new file mode 100644 index 00000000..25316d57 --- /dev/null +++ b/core/domain/src/test/java/com/niyaj/domain/cartorder/ValidateOrderCustomerPhoneUseCaseTest.kt @@ -0,0 +1,68 @@ +/* + * Copyright 2024 Sk Niyaj Ali + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package com.niyaj.domain.cartorder + +import com.niyaj.common.tags.CartOrderTestTags.CART_ORDER_PHONE_EMPTY_ERROR +import com.niyaj.common.tags.CartOrderTestTags.CUSTOMER_PHONE_LENGTH_ERROR +import com.niyaj.common.tags.CartOrderTestTags.CUSTOMER_PHONE_LETTER_ERROR +import com.niyaj.testing.util.MainDispatcherRule +import org.junit.Rule +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertNull + +class ValidateOrderCustomerPhoneUseCaseTest { + @get:Rule + val dispatcherRule = MainDispatcherRule() + val useCase = ValidateOrderCustomerPhoneUseCase() + + @Test + fun `validate with empty phone return error`() { + val result = useCase("") + assert(result.successful.not()) + assertEquals(CART_ORDER_PHONE_EMPTY_ERROR, result.errorMessage) + } + + @Test + fun `validate phone no with less than 10 digit return error`() { + val result = useCase("123") + assert(result.successful.not()) + assertEquals(CUSTOMER_PHONE_LENGTH_ERROR, result.errorMessage) + } + + @Test + fun `validate phone no with greater than 10 digit return error`() { + val result = useCase("1239078563421") + assert(result.successful.not()) + assertEquals(CUSTOMER_PHONE_LENGTH_ERROR, result.errorMessage) + } + + @Test + fun `validate phone no with with letter return error`() { + val result = useCase("123456789P") + assert(result.successful.not()) + assertEquals(CUSTOMER_PHONE_LETTER_ERROR, result.errorMessage) + } + + @Test + fun `validate with valid phone return success`() { + val result = useCase("1234567890") + assert(result.successful) + assertNull(result.errorMessage) + } +} diff --git a/core/domain/src/test/java/com/niyaj/domain/ValidateCategoryNameUseCaseTest.kt b/core/domain/src/test/java/com/niyaj/domain/category/ValidateCategoryNameUseCaseTest.kt similarity index 97% rename from core/domain/src/test/java/com/niyaj/domain/ValidateCategoryNameUseCaseTest.kt rename to core/domain/src/test/java/com/niyaj/domain/category/ValidateCategoryNameUseCaseTest.kt index 957b2bdf..fb093574 100644 --- a/core/domain/src/test/java/com/niyaj/domain/ValidateCategoryNameUseCaseTest.kt +++ b/core/domain/src/test/java/com/niyaj/domain/category/ValidateCategoryNameUseCaseTest.kt @@ -15,10 +15,9 @@ * */ -package com.niyaj.domain +package com.niyaj.domain.category import com.niyaj.common.tags.CategoryConstants -import com.niyaj.domain.category.ValidateCategoryNameUseCase import com.niyaj.testing.repository.TestCategoryRepository import com.niyaj.testing.util.MainDispatcherRule import kotlinx.coroutines.test.UnconfinedTestDispatcher diff --git a/core/domain/src/test/java/com/niyaj/domain/ValidateChargesNameUseCaseTest.kt b/core/domain/src/test/java/com/niyaj/domain/charges/ValidateChargesNameUseCaseTest.kt similarity index 97% rename from core/domain/src/test/java/com/niyaj/domain/ValidateChargesNameUseCaseTest.kt rename to core/domain/src/test/java/com/niyaj/domain/charges/ValidateChargesNameUseCaseTest.kt index d65caffb..b8037928 100644 --- a/core/domain/src/test/java/com/niyaj/domain/ValidateChargesNameUseCaseTest.kt +++ b/core/domain/src/test/java/com/niyaj/domain/charges/ValidateChargesNameUseCaseTest.kt @@ -15,10 +15,9 @@ * */ -package com.niyaj.domain +package com.niyaj.domain.charges import com.niyaj.common.tags.ChargesTestTags -import com.niyaj.domain.charges.ValidateChargesNameUseCase import com.niyaj.testing.repository.TestChargesRepository import com.niyaj.testing.util.MainDispatcherRule import kotlinx.coroutines.test.UnconfinedTestDispatcher diff --git a/core/domain/src/test/java/com/niyaj/domain/ValidateChargesPriceUseCaseTest.kt b/core/domain/src/test/java/com/niyaj/domain/charges/ValidateChargesPriceUseCaseTest.kt similarity index 96% rename from core/domain/src/test/java/com/niyaj/domain/ValidateChargesPriceUseCaseTest.kt rename to core/domain/src/test/java/com/niyaj/domain/charges/ValidateChargesPriceUseCaseTest.kt index 1111376d..72cede84 100644 --- a/core/domain/src/test/java/com/niyaj/domain/ValidateChargesPriceUseCaseTest.kt +++ b/core/domain/src/test/java/com/niyaj/domain/charges/ValidateChargesPriceUseCaseTest.kt @@ -15,10 +15,9 @@ * */ -package com.niyaj.domain +package com.niyaj.domain.charges import com.niyaj.common.tags.ChargesTestTags -import com.niyaj.domain.charges.ValidateChargesPriceUseCase import com.niyaj.testing.util.MainDispatcherRule import org.junit.Assert.assertEquals import org.junit.Assert.assertFalse diff --git a/core/domain/src/test/java/com/niyaj/domain/ValidateCustomerEmailUseCaseTest.kt b/core/domain/src/test/java/com/niyaj/domain/customer/ValidateCustomerEmailUseCaseTest.kt similarity index 97% rename from core/domain/src/test/java/com/niyaj/domain/ValidateCustomerEmailUseCaseTest.kt rename to core/domain/src/test/java/com/niyaj/domain/customer/ValidateCustomerEmailUseCaseTest.kt index 11afd39a..b802ee1e 100644 --- a/core/domain/src/test/java/com/niyaj/domain/ValidateCustomerEmailUseCaseTest.kt +++ b/core/domain/src/test/java/com/niyaj/domain/customer/ValidateCustomerEmailUseCaseTest.kt @@ -15,10 +15,9 @@ * */ -package com.niyaj.domain +package com.niyaj.domain.customer import com.niyaj.common.tags.CustomerTestTags -import com.niyaj.domain.customer.ValidateCustomerEmailUseCase import com.niyaj.testing.util.MainDispatcherRule import kotlinx.coroutines.test.runTest import org.junit.Assert.assertEquals diff --git a/core/domain/src/test/java/com/niyaj/domain/ValidateCustomerNameUseCaseTest.kt b/core/domain/src/test/java/com/niyaj/domain/customer/ValidateCustomerNameUseCaseTest.kt similarity index 96% rename from core/domain/src/test/java/com/niyaj/domain/ValidateCustomerNameUseCaseTest.kt rename to core/domain/src/test/java/com/niyaj/domain/customer/ValidateCustomerNameUseCaseTest.kt index 75a26d93..2528e98d 100644 --- a/core/domain/src/test/java/com/niyaj/domain/ValidateCustomerNameUseCaseTest.kt +++ b/core/domain/src/test/java/com/niyaj/domain/customer/ValidateCustomerNameUseCaseTest.kt @@ -15,10 +15,9 @@ * */ -package com.niyaj.domain +package com.niyaj.domain.customer import com.niyaj.common.tags.CustomerTestTags -import com.niyaj.domain.customer.ValidateCustomerNameUseCase import com.niyaj.testing.util.MainDispatcherRule import org.junit.Assert.assertEquals import org.junit.Assert.assertFalse diff --git a/core/domain/src/test/java/com/niyaj/domain/ValidateCustomerPhoneUseCaseTest.kt b/core/domain/src/test/java/com/niyaj/domain/customer/ValidateCustomerPhoneUseCaseTest.kt similarity index 97% rename from core/domain/src/test/java/com/niyaj/domain/ValidateCustomerPhoneUseCaseTest.kt rename to core/domain/src/test/java/com/niyaj/domain/customer/ValidateCustomerPhoneUseCaseTest.kt index 9aa9388f..0ee53855 100644 --- a/core/domain/src/test/java/com/niyaj/domain/ValidateCustomerPhoneUseCaseTest.kt +++ b/core/domain/src/test/java/com/niyaj/domain/customer/ValidateCustomerPhoneUseCaseTest.kt @@ -15,10 +15,9 @@ * */ -package com.niyaj.domain +package com.niyaj.domain.customer import com.niyaj.common.tags.CustomerTestTags -import com.niyaj.domain.customer.ValidateCustomerPhoneUseCase import com.niyaj.testing.repository.TestCustomerRepository import com.niyaj.testing.util.MainDispatcherRule import kotlinx.coroutines.test.UnconfinedTestDispatcher diff --git a/core/domain/src/test/java/com/niyaj/domain/ValidateEmployeeNameUseCaseTest.kt b/core/domain/src/test/java/com/niyaj/domain/employee/ValidateEmployeeNameUseCaseTest.kt similarity index 97% rename from core/domain/src/test/java/com/niyaj/domain/ValidateEmployeeNameUseCaseTest.kt rename to core/domain/src/test/java/com/niyaj/domain/employee/ValidateEmployeeNameUseCaseTest.kt index 6e2b2280..22dfeee0 100644 --- a/core/domain/src/test/java/com/niyaj/domain/ValidateEmployeeNameUseCaseTest.kt +++ b/core/domain/src/test/java/com/niyaj/domain/employee/ValidateEmployeeNameUseCaseTest.kt @@ -15,10 +15,9 @@ * */ -package com.niyaj.domain +package com.niyaj.domain.employee import com.niyaj.common.tags.EmployeeTestTags -import com.niyaj.domain.employee.ValidateEmployeeNameUseCase import com.niyaj.testing.repository.TestEmployeeRepository import com.niyaj.testing.util.MainDispatcherRule import kotlinx.coroutines.test.UnconfinedTestDispatcher diff --git a/core/domain/src/test/java/com/niyaj/domain/ValidateEmployeePhoneUseCaseTest.kt b/core/domain/src/test/java/com/niyaj/domain/employee/ValidateEmployeePhoneUseCaseTest.kt similarity index 97% rename from core/domain/src/test/java/com/niyaj/domain/ValidateEmployeePhoneUseCaseTest.kt rename to core/domain/src/test/java/com/niyaj/domain/employee/ValidateEmployeePhoneUseCaseTest.kt index 7174e3b0..914b4d90 100644 --- a/core/domain/src/test/java/com/niyaj/domain/ValidateEmployeePhoneUseCaseTest.kt +++ b/core/domain/src/test/java/com/niyaj/domain/employee/ValidateEmployeePhoneUseCaseTest.kt @@ -15,10 +15,9 @@ * */ -package com.niyaj.domain +package com.niyaj.domain.employee import com.niyaj.common.tags.EmployeeTestTags -import com.niyaj.domain.employee.ValidateEmployeePhoneUseCase import com.niyaj.testing.repository.TestEmployeeRepository import com.niyaj.testing.util.MainDispatcherRule import kotlinx.coroutines.test.UnconfinedTestDispatcher diff --git a/core/domain/src/test/java/com/niyaj/domain/ValidateEmployeePositionUseCaseTest.kt b/core/domain/src/test/java/com/niyaj/domain/employee/ValidateEmployeePositionUseCaseTest.kt similarity index 94% rename from core/domain/src/test/java/com/niyaj/domain/ValidateEmployeePositionUseCaseTest.kt rename to core/domain/src/test/java/com/niyaj/domain/employee/ValidateEmployeePositionUseCaseTest.kt index 4922c073..0183e65e 100644 --- a/core/domain/src/test/java/com/niyaj/domain/ValidateEmployeePositionUseCaseTest.kt +++ b/core/domain/src/test/java/com/niyaj/domain/employee/ValidateEmployeePositionUseCaseTest.kt @@ -15,10 +15,9 @@ * */ -package com.niyaj.domain +package com.niyaj.domain.employee import com.niyaj.common.tags.EmployeeTestTags -import com.niyaj.domain.employee.ValidateEmployeePositionUseCase import com.niyaj.testing.util.MainDispatcherRule import org.junit.Assert.assertEquals import org.junit.Assert.assertFalse diff --git a/core/domain/src/test/java/com/niyaj/domain/ValidateEmployeeSalaryUseCaseTest.kt b/core/domain/src/test/java/com/niyaj/domain/employee/ValidateEmployeeSalaryUseCaseTest.kt similarity index 96% rename from core/domain/src/test/java/com/niyaj/domain/ValidateEmployeeSalaryUseCaseTest.kt rename to core/domain/src/test/java/com/niyaj/domain/employee/ValidateEmployeeSalaryUseCaseTest.kt index 0f979205..95980cfc 100644 --- a/core/domain/src/test/java/com/niyaj/domain/ValidateEmployeeSalaryUseCaseTest.kt +++ b/core/domain/src/test/java/com/niyaj/domain/employee/ValidateEmployeeSalaryUseCaseTest.kt @@ -15,10 +15,9 @@ * */ -package com.niyaj.domain +package com.niyaj.domain.employee import com.niyaj.common.tags.EmployeeTestTags -import com.niyaj.domain.employee.ValidateEmployeeSalaryUseCase import com.niyaj.testing.util.MainDispatcherRule import org.junit.Assert.assertEquals import org.junit.Assert.assertFalse diff --git a/core/domain/src/test/java/com/niyaj/domain/ValidateExpenseAmountUseCaseTest.kt b/core/domain/src/test/java/com/niyaj/domain/expense/ValidateExpenseAmountUseCaseTest.kt similarity index 96% rename from core/domain/src/test/java/com/niyaj/domain/ValidateExpenseAmountUseCaseTest.kt rename to core/domain/src/test/java/com/niyaj/domain/expense/ValidateExpenseAmountUseCaseTest.kt index e8f1593b..5bc1ba38 100644 --- a/core/domain/src/test/java/com/niyaj/domain/ValidateExpenseAmountUseCaseTest.kt +++ b/core/domain/src/test/java/com/niyaj/domain/expense/ValidateExpenseAmountUseCaseTest.kt @@ -15,12 +15,11 @@ * */ -package com.niyaj.domain +package com.niyaj.domain.expense import com.niyaj.common.tags.ExpenseTestTags.EXPENSES_PRICE_IS_NOT_VALID import com.niyaj.common.tags.ExpenseTestTags.EXPENSE_PRICE_EMPTY_ERROR import com.niyaj.common.tags.ExpenseTestTags.EXPENSE_PRICE_LESS_THAN_TEN_ERROR -import com.niyaj.domain.expense.ValidateExpenseAmountUseCase import com.niyaj.testing.util.MainDispatcherRule import org.junit.Rule import kotlin.test.Test diff --git a/core/domain/src/test/java/com/niyaj/domain/ValidateExpenseDateUseCaseTest.kt b/core/domain/src/test/java/com/niyaj/domain/expense/ValidateExpenseDateUseCaseTest.kt similarity index 94% rename from core/domain/src/test/java/com/niyaj/domain/ValidateExpenseDateUseCaseTest.kt rename to core/domain/src/test/java/com/niyaj/domain/expense/ValidateExpenseDateUseCaseTest.kt index 0a6e60a4..12704027 100644 --- a/core/domain/src/test/java/com/niyaj/domain/ValidateExpenseDateUseCaseTest.kt +++ b/core/domain/src/test/java/com/niyaj/domain/expense/ValidateExpenseDateUseCaseTest.kt @@ -15,10 +15,9 @@ * */ -package com.niyaj.domain +package com.niyaj.domain.expense import com.niyaj.common.tags.ExpenseTestTags.EXPENSE_DATE_EMPTY_ERROR -import com.niyaj.domain.expense.ValidateExpenseDateUseCase import com.niyaj.testing.util.MainDispatcherRule import org.junit.Assert.assertEquals import org.junit.Assert.assertNull diff --git a/core/domain/src/test/java/com/niyaj/domain/ValidateExpenseNameUseCaseTest.kt b/core/domain/src/test/java/com/niyaj/domain/expense/ValidateExpenseNameUseCaseTest.kt similarity index 95% rename from core/domain/src/test/java/com/niyaj/domain/ValidateExpenseNameUseCaseTest.kt rename to core/domain/src/test/java/com/niyaj/domain/expense/ValidateExpenseNameUseCaseTest.kt index c128cafd..0fed7b1a 100644 --- a/core/domain/src/test/java/com/niyaj/domain/ValidateExpenseNameUseCaseTest.kt +++ b/core/domain/src/test/java/com/niyaj/domain/expense/ValidateExpenseNameUseCaseTest.kt @@ -15,11 +15,10 @@ * */ -package com.niyaj.domain +package com.niyaj.domain.expense import com.niyaj.common.tags.ExpenseTestTags.EXPENSE_NAME_EMPTY_ERROR import com.niyaj.common.tags.ExpenseTestTags.EXPENSE_NAME_LENGTH_ERROR -import com.niyaj.domain.expense.ValidateExpenseNameUseCase import com.niyaj.testing.util.MainDispatcherRule import org.junit.Assert.assertEquals import org.junit.Assert.assertNull diff --git a/core/domain/src/test/java/com/niyaj/domain/market/ValidateItemTypeUseCaseTest.kt b/core/domain/src/test/java/com/niyaj/domain/market/ValidateItemTypeUseCaseTest.kt new file mode 100644 index 00000000..6e7bc6a1 --- /dev/null +++ b/core/domain/src/test/java/com/niyaj/domain/market/ValidateItemTypeUseCaseTest.kt @@ -0,0 +1,47 @@ +/* + * Copyright 2024 Sk Niyaj Ali + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package com.niyaj.domain.market + +import com.niyaj.common.tags.MarketListTestTags.MARKET_ITEM_TYPE_EMPTY_ERROR +import com.niyaj.testing.util.MainDispatcherRule +import org.junit.Rule +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertNull + +class ValidateItemTypeUseCaseTest { + @get:Rule + val dispatcherRule = MainDispatcherRule() + val useCase = ValidateItemTypeUseCase() + + @Test + fun `given invalid typeId when invoke then return error`() { + val result = useCase(0) + + assert(!result.successful) + assertEquals(MARKET_ITEM_TYPE_EMPTY_ERROR, result.errorMessage) + } + + @Test + fun `given non-empty typeId when invoke then return success`() { + val result = useCase(1) + + assert(result.successful) + assertNull(result.errorMessage) + } +} diff --git a/core/domain/src/test/java/com/niyaj/domain/market/ValidateListTypeUseCaseTest.kt b/core/domain/src/test/java/com/niyaj/domain/market/ValidateListTypeUseCaseTest.kt new file mode 100644 index 00000000..83b3aeec --- /dev/null +++ b/core/domain/src/test/java/com/niyaj/domain/market/ValidateListTypeUseCaseTest.kt @@ -0,0 +1,56 @@ +/* + * Copyright 2024 Sk Niyaj Ali + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package com.niyaj.domain.market + +import com.niyaj.common.tags.MarketTypeTags.LIST_NAME_LEAST +import com.niyaj.testing.util.MainDispatcherRule +import org.junit.Rule +import org.junit.Test +import kotlin.test.assertEquals +import kotlin.test.assertFalse +import kotlin.test.assertNull + +class ValidateListTypeUseCaseTest { + @get:Rule + val dispatcherRule = MainDispatcherRule() + val useCase = ValidateListTypeUseCase() + + @Test + fun `validate with empty list type return success`() { + val result = useCase("") + + assert(result.successful) + assertNull(result.errorMessage) + } + + @Test + fun `validate with non-empty list type return success`() { + val result = useCase("AB") + + assertFalse(result.successful) + assertEquals(LIST_NAME_LEAST, result.errorMessage) + } + + @Test + fun `validate with valid list type return success`() { + val result = useCase("Test List") + + assert(result.successful) + assertNull(result.errorMessage) + } +} diff --git a/core/domain/src/test/java/com/niyaj/domain/market/ValidateListTypesUseCaseTest.kt b/core/domain/src/test/java/com/niyaj/domain/market/ValidateListTypesUseCaseTest.kt new file mode 100644 index 00000000..46a7b854 --- /dev/null +++ b/core/domain/src/test/java/com/niyaj/domain/market/ValidateListTypesUseCaseTest.kt @@ -0,0 +1,48 @@ +/* + * Copyright 2024 Sk Niyaj Ali + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package com.niyaj.domain.market + +import com.niyaj.common.tags.MarketTypeTags.LIST_TYPES_ERROR +import com.niyaj.testing.util.MainDispatcherRule +import org.junit.Rule +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertFalse +import kotlin.test.assertNull + +class ValidateListTypesUseCaseTest { + @get:Rule + val dispatcherRule = MainDispatcherRule() + val useCase = ValidateListTypesUseCase() + + @Test + fun `validate with empty list type return error`() { + val result = useCase(emptyList()) + + assertFalse(result.successful) + assertEquals(LIST_TYPES_ERROR, result.errorMessage) + } + + @Test + fun `validate with non-empty list type return success`() { + val result = useCase(listOf("AB")) + + assert(result.successful) + assertNull(result.errorMessage) + } +} diff --git a/core/domain/src/test/java/com/niyaj/domain/market/ValidateMarketItemNameUseCaseTest.kt b/core/domain/src/test/java/com/niyaj/domain/market/ValidateMarketItemNameUseCaseTest.kt new file mode 100644 index 00000000..4e2a3429 --- /dev/null +++ b/core/domain/src/test/java/com/niyaj/domain/market/ValidateMarketItemNameUseCaseTest.kt @@ -0,0 +1,92 @@ +/* + * Copyright 2024 Sk Niyaj Ali + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package com.niyaj.domain.market + +import com.niyaj.common.tags.MarketListTestTags.MARKET_ITEM_NAME_ALREADY_EXIST_ERROR +import com.niyaj.common.tags.MarketListTestTags.MARKET_ITEM_NAME_DIGIT_ERROR +import com.niyaj.common.tags.MarketListTestTags.MARKET_ITEM_NAME_EMPTY_ERROR +import com.niyaj.common.tags.MarketListTestTags.MARKET_ITEM_NAME_LENGTH_ERROR +import com.niyaj.testing.repository.TestMarketItemRepository +import com.niyaj.testing.util.MainDispatcherRule +import kotlinx.coroutines.test.UnconfinedTestDispatcher +import kotlinx.coroutines.test.runTest +import org.junit.Assert.assertEquals +import org.junit.Rule +import kotlin.test.Test +import kotlin.test.assertNull + +class ValidateMarketItemNameUseCaseTest { + @get:Rule + val dispatcherRule = MainDispatcherRule() + + private val repository = TestMarketItemRepository() + private val useCase = ValidateMarketItemNameUseCase( + repository = repository, + ioDispatcher = UnconfinedTestDispatcher(), + ) + + @Test + fun `validate with empty item name return error`() = runTest { + val result = useCase("", null) + + assert(result.successful.not()) + assertEquals(MARKET_ITEM_NAME_EMPTY_ERROR, result.errorMessage) + } + + @Test + fun `validate with item name shorter than 3 characters return error`() = runTest { + val result = useCase("Ab", null) + + assert(result.successful.not()) + assertEquals(MARKET_ITEM_NAME_LENGTH_ERROR, result.errorMessage) + } + + @Test + fun `validate with invalid item name return error`() = runTest { + val result = useCase("Test123", null) + + assert(result.successful.not()) + assertEquals(MARKET_ITEM_NAME_DIGIT_ERROR, result.errorMessage) + } + + @Test + fun `validate with item name already exists return error`() = runTest { + val item = repository.createTestItem() + val result = useCase(item.itemName, null) + + assert(result.successful.not()) + assertEquals(MARKET_ITEM_NAME_ALREADY_EXIST_ERROR, result.errorMessage) + } + + @Test + fun `validate with item name with itemId already exists return success`() = runTest { + val item = repository.createTestItem() + val result = useCase(item.itemName, item.itemId) + + assert(result.successful) + assertNull(result.errorMessage) + } + + @Test + fun `validate with valid name return success`() = runTest { + val result = useCase("Test Item", null) + + assert(result.successful) + assertNull(result.errorMessage) + } +} diff --git a/core/domain/src/test/java/com/niyaj/domain/market/ValidateMarketItemPriceUseCaseTest.kt b/core/domain/src/test/java/com/niyaj/domain/market/ValidateMarketItemPriceUseCaseTest.kt new file mode 100644 index 00000000..77e9a575 --- /dev/null +++ b/core/domain/src/test/java/com/niyaj/domain/market/ValidateMarketItemPriceUseCaseTest.kt @@ -0,0 +1,63 @@ +/* + * Copyright 2024 Sk Niyaj Ali + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package com.niyaj.domain.market + +import com.niyaj.common.tags.MarketListTestTags.MARKET_ITEM_PRICE_INVALID +import com.niyaj.common.tags.MarketListTestTags.MARKET_ITEM_PRICE_LESS_THAN_FIVE_ERROR +import com.niyaj.testing.util.MainDispatcherRule +import org.junit.Assert.assertEquals +import org.junit.Rule +import kotlin.test.Test + +class ValidateMarketItemPriceUseCaseTest { + @get:Rule + val dispatcherRule = MainDispatcherRule() + val useCase = ValidateMarketItemPriceUseCase() + + @Test + fun `given empty price when invoke then return error`() { + val result = useCase("0") + + assert(!result.successful) + assertEquals(MARKET_ITEM_PRICE_LESS_THAN_FIVE_ERROR, result.errorMessage) + } + + @Test + fun `given invalid price when invoke then return error`() { + val result = useCase("1C") + + assert(!result.successful) + assertEquals(MARKET_ITEM_PRICE_INVALID, result.errorMessage) + } + + @Test + fun `given valid price when invoke then return success`() { + val result = useCase("10") + + assert(result.successful) + assertEquals(null, result.errorMessage) + } + + @Test + fun `given null price when invoke then return success`() { + val result = useCase(null) + + assert(result.successful) + assertEquals(null, result.errorMessage) + } +} diff --git a/core/domain/src/test/java/com/niyaj/domain/market/ValidateMeasureUnitUseCaseTest.kt b/core/domain/src/test/java/com/niyaj/domain/market/ValidateMeasureUnitUseCaseTest.kt new file mode 100644 index 00000000..0b783259 --- /dev/null +++ b/core/domain/src/test/java/com/niyaj/domain/market/ValidateMeasureUnitUseCaseTest.kt @@ -0,0 +1,46 @@ +/* + * Copyright 2024 Sk Niyaj Ali + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package com.niyaj.domain.market + +import com.niyaj.common.tags.MarketListTestTags.MARKET_ITEM_MEASURE_EMPTY_ERROR +import com.niyaj.testing.util.MainDispatcherRule +import org.junit.Assert.assertEquals +import org.junit.Rule +import kotlin.test.Test + +class ValidateMeasureUnitUseCaseTest { + @get:Rule + val dispatcherRule = MainDispatcherRule() + val useCase = ValidateMeasureUnitUseCase() + + @Test + fun `given invalid unit id when invoke then return error`() { + val result = useCase(0) + + assert(!result.successful) + assertEquals(MARKET_ITEM_MEASURE_EMPTY_ERROR, result.errorMessage) + } + + @Test + fun `given non-empty unit id when invoke then return success`() { + val result = useCase(1) + + assert(result.successful) + assertEquals(null, result.errorMessage) + } +} diff --git a/core/domain/src/test/java/com/niyaj/domain/market/ValidateTypeNameUseCaseTest.kt b/core/domain/src/test/java/com/niyaj/domain/market/ValidateTypeNameUseCaseTest.kt new file mode 100644 index 00000000..d11c70d0 --- /dev/null +++ b/core/domain/src/test/java/com/niyaj/domain/market/ValidateTypeNameUseCaseTest.kt @@ -0,0 +1,81 @@ +/* + * Copyright 2024 Sk Niyaj Ali + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package com.niyaj.domain.market + +import com.niyaj.common.tags.MarketTypeTags.TYPE_NAME_IS_REQUIRED +import com.niyaj.common.tags.MarketTypeTags.TYPE_NAME_LEAST +import com.niyaj.testing.repository.TestMarketTypeRepository +import com.niyaj.testing.util.MainDispatcherRule +import kotlinx.coroutines.test.UnconfinedTestDispatcher +import kotlinx.coroutines.test.runTest +import org.junit.Assert.assertEquals +import org.junit.Rule +import kotlin.test.Test + +class ValidateTypeNameUseCaseTest { + @get:Rule + val dispatcherRule = MainDispatcherRule() + + private val repository = TestMarketTypeRepository() + private val useCase = ValidateTypeNameUseCase( + repository = repository, + ioDispatcher = UnconfinedTestDispatcher(), + ) + + @Test + fun `validate with empty type name return error`() = runTest { + val result = useCase("") + + assert(result.successful.not()) + assertEquals(TYPE_NAME_IS_REQUIRED, result.errorMessage) + } + + @Test + fun `validate with type name less than 3 characters return error`() = runTest { + val result = useCase("Te") + + assert(result.successful.not()) + assertEquals(TYPE_NAME_LEAST, result.errorMessage) + } + + @Test + fun `validate with valid type name return success`() = runTest { + val result = useCase("Test") + + assert(result.successful) + assertEquals(null, result.errorMessage) + } + + @Test + fun `validate with existing type name return error`() = runTest { + val type = repository.createTestItem() + val result = useCase(type.typeName) + + assert(result.successful.not()) + assertEquals(TYPE_NAME_IS_REQUIRED, result.errorMessage) + } + + @Test + fun `validate with existing type name and typeId return success`() = runTest { + val type = repository.createTestItem() + val result = useCase(type.typeName, type.typeId) + + assert(result.successful) + assertEquals(null, result.errorMessage) + } +} diff --git a/core/domain/src/test/java/com/niyaj/domain/market/ValidateUnitNameUseCaseTest.kt b/core/domain/src/test/java/com/niyaj/domain/market/ValidateUnitNameUseCaseTest.kt new file mode 100644 index 00000000..b1e285b6 --- /dev/null +++ b/core/domain/src/test/java/com/niyaj/domain/market/ValidateUnitNameUseCaseTest.kt @@ -0,0 +1,88 @@ +/* + * Copyright 2024 Sk Niyaj Ali + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package com.niyaj.domain.market + +import com.niyaj.common.tags.MeasureUnitTestTags.UNIT_NAME_DIGIT_ERROR +import com.niyaj.common.tags.MeasureUnitTestTags.UNIT_NAME_EMPTY_ERROR +import com.niyaj.common.tags.MeasureUnitTestTags.UNIT_NAME_LENGTH_ERROR +import com.niyaj.testing.repository.TestMeasureUnitRepository +import com.niyaj.testing.util.MainDispatcherRule +import kotlinx.coroutines.test.UnconfinedTestDispatcher +import kotlinx.coroutines.test.runTest +import org.junit.Assert.assertEquals +import org.junit.Rule +import kotlin.test.Test + +class ValidateUnitNameUseCaseTest { + @get:Rule + val dispatcherRule = MainDispatcherRule() + private val repository = TestMeasureUnitRepository() + private val useCase = ValidateUnitNameUseCase( + repository = repository, + ioDispatcher = UnconfinedTestDispatcher(), + ) + + @Test + fun `validate with empty unit name return error`() = runTest { + val result = useCase("") + + assert(result.successful.not()) + assertEquals(UNIT_NAME_EMPTY_ERROR, result.errorMessage) + } + + @Test + fun `validate with unit name shorter than 2 characters return error`() = runTest { + val result = useCase("a") + + assert(result.successful.not()) + assertEquals(UNIT_NAME_LENGTH_ERROR, result.errorMessage) + } + + @Test + fun `validate with unit name with digit return error`() = runTest { + val result = useCase("kg1") + + assert(result.successful.not()) + assertEquals(UNIT_NAME_DIGIT_ERROR, result.errorMessage) + } + + @Test + fun `validate with unit name already exists return error`() = runTest { + val unit = repository.createTestItem() + val result = useCase(unit.unitName) + + assert(result.successful.not()) + } + + @Test + fun `validate with unit name and id already exists return success`() = runTest { + val unit = repository.createTestItem() + val result = useCase(unit.unitName, unitId = unit.unitId) + + assert(result.successful) + assertEquals(null, result.errorMessage) + } + + @Test + fun `validate with valid unit name return success`() = runTest { + val result = useCase("kg") + + assert(result.successful) + assertEquals(null, result.errorMessage) + } +} diff --git a/core/domain/src/test/java/com/niyaj/domain/market/ValidateUnitValueUseCaseTest.kt b/core/domain/src/test/java/com/niyaj/domain/market/ValidateUnitValueUseCaseTest.kt new file mode 100644 index 00000000..42ca1f2b --- /dev/null +++ b/core/domain/src/test/java/com/niyaj/domain/market/ValidateUnitValueUseCaseTest.kt @@ -0,0 +1,57 @@ +/* + * Copyright 2024 Sk Niyaj Ali + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package com.niyaj.domain.market + +import com.niyaj.common.tags.MeasureUnitTestTags.UNIT_VALUE_EMPTY_ERROR +import com.niyaj.common.tags.MeasureUnitTestTags.UNIT_VALUE_INVALID +import com.niyaj.testing.util.MainDispatcherRule +import org.junit.Assert.assertNull +import org.junit.Rule +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertFalse + +class ValidateUnitValueUseCaseTest { + @get:Rule + val dispatcherRule = MainDispatcherRule() + val useCase = ValidateUnitValueUseCase() + + @Test + fun `given empty unit value when invoke then return error`() { + val result = useCase("") + + assert(!result.successful) + assertEquals(UNIT_VALUE_EMPTY_ERROR, result.errorMessage) + } + + @Test + fun `given invalid unit value when invoke then return success`() { + val result = useCase("1C") + + assertFalse(result.successful) + assertEquals(UNIT_VALUE_INVALID, result.errorMessage) + } + + @Test + fun `given non-empty unit value when invoke then return success`() { + val result = useCase("10") + + assert(result.successful) + assertNull(result.errorMessage) + } +} diff --git a/core/domain/src/test/java/com/niyaj/domain/ValidateGivenAmountUseCaseTest.kt b/core/domain/src/test/java/com/niyaj/domain/payment/ValidateGivenAmountUseCaseTest.kt similarity index 95% rename from core/domain/src/test/java/com/niyaj/domain/ValidateGivenAmountUseCaseTest.kt rename to core/domain/src/test/java/com/niyaj/domain/payment/ValidateGivenAmountUseCaseTest.kt index 950a9738..b4d09dcd 100644 --- a/core/domain/src/test/java/com/niyaj/domain/ValidateGivenAmountUseCaseTest.kt +++ b/core/domain/src/test/java/com/niyaj/domain/payment/ValidateGivenAmountUseCaseTest.kt @@ -15,12 +15,11 @@ * */ -package com.niyaj.domain +package com.niyaj.domain.payment import com.niyaj.common.tags.PaymentScreenTags.GIVEN_AMOUNT_EMPTY import com.niyaj.common.tags.PaymentScreenTags.GIVEN_AMOUNT_LENGTH_ERROR import com.niyaj.common.tags.PaymentScreenTags.GIVEN_AMOUNT_LETTER_ERROR -import com.niyaj.domain.payment.ValidateGivenAmountUseCase import com.niyaj.testing.util.MainDispatcherRule import org.junit.Rule import org.junit.Test diff --git a/core/domain/src/test/java/com/niyaj/domain/ValidateGivenDateUseCaseTest.kt b/core/domain/src/test/java/com/niyaj/domain/payment/ValidateGivenDateUseCaseTest.kt similarity index 94% rename from core/domain/src/test/java/com/niyaj/domain/ValidateGivenDateUseCaseTest.kt rename to core/domain/src/test/java/com/niyaj/domain/payment/ValidateGivenDateUseCaseTest.kt index 1f51a6ff..e685554a 100644 --- a/core/domain/src/test/java/com/niyaj/domain/ValidateGivenDateUseCaseTest.kt +++ b/core/domain/src/test/java/com/niyaj/domain/payment/ValidateGivenDateUseCaseTest.kt @@ -15,11 +15,10 @@ * */ -package com.niyaj.domain +package com.niyaj.domain.payment import com.niyaj.common.tags.PaymentScreenTags.PAYMENT_GIVEN_DATE_EMPTY import com.niyaj.common.utils.getStartTime -import com.niyaj.domain.payment.ValidateGivenDateUseCase import com.niyaj.testing.util.MainDispatcherRule import org.junit.Assert.assertEquals import org.junit.Assert.assertFalse diff --git a/core/domain/src/test/java/com/niyaj/domain/ValidatePaymentEmployeeUseCaseTest.kt b/core/domain/src/test/java/com/niyaj/domain/payment/ValidatePaymentEmployeeUseCaseTest.kt similarity index 94% rename from core/domain/src/test/java/com/niyaj/domain/ValidatePaymentEmployeeUseCaseTest.kt rename to core/domain/src/test/java/com/niyaj/domain/payment/ValidatePaymentEmployeeUseCaseTest.kt index 1c550358..162483ec 100644 --- a/core/domain/src/test/java/com/niyaj/domain/ValidatePaymentEmployeeUseCaseTest.kt +++ b/core/domain/src/test/java/com/niyaj/domain/payment/ValidatePaymentEmployeeUseCaseTest.kt @@ -15,10 +15,9 @@ * */ -package com.niyaj.domain +package com.niyaj.domain.payment import com.niyaj.common.tags.PaymentScreenTags.PAYMENT_EMPLOYEE_NAME_EMPTY -import com.niyaj.domain.payment.ValidatePaymentEmployeeUseCase import com.niyaj.testing.util.MainDispatcherRule import org.junit.Rule import kotlin.test.Test diff --git a/core/domain/src/test/java/com/niyaj/domain/ValidatePaymentModeUseCaseTest.kt b/core/domain/src/test/java/com/niyaj/domain/payment/ValidatePaymentModeUseCaseTest.kt similarity index 92% rename from core/domain/src/test/java/com/niyaj/domain/ValidatePaymentModeUseCaseTest.kt rename to core/domain/src/test/java/com/niyaj/domain/payment/ValidatePaymentModeUseCaseTest.kt index 1b202d26..94f163e9 100644 --- a/core/domain/src/test/java/com/niyaj/domain/ValidatePaymentModeUseCaseTest.kt +++ b/core/domain/src/test/java/com/niyaj/domain/payment/ValidatePaymentModeUseCaseTest.kt @@ -15,9 +15,8 @@ * */ -package com.niyaj.domain +package com.niyaj.domain.payment -import com.niyaj.domain.payment.ValidatePaymentModeUseCase import com.niyaj.model.PaymentMode import com.niyaj.testing.util.MainDispatcherRule import org.junit.Rule diff --git a/core/domain/src/test/java/com/niyaj/domain/ValidatePaymentNoteUseCaseTest.kt b/core/domain/src/test/java/com/niyaj/domain/payment/ValidatePaymentNoteUseCaseTest.kt similarity index 94% rename from core/domain/src/test/java/com/niyaj/domain/ValidatePaymentNoteUseCaseTest.kt rename to core/domain/src/test/java/com/niyaj/domain/payment/ValidatePaymentNoteUseCaseTest.kt index d96cde5d..a492da6b 100644 --- a/core/domain/src/test/java/com/niyaj/domain/ValidatePaymentNoteUseCaseTest.kt +++ b/core/domain/src/test/java/com/niyaj/domain/payment/ValidatePaymentNoteUseCaseTest.kt @@ -15,10 +15,9 @@ * */ -package com.niyaj.domain +package com.niyaj.domain.payment import com.niyaj.common.tags.PaymentScreenTags.PAYMENT_NOTE_EMPTY -import com.niyaj.domain.payment.ValidatePaymentNoteUseCase import com.niyaj.testing.util.MainDispatcherRule import org.junit.Rule import kotlin.test.Test diff --git a/core/domain/src/test/java/com/niyaj/domain/ValidatePaymentTypeUseCaseTest.kt b/core/domain/src/test/java/com/niyaj/domain/payment/ValidatePaymentTypeUseCaseTest.kt similarity index 93% rename from core/domain/src/test/java/com/niyaj/domain/ValidatePaymentTypeUseCaseTest.kt rename to core/domain/src/test/java/com/niyaj/domain/payment/ValidatePaymentTypeUseCaseTest.kt index e95f941a..07411cbb 100644 --- a/core/domain/src/test/java/com/niyaj/domain/ValidatePaymentTypeUseCaseTest.kt +++ b/core/domain/src/test/java/com/niyaj/domain/payment/ValidatePaymentTypeUseCaseTest.kt @@ -15,9 +15,8 @@ * */ -package com.niyaj.domain +package com.niyaj.domain.payment -import com.niyaj.domain.payment.ValidatePaymentTypeUseCase import com.niyaj.model.PaymentType import com.niyaj.testing.util.MainDispatcherRule import org.junit.Assert.assertEquals diff --git a/core/domain/src/test/java/com/niyaj/domain/printer/ValidateAddressReportLimitUseCaseTest.kt b/core/domain/src/test/java/com/niyaj/domain/printer/ValidateAddressReportLimitUseCaseTest.kt new file mode 100644 index 00000000..9d1860ba --- /dev/null +++ b/core/domain/src/test/java/com/niyaj/domain/printer/ValidateAddressReportLimitUseCaseTest.kt @@ -0,0 +1,62 @@ +/* + * Copyright 2024 Sk Niyaj Ali + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package com.niyaj.domain.printer + +import com.niyaj.common.tags.PrinterInfoTestTags.ADDRESS_REPORT_LENGTH_ERROR +import com.niyaj.common.tags.PrinterInfoTestTags.ADDRESS_REPORT_LIMIT_IS_REQUIRED +import com.niyaj.testing.util.MainDispatcherRule +import org.junit.Assert.assertEquals +import org.junit.Assert.assertFalse +import org.junit.Rule +import kotlin.test.Test + +class ValidateAddressReportLimitUseCaseTest { + @get:Rule + val dispatcherRule = MainDispatcherRule() + val useCase = ValidateAddressReportLimitUseCase() + + @Test + fun `validate with zero addressReportLimit returns error`() { + val result = useCase(0) + assertFalse(result.successful) + assertEquals(ADDRESS_REPORT_LIMIT_IS_REQUIRED, result.errorMessage) + } + + @Test + fun `validate with invalid addressReportLimit returns error`() { + val result = useCase(-2) + assertFalse(result.successful) + assertEquals(ADDRESS_REPORT_LIMIT_IS_REQUIRED, result.errorMessage) + } + + @Test + fun `validate with less than 10 addressReportLimit returns error`() { + val result = useCase(5) + + assertFalse(result.successful) + assertEquals(ADDRESS_REPORT_LENGTH_ERROR, result.errorMessage) + } + + @Test + fun `validate with 10 addressReportLimit returns success`() { + val result = useCase(10) + + assertEquals(true, result.successful) + assertEquals(null, result.errorMessage) + } +} diff --git a/core/domain/src/test/java/com/niyaj/domain/printer/ValidateCustomerReportLimitUseCaseTest.kt b/core/domain/src/test/java/com/niyaj/domain/printer/ValidateCustomerReportLimitUseCaseTest.kt new file mode 100644 index 00000000..ebbb98f2 --- /dev/null +++ b/core/domain/src/test/java/com/niyaj/domain/printer/ValidateCustomerReportLimitUseCaseTest.kt @@ -0,0 +1,55 @@ +/* + * Copyright 2024 Sk Niyaj Ali + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package com.niyaj.domain.printer + +import com.niyaj.common.tags.PrinterInfoTestTags.CUSTOM_REPORT_LENGTH_ERROR +import com.niyaj.common.tags.PrinterInfoTestTags.CUSTOM_REPORT_LIMIT_IS_REQUIRED +import com.niyaj.testing.util.MainDispatcherRule +import org.junit.Assert.assertEquals +import org.junit.Assert.assertFalse +import org.junit.Rule +import kotlin.test.Test + +class ValidateCustomerReportLimitUseCaseTest { + @get:Rule + val dispatcherRule = MainDispatcherRule() + val useCase = ValidateCustomerReportLimitUseCase() + + @Test + fun `validate with zero limit returns error`() { + val result = useCase(0) + assertFalse(result.successful) + assertEquals(CUSTOM_REPORT_LIMIT_IS_REQUIRED, result.errorMessage) + } + + @Test + fun `validate with less than 10 limit returns error`() { + val result = useCase(5) + + assertFalse(result.successful) + assertEquals(CUSTOM_REPORT_LENGTH_ERROR, result.errorMessage) + } + + @Test + fun `validate with 10 limit returns success`() { + val result = useCase(10) + + assertEquals(true, result.successful) + assertEquals(null, result.errorMessage) + } +} diff --git a/core/domain/src/test/java/com/niyaj/domain/printer/ValidateNbrLinesUseCaseTest.kt b/core/domain/src/test/java/com/niyaj/domain/printer/ValidateNbrLinesUseCaseTest.kt new file mode 100644 index 00000000..aeeee95b --- /dev/null +++ b/core/domain/src/test/java/com/niyaj/domain/printer/ValidateNbrLinesUseCaseTest.kt @@ -0,0 +1,54 @@ +/* + * Copyright 2024 Sk Niyaj Ali + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package com.niyaj.domain.printer + +import com.niyaj.common.tags.PrinterInfoTestTags.NBR_LINES_IS_REQUIRED +import com.niyaj.testing.util.MainDispatcherRule +import org.junit.Assert.assertEquals +import org.junit.Assert.assertFalse +import org.junit.Rule +import kotlin.test.Test + +class ValidateNbrLinesUseCaseTest { + @get:Rule + val dispatcherRule = MainDispatcherRule() + val useCase = ValidateNbrLinesUseCase() + + @Test + fun `validate with zero nbrLines returns error`() { + val result = useCase(0) + assertFalse(result.successful) + assertEquals(NBR_LINES_IS_REQUIRED, result.errorMessage) + } + + @Test + fun `validate with invalid nbrLines returns error`() { + val result = useCase(-2) + + assertFalse(result.successful) + assertEquals(NBR_LINES_IS_REQUIRED, result.errorMessage) + } + + @Test + fun `validate with valid nbrLines returns success`() { + val result = useCase(10) + + assertEquals(true, result.successful) + assertEquals(null, result.errorMessage) + } +} diff --git a/core/domain/src/test/java/com/niyaj/domain/printer/ValidatePrinterDpiUseCaseTest.kt b/core/domain/src/test/java/com/niyaj/domain/printer/ValidatePrinterDpiUseCaseTest.kt new file mode 100644 index 00000000..c1cccdcb --- /dev/null +++ b/core/domain/src/test/java/com/niyaj/domain/printer/ValidatePrinterDpiUseCaseTest.kt @@ -0,0 +1,54 @@ +/* + * Copyright 2024 Sk Niyaj Ali + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package com.niyaj.domain.printer + +import com.niyaj.common.tags.PrinterInfoTestTags.DPI_IS_REQUIRED +import com.niyaj.testing.util.MainDispatcherRule +import org.junit.Assert.assertEquals +import org.junit.Assert.assertFalse +import org.junit.Rule +import kotlin.test.Test + +class ValidatePrinterDpiUseCaseTest { + @get:Rule + val dispatcherRule = MainDispatcherRule() + val useCase = ValidatePrinterDpiUseCase() + + @Test + fun `validate with zero dpi returns error`() { + val result = useCase(0) + assertFalse(result.successful) + assertEquals(DPI_IS_REQUIRED, result.errorMessage) + } + + @Test + fun `validate with invalid dpi returns error`() { + val result = useCase(-2) + + assertFalse(result.successful) + assertEquals(DPI_IS_REQUIRED, result.errorMessage) + } + + @Test + fun `validate with valid dpi returns success`() { + val result = useCase(10) + + assertEquals(true, result.successful) + assertEquals(null, result.errorMessage) + } +} diff --git a/core/domain/src/test/java/com/niyaj/domain/printer/ValidatePrinterWidthUseCaseTest.kt b/core/domain/src/test/java/com/niyaj/domain/printer/ValidatePrinterWidthUseCaseTest.kt new file mode 100644 index 00000000..d832caf2 --- /dev/null +++ b/core/domain/src/test/java/com/niyaj/domain/printer/ValidatePrinterWidthUseCaseTest.kt @@ -0,0 +1,54 @@ +/* + * Copyright 2024 Sk Niyaj Ali + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package com.niyaj.domain.printer + +import com.niyaj.common.tags.PrinterInfoTestTags.WIDTH_IS_REQUIRED +import com.niyaj.testing.util.MainDispatcherRule +import org.junit.Assert.assertEquals +import org.junit.Assert.assertFalse +import org.junit.Rule +import kotlin.test.Test + +class ValidatePrinterWidthUseCaseTest { + @get:Rule + val dispatcherRule = MainDispatcherRule() + val useCase = ValidatePrinterWidthUseCase() + + @Test + fun `validate with zero width returns error`() { + val result = useCase(0f) + assertFalse(result.successful) + assertEquals(WIDTH_IS_REQUIRED, result.errorMessage) + } + + @Test + fun `validate with invalid width returns error`() { + val result = useCase(-0.2f) + + assertFalse(result.successful) + assertEquals(WIDTH_IS_REQUIRED, result.errorMessage) + } + + @Test + fun `validate with valid width returns success`() { + val result = useCase(10f) + + assertEquals(true, result.successful) + assertEquals(null, result.errorMessage) + } +} diff --git a/core/domain/src/test/java/com/niyaj/domain/printer/ValidateProductNameLengthUseCaseTest.kt b/core/domain/src/test/java/com/niyaj/domain/printer/ValidateProductNameLengthUseCaseTest.kt new file mode 100644 index 00000000..d6d33499 --- /dev/null +++ b/core/domain/src/test/java/com/niyaj/domain/printer/ValidateProductNameLengthUseCaseTest.kt @@ -0,0 +1,55 @@ +/* + * Copyright 2024 Sk Niyaj Ali + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package com.niyaj.domain.printer + +import com.niyaj.common.tags.PrinterInfoTestTags.PRODUCT_NAME_LENGTH_IS_ERROR +import com.niyaj.common.tags.PrinterInfoTestTags.PRODUCT_NAME_LENGTH_IS_REQUIRED +import com.niyaj.testing.util.MainDispatcherRule +import org.junit.Assert.assertEquals +import org.junit.Assert.assertFalse +import org.junit.Rule +import kotlin.test.Test + +class ValidateProductNameLengthUseCaseTest { + @get:Rule + val dispatcherRule = MainDispatcherRule() + val useCase = ValidateProductNameLengthUseCase() + + @Test + fun `validate with zero name length returns error`() { + val result = useCase(0) + assertFalse(result.successful) + assertEquals(PRODUCT_NAME_LENGTH_IS_REQUIRED, result.errorMessage) + } + + @Test + fun `validate length less than 10 returns error`() { + val result = useCase(2) + + assertFalse(result.successful) + assertEquals(PRODUCT_NAME_LENGTH_IS_ERROR, result.errorMessage) + } + + @Test + fun `validate length 10 returns success`() { + val result = useCase(10) + + assertEquals(true, result.successful) + assertEquals(null, result.errorMessage) + } +} diff --git a/core/domain/src/test/java/com/niyaj/domain/printer/ValidateProductReportLimitUseCaseTest.kt b/core/domain/src/test/java/com/niyaj/domain/printer/ValidateProductReportLimitUseCaseTest.kt new file mode 100644 index 00000000..ef86c2a5 --- /dev/null +++ b/core/domain/src/test/java/com/niyaj/domain/printer/ValidateProductReportLimitUseCaseTest.kt @@ -0,0 +1,55 @@ +/* + * Copyright 2024 Sk Niyaj Ali + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package com.niyaj.domain.printer + +import com.niyaj.common.tags.PrinterInfoTestTags.PRODUCT_REPORT_LENGTH_ERROR +import com.niyaj.common.tags.PrinterInfoTestTags.PRODUCT_REPORT_LIMIT_IS_REQUIRED +import com.niyaj.testing.util.MainDispatcherRule +import org.junit.Assert.assertEquals +import org.junit.Assert.assertFalse +import org.junit.Rule +import kotlin.test.Test + +class ValidateProductReportLimitUseCaseTest { + @get:Rule + val dispatcherRule = MainDispatcherRule() + val useCase = ValidateProductReportLimitUseCase() + + @Test + fun `validate with zero limit returns error`() { + val result = useCase(0) + assertFalse(result.successful) + assertEquals(PRODUCT_REPORT_LIMIT_IS_REQUIRED, result.errorMessage) + } + + @Test + fun `validate with less than 20 limit returns error`() { + val result = useCase(10) + + assertFalse(result.successful) + assertEquals(PRODUCT_REPORT_LENGTH_ERROR, result.errorMessage) + } + + @Test + fun `validate with 20 limit returns success`() { + val result = useCase(20) + + assertEquals(true, result.successful) + assertEquals(null, result.errorMessage) + } +} diff --git a/core/domain/src/test/java/com/niyaj/domain/product/ValidateProductCategoryUseCaseTest.kt b/core/domain/src/test/java/com/niyaj/domain/product/ValidateProductCategoryUseCaseTest.kt new file mode 100644 index 00000000..d29c40bf --- /dev/null +++ b/core/domain/src/test/java/com/niyaj/domain/product/ValidateProductCategoryUseCaseTest.kt @@ -0,0 +1,45 @@ +/* + * Copyright 2024 Sk Niyaj Ali + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package com.niyaj.domain.product + +import com.niyaj.common.tags.ProductTestTags.PRODUCT_CATEGORY_EMPTY_ERROR +import com.niyaj.testing.util.MainDispatcherRule +import org.junit.Rule +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertNull + +class ValidateProductCategoryUseCaseTest { + @get:Rule + val dispatcherRule = MainDispatcherRule() + val useCase = ValidateProductCategoryUseCase() + + @Test + fun `when category id is 0 then return error`() { + val result = useCase(0) + assert(result.successful.not()) + assertEquals(PRODUCT_CATEGORY_EMPTY_ERROR, result.errorMessage) + } + + @Test + fun `when category id is not 0 then return success`() { + val result = useCase(1) + assert(result.successful) + assertNull(result.errorMessage) + } +} diff --git a/core/domain/src/test/java/com/niyaj/domain/product/ValidateProductNameUseCaseTest.kt b/core/domain/src/test/java/com/niyaj/domain/product/ValidateProductNameUseCaseTest.kt new file mode 100644 index 00000000..3ce0c8d5 --- /dev/null +++ b/core/domain/src/test/java/com/niyaj/domain/product/ValidateProductNameUseCaseTest.kt @@ -0,0 +1,78 @@ +/* + * Copyright 2024 Sk Niyaj Ali + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package com.niyaj.domain.product + +import com.niyaj.common.tags.ProductTestTags.PRODUCT_NAME_ALREADY_EXIST_ERROR +import com.niyaj.common.tags.ProductTestTags.PRODUCT_NAME_EMPTY_ERROR +import com.niyaj.common.tags.ProductTestTags.PRODUCT_NAME_LENGTH_ERROR +import com.niyaj.testing.repository.TestProductRepository +import com.niyaj.testing.util.MainDispatcherRule +import kotlinx.coroutines.test.UnconfinedTestDispatcher +import kotlinx.coroutines.test.runTest +import org.junit.Rule +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertNull + +class ValidateProductNameUseCaseTest { + @get:Rule + val dispatcherRule = MainDispatcherRule() + private val repository = TestProductRepository() + + private val useCase = ValidateProductNameUseCase( + repository = repository, + ioDispatcher = UnconfinedTestDispatcher(), + ) + + @Test + fun `when product name is empty then return error`() = runTest { + val result = useCase("") + assert(result.successful.not()) + assertEquals(PRODUCT_NAME_EMPTY_ERROR, result.errorMessage) + } + + @Test + fun `when product name is not empty then return success`() = runTest { + val result = useCase("Product Name") + assert(result.successful) + assertNull(result.errorMessage) + } + + @Test + fun `when product name is less than 4 then return error`() = runTest { + val result = useCase("Pro") + assert(result.successful.not()) + assertEquals(PRODUCT_NAME_LENGTH_ERROR, result.errorMessage) + } + + @Test + fun `when product name already exist return error`() = runTest { + val product = repository.createTestProduct() + val result = useCase(product.productName) + assert(result.successful.not()) + assertEquals(PRODUCT_NAME_ALREADY_EXIST_ERROR, result.errorMessage) + } + + @Test + fun `when product name already exist with valid id return success`() = runTest { + val product = repository.createTestProduct() + val result = useCase(product.productName, product.productId) + assert(result.successful) + assertNull(result.errorMessage) + } +} diff --git a/core/domain/src/test/java/com/niyaj/domain/product/ValidateProductPriceUseCaseTest.kt b/core/domain/src/test/java/com/niyaj/domain/product/ValidateProductPriceUseCaseTest.kt new file mode 100644 index 00000000..77f8a0b2 --- /dev/null +++ b/core/domain/src/test/java/com/niyaj/domain/product/ValidateProductPriceUseCaseTest.kt @@ -0,0 +1,54 @@ +/* + * Copyright 2024 Sk Niyaj Ali + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package com.niyaj.domain.product + +import com.niyaj.common.tags.ProductTestTags.PRODUCT_PRICE_EMPTY_ERROR +import com.niyaj.common.tags.ProductTestTags.PRODUCT_PRICE_LENGTH_ERROR +import com.niyaj.testing.util.MainDispatcherRule +import org.junit.Assert.assertEquals +import org.junit.Rule +import kotlin.test.Test +import kotlin.test.assertFalse +import kotlin.test.assertNull + +class ValidateProductPriceUseCaseTest { + @get:Rule + val dispatcherRule = MainDispatcherRule() + val useCase = ValidateProductPriceUseCase() + + @Test + fun `when product price is 0 then return error`() { + val result = useCase(0) + assert(result.successful.not()) + assertEquals(PRODUCT_PRICE_EMPTY_ERROR, result.errorMessage) + } + + @Test + fun `when product price is less than 10 return error`() { + val result = useCase(10) + assertFalse(result.successful) + assertEquals(PRODUCT_PRICE_LENGTH_ERROR, result.errorMessage) + } + + @Test + fun `when product price is greater than 10 return success`() { + val result = useCase(11) + assert(result.successful) + assertNull(result.errorMessage) + } +} diff --git a/core/domain/src/test/java/com/niyaj/domain/product/ValidateProductTagUseCaseTest.kt b/core/domain/src/test/java/com/niyaj/domain/product/ValidateProductTagUseCaseTest.kt new file mode 100644 index 00000000..7b9cf16e --- /dev/null +++ b/core/domain/src/test/java/com/niyaj/domain/product/ValidateProductTagUseCaseTest.kt @@ -0,0 +1,47 @@ +/* + * Copyright 2024 Sk Niyaj Ali + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package com.niyaj.domain.product + +import com.niyaj.common.tags.ProductTestTags.PRODUCT_TAG_LENGTH_ERROR +import com.niyaj.testing.util.MainDispatcherRule +import org.junit.Rule +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertFalse +import kotlin.test.assertNull +import kotlin.test.assertTrue + +class ValidateProductTagUseCaseTest { + @get:Rule + val dispatcherRule = MainDispatcherRule() + val useCase = ValidateProductTagUseCase() + + @Test + fun `when tag name is not empty and less than 3 digit then return error`() { + val result = useCase("AB") + assertFalse(result.successful) + assertEquals(PRODUCT_TAG_LENGTH_ERROR, result.errorMessage) + } + + @Test + fun `when tag name is valid then return success`() { + val result = useCase("Test") + assertTrue(result.successful) + assertNull(result.errorMessage) + } +} diff --git a/core/model/src/main/java/com/niyaj/model/MarketItemAndQuantity.kt b/core/model/src/main/java/com/niyaj/model/MarketItemAndQuantity.kt index 9f7580ab..b396d8e3 100644 --- a/core/model/src/main/java/com/niyaj/model/MarketItemAndQuantity.kt +++ b/core/model/src/main/java/com/niyaj/model/MarketItemAndQuantity.kt @@ -39,7 +39,7 @@ data class MarketItemAndQuantity( val itemQuantity: Double? = null, ) -fun List.searchItems(searchText: String): List { +fun List.searchMarketType(searchText: String): List { return if (searchText.isNotEmpty()) { this.filter { it.itemName.contains(searchText, true) || diff --git a/core/model/src/main/java/com/niyaj/model/MarketType.kt b/core/model/src/main/java/com/niyaj/model/MarketType.kt index 3928003c..9770f9fd 100644 --- a/core/model/src/main/java/com/niyaj/model/MarketType.kt +++ b/core/model/src/main/java/com/niyaj/model/MarketType.kt @@ -36,7 +36,7 @@ data class MarketType( val updatedAt: Long? = null, ) -fun List.searchItems(searchText: String): List { +fun List.searchMarketType(searchText: String): List { return if (searchText.isNotEmpty()) { this.filter { it.typeName.contains(searchText, ignoreCase = true) diff --git a/core/model/src/main/java/com/niyaj/model/Product.kt b/core/model/src/main/java/com/niyaj/model/Product.kt index afe9a352..df41697d 100644 --- a/core/model/src/main/java/com/niyaj/model/Product.kt +++ b/core/model/src/main/java/com/niyaj/model/Product.kt @@ -44,7 +44,7 @@ data class Product( /** * Filter products */ -fun List.filterProducts(searchText: String): List { +fun List.searchProducts(searchText: String): List { return if (searchText.isNotEmpty()) { this.filter { it.productName.contains(searchText, true) || diff --git a/core/testing/src/main/java/com/niyaj/testing/repository/TestMarketItemRepository.kt b/core/testing/src/main/java/com/niyaj/testing/repository/TestMarketItemRepository.kt new file mode 100644 index 00000000..1bb8ea85 --- /dev/null +++ b/core/testing/src/main/java/com/niyaj/testing/repository/TestMarketItemRepository.kt @@ -0,0 +1,141 @@ +/* + * Copyright 2024 Sk Niyaj Ali + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package com.niyaj.testing.repository + +import com.niyaj.common.result.Resource +import com.niyaj.data.repository.MarketItemRepository +import com.niyaj.model.MarketItem +import com.niyaj.model.MarketType +import com.niyaj.model.MarketTypeIdAndName +import com.niyaj.model.MeasureUnit +import com.niyaj.model.searchMarketItems +import com.niyaj.model.searchMarketType +import com.niyaj.model.searchMeasureUnit +import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.flow.mapLatest +import kotlinx.coroutines.flow.update +import org.jetbrains.annotations.TestOnly + +class TestMarketItemRepository : MarketItemRepository { + + /** + * The backing market item list for testing + */ + private val items = MutableStateFlow(mutableListOf()) + private val measureUnits = MutableStateFlow(mutableListOf()) + private val marketTypes = MutableStateFlow(mutableListOf()) + + override suspend fun getAllMarketItems(searchText: String): Flow> { + return items.mapLatest { it.searchMarketItems(searchText) } + } + + override suspend fun getAllMeasureUnits(searchText: String): Flow> { + return measureUnits.mapLatest { it.searchMeasureUnit(searchText) } + } + + override suspend fun getAllMarketItemLists( + searchText: String, + removedItems: List, + ): Flow> { + return items.mapLatest { list -> + list.filterNot { it.itemId in removedItems } + .searchMarketItems(searchText) + } + } + + override suspend fun getMarketItemById(itemId: Int): Resource { + val item = items.value.find { it.itemId == itemId } + return if (item != null) { + Resource.Success(item) + } else { + Resource.Error("Item not found") + } + } + + override suspend fun getAllItemType(searchText: String): Flow> { + return marketTypes.mapLatest { list -> + list.searchMarketType(searchText) + .map { MarketTypeIdAndName(it.typeId, it.typeName) } + } + } + + override suspend fun upsertMarketItem(newMarketItem: MarketItem): Resource { + val index = items.value.indexOfFirst { it.itemId == newMarketItem.itemId } + + if (index != -1) { + items.value[index] = newMarketItem + } else { + items.value.add(newMarketItem) + } + + return Resource.Success(true) + } + + override suspend fun deleteMarketItems(itemIds: List): Resource { + return Resource.Success(items.value.removeAll { it.itemId in itemIds }) + } + + override suspend fun findItemByName(itemName: String, itemId: Int?): Boolean { + return items.value.any { + if (itemId != null) { + it.itemName == itemName && it.itemId != itemId + } else { + it.itemName == itemName + } + } + } + + override suspend fun importMarketItemsToDatabase(marketItems: List): Resource { + marketItems.forEach { + upsertMarketItem(it) + } + + return Resource.Success(true) + } + + @TestOnly + fun updateMarketItemData(marketItems: List) { + items.update { marketItems.toMutableList() } + } + + @TestOnly + fun updateMeasureUnitData(items: List) { + measureUnits.update { items.toMutableList() } + } + + @TestOnly + fun updateMarketTypeData(items: List) { + marketTypes.update { items.toMutableList() } + } + + @TestOnly + fun createTestItem(): MarketItem { + val item = MarketItem( + itemId = 1, + itemName = "Test Item", + itemPrice = "100", + itemType = MarketTypeIdAndName(1, "Test Type"), + itemMeasureUnit = MeasureUnit(1, "Test Unit", 0.5), + ) + + items.value.add(item) + + return item + } +} diff --git a/core/testing/src/main/java/com/niyaj/testing/repository/TestMarketTypeRepository.kt b/core/testing/src/main/java/com/niyaj/testing/repository/TestMarketTypeRepository.kt new file mode 100644 index 00000000..bfbcf1f1 --- /dev/null +++ b/core/testing/src/main/java/com/niyaj/testing/repository/TestMarketTypeRepository.kt @@ -0,0 +1,98 @@ +/* + * Copyright 2024 Sk Niyaj Ali + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package com.niyaj.testing.repository + +import com.niyaj.common.result.Resource +import com.niyaj.common.utils.getStartDateLong +import com.niyaj.data.repository.MarketTypeRepository +import com.niyaj.model.MarketType +import com.niyaj.model.searchMarketType +import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.flow.mapLatest +import kotlinx.coroutines.flow.update +import org.jetbrains.annotations.TestOnly + +class TestMarketTypeRepository : MarketTypeRepository { + + /** + * The backing market type list for testing + */ + private val items = MutableStateFlow(mutableListOf()) + + override suspend fun getAllMarketTypes(searchText: String): Flow> { + return items.mapLatest { it.searchMarketType(searchText) } + } + + override suspend fun getMarketTypeById(id: Int): MarketType? { + return items.value.find { it.typeId == id } + } + + override suspend fun createOrUpdateMarketType(marketType: MarketType): Resource { + val index = items.value.indexOfFirst { it.typeId == marketType.typeId } + + if (index != -1) { + items.value[index] = marketType + } else { + items.value.add(marketType) + } + + return Resource.Success(true) + } + + override suspend fun deleteMarketTypes(items: List): Resource { + return Resource.Success(this.items.value.removeAll { it.typeId in items }) + } + + override suspend fun findMarketTypeByName(typeName: String, typeId: Int?): Boolean { + return items.value.any { + if (typeId != null) { + it.typeName == typeName && it.typeId != typeId + } else { + it.typeName == typeName + } + } + } + + override suspend fun importDataFromFilesToDatabase(data: List): Resource { + data.forEach { + createOrUpdateMarketType(it) + } + + return Resource.Success(true) + } + + @TestOnly + fun createTestItem(): MarketType { + val item = MarketType( + typeId = 1, + typeName = "Test Type", + listTypes = listOf("Test List Type"), + createdAt = getStartDateLong, + ) + + items.value.add(item) + + return item + } + + @TestOnly + fun updateMarketTypeData(marketTypes: List) { + items.update { marketTypes.toMutableList() } + } +} diff --git a/core/testing/src/main/java/com/niyaj/testing/repository/TestMeasureUnitRepository.kt b/core/testing/src/main/java/com/niyaj/testing/repository/TestMeasureUnitRepository.kt new file mode 100644 index 00000000..58821bfd --- /dev/null +++ b/core/testing/src/main/java/com/niyaj/testing/repository/TestMeasureUnitRepository.kt @@ -0,0 +1,99 @@ +/* + * Copyright 2024 Sk Niyaj Ali + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package com.niyaj.testing.repository + +import com.niyaj.common.result.Resource +import com.niyaj.data.repository.MeasureUnitRepository +import com.niyaj.model.MeasureUnit +import com.niyaj.model.searchMeasureUnit +import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.flow.mapLatest +import kotlinx.coroutines.flow.update +import org.jetbrains.annotations.TestOnly + +class TestMeasureUnitRepository : MeasureUnitRepository { + /** + * The backing measure unit list for testing + */ + private val items = MutableStateFlow(mutableListOf()) + + override suspend fun getAllMeasureUnits(searchText: String): Flow> { + return items.mapLatest { it.searchMeasureUnit(searchText) } + } + + override suspend fun getMeasureUnitById(unitId: Int): Resource { + val item = items.value.find { it.unitId == unitId } + return if (item != null) { + Resource.Success(item) + } else { + Resource.Error("Item not found") + } + } + + override suspend fun upsertMeasureUnit(newUnit: MeasureUnit): Resource { + val index = items.value.indexOfFirst { it.unitId == newUnit.unitId } + + if (index != -1) { + items.value[index] = newUnit + } else { + items.value.add(newUnit) + } + + return Resource.Success(true) + } + + override suspend fun deleteMeasureUnits(unitIds: List): Resource { + return Resource.Success(items.value.removeAll { it.unitId in unitIds }) + } + + override suspend fun findMeasureUnitByName(unitName: String, unitId: Int?): Boolean { + return items.value.any { + if (unitId != null) { + it.unitName == unitName && it.unitId != unitId + } else { + it.unitName == unitName + } + } + } + + override suspend fun importDataFromFilesToDatabase(units: List): Resource { + units.forEach { + upsertMeasureUnit(it) + } + + return Resource.Success(true) + } + + @TestOnly + fun updateMeasureUnitData(units: List) { + items.update { units.toMutableList() } + } + + @TestOnly + fun createTestItem(): MeasureUnit { + val item = MeasureUnit( + unitId = 1, + unitName = "Test Unit", + unitValue = 0.5, + ) + + items.value.add(item) + return item + } +} diff --git a/core/testing/src/main/java/com/niyaj/testing/repository/TestProductRepository.kt b/core/testing/src/main/java/com/niyaj/testing/repository/TestProductRepository.kt new file mode 100644 index 00000000..00b02dc5 --- /dev/null +++ b/core/testing/src/main/java/com/niyaj/testing/repository/TestProductRepository.kt @@ -0,0 +1,167 @@ +/* + * Copyright 2024 Sk Niyaj Ali + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package com.niyaj.testing.repository + +import com.niyaj.common.result.Resource +import com.niyaj.common.utils.getStartDateLong +import com.niyaj.data.repository.ProductRepository +import com.niyaj.model.Category +import com.niyaj.model.Product +import com.niyaj.model.ProductIdWithPrice +import com.niyaj.model.ProductWiseOrder +import com.niyaj.model.searchProducts +import kotlinx.collections.immutable.ImmutableList +import kotlinx.collections.immutable.toImmutableList +import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.flow.mapLatest +import kotlinx.coroutines.flow.update +import org.jetbrains.annotations.TestOnly + +class TestProductRepository : ProductRepository { + + /** + * The backing product, category list for testing + */ + private val productList = MutableStateFlow(mutableListOf()) + private val categoryList = MutableStateFlow(mutableListOf()) + private val productWiseOrder = MutableStateFlow(mutableListOf()) + + override fun getAllCategory(): Flow> = + categoryList.mapLatest { it.toImmutableList() } + + override suspend fun getCategoryById(categoryId: Int): Category? { + return categoryList.value.find { it.categoryId == categoryId } + } + + override suspend fun getAllProduct( + searchText: String, + selectedCategory: Int, + ): Flow> { + return productList.mapLatest { list -> + list.filter { + if (selectedCategory != 0) it.categoryId == selectedCategory else true + }.searchProducts(searchText) + } + } + + override suspend fun getProductById(productId: Int): Resource { + val product = productList.value.find { it.productId == productId } + return if (product != null) { + Resource.Success(product) + } else { + Resource.Error("Product not found") + } + } + + override suspend fun upsertProduct(newProduct: Product): Resource { + val index = productList.value.indexOfFirst { it.productId == newProduct.productId } + return if (index != -1) { + productList.value[index] = newProduct + Resource.Success(true) + } else { + productList.value.add(newProduct) + Resource.Success(true) + } + } + + override suspend fun deleteProducts(productIds: List): Resource { + return Resource.Success(productList.value.removeAll { it.productId in productIds }) + } + + override suspend fun getProductPrice(productId: Int): Int { + return productList.value.find { it.productId == productId }?.productPrice ?: 0 + } + + override suspend fun findProductByName(productName: String, productId: Int?): Boolean { + return productList.value.any { + if (productId != null) { + it.productName == productName && it.productId != productId + } else { + it.productName == productName + } + } + } + + override suspend fun getProductWiseOrderDetails(productId: Int): Flow> { + return productWiseOrder + } + + override suspend fun importProductsToDatabase(products: List): Resource { + products.forEach { + upsertProduct(it) + } + return Resource.Success(true) + } + + override suspend fun increaseProductsPrice(products: List): Resource { + products.forEach { (productId, price) -> + productList.value.indexOfFirst { it.productId == productId } + .takeIf { it != -1 } + ?.let { index -> + productList.value[index] = productList.value[index] + .copy(productPrice = productList.value[index].productPrice + price) + } + } + + return Resource.Success(true) + } + + override suspend fun decreaseProductsPrice(products: List): Resource { + products.forEach { (productId, price) -> + productList.value.indexOfFirst { it.productId == productId } + .takeIf { it != -1 } + ?.let { index -> + productList.value[index] = productList.value[index] + .copy(productPrice = productList.value[index].productPrice - price) + } + } + + return Resource.Success(true) + } + + @TestOnly + fun setCategoryList(list: List) { + categoryList.update { list.toMutableList() } + } + + @TestOnly + fun setProductList(list: List) { + productList.update { list.toMutableList() } + } + + @TestOnly + fun setProductWiseOrderList(list: List) { + productWiseOrder.update { list.toMutableList() } + } + + @TestOnly + fun createTestProduct(): Product { + val product = Product( + productId = 1, + productName = "Test Product", + productPrice = 100, + categoryId = 1, + productDescription = "Test Description", + createdAt = getStartDateLong, + ) + + productList.value.add(product) + return product + } +} diff --git a/feature/customer/src/test/screenshots/CustomerDetailsScreenPopulated_phone.png b/feature/customer/src/test/screenshots/CustomerDetailsScreenPopulated_phone.png index ae72fd64..f07f1ed0 100644 Binary files a/feature/customer/src/test/screenshots/CustomerDetailsScreenPopulated_phone.png and b/feature/customer/src/test/screenshots/CustomerDetailsScreenPopulated_phone.png differ diff --git a/feature/home/src/main/java/com/niyaj/home/HomeViewModel.kt b/feature/home/src/main/java/com/niyaj/home/HomeViewModel.kt index 75877d8c..028bfaa8 100644 --- a/feature/home/src/main/java/com/niyaj/home/HomeViewModel.kt +++ b/feature/home/src/main/java/com/niyaj/home/HomeViewModel.kt @@ -143,7 +143,10 @@ internal fun AnalyticsHelper.logAddProductToCart(orderId: Int, productId: Int) { event = AnalyticsEvent( type = "product_added_to_cart", extras = listOf( - AnalyticsEvent.Param("product_added_to_cart", "orderId - $orderId & productId - $productId"), + AnalyticsEvent.Param( + "product_added_to_cart", + "orderId - $orderId & productId - $productId", + ), ), ), ) @@ -154,7 +157,10 @@ internal fun AnalyticsHelper.logRemoveProductFromCart(orderId: Int, productId: I event = AnalyticsEvent( type = "product_removed_from_cart", extras = listOf( - AnalyticsEvent.Param("product_removed_from_cart", "orderId - $orderId & productId - $productId"), + AnalyticsEvent.Param( + "product_removed_from_cart", + "orderId - $orderId & productId - $productId", + ), ), ), ) diff --git a/feature/market/src/test/java/com/niyaj/market/MarketListItemScreenScreenshotTest.kt b/feature/market/src/test/java/com/niyaj/market/MarketListItemScreenScreenshotTest.kt index f856a13e..b71044fc 100644 --- a/feature/market/src/test/java/com/niyaj/market/MarketListItemScreenScreenshotTest.kt +++ b/feature/market/src/test/java/com/niyaj/market/MarketListItemScreenScreenshotTest.kt @@ -21,7 +21,7 @@ import androidx.activity.ComponentActivity import androidx.compose.ui.test.junit4.createAndroidComposeRule import com.niyaj.designsystem.theme.PoposRoomTheme import com.niyaj.market.marketListItem.MarketListItemScreenContent -import com.niyaj.model.searchItems +import com.niyaj.model.searchMarketType import com.niyaj.poposroom.core.testing.util.captureForPhone import com.niyaj.ui.event.UiState import com.niyaj.ui.parameterProvider.MarketItemAndQuantityData @@ -132,7 +132,7 @@ class MarketListItemScreenScreenshotTest { composeTestRule.captureForPhone("MarketListItemShowSearchBarAndGetEmptyResult") { PoposRoomTheme { MarketListItemScreenContent( - uiState = UiState.Success(marketItemAndQuantity.searchItems("search")), + uiState = UiState.Success(marketItemAndQuantity.searchMarketType("search")), marketDetails = maretListAndType, showSearchBar = true, searchText = "search", @@ -158,7 +158,7 @@ class MarketListItemScreenScreenshotTest { composeTestRule.captureForPhone("MarketListItemShowSearchBarAndGetSuccessResult") { PoposRoomTheme { MarketListItemScreenContent( - uiState = UiState.Success(marketItemAndQuantity.searchItems("Tomatoes")), + uiState = UiState.Success(marketItemAndQuantity.searchMarketType("Tomatoes")), marketDetails = maretListAndType, showSearchBar = true, searchText = "Tomatoes", diff --git a/feature/market/src/test/java/com/niyaj/market/MarketListItemsScreenScreenshotTest.kt b/feature/market/src/test/java/com/niyaj/market/MarketListItemsScreenScreenshotTest.kt index 1ae7f174..705748e6 100644 --- a/feature/market/src/test/java/com/niyaj/market/MarketListItemsScreenScreenshotTest.kt +++ b/feature/market/src/test/java/com/niyaj/market/MarketListItemsScreenScreenshotTest.kt @@ -21,7 +21,7 @@ import androidx.activity.ComponentActivity import androidx.compose.ui.test.junit4.createAndroidComposeRule import com.niyaj.designsystem.theme.PoposRoomTheme import com.niyaj.market.marketListItem.MarketListItemsScreenContent -import com.niyaj.model.searchItems +import com.niyaj.model.searchMarketType import com.niyaj.poposroom.core.testing.util.captureForPhone import com.niyaj.ui.event.UiState import com.niyaj.ui.parameterProvider.MarketItemAndQuantityData @@ -132,7 +132,7 @@ class MarketListItemsScreenScreenshotTest { composeTestRule.captureForPhone("MarketListItemsShowSearchBarAndGetEmptyResult") { PoposRoomTheme { MarketListItemsScreenContent( - uiState = UiState.Success(marketItemAndQuantities.searchItems("search")), + uiState = UiState.Success(marketItemAndQuantities.searchMarketType("search")), marketDetails = maretListAndType, showSearchBar = true, searchText = "search", @@ -158,7 +158,7 @@ class MarketListItemsScreenScreenshotTest { composeTestRule.captureForPhone("MarketListItemsShowSearchBarAndGetSuccessResult") { PoposRoomTheme { MarketListItemsScreenContent( - uiState = UiState.Success(marketItemAndQuantities.searchItems("Tomatoes")), + uiState = UiState.Success(marketItemAndQuantities.searchMarketType("Tomatoes")), marketDetails = maretListAndType, showSearchBar = true, searchText = "Tomatoes", diff --git a/feature/market/src/test/java/com/niyaj/market/MarketTypeScreenScreenshotTest.kt b/feature/market/src/test/java/com/niyaj/market/MarketTypeScreenScreenshotTest.kt index 91e7dc4b..744e502d 100644 --- a/feature/market/src/test/java/com/niyaj/market/MarketTypeScreenScreenshotTest.kt +++ b/feature/market/src/test/java/com/niyaj/market/MarketTypeScreenScreenshotTest.kt @@ -27,7 +27,7 @@ import com.niyaj.market.marketType.createOrUpdate.defaultListTypes import com.niyaj.market.marketType.settings.ExportMarketTypeScreenContent import com.niyaj.market.marketType.settings.ImportMarketTypeScreenContent import com.niyaj.market.marketType.settings.MarketTypeSettingsScreenContent -import com.niyaj.model.searchItems +import com.niyaj.model.searchMarketType import com.niyaj.poposroom.core.testing.util.captureForPhone import com.niyaj.ui.event.UiState import com.niyaj.ui.parameterProvider.MarketTypePreviewData @@ -201,7 +201,7 @@ class MarketTypeScreenScreenshotTest { composeTestRule.captureForPhone("MarketTypeShowSearchBarAndGetSuccessResult") { PoposRoomTheme { MarketTypeScreenContent( - uiState = UiState.Success(marketTypes.searchItems("Fruits")), + uiState = UiState.Success(marketTypes.searchMarketType("Fruits")), selectedItems = listOf(), showSearchBar = true, searchText = "Fruits", @@ -372,7 +372,7 @@ class MarketTypeScreenScreenshotTest { PoposRoomTheme { ExportMarketTypeScreenContent( items = marketTypes - .searchItems("text").toImmutableList(), + .searchMarketType("text").toImmutableList(), selectedItems = persistentListOf(), isLoading = false, showSearchBar = true, @@ -398,7 +398,7 @@ class MarketTypeScreenScreenshotTest { PoposRoomTheme { ExportMarketTypeScreenContent( items = marketTypes - .searchItems("Bakery").toImmutableList(), + .searchMarketType("Bakery").toImmutableList(), selectedItems = persistentListOf(), showSearchBar = true, isLoading = false, diff --git a/feature/market/src/test/screenshots/MarketListItemsScreenEmptyContent_phone.png b/feature/market/src/test/screenshots/MarketListItemsScreenEmptyContent_phone.png index ed47b6bd..40adab83 100644 Binary files a/feature/market/src/test/screenshots/MarketListItemsScreenEmptyContent_phone.png and b/feature/market/src/test/screenshots/MarketListItemsScreenEmptyContent_phone.png differ diff --git a/feature/market/src/test/screenshots/MarketListItemsScreenLoading_phone.png b/feature/market/src/test/screenshots/MarketListItemsScreenLoading_phone.png index a4ccde9d..2ee0de32 100644 Binary files a/feature/market/src/test/screenshots/MarketListItemsScreenLoading_phone.png and b/feature/market/src/test/screenshots/MarketListItemsScreenLoading_phone.png differ diff --git a/feature/market/src/test/screenshots/MarketListItemsScreenSuccessContent_phone.png b/feature/market/src/test/screenshots/MarketListItemsScreenSuccessContent_phone.png index 3d1eb4ee..2f69a477 100644 Binary files a/feature/market/src/test/screenshots/MarketListItemsScreenSuccessContent_phone.png and b/feature/market/src/test/screenshots/MarketListItemsScreenSuccessContent_phone.png differ diff --git a/feature/market/src/test/screenshots/MarketListItemsShowSearchBarAndGetEmptyResult_phone.png b/feature/market/src/test/screenshots/MarketListItemsShowSearchBarAndGetEmptyResult_phone.png index d8d9c5a8..5d2f72cb 100644 Binary files a/feature/market/src/test/screenshots/MarketListItemsShowSearchBarAndGetEmptyResult_phone.png and b/feature/market/src/test/screenshots/MarketListItemsShowSearchBarAndGetEmptyResult_phone.png differ diff --git a/feature/market/src/test/screenshots/MarketListItemsShowSearchBarAndGetSuccessResult_phone.png b/feature/market/src/test/screenshots/MarketListItemsShowSearchBarAndGetSuccessResult_phone.png index b605f616..7daa87fa 100644 Binary files a/feature/market/src/test/screenshots/MarketListItemsShowSearchBarAndGetSuccessResult_phone.png and b/feature/market/src/test/screenshots/MarketListItemsShowSearchBarAndGetSuccessResult_phone.png differ diff --git a/feature/product/build.gradle.kts b/feature/product/build.gradle.kts index d70fc534..456dd58e 100644 --- a/feature/product/build.gradle.kts +++ b/feature/product/build.gradle.kts @@ -18,8 +18,6 @@ plugins { alias(libs.plugins.popos.android.feature) alias(libs.plugins.popos.android.library.compose) - alias(libs.plugins.popos.android.library.jacoco) - alias(libs.plugins.roborazzi) } android { @@ -39,16 +37,4 @@ dependencies { // Dant'su Pos Printer implementation(libs.pos.printer) - - //RaamCosta Library - implementation(libs.raamcosta.animation.core) - ksp(libs.raamcosta.ksp) - - testImplementation(libs.hilt.android.testing) - testImplementation(libs.robolectric) - testImplementation(projects.core.testing) - testImplementation(projects.core.screenshotTesting) - testDemoImplementation(libs.roborazzi) - - androidTestImplementation(projects.core.testing) } \ No newline at end of file diff --git a/feature/product/src/main/java/com/niyaj/product/ProductViewModel.kt b/feature/product/src/main/java/com/niyaj/product/ProductViewModel.kt index 62dff70b..2e0adb63 100644 --- a/feature/product/src/main/java/com/niyaj/product/ProductViewModel.kt +++ b/feature/product/src/main/java/com/niyaj/product/ProductViewModel.kt @@ -21,6 +21,7 @@ import androidx.compose.runtime.snapshotFlow import androidx.lifecycle.viewModelScope import com.niyaj.common.result.Resource import com.niyaj.core.analytics.AnalyticsEvent +import com.niyaj.core.analytics.AnalyticsEvent.Param import com.niyaj.core.analytics.AnalyticsHelper import com.niyaj.data.repository.ProductRepository import com.niyaj.ui.event.BaseViewModel @@ -28,7 +29,6 @@ import com.niyaj.ui.event.UiState import com.niyaj.ui.utils.UiEvent import dagger.hilt.android.lifecycle.HiltViewModel import kotlinx.collections.immutable.persistentListOf -import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.SharingStarted import kotlinx.coroutines.flow.asStateFlow @@ -51,18 +51,13 @@ class ProductViewModel @Inject constructor( private val observableSearchText = snapshotFlow { searchText.value } - @OptIn(ExperimentalCoroutinesApi::class) val products = observableSearchText.combine(_selectedCategory) { text, category -> productRepository.getAllProduct(text, category) }.flatMapLatest { it -> it.map { items -> totalItems = items.map { it.productId } - if (items.isEmpty()) { - UiState.Empty - } else { - UiState.Success(items) - } + if (items.isEmpty()) UiState.Empty else UiState.Success(items) } }.stateIn( scope = viewModelScope, @@ -110,9 +105,7 @@ internal fun AnalyticsHelper.logDeletedProducts(data: List) { logEvent( event = AnalyticsEvent( type = "products_deleted", - extras = listOf( - com.niyaj.core.analytics.AnalyticsEvent.Param("products_deleted", data.toString()), - ), + extras = listOf(Param("products_deleted", data.toString())), ), ) } diff --git a/feature/product/src/main/java/com/niyaj/product/settings/ProductSettingsViewModel.kt b/feature/product/src/main/java/com/niyaj/product/settings/ProductSettingsViewModel.kt index 3b652a5a..f54295d1 100644 --- a/feature/product/src/main/java/com/niyaj/product/settings/ProductSettingsViewModel.kt +++ b/feature/product/src/main/java/com/niyaj/product/settings/ProductSettingsViewModel.kt @@ -81,10 +81,6 @@ class ProductSettingsViewModel @Inject constructor( private val _productPrice = mutableIntStateOf(0) val productPrice: State = _productPrice - private fun getProducts(categoryId: Int): List { - return products.value.filter { it.categoryId == categoryId }.map { it.productId } - } - /** * */ @@ -259,6 +255,10 @@ class ProductSettingsViewModel @Inject constructor( _selectedCategory.clear() } } + + private fun getProducts(categoryId: Int): List { + return products.value.filter { it.categoryId == categoryId }.map { it.productId } + } } internal fun AnalyticsHelper.logImportedProductFromFile(totalProduct: Int) { diff --git a/feature/product/src/test/java/com/niyaj/product/ProductScreenScreenshotTest.kt b/feature/product/src/test/java/com/niyaj/product/ProductScreenScreenshotTest.kt index 18c67bd0..8ddbe01e 100644 --- a/feature/product/src/test/java/com/niyaj/product/ProductScreenScreenshotTest.kt +++ b/feature/product/src/test/java/com/niyaj/product/ProductScreenScreenshotTest.kt @@ -24,7 +24,7 @@ import androidx.compose.ui.test.junit4.createAndroidComposeRule import com.niyaj.common.utils.toBarDate import com.niyaj.designsystem.theme.PoposRoomTheme import com.niyaj.model.Category -import com.niyaj.model.filterProducts +import com.niyaj.model.searchProducts import com.niyaj.poposroom.core.testing.util.captureForPhone import com.niyaj.product.createOrUpdate.AddEditProductScreenContent import com.niyaj.product.createOrUpdate.AddEditProductState @@ -208,7 +208,7 @@ class ProductScreenScreenshotTest { composeTestRule.captureForPhone("ShowSearchBarAndGetEmptyResult") { PoposRoomTheme { ProductScreenContent( - uiState = UiState.Success(productList.filterProducts("search")), + uiState = UiState.Success(productList.searchProducts("search")), selectedItems = listOf(), categories = categoryList, selectedCategory = 0, @@ -239,7 +239,7 @@ class ProductScreenScreenshotTest { composeTestRule.captureForPhone("ShowSearchBarAndGetSuccessResult") { PoposRoomTheme { ProductScreenContent( - uiState = UiState.Success(productList.filterProducts("Chicken")), + uiState = UiState.Success(productList.searchProducts("Chicken")), selectedItems = listOf(), categories = categoryList, selectedCategory = 0, @@ -546,7 +546,7 @@ class ProductScreenScreenshotTest { composeTestRule.captureForPhone("ExportScreenPerformSearchAndGetEmptyResult") { PoposRoomTheme { ExportProductScreenContent( - items = productList.filterProducts("search").toImmutableList(), + items = productList.searchProducts("search").toImmutableList(), selectedItems = persistentListOf(), selectedCategory = emptyList(), categories = categoryList, @@ -573,7 +573,7 @@ class ProductScreenScreenshotTest { composeTestRule.captureForPhone("ExportScreenPerformSearchAndGetSomeResult") { PoposRoomTheme { ExportProductScreenContent( - items = productList.filterProducts("Vegetable").toImmutableList(), + items = productList.searchProducts("Vegetable").toImmutableList(), selectedItems = persistentListOf(), selectedCategory = emptyList(), categories = categoryList, diff --git a/feature/product/src/test/java/com/niyaj/product/ProductSettingsViewModelTest.kt b/feature/product/src/test/java/com/niyaj/product/ProductSettingsViewModelTest.kt new file mode 100644 index 00000000..fdcbccf8 --- /dev/null +++ b/feature/product/src/test/java/com/niyaj/product/ProductSettingsViewModelTest.kt @@ -0,0 +1,174 @@ +/* + * Copyright 2024 Sk Niyaj Ali + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package com.niyaj.product + +import app.cash.turbine.test +import com.niyaj.model.searchProducts +import com.niyaj.product.settings.ProductSettingsEvent +import com.niyaj.product.settings.ProductSettingsViewModel +import com.niyaj.testing.repository.TestProductRepository +import com.niyaj.testing.util.MainDispatcherRule +import com.niyaj.testing.util.TestAnalyticsHelper +import com.niyaj.ui.parameterProvider.ProductPreviewData +import com.niyaj.ui.utils.UiEvent +import kotlinx.coroutines.flow.collect +import kotlinx.coroutines.launch +import kotlinx.coroutines.test.UnconfinedTestDispatcher +import kotlinx.coroutines.test.advanceUntilIdle +import kotlinx.coroutines.test.runTest +import org.junit.Assert.assertTrue +import org.junit.Before +import org.junit.Rule +import kotlin.test.Test +import kotlin.test.assertEquals + +class ProductSettingsViewModelTest { + @get:Rule + val dispatcherRule = MainDispatcherRule() + + private val itemList = ProductPreviewData.productList + private val repository = TestProductRepository() + private val analyticsHelper = TestAnalyticsHelper() + private lateinit var viewModel: ProductSettingsViewModel + + @Before + fun setup() { + viewModel = ProductSettingsViewModel(repository, analyticsHelper) + } + + @Test + fun `when search text changes, charges are updated`() = runTest { + repository.setProductList(itemList) + + viewModel.searchTextChanged("Chicken Biryani") + advanceUntilIdle() + + viewModel.products.test { + assertEquals(itemList.searchProducts("Chicken Biryani"), awaitItem()) + } + } + + @Test + fun `when GetExportedProduct event is triggered with no selection, all items are exported`() = + runTest { + val job = launch(UnconfinedTestDispatcher()) { viewModel.products.collect() } + + repository.setProductList(itemList) + + viewModel.onEvent(ProductSettingsEvent.GetExportedProduct) + testScheduler.advanceUntilIdle() + + viewModel.exportedProducts.test { + assertEquals(itemList, awaitItem()) + } + + job.cancel() + } + + @Test + fun `when GetExportedItems event is triggered with selection, only selected items are exported`() = + runTest { + val job = launch(UnconfinedTestDispatcher()) { viewModel.products.collect() } + + repository.setProductList(itemList) + + viewModel.selectItem(1) + viewModel.selectItem(3) + + viewModel.onEvent(ProductSettingsEvent.GetExportedProduct) + advanceUntilIdle() + + assertEquals( + itemList.filter { it.productId == 1 || it.productId == 3 }, + viewModel.exportedProducts.value, + ) + + job.cancel() + } + + @Test + fun `when OnImportProductsFromFile event is triggered, importedItems are updated`() = + runTest { + viewModel.onEvent(ProductSettingsEvent.OnImportProductsFromFile(itemList)) + testScheduler.advanceUntilIdle() + + assertEquals(itemList, viewModel.importedProducts.value) + } + + @Test + fun `when ImportProductsToDatabase event is triggered with no selection, all imported items are added`() = + runTest { + val job = launch { viewModel.products.collect() } + + viewModel.onEvent(ProductSettingsEvent.OnImportProductsFromFile(itemList)) + assertEquals(itemList, viewModel.importedProducts.value) + viewModel.onEvent(ProductSettingsEvent.ImportProductsToDatabase) + advanceUntilIdle() + + viewModel.eventFlow.test { + val event = awaitItem() + assertTrue(event is UiEvent.OnSuccess) + assertEquals( + "${itemList.size} Products has been imported.", + (event as UiEvent.OnSuccess).successMessage, + ) + } + + advanceUntilIdle() + + viewModel.products.test { + assertEquals(itemList, awaitItem()) + } + + job.cancel() + } + + @Test + fun `when ImportProductsToDatabase event is triggered with selection, only selected items are added`() = + runTest { + val job = launch { viewModel.products.collect() } + + viewModel.onEvent(ProductSettingsEvent.OnImportProductsFromFile(itemList)) + + assertEquals(itemList, viewModel.importedProducts.value) + + viewModel.selectItem(2) + viewModel.selectItem(4) + + viewModel.onEvent(ProductSettingsEvent.ImportProductsToDatabase) + advanceUntilIdle() + + viewModel.eventFlow.test { + val event = awaitItem() + assertTrue(event is UiEvent.OnSuccess) + assertEquals( + "2 Products has been imported.", + (event as UiEvent.OnSuccess).successMessage, + ) + } + + advanceUntilIdle() + + viewModel.products.test { + val event = awaitItem() + assertEquals(itemList.filter { it.productId == 2 || it.productId == 4 }, event) + } + + job.cancel() + } +} diff --git a/feature/product/src/test/java/com/niyaj/product/ProductViewModelTest.kt b/feature/product/src/test/java/com/niyaj/product/ProductViewModelTest.kt new file mode 100644 index 00000000..3fbdd2a3 --- /dev/null +++ b/feature/product/src/test/java/com/niyaj/product/ProductViewModelTest.kt @@ -0,0 +1,259 @@ +/* + * Copyright 2024 Sk Niyaj Ali + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package com.niyaj.product + +import app.cash.turbine.test +import com.niyaj.model.searchProducts +import com.niyaj.testing.repository.TestProductRepository +import com.niyaj.testing.util.MainDispatcherRule +import com.niyaj.testing.util.TestAnalyticsHelper +import com.niyaj.ui.event.UiState +import com.niyaj.ui.parameterProvider.CategoryPreviewData +import com.niyaj.ui.parameterProvider.ProductPreviewData +import kotlinx.collections.immutable.persistentListOf +import kotlinx.collections.immutable.toImmutableList +import kotlinx.coroutines.flow.collect +import kotlinx.coroutines.launch +import kotlinx.coroutines.test.UnconfinedTestDispatcher +import kotlinx.coroutines.test.advanceUntilIdle +import kotlinx.coroutines.test.runTest +import org.junit.Before +import org.junit.Rule +import kotlin.test.Test +import kotlin.test.assertContains +import kotlin.test.assertEquals + +class ProductViewModelTest { + + @get:Rule + val dispatcherRule = MainDispatcherRule() + + private val itemList = ProductPreviewData.productList + private val repository = TestProductRepository() + private val analyticsHelper = TestAnalyticsHelper() + private lateinit var viewModel: ProductViewModel + + @Before + fun setup() { + viewModel = ProductViewModel(repository, analyticsHelper) + } + + @Test + fun productsState_initially_Loading() = runTest { + assertEquals(UiState.Loading, viewModel.products.value) + } + + @Test + fun productState_isEmpty_whenDataIsEmpty() = runTest { + val job = launch(UnconfinedTestDispatcher()) { viewModel.products.collect() } + + assertEquals(UiState.Empty, viewModel.products.value) + + job.cancel() + } + + @Test + fun productState_isSuccess_whenDataIsAvailable() = runTest { + val job = launch(UnconfinedTestDispatcher()) { viewModel.products.collect() } + repository.setProductList(itemList) + + viewModel.products.test { + assertEquals(UiState.Success(itemList), awaitItem()) + } + + job.cancel() + } + + @Test + fun category_isEmpty_whenDataIsEmpty() = runTest { + val job = launch(UnconfinedTestDispatcher()) { viewModel.categories.collect() } + + assertEquals(persistentListOf(), viewModel.categories.value) + + job.cancel() + } + + @Test + fun category_available_whenDataIsAvailable() = runTest { + val job = launch(UnconfinedTestDispatcher()) { viewModel.categories.collect() } + val categoryList = CategoryPreviewData.categoryList + repository.setCategoryList(categoryList) + + assertEquals(categoryList.toImmutableList(), viewModel.categories.value) + + job.cancel() + } + + @Test + fun onSelectCategory_shouldUpdateSelectedCategory() = runTest { + val job = launch(UnconfinedTestDispatcher()) { viewModel.selectedCategory.collect() } + val category = CategoryPreviewData.categoryList.first() + + viewModel.selectCategory(category.categoryId) + + assertEquals(category.categoryId, viewModel.selectedCategory.value) + + job.cancel() + } + + @Test + fun onSelectCategory_shouldUpdateProducts() = runTest { + val job = launch(UnconfinedTestDispatcher()) { viewModel.products.collect() } + val category = CategoryPreviewData.categoryList.first() + val productList = ProductPreviewData.productList.filter { it.categoryId == category.categoryId } + + repository.setProductList(itemList) + viewModel.selectCategory(category.categoryId) + + assertEquals(UiState.Success(productList), viewModel.products.value) + + job.cancel() + } + + @Test + fun onSelectExistingCategory_shouldUpdateProductsAndDeselectSelectedCategory() = runTest { + val job = launch(UnconfinedTestDispatcher()) { viewModel.products.collect() } + val job2 = launch(UnconfinedTestDispatcher()) { viewModel.selectedCategory.collect() } + + val category = CategoryPreviewData.categoryList.first() + val productList = ProductPreviewData.productList.filter { it.categoryId == category.categoryId } + + repository.setProductList(itemList) + viewModel.selectCategory(category.categoryId) + + assertEquals(UiState.Success(productList), viewModel.products.value) + + viewModel.selectCategory(category.categoryId) + assertEquals(0, viewModel.selectedCategory.value) + assertEquals(UiState.Success(itemList), viewModel.products.value) + + job.cancel() + job2.cancel() + } + + @Test + fun search_withInvalidData_returnEmptyResult() = runTest { + repository.setProductList(itemList) + viewModel.searchTextChanged("Invalid") + + advanceUntilIdle() + + viewModel.products.test { + assertEquals(UiState.Empty, awaitItem()) + } + } + + @Test + fun search_withValidData_returnResult() = runTest { + repository.setProductList(itemList) + viewModel.searchTextChanged("Chicken Biryani") + + advanceUntilIdle() + + viewModel.products.test { + assertEquals( + UiState.Success( + itemList.searchProducts("Chicken Biryani"), + ), + awaitItem(), + ) + } + } + + @Test + fun search_onClose_returnAllData() = runTest { + repository.setProductList(itemList) + viewModel.searchTextChanged("Extra") + + advanceUntilIdle() + + viewModel.products.test { + assertEquals(UiState.Empty, awaitItem()) + cancelAndIgnoreRemainingEvents() + } + + viewModel.closeSearchBar() + advanceUntilIdle() + + viewModel.products.test { + assertEquals(UiState.Success(itemList), awaitItem()) + } + } + + @Test + fun deleteProducts_shouldRemoveFromList() = runTest { + repository.setProductList(itemList) + val productId = itemList.first().productId + + viewModel.selectItem(productId) + viewModel.deleteItems() + + advanceUntilIdle() + + viewModel.products.test { + assertEquals(UiState.Success(itemList.drop(1)), awaitItem()) + } + + assert(productId !in viewModel.selectedItems.toList()) + } + + @Test + fun selectProducts_onSelectItem_shouldUpdateSelectedProducts() = runTest { + repository.setProductList(itemList) + + val productId = itemList.first().productId + viewModel.selectItem(productId) + + advanceUntilIdle() + + assertContains(viewModel.selectedItems.toList(), productId) + } + + @Test + fun selectAllItems_onSelectAllItems_shouldUpdateSelectedItems() = runTest { + val job = launch(UnconfinedTestDispatcher()) { viewModel.products.collect() } + + repository.setProductList(itemList) + viewModel.selectAllItems() + + advanceUntilIdle() + + assertEquals(itemList.map { it.productId }, viewModel.selectedItems.toList()) + + job.cancel() + } + + @Test + fun deselectItems_onDeselectItems_shouldClearSelectedItems() = runTest { + val job = launch(UnconfinedTestDispatcher()) { viewModel.products.collect() } + repository.setProductList(itemList) + + viewModel.selectAllItems() + assertEquals(viewModel.selectedItems.toList(), itemList.map { it.productId }) + + advanceUntilIdle() + + viewModel.deselectItems() + + advanceUntilIdle() + + assertEquals(emptyList(), viewModel.selectedItems.toList()) + + job.cancel() + } +}