mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-06 09:08:52 -06:00
CSV export: Add more fields to Habits.csv
This commit is contained in:
@@ -22,6 +22,7 @@ import com.opencsv.CSVWriter
|
||||
import java.io.IOException
|
||||
import java.io.Writer
|
||||
import java.util.LinkedList
|
||||
import java.util.Locale
|
||||
import javax.annotation.concurrent.ThreadSafe
|
||||
|
||||
/**
|
||||
@@ -182,24 +183,34 @@ abstract class HabitList : Iterable<Habit> {
|
||||
val header = arrayOf(
|
||||
"Position",
|
||||
"Name",
|
||||
"Type",
|
||||
"Question",
|
||||
"Description",
|
||||
"NumRepetitions",
|
||||
"Interval",
|
||||
"Color"
|
||||
"FrequencyNumerator",
|
||||
"FrequencyDenominator",
|
||||
"Color",
|
||||
"Unit",
|
||||
"Target Type",
|
||||
"Target Value",
|
||||
"Archived?",
|
||||
)
|
||||
val csv = CSVWriter(out)
|
||||
csv.writeNext(header, false)
|
||||
for (habit in this) {
|
||||
val (numerator, denominator) = habit.frequency
|
||||
val cols = arrayOf(
|
||||
String.format("%03d", indexOf(habit) + 1),
|
||||
String.format(Locale.US, "%03d", indexOf(habit) + 1),
|
||||
habit.name,
|
||||
habit.type.name,
|
||||
habit.question,
|
||||
habit.description,
|
||||
numerator.toString(),
|
||||
denominator.toString(),
|
||||
habit.color.toCsvColor()
|
||||
habit.color.toCsvColor(),
|
||||
if (habit.isNumerical) habit.unit else "",
|
||||
if (habit.isNumerical) habit.targetType.name else "",
|
||||
if (habit.isNumerical) habit.targetValue.toString() else "",
|
||||
habit.isArchived.toString(),
|
||||
)
|
||||
csv.writeNext(cols, false)
|
||||
}
|
||||
|
||||
@@ -18,7 +18,6 @@
|
||||
*/
|
||||
package org.isoron.uhabits.core.io
|
||||
|
||||
import org.apache.commons.io.FileUtils
|
||||
import org.apache.commons.io.IOUtils
|
||||
import org.isoron.uhabits.core.BaseUnitTest
|
||||
import org.isoron.uhabits.core.models.Habit
|
||||
@@ -30,6 +29,7 @@ import java.io.IOException
|
||||
import java.nio.file.Files
|
||||
import java.util.*
|
||||
import java.util.zip.ZipFile
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
class HabitsCSVExporterTest : BaseUnitTest() {
|
||||
@@ -108,15 +108,12 @@ class HabitsCSVExporterTest : BaseUnitTest() {
|
||||
|
||||
private fun assertFileAndReferenceAreEqual(s: String) {
|
||||
val assetFilename = String.format("csv_export/%s", s)
|
||||
val file = File.createTempFile("asset", "")
|
||||
file.deleteOnExit()
|
||||
copyAssetToFile(assetFilename, file)
|
||||
|
||||
assertTrue(
|
||||
FileUtils.contentEquals(
|
||||
file,
|
||||
File(String.format("%s/%s", baseDir.absolutePath, s))
|
||||
)
|
||||
)
|
||||
val expectedFile = File(String.format("%s/%s", baseDir.absolutePath, s))
|
||||
val actualFile = File.createTempFile("asset", "")
|
||||
actualFile.deleteOnExit()
|
||||
copyAssetToFile(assetFilename, actualFile)
|
||||
val expectedContents = expectedFile.readText()
|
||||
val actualContents = actualFile.readText()
|
||||
assertEquals(expectedContents, actualContents, "content mismatch for $s")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -202,13 +202,16 @@ class HabitListTest : BaseUnitTest() {
|
||||
h2.description = ""
|
||||
h2.frequency = Frequency(2, 3)
|
||||
h2.color = PaletteColor(5)
|
||||
val h3 = fixtures.createNumericalHabit()
|
||||
list.add(h1)
|
||||
list.add(h2)
|
||||
list.add(h3)
|
||||
val expectedCSV =
|
||||
"""
|
||||
Position,Name,Question,Description,NumRepetitions,Interval,Color
|
||||
001,Meditate,Did you meditate this morning?,this is a test description,1,1,#FF8F00
|
||||
002,Wake up early,Did you wake up before 6am?,,2,3,#AFB42B
|
||||
Position,Name,Type,Question,Description,FrequencyNumerator,FrequencyDenominator,Color,Unit,Target Type,Target Value,Archived?
|
||||
001,Meditate,YES_NO,Did you meditate this morning?,this is a test description,1,1,#FF8F00,,,,false
|
||||
002,Run,NUMERICAL,How many miles did you run today?,,1,1,#E64A19,miles,AT_LEAST,2.0,false
|
||||
003,Wake up early,YES_NO,Did you wake up before 6am?,,2,3,#AFB42B,,,,false
|
||||
|
||||
""".trimIndent()
|
||||
val writer = StringWriter()
|
||||
|
||||
Reference in New Issue
Block a user