Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.woocommerce.android.ui.woopos.common.data.searchbyidentifier

import com.woocommerce.android.model.Product
import com.woocommerce.android.ui.woopos.common.data.models.WooPosProductModelVersion2
import kotlinx.coroutines.async
import kotlinx.coroutines.coroutineScope
import javax.inject.Inject
Expand All @@ -9,11 +9,11 @@ class WooPosSearchByIdentifierProcessVariationResult @Inject constructor(
private val variationFetch: WooPosSearchByIdentifierVariationFetch,
private val productGetOrFetch: WooPosSearchByIdentifierProductGetOrFetch
) {
suspend operator fun invoke(product: Product): WooPosSearchByIdentifierResult = coroutineScope {
suspend operator fun invoke(product: WooPosProductModelVersion2): WooPosSearchByIdentifierResult = coroutineScope {
val parentId = product.parentId
val variationId = product.remoteId

if (parentId <= 0) {
if (parentId == null || parentId <= 0) {
return@coroutineScope WooPosSearchByIdentifierResult.Failure(
WooPosSearchByIdentifierResult.Error.NotFound
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.woocommerce.android.ui.woopos.common.data.searchbyidentifier

import com.woocommerce.android.ui.woopos.common.data.WooPosProductsCache
import com.woocommerce.android.ui.woopos.common.data.models.WooPosProductModelVersion2
import javax.inject.Inject

class WooPosSearchByIdentifierResultConverter @Inject constructor(
Expand All @@ -15,7 +16,7 @@ class WooPosSearchByIdentifierResultConverter @Inject constructor(
return when (result) {
is WooPosSearchByIdentifierResult.Success -> {
val product = result.product
if (product.type.equals("variation", ignoreCase = true)) {
if (product.type == WooPosProductModelVersion2.WooPosProductType.VARIATION) {
variationProcess(product)
} else {
productsCache.addAll(listOf(product))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.woocommerce.android.ui.woopos.home.cart

import com.automattic.android.tracks.crashlogging.CrashLogging
import com.woocommerce.android.ui.woopos.common.data.WooPosProductsCache
import com.woocommerce.android.ui.woopos.common.data.models.WooPosProductModelVersion2
import com.woocommerce.android.ui.woopos.common.util.WooPosLogWrapper
import com.woocommerce.android.ui.woopos.home.ParentToChildrenEvent
import com.woocommerce.android.ui.woopos.home.cart.WooPosCartItemViewState.Coupon.CouponValidationState
Expand Down Expand Up @@ -108,11 +109,26 @@ class WooPosCartItemsUpdater @Inject constructor(
updatedItem: WooPosCartItemViewState.Product,
updatedProduct: ParentToChildrenEvent.OrderCreated.ProductInfo
) {
// TBD Local Catalog The app should update cache based on the data coming from backend not from the view layer
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the data coming here from the view layer?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, or at least we are receiving WooPosCartItemViewState.Product which is a ViewState.

productsCache.getProductById(updatedItem.id)?.let { product ->
productsCache.updateProduct(
product.copy(
name = updatedItem.name,
price = updatedProduct.subtotalPricePerItem(),
pricing = when (product.pricing) {
WooPosProductModelVersion2.WooPosPricing.NoPricing ->
WooPosProductModelVersion2.WooPosPricing.NoPricing

is WooPosProductModelVersion2.WooPosPricing.RegularPricing ->
WooPosProductModelVersion2.WooPosPricing.RegularPricing(
updatedProduct.subtotalPricePerItem()
)

is WooPosProductModelVersion2.WooPosPricing.SalePricing ->
WooPosProductModelVersion2.WooPosPricing.SalePricing(
product.pricing.regularPrice,
updatedProduct.subtotalPricePerItem()
)
}
)
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package com.woocommerce.android.ui.woopos.common.data.searchbyidentifier

import com.woocommerce.android.model.Product
import com.woocommerce.android.ui.woopos.common.data.WooPosVariation
import com.woocommerce.android.ui.woopos.common.data.models.WooPosProductModelVersion2
import com.woocommerce.android.ui.woopos.util.generateWooPosProduct
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.test.runTest
import org.junit.Assert.assertEquals
Expand All @@ -25,11 +26,8 @@ class WooPosSearchByIdentifierProcessVariationResultTest {
// GIVEN
val parentId = 123L
val variationId = 456L
val product: Product = mock {
on { this.parentId }.thenReturn(parentId)
on { remoteId }.thenReturn(variationId)
}
val parentProduct: Product = mock()
val product: WooPosProductModelVersion2 = generateWooPosProduct(productId = variationId, parentId = parentId)
val parentProduct: WooPosProductModelVersion2 = generateWooPosProduct()
val variation = WooPosVariation(
remoteVariationId = variationId,
remoteProductId = parentId,
Expand Down Expand Up @@ -60,10 +58,7 @@ class WooPosSearchByIdentifierProcessVariationResultTest {
@Test
fun `given product with invalid parent id, when invoke called, then return product not found failure`() = runTest {
// GIVEN
val product: Product = mock {
on { parentId }.thenReturn(0L)
on { remoteId }.thenReturn(456L)
}
val product: WooPosProductModelVersion2 = generateWooPosProduct(productId = 456L, parentId = 0L)

// WHEN
val result = sut(product)
Expand All @@ -80,10 +75,7 @@ class WooPosSearchByIdentifierProcessVariationResultTest {
// GIVEN
val parentId = 123L
val variationId = 456L
val product: Product = mock {
on { this.parentId }.thenReturn(parentId)
on { remoteId }.thenReturn(variationId)
}
val product: WooPosProductModelVersion2 = generateWooPosProduct(productId = variationId, parentId = parentId)

runBlocking {
whenever(
Expand Down Expand Up @@ -113,10 +105,7 @@ class WooPosSearchByIdentifierProcessVariationResultTest {
// GIVEN
val parentId = 123L
val variationId = 456L
val product: Product = mock {
on { this.parentId }.thenReturn(parentId)
on { remoteId }.thenReturn(variationId)
}
val product: WooPosProductModelVersion2 = generateWooPosProduct(productId = variationId, parentId = parentId)
val variation = WooPosVariation(
remoteVariationId = variationId,
remoteProductId = parentId,
Expand Down Expand Up @@ -148,10 +137,7 @@ class WooPosSearchByIdentifierProcessVariationResultTest {
// GIVEN
val parentId = 123L
val variationId = 456L
val product: Product = mock {
on { this.parentId }.thenReturn(parentId)
on { remoteId }.thenReturn(variationId)
}
val product: WooPosProductModelVersion2 = generateWooPosProduct(productId = variationId, parentId = parentId)

val variationResult = WooPosSearchByIdentifierVariationFetch.VariationFetchResult.Failure(
WooPosSearchByIdentifierResult.Error.UnknownError("Variation not found for ID: $variationId")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import com.woocommerce.android.ui.products.ProductTestUtils
import com.woocommerce.android.ui.woopos.common.data.WooPosProductsCache
import com.woocommerce.android.ui.woopos.common.data.WooPosVariation
import com.woocommerce.android.ui.woopos.common.data.WooPosVariationMapper
import com.woocommerce.android.ui.woopos.common.data.models.WooPosProductModelVersion2
import com.woocommerce.android.ui.woopos.common.data.toWooPosVariation
import com.woocommerce.android.ui.woopos.util.WooPosCoroutineTestRule
import com.woocommerce.android.ui.woopos.util.generateWooPosProduct
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.UnconfinedTestDispatcher
import kotlinx.coroutines.test.runTest
Expand All @@ -30,7 +32,7 @@ class WooPosSearchByIdentifierResultConverterTest {
private val variationProcess: WooPosSearchByIdentifierProcessVariationResult = mock()
private val variationMapper: WooPosVariationMapper = mock()

private val testProduct = ProductTestUtils.generateProduct()
private val testProduct = generateWooPosProduct()
private val testProductVariation = ProductTestUtils.generateProductVariation()
private val testVariation by lazy { testProductVariation.toWooPosVariation(variationMapper) }

Expand Down Expand Up @@ -69,7 +71,7 @@ class WooPosSearchByIdentifierResultConverterTest {
@Test
fun `given variation product success result, when converting, should process variation`() = runTest {
// GIVEN
val variationProduct = testProduct.copy(type = "variation")
val variationProduct = testProduct.copy(type = WooPosProductModelVersion2.WooPosProductType.VARIATION)
val successResult = WooPosSearchByIdentifierResult.Success(variationProduct)
val variationSuccessResult = WooPosSearchByIdentifierResult.VariationSuccess(testVariation, testProduct)
val searchFunction: suspend () -> WooPosSearchByIdentifierResult = { successResult }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ package com.woocommerce.android.ui.woopos.home.cart

import androidx.arch.core.executor.testing.InstantTaskExecutorRule
import com.automattic.android.tracks.crashlogging.CrashLogging
import com.woocommerce.android.model.Product
import com.woocommerce.android.ui.woopos.common.data.WooPosProductsCache
import com.woocommerce.android.ui.woopos.common.data.models.WooPosProductModelVersion2
import com.woocommerce.android.ui.woopos.common.util.WooPosLogWrapper
import com.woocommerce.android.ui.woopos.home.ParentToChildrenEvent
import com.woocommerce.android.ui.woopos.home.cart.WooPosCartItemViewState.Coupon.CouponValidationState
import com.woocommerce.android.ui.woopos.util.WooPosCoroutineTestRule
import com.woocommerce.android.ui.woopos.util.format.WooPosFormatPrice
import com.woocommerce.android.ui.woopos.util.generateWooPosProduct
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.runTest
import org.assertj.core.api.Assertions.assertThat
Expand Down Expand Up @@ -45,7 +46,8 @@ class WooPosCartItemsUpdaterTest {
formatPrice = formatPrice,
productsCache = productsCache,
wooPosLogWrapper = logger,
crashLogger = crashLogger
crashLogger = crashLogger,

)

@Test
Expand All @@ -67,7 +69,7 @@ class WooPosCartItemsUpdaterTest {
basePrice = BigDecimal("10.0"),
quantity = 1f
)
val cachedProduct = mock<Product>()
val cachedProduct = generateWooPosProduct()
whenever(productsCache.getProductById(1L)).thenReturn(cachedProduct)

// WHEN
Expand All @@ -82,7 +84,7 @@ class WooPosCartItemsUpdaterTest {
verify(productsCache).updateProduct(
cachedProduct.copy(
name = "Updated Name",
price = BigDecimal("10.0")
pricing = WooPosProductModelVersion2.WooPosPricing.RegularPricing(BigDecimal("10.0"))
)
)
}
Expand All @@ -108,7 +110,7 @@ class WooPosCartItemsUpdaterTest {
basePrice = BigDecimal("10.0"),
quantity = 1f
)
val cachedProduct = mock<Product>()
val cachedProduct = generateWooPosProduct()
whenever(productsCache.getProductById(1L)).thenReturn(cachedProduct)

// WHEN
Expand All @@ -123,7 +125,7 @@ class WooPosCartItemsUpdaterTest {
verify(productsCache).updateProduct(
cachedProduct.copy(
name = "Updated Variation",
price = BigDecimal("10.0")
pricing = WooPosProductModelVersion2.WooPosPricing.RegularPricing(BigDecimal("10.0"))
)
)
}
Expand Down Expand Up @@ -179,7 +181,7 @@ class WooPosCartItemsUpdaterTest {
basePrice = BigDecimal("10.0"),
quantity = 1f
)
val cachedProduct = mock<Product>()
val cachedProduct = generateWooPosProduct()
whenever(productsCache.getProductById(1L)).thenReturn(cachedProduct)

// WHEN
Expand All @@ -199,7 +201,7 @@ class WooPosCartItemsUpdaterTest {
verify(productsCache).updateProduct(
cachedProduct.copy(
name = "Updated Product 1",
price = BigDecimal("10.0")
pricing = WooPosProductModelVersion2.WooPosPricing.RegularPricing(BigDecimal("10.0"))
)
)
verify(productsCache).deleteProduct(2L)
Expand All @@ -224,7 +226,7 @@ class WooPosCartItemsUpdaterTest {
basePrice = BigDecimal("10.0"),
quantity = 1f
)
val cachedProduct = mock<Product>()
val cachedProduct = generateWooPosProduct()
whenever(productsCache.getProductById(1L)).thenReturn(cachedProduct)

// WHEN
Expand All @@ -245,7 +247,7 @@ class WooPosCartItemsUpdaterTest {
verify(productsCache).updateProduct(
cachedProduct.copy(
name = "Updated Product",
price = BigDecimal("10.0")
pricing = WooPosProductModelVersion2.WooPosPricing.RegularPricing(BigDecimal("10.0"))
)
)
verify(productsCache).deleteProduct(1L)
Expand Down Expand Up @@ -320,7 +322,7 @@ class WooPosCartItemsUpdaterTest {
basePrice = BigDecimal("10.0"),
quantity = 1f
)
val cachedProduct = mock<Product>()
val cachedProduct = generateWooPosProduct()
whenever(productsCache.getProductById(1L)).thenReturn(cachedProduct)

// WHEN
Expand All @@ -330,7 +332,7 @@ class WooPosCartItemsUpdaterTest {
verify(productsCache).updateProduct(
cachedProduct.copy(
name = "Updated Name",
price = BigDecimal("10.0")
pricing = WooPosProductModelVersion2.WooPosPricing.RegularPricing(BigDecimal("10.0"))
)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.woocommerce.android.ui.woopos.common.data.models.WooPosProductModelVe

fun generateWooPosProduct(
productId: Long = 1,
parentId: Long? = null,
productName: String = "Product 1",
status: WooPosProductStatus = WooPosProductStatus.PUBLISH,
amount: String = "10.0",
Expand All @@ -20,7 +21,7 @@ fun generateWooPosProduct(
pricing = WooPosPricing.RegularPricing(amount.toBigDecimal()),
type = productType,
isDownloadable = isDownloadable,
parentId = null,
parentId = parentId,
sku = "",
globalUniqueId = "",
status = status,
Expand Down