Write tests for CSV export

This commit is contained in:
2016-03-24 21:36:41 -04:00
parent 2dfbcfcdb0
commit 1120f56dd4
8 changed files with 133 additions and 33 deletions

View File

@@ -76,6 +76,7 @@ public class CheckmarkList
public int[] getValues(long fromTimestamp, long toTimestamp)
{
compute(fromTimestamp, toTimestamp);
if(fromTimestamp > toTimestamp) return new int[0];
String query = "select value, timestamp from Checkmarks where " +
@@ -127,6 +128,21 @@ public class CheckmarkList
return getValues(fromTimestamp, toTimestamp);
}
/**
* Computes and stores one checkmark for each day, since the first repetition until today.
* Days that already have a corresponding checkmark are skipped.
*/
protected void computeAll()
{
Repetition oldestRep = habit.repetitions.getOldest();
if(oldestRep == null) return;
Long fromTimestamp = oldestRep.timestamp;
Long toTimestamp = DateHelper.getStartOfToday();
compute(fromTimestamp, toTimestamp);
}
/**
* Computes and stores one checkmark for each day that falls inside the specified interval of
* time. Days that already have a corresponding checkmark are skipped.
@@ -234,8 +250,18 @@ public class CheckmarkList
else return Checkmark.UNCHECKED;
}
/**
* Writes the entire list of checkmarks to the given writer, in CSV format. There is one
* line for each checkmark. Each line contains two fields: timestamp and value.
*
* @param out the writer where the CSV will be output
* @throws IOException in case write operations fail
*/
public void writeCSV(Writer out) throws IOException
{
computeAll();
SimpleDateFormat dateFormat = DateHelper.getCSVDateFormat();
String query = "select timestamp, value from checkmarks where habit = ? order by timestamp";
@@ -255,5 +281,6 @@ public class CheckmarkList
} while(cursor.moveToNext());
cursor.close();
out.close();
}
}

View File

@@ -20,7 +20,6 @@
package org.isoron.uhabits.models;
import android.annotation.SuppressLint;
import android.graphics.Color;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
@@ -34,16 +33,13 @@ import com.activeandroid.query.From;
import com.activeandroid.query.Select;
import com.activeandroid.query.Update;
import com.activeandroid.util.SQLiteUtils;
import com.opencsv.CSVReader;
import com.opencsv.CSVWriter;
import org.isoron.uhabits.helpers.ColorHelper;
import org.isoron.uhabits.helpers.DateHelper;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
@@ -478,9 +474,18 @@ public class Habit extends Model
reminderDays = DateHelper.ALL_WEEK_DAYS;
}
/**
* Writes the list of habits to the given writer, in CSV format. There is one line for each
* habit, containing the fields name, description, frequency numerator, frequency denominator
* and color. The color is written in HTML format (#000000).
*
* @param habits the list of habits to write
* @param out the writer that will receive the result
* @throws IOException if write operations fail
*/
public static void writeCSV(List<Habit> habits, Writer out) throws IOException
{
String header[] = { "Name", "Description", "FrequencyNumerator", "FrequencyDenominator", "Color" };
String header[] = { "Name", "Description", "NumRepetitions", "Interval", "Color" };
CSVWriter csv = new CSVWriter(out);
csv.writeNext(header, false);
@@ -494,24 +499,4 @@ public class Habit extends Model
csv.close();
}
public List<Habit> parseCSV(Reader in)
{
CSVReader csv = new CSVReader(in);
List<Habit> habits = new LinkedList<>();
for(String cols[] : csv)
{
Habit habit = new Habit();
habit.name = cols[0];
habit.description = cols[1];
habit.freqNum = Integer.parseInt(cols[2]);
habit.freqDen = Integer.parseInt(cols[3]);
habit.color = Color.parseColor(cols[4]);
habits.add(habit);
}
return habits;
}
}

View File

@@ -99,6 +99,19 @@ public class ScoreList
.execute();
}
/**
* Computes and saves the scores that are missing since the first repetition of the habit.
*/
private void computeAll()
{
Repetition oldestRep = habit.repetitions.getOldest();
if(oldestRep == null) return;
long fromTimestamp = oldestRep.timestamp;
long toTimestamp = DateHelper.getStartOfToday();
compute(fromTimestamp, toTimestamp);
}
/**
* Computes and saves the scores that are missing inside a given time interval. Scores that
* have already been computed are skipped, therefore there is no harm in calling this function
@@ -285,6 +298,8 @@ public class ScoreList
public void writeCSV(Writer out) throws IOException
{
computeAll();
SimpleDateFormat dateFormat = DateHelper.getCSVDateFormat();
String query = "select timestamp, score from score where habit = ? order by timestamp";
@@ -298,11 +313,12 @@ public class ScoreList
do
{
String timestamp = dateFormat.format(new Date(cursor.getLong(0)));
String score = String.format("%.2f", ((float) cursor.getInt(1)) / Score.MAX_VALUE);
String score = String.format("%.4f", ((float) cursor.getInt(1)) / Score.MAX_VALUE);
out.write(String.format("%s,%s\n", timestamp, score));
} while(cursor.moveToNext());
cursor.close();
out.close();
}
}