Skip to content
This repository was archived by the owner on Feb 4, 2025. It is now read-only.

Commit 576aec4

Browse files
committed
Add unit tests for batch orders status update
1 parent 37c74d0 commit 576aec4

File tree

1 file changed

+99
-0
lines changed

1 file changed

+99
-0
lines changed

example/src/test/java/org/wordpress/android/fluxc/wc/order/WCOrderStoreTest.kt

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@ import org.wordpress.android.fluxc.model.SiteModel
3535
import org.wordpress.android.fluxc.model.WCOrderListDescriptor
3636
import org.wordpress.android.fluxc.model.WCOrderStatusModel
3737
import org.wordpress.android.fluxc.model.WCOrderSummaryModel
38+
import org.wordpress.android.fluxc.network.rest.wpcom.wc.order.BatchOrderApiResponse
3839
import org.wordpress.android.fluxc.network.rest.wpcom.wc.order.CoreOrderStatus
40+
import org.wordpress.android.fluxc.network.rest.wpcom.wc.order.CoreOrderStatus.COMPLETED
41+
import org.wordpress.android.fluxc.network.rest.wpcom.wc.order.OrderDto
3942
import org.wordpress.android.fluxc.network.rest.wpcom.wc.order.OrderRestClient
4043
import org.wordpress.android.fluxc.persistence.OrderSqlUtils
4144
import org.wordpress.android.fluxc.persistence.WCAndroidDatabase
@@ -46,6 +49,7 @@ import org.wordpress.android.fluxc.persistence.dao.OrdersDaoDecorator
4649
import org.wordpress.android.fluxc.store.InsertOrder
4750
import org.wordpress.android.fluxc.store.WCOrderFetcher
4851
import org.wordpress.android.fluxc.store.WCOrderStore
52+
import org.wordpress.android.fluxc.store.WCOrderStore.BulkUpdateOrderStatusResponsePayload
4953
import org.wordpress.android.fluxc.store.WCOrderStore.FetchHasOrdersResponsePayload
5054
import org.wordpress.android.fluxc.store.WCOrderStore.FetchOrderListResponsePayload
5155
import org.wordpress.android.fluxc.store.WCOrderStore.FetchOrderStatusOptionsResponsePayload
@@ -605,6 +609,101 @@ class WCOrderStoreTest {
605609
}
606610
}
607611

612+
@Test
613+
fun `given successful response for all orders when batch updating status then returns successful orders`() {
614+
runBlocking {
615+
// Given
616+
val site = SiteModel().apply { id = 1 }
617+
val orderIds = listOf(1L, 2L, 3L)
618+
val newStatus = COMPLETED.value
619+
620+
// Create mocked OrderDto objects for success responses
621+
val order1 = mock<OrderDto>().apply {
622+
whenever(id).thenReturn(1L)
623+
whenever(status).thenReturn(COMPLETED.value)
624+
}
625+
val order2 = mock<OrderDto>().apply {
626+
whenever(id).thenReturn(2L)
627+
whenever(status).thenReturn(COMPLETED.value)
628+
}
629+
val order3 = mock<OrderDto>().apply {
630+
whenever(id).thenReturn(3L)
631+
whenever(status).thenReturn(COMPLETED.value)
632+
}
633+
634+
val successResponses = listOf(
635+
BatchOrderApiResponse.OrderResponse.Success(order1),
636+
BatchOrderApiResponse.OrderResponse.Success(order2),
637+
BatchOrderApiResponse.OrderResponse.Success(order3)
638+
)
639+
640+
whenever(orderRestClient.batchUpdateOrdersStatus(site, orderIds, newStatus))
641+
.thenReturn(BulkUpdateOrderStatusResponsePayload(successResponses))
642+
643+
// When
644+
val result = orderStore.batchUpdateOrdersStatus(site, orderIds, newStatus)
645+
646+
// Then
647+
assertThat(result.isError).isFalse()
648+
result.model?.let { updateResult ->
649+
assertEquals(orderIds, updateResult.updatedOrders)
650+
assertTrue(updateResult.failedOrders.isEmpty())
651+
}
652+
}
653+
}
654+
655+
@Test
656+
fun `given mixed success and failure response when batch updating status then returns successful and failed orders`() {
657+
runBlocking {
658+
// Given
659+
val site = SiteModel().apply { id = 1 }
660+
val orderIds = listOf(1L, 2L, 3L)
661+
val newStatus = COMPLETED.value
662+
663+
// Mock successful orders
664+
val order1 = mock<OrderDto>().apply {
665+
whenever(id).thenReturn(1L)
666+
whenever(status).thenReturn(COMPLETED.value)
667+
}
668+
val order3 = mock<OrderDto>().apply {
669+
whenever(id).thenReturn(3L)
670+
whenever(status).thenReturn(COMPLETED.value)
671+
}
672+
673+
val mixedResponses = listOf(
674+
BatchOrderApiResponse.OrderResponse.Success(order1),
675+
BatchOrderApiResponse.OrderResponse.Error(
676+
id = 2L,
677+
error = BatchOrderApiResponse.ErrorResponse(
678+
code = "woocommerce_rest_shop_order_invalid_id",
679+
message = "Invalid ID.",
680+
data = BatchOrderApiResponse.ErrorData(status = 400)
681+
)
682+
),
683+
BatchOrderApiResponse.OrderResponse.Success(order3)
684+
)
685+
686+
whenever(orderRestClient.batchUpdateOrdersStatus(site, orderIds, newStatus))
687+
.thenReturn(BulkUpdateOrderStatusResponsePayload(mixedResponses))
688+
689+
// When
690+
val result = orderStore.batchUpdateOrdersStatus(site, orderIds, newStatus)
691+
692+
// Then
693+
assertThat(result.isError).isFalse()
694+
result.model?.let { updateResult ->
695+
assertEquals(listOf(1L, 3L), updateResult.updatedOrders)
696+
assertEquals(1, updateResult.failedOrders.size)
697+
with(updateResult.failedOrders[0]) {
698+
assertEquals(2L, id)
699+
assertEquals("woocommerce_rest_shop_order_invalid_id", errorCode)
700+
assertEquals("Invalid ID.", errorMessage)
701+
assertEquals(400, errorStatus)
702+
}
703+
}
704+
}
705+
}
706+
608707
private fun setupMissingOrders(): MutableMap<WCOrderSummaryModel, OrderEntity?> {
609708
return mutableMapOf<WCOrderSummaryModel, OrderEntity?>().apply {
610709
(21L..30L).forEach { index ->

0 commit comments

Comments
 (0)