11package org.wordpress.android.fluxc.wc.stats
22
33import com.yarolegovich.wellsql.WellSql
4+ import junit.framework.TestCase.assertFalse
45import kotlinx.coroutines.runBlocking
56import org.hamcrest.CoreMatchers.anyOf
67import org.hamcrest.CoreMatchers.not
@@ -23,7 +24,13 @@ import org.wordpress.android.fluxc.UnitTestUtils
2324import org.wordpress.android.fluxc.model.SiteModel
2425import org.wordpress.android.fluxc.model.WCNewVisitorStatsModel
2526import 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
2632import org.wordpress.android.fluxc.network.rest.wpcom.wc.bundlestats.BundleStatsRestClient
33+ import org.wordpress.android.fluxc.network.rest.wpcom.wc.bundlestats.BundleStatsTotals
2734import org.wordpress.android.fluxc.network.rest.wpcom.wc.orderstats.OrderStatsRestClient
2835import org.wordpress.android.fluxc.persistence.WCVisitorStatsSqlUtils
2936import 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