diff --git a/uhabits-core/assets/test/habitbull3.csv b/uhabits-core/assets/test/habitbull3.csv new file mode 100644 index 000000000..65b7bbbab --- /dev/null +++ b/uhabits-core/assets/test/habitbull3.csv @@ -0,0 +1,11 @@ +HabitName,HabitDescription,HabitCategory,CalendarDate,Value,CommentText +Pushups,,Fitness,2021-09-01,30, +Pushups,,Fitness,2022-01-08,100, +Pushups,,Fitness,2022-01-09,100, +Pushups,,Fitness,2022-01-10,100, +Pushups,,Fitness,2022-01-11,100, +Pushups,,Fitness,2022-01-12,100, +Pushups,,Fitness,2022-01-13,100, +run,,Fitness,2022-01-03,1, +run,,Fitness,2022-01-18,1, +run,,Fitness,2022-01-19,1, diff --git a/uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/io/HabitBullCSVImporter.kt b/uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/io/HabitBullCSVImporter.kt index 140122db7..4d47ca8c2 100644 --- a/uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/io/HabitBullCSVImporter.kt +++ b/uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/io/HabitBullCSVImporter.kt @@ -23,6 +23,7 @@ import org.isoron.uhabits.core.models.Entry import org.isoron.uhabits.core.models.Frequency import org.isoron.uhabits.core.models.Habit import org.isoron.uhabits.core.models.HabitList +import org.isoron.uhabits.core.models.HabitType import org.isoron.uhabits.core.models.ModelFactory import org.isoron.uhabits.core.models.Timestamp import java.io.BufferedReader @@ -50,7 +51,7 @@ class HabitBullCSVImporter logging: Logging, ) : AbstractImporter() { - val logger = logging.getLogger("HabitBullCSVImporter") + private val logger = logging.getLogger("HabitBullCSVImporter") override fun canHandle(file: File): Boolean { val reader = BufferedReader(FileReader(file)) @@ -77,10 +78,16 @@ class HabitBullCSVImporter logger.info("Creating habit: $name") } val notes = cols[5] ?: "" - if (parseInt(cols[4]) == 1) { - h.originalEntries.add(Entry(timestamp, Entry.YES_MANUAL, notes)) - } else { - h.originalEntries.add(Entry(timestamp, Entry.NO, notes)) + when (val value = parseInt(cols[4])) { + 0 -> h.originalEntries.add(Entry(timestamp, Entry.NO, notes)) + 1 -> h.originalEntries.add(Entry(timestamp, Entry.YES_MANUAL, notes)) + else -> { + if (value > 1 && h.type != HabitType.NUMERICAL) { + logger.info("Found a value of $value, considering this habit as numerical.") + h.type = HabitType.NUMERICAL + } + h.originalEntries.add(Entry(timestamp, value, notes)) + } } } } diff --git a/uhabits-core/src/jvmTest/java/org/isoron/uhabits/core/io/ImportTest.kt b/uhabits-core/src/jvmTest/java/org/isoron/uhabits/core/io/ImportTest.kt index daad1784e..b8949d4b0 100644 --- a/uhabits-core/src/jvmTest/java/org/isoron/uhabits/core/io/ImportTest.kt +++ b/uhabits-core/src/jvmTest/java/org/isoron/uhabits/core/io/ImportTest.kt @@ -26,6 +26,7 @@ import org.isoron.uhabits.core.BaseUnitTest import org.isoron.uhabits.core.models.Entry import org.isoron.uhabits.core.models.Frequency import org.isoron.uhabits.core.models.Habit +import org.isoron.uhabits.core.models.HabitType import org.isoron.uhabits.core.models.Timestamp import org.isoron.uhabits.core.utils.DateUtils.Companion.getStartOfTodayCalendar import org.isoron.uhabits.core.utils.DateUtils.Companion.setFixedLocalTime @@ -73,6 +74,30 @@ class ImportTest : BaseUnitTest() { assertTrue(isNotesEqual(habit, 2019, 6, 14, "Habit 3 notes")) } + @Test + @Throws(IOException::class) + fun testHabitBullCSV3() { + importFromFile("habitbull3.csv") + assertThat(habitList.size(), equalTo(2)) + + val habit = habitList.getByPosition(0) + assertThat(habit.name, equalTo("Pushups")) + assertThat(habit.type, equalTo(HabitType.NUMERICAL)) + assertThat(habit.description, equalTo("")) + assertThat(habit.frequency, equalTo(Frequency.DAILY)) + assertThat(getValue(habit, 2021, 9, 1), equalTo(30)) + assertThat(getValue(habit, 2022, 1, 8), equalTo(100)) + + val habit2 = habitList.getByPosition(1) + assertThat(habit2.name, equalTo("run")) + assertThat(habit2.type, equalTo(HabitType.YES_NO)) + assertThat(habit2.description, equalTo("")) + assertThat(habit2.frequency, equalTo(Frequency.DAILY)) + assertTrue(isChecked(habit2, 2022, 1, 3)) + assertTrue(isChecked(habit2, 2022, 1, 18)) + assertTrue(isChecked(habit2, 2022, 1, 19)) + } + @Test @Throws(IOException::class) fun testLoopDB() { @@ -124,10 +149,14 @@ class ImportTest : BaseUnitTest() { } private fun isChecked(h: Habit, year: Int, month: Int, day: Int): Boolean { + return getValue(h, year, month, day) == Entry.YES_MANUAL + } + + private fun getValue(h: Habit, year: Int, month: Int, day: Int): Int { val date = getStartOfTodayCalendar() date.set(year, month - 1, day) val timestamp = Timestamp(date) - return h.originalEntries.get(timestamp).value == Entry.YES_MANUAL + return h.originalEntries.get(timestamp).value } private fun isNotesEqual(h: Habit, year: Int, month: Int, day: Int, notes: String): Boolean {