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

Commit 80cd38c

Browse files
author
Alejo
committed
unit test bundle stats in stats store
1 parent 2e6feff commit 80cd38c

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed

example/src/test/java/org/wordpress/android/fluxc/wc/stats/WCStatsStoreTest.kt

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.wordpress.android.fluxc.wc.stats
22

33
import com.yarolegovich.wellsql.WellSql
4+
import junit.framework.TestCase.assertFalse
45
import kotlinx.coroutines.runBlocking
56
import org.hamcrest.CoreMatchers.anyOf
67
import org.hamcrest.CoreMatchers.not
@@ -23,7 +24,13 @@ import org.wordpress.android.fluxc.UnitTestUtils
2324
import org.wordpress.android.fluxc.model.SiteModel
2425
import org.wordpress.android.fluxc.model.WCNewVisitorStatsModel
2526
import org.wordpress.android.fluxc.model.WCRevenueStatsModel
27+
import org.wordpress.android.fluxc.network.BaseRequest.GenericErrorType
28+
import org.wordpress.android.fluxc.network.rest.wpcom.wc.WooError
29+
import org.wordpress.android.fluxc.network.rest.wpcom.wc.WooErrorType
30+
import org.wordpress.android.fluxc.network.rest.wpcom.wc.WooPayload
31+
import org.wordpress.android.fluxc.network.rest.wpcom.wc.bundlestats.BundleStatsApiResponse
2632
import org.wordpress.android.fluxc.network.rest.wpcom.wc.bundlestats.BundleStatsRestClient
33+
import org.wordpress.android.fluxc.network.rest.wpcom.wc.bundlestats.BundleStatsTotals
2734
import org.wordpress.android.fluxc.network.rest.wpcom.wc.orderstats.OrderStatsRestClient
2835
import org.wordpress.android.fluxc.persistence.WCVisitorStatsSqlUtils
2936
import org.wordpress.android.fluxc.persistence.WellSqlConfig
@@ -872,4 +879,72 @@ class WCStatsStoreTest {
872879
assertEquals(defaultWeekVisitorStats["2019-07-17"],0)
873880
assertEquals(defaultWeekVisitorStats["2019-07-18"],0)
874881
}
882+
883+
@Test
884+
fun testFetchBundlesErrorResponse() = runBlocking {
885+
val error = WooError(
886+
type = WooErrorType.INVALID_RESPONSE,
887+
original = GenericErrorType.INVALID_RESPONSE,
888+
message = "Invalid Response"
889+
)
890+
val response: WooPayload<BundleStatsApiResponse> = WooPayload(error)
891+
892+
whenever(mockBundleStatsRestClient.fetchBundleStats(any(), any(), any(), any()))
893+
.thenReturn(response)
894+
895+
val result = wcStatsStore.fetchProductBundlesStats(
896+
SiteModel(),
897+
"2024-03-01",
898+
endDate = "2024-04-01",
899+
interval = "day"
900+
)
901+
902+
assertTrue(result.isError)
903+
assertTrue(result.model == null)
904+
assertThat(result.error, isEqual(error))
905+
}
906+
907+
@Test
908+
fun testFetchBundlesNullResponse() = runBlocking {
909+
val response: WooPayload<BundleStatsApiResponse> = WooPayload(null)
910+
911+
whenever(mockBundleStatsRestClient.fetchBundleStats(any(), any(), any(), any()))
912+
.thenReturn(response)
913+
914+
val result = wcStatsStore.fetchProductBundlesStats(
915+
SiteModel(),
916+
"2024-03-01",
917+
endDate = "2024-04-01",
918+
interval = "day"
919+
)
920+
921+
assertTrue(result.isError)
922+
assertTrue(result.model == null)
923+
assertThat(result.error.type, isEqual(WooErrorType.GENERIC_ERROR))
924+
}
925+
926+
@Test
927+
fun testFetchBundlesSuccessResponse() = runBlocking {
928+
val totals = BundleStatsTotals(
929+
itemsSold = 5,
930+
netRevenue = 1000.00
931+
)
932+
val statsResponse = BundleStatsApiResponse(totals = totals)
933+
val response: WooPayload<BundleStatsApiResponse> = WooPayload(statsResponse)
934+
935+
whenever(mockBundleStatsRestClient.fetchBundleStats(any(), any(), any(), any()))
936+
.thenReturn(response)
937+
938+
val result = wcStatsStore.fetchProductBundlesStats(
939+
SiteModel(),
940+
"2024-03-01",
941+
endDate = "2024-04-01",
942+
interval = "day"
943+
)
944+
945+
assertFalse(result.isError)
946+
assertTrue(result.model != null)
947+
assertEquals(result.model!!.itemsSold, totals.itemsSold)
948+
assertEquals(result.model!!.netRevenue, totals.netRevenue)
949+
}
875950
}

0 commit comments

Comments
 (0)