Skip to content

Commit

Permalink
Enhancement - Unit Test for Product Feature (#1057)
Browse files Browse the repository at this point in the history
* Enhancement - Unit Test for Product Feature

- Close #1056 
* 🤖 Updates screenshots
  • Loading branch information
niyajali authored Aug 17, 2024
1 parent 7764ead commit 095be4e
Show file tree
Hide file tree
Showing 76 changed files with 2,323 additions and 112 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/Build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,15 @@ 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
uses: MeilCli/slack-upload-file@v4
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:
Expand Down Expand Up @@ -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'
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -47,7 +47,7 @@ class MarketListItemRepositoryImpl @Inject constructor(
marketListDao
.getAllMarketItemsById(listTypeId)
.distinctUntilChanged()
.mapLatest { it.searchItems(searchText) }
.mapLatest { it.searchMarketType(searchText) }
}
}

Expand All @@ -58,7 +58,7 @@ class MarketListItemRepositoryImpl @Inject constructor(
return withContext(ioDispatcher) {
marketListDao.getAllMarketItemsByIds(listTypeIds)
.distinctUntilChanged()
.mapLatest { it.searchItems(searchText) }
.mapLatest { it.searchMarketType(searchText) }
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -42,7 +42,7 @@ class MarketTypeRepositoryImpl @Inject constructor(
return withContext(ioDispatcher) {
marketTypeDao.getAllMarketTypes()
.mapLatest(List<MarketTypeEntity>::asExternalModel)
.mapLatest { it.searchItems(searchText) }
.mapLatest { it.searchMarketType(searchText) }
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -74,7 +74,7 @@ class ProductRepositoryImpl @Inject constructor(
list
}
}.mapLatest {
it.filterProducts(searchText)
it.searchProducts(searchText)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ 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,
errorMessage = PRODUCT_PRICE_EMPTY_ERROR,
)
}

if (type.isNullOrEmpty() && productPrice < 10) {
if (productPrice < 10) {
return ValidationResult(
successful = false,
errorMessage = PRODUCT_PRICE_LENGTH_ERROR,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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)
}
}
Original file line number Diff line number Diff line change
@@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading

0 comments on commit 095be4e

Please sign in to comment.