Skip to content

Commit

Permalink
✨ Update unit test for allTimeChart
Browse files Browse the repository at this point in the history
- Now the funSpec test are made in getFormattedValue cheking if it
  return correctly
  • Loading branch information
gBL17 committed Sep 19, 2024
1 parent 06ef90a commit e824e8d
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,13 @@ import com.github.mikephil.charting.data.Entry
import com.github.mikephil.charting.data.LineDataSet
import java.time.LocalDate.now

private fun calculateGramsDistributionPerDaySinceFirstUse(uses: List<Use>): List<Entry> {
val dayBeforeFirstUseDate = uses.minBy { it.date }.localDate.toEpochDay()
val daysSinceFirstUse = (dayBeforeFirstUseDate..now().toEpochDay()).toList()
return daysSinceFirstUse.associateWith {uses.filter { u -> u.localDate.toEpochDay() == it } }.toSortedMap()
.map { (k, v) -> Entry((k - dayBeforeFirstUseDate).toFloat(), v.totalGrams.toFloat())
}
@Composable
fun createAllTimeDistribution(uses: List<Use>): LineDataSet {
val label = stringResource(R.string.grams_distribution_over_days_since_first_use)
return createAllTimeLineDataSet(calculateAllTimeGramsDistribution(uses), label)
}

fun createLineDataSet(entryList: List<Entry>, label: String):LineDataSet {
fun createAllTimeLineDataSet(entryList: List<Entry>, label: String):LineDataSet {
return LineDataSet(entryList, label).apply {
valueFormatter = GramsValueFormatter
lineWidth = 6f
Expand All @@ -35,8 +33,10 @@ fun createLineDataSet(entryList: List<Entry>, label: String):LineDataSet {
}
}

@Composable
fun createAllTimeDistribution(uses: List<Use>): LineDataSet {
val label = stringResource(R.string.grams_distribution_over_days_since_first_use)
return createLineDataSet(calculateGramsDistributionPerDaySinceFirstUse(uses), label)
private fun calculateAllTimeGramsDistribution(uses: List<Use>): List<Entry> {
val dayBeforeFirstUseDate = uses.minBy { it.date }.localDate.toEpochDay()
val daysSinceFirstUse = (dayBeforeFirstUseDate..now().toEpochDay()).toList()
return daysSinceFirstUse.associateWith {uses.filter { u -> u.localDate.toEpochDay() == it } }.toSortedMap()
.map { (k, v) -> Entry((k - dayBeforeFirstUseDate).toFloat(), v.totalGrams.toFloat())
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package br.com.colman.petals.statistics.graph.formatter

import io.kotest.core.spec.style.FunSpec
import io.kotest.data.Row2
import io.kotest.data.forAll
import io.kotest.matchers.shouldBe
import java.time.LocalDateTime.now
import java.time.format.DateTimeFormatter

class DaysSinceFirstUseFormatter: FunSpec({

test("getFormattedValue returns correctly formatted days since first use (yyyy-MM-dd)"){
val dateFormat = "yyyy-MM-dd"
val formatter = DateTimeFormatter.ofPattern(dateFormat)
forAll(
Row2(0f, now().minusDays(3).format(formatter).toString()),
Row2(1f, now().minusDays(2).format(formatter).toString()),
Row2(2f, now().minusDays(1).format(formatter).toString()),
Row2(3f, now().format(formatter).toString())
){ value, expectedDay ->
val actual = DaysSinceFirstUseFormatter.getFormattedValue((value), null)
actual shouldBe expectedDay
}
}
})

0 comments on commit e824e8d

Please sign in to comment.