Skip to content

Commit

Permalink
WTA #44: Started adding tests for GetCachedHomePageUiData.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacob3075 committed Oct 22, 2022
1 parent 9534492 commit 7b75120
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -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())
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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,
Expand All @@ -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),
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Either<Error, CachedHomePageUiData?>>()

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<Either<Error, CachedHomePageUiData?>>) = 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)
}
}
Original file line number Diff line number Diff line change
@@ -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)))
}
}

0 comments on commit 7b75120

Please sign in to comment.