-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WTA #39: Added simple test case and code for it.
- Loading branch information
Showing
6 changed files
with
137 additions
and
12 deletions.
There are no files selected for viewing
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
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
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
39 changes: 37 additions & 2 deletions
39
home/src/main/java/com/jacob/wakatimeapp/home/domain/usecases/CalculateCurrentStreakUC.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 |
---|---|---|
@@ -1,22 +1,57 @@ | ||
package com.jacob.wakatimeapp.home.domain.usecases | ||
|
||
import arrow.core.Either | ||
import arrow.core.computations.either | ||
import com.jacob.wakatimeapp.core.common.today | ||
import com.jacob.wakatimeapp.core.models.Error | ||
import com.jacob.wakatimeapp.core.models.Time | ||
import com.jacob.wakatimeapp.home.data.local.HomePageCache | ||
import com.jacob.wakatimeapp.home.domain.models.StreakRange | ||
import javax.inject.Inject | ||
import javax.inject.Singleton | ||
import kotlin.coroutines.CoroutineContext | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.channelFlow | ||
import kotlinx.coroutines.flow.first | ||
import kotlinx.coroutines.launch | ||
import kotlinx.datetime.LocalDate | ||
|
||
@Singleton | ||
class CalculateCurrentStreakUC @Inject constructor( | ||
dispatcher: CoroutineContext = Dispatchers.IO, | ||
private val homePageCache: HomePageCache, | ||
) { | ||
|
||
operator fun invoke(): Flow<Either<Error, StreakRange>> { | ||
TODO() | ||
operator fun invoke(): Flow<Either<Error, StreakRange>> = channelFlow { | ||
val currentStreakFlow = homePageCache.getCurrentStreak() | ||
val last7DaysStatsFlow = homePageCache.getLast7DaysStats() | ||
|
||
launch { currentStreakFlow.collect { send(it) } } | ||
|
||
last7DaysStatsFlow.collect { last7DaysStatsEither -> | ||
either { | ||
val last7DaysStats = last7DaysStatsEither.bind() | ||
val currentStreak = currentStreakFlow.first().bind() | ||
|
||
val todaysStats = last7DaysStats.weeklyTimeSpent[LocalDate.today] ?: Time.ZERO | ||
|
||
if (todaysStats == Time.ZERO) return@either | ||
|
||
val updatedStreakRange = if (currentStreak == StreakRange.ZERO) { | ||
StreakRange( | ||
start = LocalDate.today, | ||
end = LocalDate.today, | ||
) | ||
} else { | ||
StreakRange( | ||
start = currentStreak.start, | ||
end = LocalDate.today, | ||
) | ||
} | ||
|
||
homePageCache.updateCurrentStreak(updatedStreakRange) | ||
} | ||
} | ||
} | ||
} |
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
52 changes: 52 additions & 0 deletions
52
.../src/test/java/com/jacob/wakatimeapp/home/domain/usecases/CalculateCurrentStreakUCTest.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,52 @@ | ||
package com.jacob.wakatimeapp.home.domain.usecases | ||
|
||
import arrow.core.right | ||
import com.jacob.wakatimeapp.core.models.Time | ||
import com.jacob.wakatimeapp.home.domain.models.StreakRange | ||
import com.jacob.wakatimeapp.home.domain.usecases.CalculateCurrentStreakUCRobot.Companion.last7DaysStats | ||
import com.jacob.wakatimeapp.home.domain.usecases.CalculateCurrentStreakUCRobot.Companion.streakRange | ||
import com.jacob.wakatimeapp.home.domain.usecases.CalculateCurrentStreakUCRobot.Companion.today | ||
import kotlinx.coroutines.ExperimentalCoroutinesApi | ||
import kotlinx.coroutines.test.runTest | ||
import kotlinx.datetime.DatePeriod | ||
import kotlinx.datetime.LocalDate | ||
import kotlinx.datetime.minus | ||
import org.junit.jupiter.api.Test | ||
|
||
@OptIn(ExperimentalCoroutinesApi::class) | ||
internal class CalculateCurrentStreakUCTest { | ||
private val robot = CalculateCurrentStreakUCRobot() | ||
|
||
@Test | ||
internal fun `when there is no value in the cache, then set current streak value to 0`() = | ||
runTest { | ||
val map = mutableMapOf<LocalDate, Time>() | ||
for (i in 0 until 7) { | ||
map[today.minus(DatePeriod(years = 0, months = 0, days = i))] = Time.ZERO | ||
} | ||
|
||
val updatedLast7DaysStats = last7DaysStats.copy(weeklyTimeSpent = map) | ||
|
||
robot.buildUseCase() | ||
.mockGetCurrentStreak(StreakRange.ZERO.right()) | ||
.mockGetLast7DaysStats(updatedLast7DaysStats.right()) | ||
.callUseCase() | ||
.resultSizeShouldBe(1) | ||
.resultsShouldContain(streakRange.right()) | ||
} | ||
|
||
@Test | ||
internal fun `when there is a value in the cache, then increase the value by 1`() { | ||
TODO("Not yet implemented") | ||
} | ||
|
||
@Test | ||
internal fun `when last 7 days stats cache sends multiple values, then streak should only increase by 1`() { | ||
TODO("Not yet implemented") | ||
} | ||
|
||
@Test | ||
internal fun `when there is non 0 value in cache and todays stats arrives later, then streak should increase by 1`() { | ||
TODO("Not yet implemented") | ||
} | ||
} |