11package org.wordpress.android.fluxc.wc.stats
22
3+ import androidx.room.Room
34import com.yarolegovich.wellsql.WellSql
5+ import junit.framework.TestCase.assertFalse
46import kotlinx.coroutines.runBlocking
57import org.hamcrest.CoreMatchers.anyOf
68import org.hamcrest.CoreMatchers.not
@@ -23,7 +25,18 @@ import org.wordpress.android.fluxc.UnitTestUtils
2325import org.wordpress.android.fluxc.model.SiteModel
2426import org.wordpress.android.fluxc.model.WCNewVisitorStatsModel
2527import org.wordpress.android.fluxc.model.WCRevenueStatsModel
28+ import org.wordpress.android.fluxc.model.WCVisitorStatsSummary
29+ import org.wordpress.android.fluxc.network.BaseRequest.GenericErrorType
30+ import org.wordpress.android.fluxc.network.rest.wpcom.wc.WooError
31+ import org.wordpress.android.fluxc.network.rest.wpcom.wc.WooErrorType
32+ import org.wordpress.android.fluxc.network.rest.wpcom.wc.WooErrorType.GENERIC_ERROR
33+ import org.wordpress.android.fluxc.network.rest.wpcom.wc.WooPayload
34+ import org.wordpress.android.fluxc.network.rest.wpcom.wc.bundlestats.BundleStatsApiResponse
35+ import org.wordpress.android.fluxc.network.rest.wpcom.wc.bundlestats.BundleStatsRestClient
36+ import org.wordpress.android.fluxc.network.rest.wpcom.wc.bundlestats.BundleStatsTotals
2637import org.wordpress.android.fluxc.network.rest.wpcom.wc.orderstats.OrderStatsRestClient
38+ import org.wordpress.android.fluxc.network.rest.wpcom.wc.orderstats.VisitorStatsSummaryApiResponse
39+ import org.wordpress.android.fluxc.persistence.WCAndroidDatabase
2740import org.wordpress.android.fluxc.persistence.WCVisitorStatsSqlUtils
2841import org.wordpress.android.fluxc.persistence.WellSqlConfig
2942import org.wordpress.android.fluxc.store.WCStatsStore
@@ -44,8 +57,9 @@ import org.hamcrest.CoreMatchers.`is` as isEqual
4457@RunWith(RobolectricTestRunner ::class )
4558class WCStatsStoreTest {
4659 private val mockOrderStatsRestClient = mock<OrderStatsRestClient >()
60+ private val mockBundleStatsRestClient = mock<BundleStatsRestClient >()
4761 private val appContext = RuntimeEnvironment .application.applicationContext
48- private val wcStatsStore = WCStatsStore ( Dispatcher (), mockOrderStatsRestClient, initCoroutineEngine())
62+ private lateinit var wcStatsStore: WCStatsStore
4963
5064 @Before
5165 fun setUp () {
@@ -59,6 +73,18 @@ class WCStatsStoreTest {
5973 )
6074 WellSql .init (config)
6175 config.reset()
76+
77+ val database = Room .inMemoryDatabaseBuilder(appContext, WCAndroidDatabase ::class .java)
78+ .allowMainThreadQueries()
79+ .build()
80+
81+ wcStatsStore = WCStatsStore (
82+ dispatcher = Dispatcher (),
83+ wcOrderStatsClient = mockOrderStatsRestClient,
84+ bundleStatsRestClient = mockBundleStatsRestClient,
85+ coroutineEngine = initCoroutineEngine(),
86+ visitorSummaryStatsDao = database.visitorSummaryStatsDao
87+ )
6288 }
6389
6490 @Test
@@ -846,7 +872,7 @@ class WCStatsStoreTest {
846872 }
847873
848874 @Test
849- fun testGetNewVisitorStatsWithInvalidData (){
875+ fun testGetNewVisitorStatsWithInvalidData () {
850876 // wrong-visitor-stats-data.json includes different wrong formatted data to ensure
851877 // that getNewVisitorStats is resilient and can recover from unexpected data
852878 //
@@ -865,4 +891,125 @@ class WCStatsStoreTest {
865891 assertEquals(defaultWeekVisitorStats[" 2019-07-17" ],0 )
866892 assertEquals(defaultWeekVisitorStats[" 2019-07-18" ],0 )
867893 }
894+
895+ @Test
896+ fun testFetchBundlesErrorResponse () = runBlocking {
897+ val error = WooError (
898+ type = WooErrorType .INVALID_RESPONSE ,
899+ original = GenericErrorType .INVALID_RESPONSE ,
900+ message = " Invalid Response"
901+ )
902+ val response: WooPayload <BundleStatsApiResponse > = WooPayload (error)
903+
904+ whenever(mockBundleStatsRestClient.fetchBundleStats(any(), any(), any(), any()))
905+ .thenReturn(response)
906+
907+ val result = wcStatsStore.fetchProductBundlesStats(
908+ SiteModel (),
909+ " 2024-03-01" ,
910+ endDate = " 2024-04-01" ,
911+ interval = " day"
912+ )
913+
914+ assertTrue(result.isError)
915+ assertTrue(result.model == null )
916+ assertThat(result.error, isEqual(error))
917+ }
918+
919+ @Test
920+ fun testFetchBundlesNullResponse () = runBlocking {
921+ val response: WooPayload <BundleStatsApiResponse > = WooPayload (null )
922+
923+ whenever(mockBundleStatsRestClient.fetchBundleStats(any(), any(), any(), any()))
924+ .thenReturn(response)
925+
926+ val result = wcStatsStore.fetchProductBundlesStats(
927+ SiteModel (),
928+ " 2024-03-01" ,
929+ endDate = " 2024-04-01" ,
930+ interval = " day"
931+ )
932+
933+ assertTrue(result.isError)
934+ assertTrue(result.model == null )
935+ assertThat(result.error.type, isEqual(WooErrorType .GENERIC_ERROR ))
936+ }
937+
938+ @Test
939+ fun testFetchBundlesSuccessResponse () = runBlocking {
940+ val totals = BundleStatsTotals (
941+ itemsSold = 5 ,
942+ netRevenue = 1000.00
943+ )
944+ val statsResponse = BundleStatsApiResponse (totals = totals)
945+ val response: WooPayload <BundleStatsApiResponse > = WooPayload (statsResponse)
946+
947+ whenever(mockBundleStatsRestClient.fetchBundleStats(any(), any(), any(), any()))
948+ .thenReturn(response)
949+
950+ val result = wcStatsStore.fetchProductBundlesStats(
951+ SiteModel (),
952+ " 2024-03-01" ,
953+ endDate = " 2024-04-01" ,
954+ interval = " day"
955+ )
956+
957+ assertFalse(result.isError)
958+ assertTrue(result.model != null )
959+ assertEquals(result.model!! .itemsSold, totals.itemsSold)
960+ assertEquals(result.model!! .netRevenue, totals.netRevenue)
961+ }
962+
963+ @Test
964+ fun testSuccessfulFetchingVisitorSummaryStats () = runBlocking {
965+ val site = SiteModel ().apply { id = 0 }
966+ val apiResponse = VisitorStatsSummaryApiResponse (
967+ date = " 2024-03-01" ,
968+ period = " day" ,
969+ views = 3 ,
970+ visitors = 2
971+ )
972+ whenever(
973+ mockOrderStatsRestClient.fetchVisitorStatsSummary(
974+ site = site,
975+ granularity = StatsGranularity .DAYS ,
976+ date = " 2024-03-01" ,
977+ force = false
978+ )
979+ ).thenReturn(WooPayload (apiResponse))
980+
981+ val result = wcStatsStore.fetchVisitorStatsSummary(
982+ site = site,
983+ granularity = StatsGranularity .DAYS ,
984+ date = " 2024-03-01"
985+ )
986+
987+ assertEquals(false , result.isError)
988+ assertEquals(WCVisitorStatsSummary (StatsGranularity .DAYS , " 2024-03-01" , 3 , 2 ), result.model)
989+ assertEquals(
990+ WCVisitorStatsSummary (StatsGranularity .DAYS , " 2024-03-01" , 3 , 2 ),
991+ wcStatsStore.getVisitorStatsSummary(site, StatsGranularity .DAYS , " 2024-03-01" )
992+ )
993+ }
994+
995+ @Test
996+ fun testFailedFetchingVisitorSummaryStats () = runBlocking {
997+ val site = SiteModel ().apply { id = 0 }
998+ whenever(
999+ mockOrderStatsRestClient.fetchVisitorStatsSummary(
1000+ site = site,
1001+ granularity = StatsGranularity .DAYS ,
1002+ date = " 2024-03-01" ,
1003+ force = false
1004+ )
1005+ ).thenReturn(WooPayload (WooError (GENERIC_ERROR , GenericErrorType .UNKNOWN )))
1006+
1007+ val result = wcStatsStore.fetchVisitorStatsSummary(
1008+ site = site,
1009+ granularity = StatsGranularity .DAYS ,
1010+ date = " 2024-03-01"
1011+ )
1012+
1013+ assertEquals(true , result.isError)
1014+ }
8681015}
0 commit comments