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