Skip to content

Commit

Permalink
WTA #44: Moved RecalculateLatestStreakService to use-cases package.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacob3075 committed Oct 29, 2022
1 parent 56d606a commit 4d50496
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 17 deletions.
23 changes: 23 additions & 0 deletions .idea/runConfigurations/Home_Unit_Tests.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ 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.InstantProvider
import com.jacob.wakatimeapp.home.domain.RecalculateLatestStreakService
import com.jacob.wakatimeapp.home.domain.getLatestStreakInRange
import com.jacob.wakatimeapp.home.domain.models.StreakRange
import javax.inject.Inject
Expand All @@ -21,7 +20,7 @@ import kotlinx.datetime.minus
class CalculateCurrentStreakUC @Inject constructor(
private val homePageCache: HomePageCache,
private val instantProvider: InstantProvider,
private val recalculateLatestStreakService: RecalculateLatestStreakService,
private val recalculateLatestStreakUC: RecalculateLatestStreakUC,
) {

suspend operator fun invoke(): Either<Error, StreakRange> = either {
Expand Down Expand Up @@ -61,7 +60,7 @@ class CalculateCurrentStreakUC @Inject constructor(
private suspend fun whenFailedToCombine(
recalculatedStreakForLast7Days: StreakRange,
) = when (recalculatedStreakForLast7Days.days) {
7 -> recalculateLatestStreakService.calculate(
7 -> recalculateLatestStreakUC.calculate(
start = instantProvider.now().toDate().minus(7, DateTimeUnit.DAY),
value = 1,
unit = DateTimeUnit.MONTH
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package com.jacob.wakatimeapp.home.domain
package com.jacob.wakatimeapp.home.domain.usecases

import arrow.core.Either
import arrow.core.computations.either
import com.jacob.wakatimeapp.core.models.Error
import com.jacob.wakatimeapp.home.data.network.HomePageNetworkData
import com.jacob.wakatimeapp.home.domain.getLatestStreakInRange
import com.jacob.wakatimeapp.home.domain.models.StreakRange
import javax.inject.Inject
import javax.inject.Singleton
Expand All @@ -14,7 +15,7 @@ import kotlinx.datetime.daysUntil
import kotlinx.datetime.minus

@Singleton
class RecalculateLatestStreakService @Inject constructor(
class RecalculateLatestStreakUC @Inject constructor(
private val homePageNetworkData: HomePageNetworkData,
) {
suspend fun calculate(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ 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.InstantProvider
import com.jacob.wakatimeapp.home.domain.RecalculateLatestStreakService
import com.jacob.wakatimeapp.home.domain.models.Last7DaysStats
import com.jacob.wakatimeapp.home.domain.models.StreakRange
import io.kotest.assertions.asClue
Expand All @@ -29,7 +28,7 @@ internal class CalculateCurrentStreakUCRobot {
private var result: Either<Error, StreakRange>? = null

private val mockCache: HomePageCache = mockk(relaxUnitFun = true)
private val mockRecalculateStreak: RecalculateLatestStreakService = mockk()
private val mockRecalculateStreak: RecalculateLatestStreakUC = mockk()

fun buildUseCase() = apply {
clearMocks(mockCache, mockRecalculateStreak)
Expand All @@ -42,7 +41,7 @@ internal class CalculateCurrentStreakUCRobot {
override fun now() = currentDayInstant
},
homePageCache = mockCache,
recalculateLatestStreakService = mockRecalculateStreak,
recalculateLatestStreakUC = mockRecalculateStreak,
)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.jacob.wakatimeapp.home.domain
package com.jacob.wakatimeapp.home.domain.usecases

import arrow.core.Either
import com.jacob.wakatimeapp.core.models.DailyStats
Expand All @@ -17,8 +17,8 @@ import kotlinx.datetime.DateTimeUnit
import kotlinx.datetime.LocalDate
import kotlinx.datetime.plus

internal class RecalculateLatestStreakServiceRobot {
private lateinit var service: RecalculateLatestStreakService
internal class RecalculateLatestStreakUCRobot {
private lateinit var useCase: RecalculateLatestStreakUC

private val mockHomePageNetworkData: HomePageNetworkData = mockk()

Expand All @@ -28,11 +28,11 @@ internal class RecalculateLatestStreakServiceRobot {
clearMocks(mockHomePageNetworkData)
result = null

service = RecalculateLatestStreakService(mockHomePageNetworkData)
useCase = RecalculateLatestStreakUC(mockHomePageNetworkData)
}

suspend fun calculate(start: LocalDate, value: Int, unit: DateTimeUnit.DateBased) = apply {
result = service.calculate(
result = useCase.calculate(
start,
value,
unit,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package com.jacob.wakatimeapp.home.domain
package com.jacob.wakatimeapp.home.domain.usecases

import arrow.core.right
import com.jacob.wakatimeapp.core.models.Stats
import com.jacob.wakatimeapp.core.models.StatsRange
import com.jacob.wakatimeapp.core.models.Time
import com.jacob.wakatimeapp.home.domain.RecalculateLatestStreakServiceRobot.Companion.createDailyStats
import com.jacob.wakatimeapp.home.domain.models.StreakRange
import com.jacob.wakatimeapp.home.domain.usecases.RecalculateLatestStreakUCRobot.Companion.createDailyStats
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.runTest
import kotlinx.datetime.DateTimeUnit
Expand All @@ -16,8 +16,8 @@ import kotlinx.datetime.toLocalDate
import org.junit.jupiter.api.Test

@OptIn(ExperimentalCoroutinesApi::class)
internal class RecalculateLatestStreakServiceTest {
private val robot = RecalculateLatestStreakServiceRobot()
internal class RecalculateLatestStreakUCTest {
private val robot = RecalculateLatestStreakUCRobot()

@Test
internal fun `when called with duration and there is no valid streak, then return empty streak`() =
Expand Down

0 comments on commit 4d50496

Please sign in to comment.