-
Notifications
You must be signed in to change notification settings - Fork 131
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Alejo
committed
Jan 25, 2024
1 parent
cc5e075
commit 502cdcc
Showing
1 changed file
with
148 additions
and
0 deletions.
There are no files selected for viewing
148 changes: 148 additions & 0 deletions
148
WooCommerce/src/test/kotlin/com/woocommerce/android/ui/mystore/StatsRepositoryTests.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
package com.woocommerce.android.ui.mystore | ||
|
||
import com.woocommerce.android.tools.SelectedSite | ||
import com.woocommerce.android.ui.mystore.data.StatsRepository | ||
import com.woocommerce.android.viewmodel.BaseUnitTest | ||
import kotlinx.coroutines.ExperimentalCoroutinesApi | ||
import org.assertj.core.api.Assertions.assertThat | ||
import org.junit.Before | ||
import org.junit.Test | ||
import org.mockito.kotlin.any | ||
import org.mockito.kotlin.eq | ||
import org.mockito.kotlin.mock | ||
import org.mockito.kotlin.whenever | ||
import org.wordpress.android.fluxc.action.WCStatsAction | ||
import org.wordpress.android.fluxc.model.SiteModel | ||
import org.wordpress.android.fluxc.model.WCRevenueStatsModel | ||
import org.wordpress.android.fluxc.store.WCLeaderboardsStore | ||
import org.wordpress.android.fluxc.store.WCOrderStore | ||
import org.wordpress.android.fluxc.store.WCStatsStore | ||
import org.wordpress.android.fluxc.store.WooCommerceStore | ||
|
||
@OptIn(ExperimentalCoroutinesApi::class) | ||
class StatsRepositoryTests : BaseUnitTest() { | ||
|
||
private val selectedSite: SelectedSite = mock() | ||
private val wcStatsStore: WCStatsStore = mock() | ||
private val wcOrderStore: WCOrderStore = mock() | ||
private val wcLeaderboardsStore: WCLeaderboardsStore = mock() | ||
private val wooCommerceStore: WooCommerceStore = mock() | ||
|
||
private lateinit var sut: StatsRepository | ||
|
||
private val defaultSiteModel = SiteModel() | ||
|
||
@Before | ||
fun setup() { | ||
sut = StatsRepository( | ||
selectedSite = selectedSite, | ||
wcStatsStore = wcStatsStore, | ||
wcOrderStore = wcOrderStore, | ||
wcLeaderboardsStore = wcLeaderboardsStore, | ||
wooCommerceStore = wooCommerceStore | ||
) | ||
} | ||
|
||
@Test | ||
fun `when visitors and revenue requests succeed then a success response is returned containing both value`() = testBlocking { | ||
val granularity = WCStatsStore.StatsGranularity.DAYS | ||
val startDate = "2024-01-25 00:00:00" | ||
val endDate = "2024-01-25 23:59:59" | ||
val visitorStatsResponse = WCStatsStore.OnWCStatsChanged( | ||
rowsAffected = 2, | ||
granularity = granularity, | ||
quantity = "5", | ||
date = startDate | ||
) | ||
|
||
val revenueStatsResponse = WCStatsStore.OnWCRevenueStatsChanged( | ||
rowsAffected = 2, | ||
granularity = granularity, | ||
startDate = startDate, | ||
endDate = endDate | ||
) | ||
|
||
whenever(selectedSite.get()).thenReturn(defaultSiteModel) | ||
whenever(wooCommerceStore.getSiteSettings(any())).thenReturn(null) | ||
whenever(wcStatsStore.fetchNewVisitorStats(any())).thenReturn(visitorStatsResponse) | ||
whenever(wcStatsStore.fetchRevenueStats(any())).thenReturn(revenueStatsResponse) | ||
whenever(wcStatsStore.getRawRevenueStats(eq(defaultSiteModel), eq(granularity), eq(startDate), eq(endDate))) | ||
.thenReturn(WCRevenueStatsModel()) | ||
|
||
val result = sut.fetchStats( | ||
granularity = WCStatsStore.StatsGranularity.DAYS, | ||
forced = true, | ||
includeVisitorStats = true | ||
) | ||
|
||
assertThat(result.isError).isEqualTo(false) | ||
assertThat(result.model).isNotNull | ||
assertThat(result.model!!.revenue).isNotNull | ||
assertThat(result.model!!.visitors).isNotNull | ||
} | ||
|
||
@Test | ||
fun `when visitors requests fails then a success response is returned with visitors null`() = testBlocking { | ||
val granularity = WCStatsStore.StatsGranularity.DAYS | ||
val startDate = "2024-01-25 00:00:00" | ||
val endDate = "2024-01-25 23:59:59" | ||
val visitorStatsResponse = WCStatsStore.OnWCStatsChanged(0, granularity).also { | ||
it.error = WCStatsStore.OrderStatsError() | ||
it.causeOfChange = WCStatsAction.FETCH_NEW_VISITOR_STATS | ||
} | ||
|
||
val revenueStatsResponse = WCStatsStore.OnWCRevenueStatsChanged( | ||
rowsAffected = 2, | ||
granularity = granularity, | ||
startDate = startDate, | ||
endDate = endDate | ||
) | ||
|
||
whenever(selectedSite.get()).thenReturn(defaultSiteModel) | ||
whenever(wooCommerceStore.getSiteSettings(any())).thenReturn(null) | ||
whenever(wcStatsStore.fetchNewVisitorStats(any())).thenReturn(visitorStatsResponse) | ||
whenever(wcStatsStore.fetchRevenueStats(any())).thenReturn(revenueStatsResponse) | ||
whenever(wcStatsStore.getRawRevenueStats(eq(defaultSiteModel), eq(granularity), eq(startDate), eq(endDate))) | ||
.thenReturn(WCRevenueStatsModel()) | ||
|
||
val result = sut.fetchStats( | ||
granularity = WCStatsStore.StatsGranularity.DAYS, | ||
forced = true, | ||
includeVisitorStats = true | ||
) | ||
|
||
assertThat(result.isError).isEqualTo(false) | ||
assertThat(result.model).isNotNull | ||
assertThat(result.model!!.revenue).isNotNull | ||
assertThat(result.model!!.visitors).isNull() | ||
} | ||
|
||
@Test | ||
fun `when revenue requests fails then an error is returned`() = testBlocking { | ||
val granularity = WCStatsStore.StatsGranularity.DAYS | ||
val startDate = "2024-01-25 00:00:00" | ||
val visitorStatsResponse = WCStatsStore.OnWCStatsChanged( | ||
rowsAffected = 2, | ||
granularity = granularity, | ||
quantity = "5", | ||
date = startDate | ||
) | ||
|
||
val revenueStatsResponse = WCStatsStore.OnWCRevenueStatsChanged(0, granularity) | ||
.also { it.error = WCStatsStore.OrderStatsError() } | ||
|
||
whenever(selectedSite.get()).thenReturn(defaultSiteModel) | ||
whenever(wooCommerceStore.getSiteSettings(any())).thenReturn(null) | ||
whenever(wcStatsStore.fetchNewVisitorStats(any())).thenReturn(visitorStatsResponse) | ||
whenever(wcStatsStore.fetchRevenueStats(any())).thenReturn(revenueStatsResponse) | ||
|
||
val result = sut.fetchStats( | ||
granularity = WCStatsStore.StatsGranularity.DAYS, | ||
forced = true, | ||
includeVisitorStats = true | ||
) | ||
|
||
assertThat(result.isError).isEqualTo(true) | ||
assertThat(result.model).isNull() | ||
} | ||
} |