Write missing tests and docs for RepetitionList

pull/69/head
Alinson S. Xavier 10 years ago
parent 79b6ef8200
commit eb017bf99b

@ -61,7 +61,7 @@ public class RepetitionListTest
} }
@Test @Test
public void contains_testNonDailyHabit() public void contains()
{ {
long current = DateHelper.getStartOfToday(); long current = DateHelper.getStartOfToday();
@ -79,7 +79,7 @@ public class RepetitionListTest
} }
@Test @Test
public void delete_test() public void delete()
{ {
long timestamp = DateHelper.getStartOfToday(); long timestamp = DateHelper.getStartOfToday();
assertThat(habit.repetitions.contains(timestamp), equalTo(true)); assertThat(habit.repetitions.contains(timestamp), equalTo(true));
@ -89,7 +89,7 @@ public class RepetitionListTest
} }
@Test @Test
public void toggle_test() public void toggle()
{ {
long timestamp = DateHelper.getStartOfToday(); long timestamp = DateHelper.getStartOfToday();
assertThat(habit.repetitions.contains(timestamp), equalTo(true)); assertThat(habit.repetitions.contains(timestamp), equalTo(true));
@ -102,7 +102,7 @@ public class RepetitionListTest
} }
@Test @Test
public void getWeekDayFrequency_test() public void getWeekDayFrequency()
{ {
Random random = new Random(); Random random = new Random();
Integer weekdayCount[][] = new Integer[12][7]; Integer weekdayCount[][] = new Integer[12][7];
@ -159,4 +159,16 @@ public class RepetitionListTest
day.set(2015, 11, 1); day.set(2015, 11, 1);
assertThat(freq.get(day.getTimeInMillis()), equalTo(null)); assertThat(freq.get(day.getTimeInMillis()), equalTo(null));
} }
@Test
public void count()
{
long to = DateHelper.getStartOfToday();
long from = to - 9 * DateHelper.millisecondsInOneDay;
assertThat(habit.repetitions.count(from, to), equalTo(6));
to = DateHelper.getStartOfToday() - DateHelper.millisecondsInOneDay;
from = to - 5 * DateHelper.millisecondsInOneDay;
assertThat(habit.repetitions.count(from, to), equalTo(3));
}
} }

@ -26,10 +26,15 @@ import com.activeandroid.annotation.Table;
@Table(name = "Repetitions") @Table(name = "Repetitions")
public class Repetition extends Model public class Repetition extends Model
{ {
/**
* Habit to which this repetition belong.
*/
@Column(name = "habit") @Column(name = "habit")
public Habit habit; public Habit habit;
/**
* Timestamp of the day this repetition occurred. Time of day should be midnight (UTC).
*/
@Column(name = "timestamp") @Column(name = "timestamp")
public Long timestamp; public Long timestamp;
} }

@ -21,6 +21,8 @@ package org.isoron.uhabits.models;
import android.database.Cursor; import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import com.activeandroid.Cache; import com.activeandroid.Cache;
import com.activeandroid.query.Delete; import com.activeandroid.query.Delete;
@ -35,14 +37,15 @@ import java.util.HashMap;
public class RepetitionList public class RepetitionList
{ {
@NonNull
private Habit habit; private Habit habit;
public RepetitionList(Habit habit) public RepetitionList(@NonNull Habit habit)
{ {
this.habit = habit; this.habit = habit;
} }
@NonNull
protected From select() protected From select()
{ {
return new Select().from(Repetition.class) return new Select().from(Repetition.class)
@ -51,6 +54,7 @@ public class RepetitionList
.orderBy("timestamp"); .orderBy("timestamp");
} }
@NonNull
protected From selectFromTo(long timeFrom, long timeTo) protected From selectFromTo(long timeFrom, long timeTo)
{ {
return select().and("timestamp >= ?", timeFrom).and("timestamp <= ?", timeTo); return select().and("timestamp >= ?", timeFrom).and("timestamp <= ?", timeTo);
@ -114,6 +118,7 @@ public class RepetitionList
* *
* @return oldest repetition for the habit * @return oldest repetition for the habit
*/ */
@Nullable
public Repetition getOldest() public Repetition getOldest()
{ {
return (Repetition) select().limit(1).executeSingle(); return (Repetition) select().limit(1).executeSingle();
@ -129,6 +134,7 @@ public class RepetitionList
* *
* @return total number of repetitions by month versus day of week * @return total number of repetitions by month versus day of week
*/ */
@NonNull
public HashMap<Long, Integer[]> getWeekdayFrequency() public HashMap<Long, Integer[]> getWeekdayFrequency()
{ {
Repetition oldestRep = getOldest(); Repetition oldestRep = getOldest();

Loading…
Cancel
Save