Skip to content

Commit

Permalink
WTA #44: Added some more tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacob3075 committed Oct 22, 2022
1 parent 09f8531 commit f840208
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class GetCachedHomePageUiData @Inject constructor(
last7DaysStats = last7DaysStats,
userDetails = userDetails.toHomePageUserDetails(),
streaks = streaks,
isStateData = validDataInCache(
isStateData = !validDataInCache(
lastRequestTime = lastRequestTime,
cacheValidityTime = cacheValidity
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ import com.jacob.wakatimeapp.home.domain.InstantProvider
import com.jacob.wakatimeapp.home.domain.models.Last7DaysStats
import com.jacob.wakatimeapp.home.domain.models.StreakRange
import com.jacob.wakatimeapp.home.domain.usecases.GetCachedHomePageUiDataRobot.Companion.currentDayInstant
import io.kotest.assertions.arrow.core.shouldBeLeft
import io.kotest.assertions.arrow.core.shouldBeRight
import io.kotest.matchers.nulls.shouldBeNull
import io.kotest.matchers.nulls.shouldNotBeNull
import io.kotest.matchers.shouldBe
import io.mockk.clearMocks
Expand Down Expand Up @@ -64,8 +66,6 @@ internal class GetCachedHomePageUiDataRobot {
}

block(context, this@GetCachedHomePageUiDataRobot)

receiveTurbine!!.awaitComplete()
}

context (ItemAssertionContext)
Expand All @@ -78,6 +78,11 @@ internal class GetCachedHomePageUiDataRobot {
item.shouldBeRight()
}

context (ItemAssertionContext)
fun itemShouldBeLeft() = apply {
item.shouldBeLeft()
}

context (ItemAssertionContext)
fun itemShouldNotBeNull() = apply {
item.fold(
Expand All @@ -86,11 +91,28 @@ internal class GetCachedHomePageUiDataRobot {
)
}

context (ItemAssertionContext)
fun itemShouldBeNull() = apply {
item.fold(
ifLeft = { 1 shouldBe 2 },
ifRight = { it.shouldBeNull() }
)
}

context (ItemAssertionContext)
fun itemShouldNotBeStale() = apply {
item.map { it!!.isStateData } shouldBeRight false
}

context (ItemAssertionContext)
fun itemShouldBeStale() = apply {
item.map { it!!.isStateData } shouldBeRight true
}

suspend fun expectNoMoreItems() = apply {
receiveTurbine!!.awaitComplete()
}

fun setLastRequestTime(previousDay: Instant) = apply {
coEvery { mockHomePageCache.getLastRequestTime() } returns flowOf(previousDay)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package com.jacob.wakatimeapp.home.domain.usecases

import arrow.core.Either
import arrow.core.left
import arrow.core.right
import com.jacob.wakatimeapp.core.models.Error
import com.jacob.wakatimeapp.home.domain.usecases.GetCachedHomePageUiDataRobot.Companion.currentDayInstant
import com.jacob.wakatimeapp.home.domain.usecases.GetCachedHomePageUiDataRobot.Companion.currentStreak
import com.jacob.wakatimeapp.home.domain.usecases.GetCachedHomePageUiDataRobot.Companion.last7DaysStats
Expand All @@ -24,8 +25,9 @@ internal class GetCachedHomePageUiDataTest {
.setLastRequestTime(startOfDay - 2.hours)
.callUseCase(this)
.withNextItem {
itemShouldBe(Either.Right(null))
itemShouldBeNull()
}
.expectNoMoreItems()
}

@Test
Expand All @@ -35,8 +37,9 @@ internal class GetCachedHomePageUiDataTest {
.setLastRequestTime(startOfDay - 5.minutes)
.callUseCase(this)
.withNextItem {
itemShouldBe(Either.Right(null))
itemShouldBeNull()
}
.expectNoMoreItems()
}

@Test
Expand All @@ -48,10 +51,46 @@ internal class GetCachedHomePageUiDataTest {
.mockLast7DaysStats(last7DaysStats.right())
.mockCurrentStreak(currentStreak.right())
.callUseCase(this)
.withNextItem {
itemShouldBeRight()
.itemShouldNotBeNull()
.itemShouldBeStale()
}
.expectNoMoreItems()
}

@Test
internal fun `when last request time is less than default cache lifetime, the caches data should be returned with is stale set to false`() =
runTest {
robot.buildUseCase()
.setLastRequestTime(currentDayInstant - 10.minutes)
.mockUserDetails(userDetails)
.mockLast7DaysStats(last7DaysStats.right())
.mockCurrentStreak(currentStreak.right())
.callUseCase(this)
.withNextItem {
itemShouldBeRight()
.itemShouldNotBeNull()
.itemShouldNotBeStale()
}
.expectNoMoreItems()
}

@Test
internal fun `when an error occurs while getting data from the cache, then the errors should be propagated up`() =
runTest {
val networkError = Error.NetworkErrors.create("Error", 400).left()
val databaseError = Error.DatabaseError.UnknownError("Error", Throwable()).left()
robot.buildUseCase()
.setLastRequestTime(currentDayInstant - 20.minutes)
.mockUserDetails(userDetails)
.mockLast7DaysStats(networkError)
.mockCurrentStreak(databaseError)
.callUseCase(this)
.withNextItem {
itemShouldBeLeft()
.itemShouldBe(networkError)
}
.expectNoMoreItems()
}
}

0 comments on commit f840208

Please sign in to comment.