From 7b7512078261194a9ede0cde78d5a0214e73fe45 Mon Sep 17 00:00:00 2001 From: Jacob Bosco Date: Sat, 22 Oct 2022 11:43:13 +0530 Subject: [PATCH] WTA #44: Started adding tests for GetCachedHomePageUiData. --- .../jacob/wakatimeapp/core/common/Utils.kt | 6 +- .../usecases/GetCachedHomePageUiData.kt | 17 ++--- .../usecases/GetCachedHomePageUiDataRobot.kt | 75 +++++++++++++++++++ .../usecases/GetCachedHomePageUiDataTest.kt | 22 ++++++ 4 files changed, 107 insertions(+), 13 deletions(-) create mode 100644 home/src/test/java/com/jacob/wakatimeapp/home/domain/usecases/GetCachedHomePageUiDataRobot.kt create mode 100644 home/src/test/java/com/jacob/wakatimeapp/home/domain/usecases/GetCachedHomePageUiDataTest.kt diff --git a/core/common/src/main/java/com/jacob/wakatimeapp/core/common/Utils.kt b/core/common/src/main/java/com/jacob/wakatimeapp/core/common/Utils.kt index 15f76fb8..f6e54b52 100644 --- a/core/common/src/main/java/com/jacob/wakatimeapp/core/common/Utils.kt +++ b/core/common/src/main/java/com/jacob/wakatimeapp/core/common/Utils.kt @@ -1,14 +1,14 @@ -package com.jacob.wakatimeapp.core.common // ktlint-disable filename +package com.jacob.wakatimeapp.core.common import java.time.format.TextStyle.SHORT import java.util.Locale -import kotlinx.datetime.Instant +import kotlinx.datetime.Clock import kotlinx.datetime.LocalDate import kotlinx.datetime.TimeZone import kotlinx.datetime.toLocalDateTime val LocalDate.Companion.today - get() = Instant.DISTANT_PAST.toLocalDateTime(TimeZone.currentSystemDefault()).date + get() = Clock.System.now().toLocalDateTime(TimeZone.currentSystemDefault()).date fun LocalDate.getDisplayNameForDay(): String = dayOfWeek.getDisplayName(SHORT, Locale.getDefault()) diff --git a/home/src/main/java/com/jacob/wakatimeapp/home/domain/usecases/GetCachedHomePageUiData.kt b/home/src/main/java/com/jacob/wakatimeapp/home/domain/usecases/GetCachedHomePageUiData.kt index 8745cb3e..ede274de 100644 --- a/home/src/main/java/com/jacob/wakatimeapp/home/domain/usecases/GetCachedHomePageUiData.kt +++ b/home/src/main/java/com/jacob/wakatimeapp/home/domain/usecases/GetCachedHomePageUiData.kt @@ -34,7 +34,7 @@ class GetCachedHomePageUiData @Inject constructor( operator fun invoke(cacheValidity: CacheValidity = DEFAULT) = channelFlow { val initialLastRequestTime = homePageCache.getLastRequestTime().first() - if (firstRequestOfDay(initialLastRequestTime)) { + if (initialLastRequestTime.isFirstRequestOfDay()) { send(Right(null)) return@channelFlow } @@ -73,7 +73,12 @@ class GetCachedHomePageUiData @Inject constructor( private fun getStreaks() = homePageCache.getCurrentStreak() - private fun firstRequestOfDay(lastRequestTime: Instant) = lastRequestTime.isPreviousDay() + private fun Instant.isFirstRequestOfDay(): Boolean { + val lastRequestDate = toLocalDateTime(instantProvider.timeZone).date.toEpochDays() + val currentDate = + instantProvider.now().toLocalDateTime(instantProvider.timeZone).date.toEpochDays() + return currentDate - lastRequestDate >= 1 + } private fun validDataInCache( lastRequestTime: Instant, @@ -83,14 +88,6 @@ class GetCachedHomePageUiData @Inject constructor( return minutesBetweenLastRequest.inWholeMinutes < cacheValidityTime.minutes } - private fun Instant.isPreviousDay(): Boolean { - val lastRequestDate = this.toLocalDateTime(instantProvider.timeZone).date.toEpochDays() - val currentDate = - instantProvider.now().toLocalDateTime(instantProvider.timeZone).date.toEpochDays() - - return currentDate - lastRequestDate >= 1 - } - enum class CacheValidity(val minutes: Long) { DEFAULT(15L), INVALID(0L), diff --git a/home/src/test/java/com/jacob/wakatimeapp/home/domain/usecases/GetCachedHomePageUiDataRobot.kt b/home/src/test/java/com/jacob/wakatimeapp/home/domain/usecases/GetCachedHomePageUiDataRobot.kt new file mode 100644 index 00000000..83c0782e --- /dev/null +++ b/home/src/test/java/com/jacob/wakatimeapp/home/domain/usecases/GetCachedHomePageUiDataRobot.kt @@ -0,0 +1,75 @@ +package com.jacob.wakatimeapp.home.domain.usecases + +import arrow.core.Either +import com.jacob.wakatimeapp.core.common.auth.AuthDataStore +import com.jacob.wakatimeapp.core.models.Error +import com.jacob.wakatimeapp.home.data.local.HomePageCache +import com.jacob.wakatimeapp.home.domain.InstantProvider +import io.kotest.matchers.collections.shouldContainExactly +import io.kotest.matchers.shouldBe +import io.mockk.clearMocks +import io.mockk.coEvery +import io.mockk.mockk +import kotlin.time.Duration.Companion.days +import kotlinx.coroutines.flow.flowOf +import kotlinx.coroutines.flow.toList +import kotlinx.datetime.Instant +import kotlinx.datetime.TimeZone + +internal class GetCachedHomePageUiDataRobot { + private lateinit var useCase: GetCachedHomePageUiData + + private val results = mutableListOf>() + + private val mockHomePageCache: HomePageCache = mockk() + private val mockAuthDataStore: AuthDataStore = mockk() + + fun buildUseCase(instantProvider: InstantProvider = defaultTestInstantProvider) = apply { + clearMocks(mockHomePageCache, mockAuthDataStore) + results.clear() + + useCase = GetCachedHomePageUiData( + instantProvider = instantProvider, + homePageCache = mockHomePageCache, + authDataStore = mockAuthDataStore, + ) + } + + suspend fun callUseCase() = apply { + useCase().toList(results) + } + + fun setLastRequestTime(previousDay: Instant) = apply { + coEvery { mockHomePageCache.getLastRequestTime() } returns flowOf(previousDay) + } + + fun resultsSizeShouldBe(size: Int) = apply { + results.size shouldBe size + } + + fun resultsShouldBe(expected: List>) = apply { + results shouldContainExactly expected + } + + companion object { + val defaultTestInstantProvider = object : InstantProvider { + override val timeZone = TimeZone.UTC + + override fun now() = currentDayInstant + } + + /** + * Start of a random day + * + * Value: + * - date: 11/10/2022 (dd/mm/yyyy) + * - time: 00:00:00 (hh:mm::ss) + */ + val currentDayInstant = Instant.parse("2022-10-11T00:00:00Z") + + /** + * Takes [currentDayInstant] and subtracts 1 day from it + */ + val previousDayInstant = currentDayInstant.minus(1.days) + } +} diff --git a/home/src/test/java/com/jacob/wakatimeapp/home/domain/usecases/GetCachedHomePageUiDataTest.kt b/home/src/test/java/com/jacob/wakatimeapp/home/domain/usecases/GetCachedHomePageUiDataTest.kt new file mode 100644 index 00000000..e66f187e --- /dev/null +++ b/home/src/test/java/com/jacob/wakatimeapp/home/domain/usecases/GetCachedHomePageUiDataTest.kt @@ -0,0 +1,22 @@ +package com.jacob.wakatimeapp.home.domain.usecases + +import arrow.core.Either +import com.jacob.wakatimeapp.home.domain.usecases.GetCachedHomePageUiDataRobot.Companion.previousDayInstant +import kotlinx.coroutines.ExperimentalCoroutinesApi +import kotlinx.coroutines.test.runTest +import org.junit.jupiter.api.Test + +@OptIn(ExperimentalCoroutinesApi::class) +internal class GetCachedHomePageUiDataTest { + private val robot = GetCachedHomePageUiDataRobot() + + @Test + internal fun `when last request was made the previous day, then use case should return null`() = + runTest { + robot.buildUseCase() + .setLastRequestTime(previousDayInstant) + .callUseCase() + .resultsSizeShouldBe(1) + .resultsShouldBe(listOf(Either.Right(null))) + } +}