mirror of https://github.com/iSoron/uhabits.git
parent
2b23b36e36
commit
9a44774284
@ -0,0 +1,117 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2016 Álinson Santos Xavier <isoron@gmail.com>
|
||||||
|
*
|
||||||
|
* This file is part of Loop Habit Tracker.
|
||||||
|
*
|
||||||
|
* Loop Habit Tracker is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by the
|
||||||
|
* Free Software Foundation, either version 3 of the License, or (at your
|
||||||
|
* option) any later version.
|
||||||
|
*
|
||||||
|
* Loop Habit Tracker is distributed in the hope that it will be useful, but
|
||||||
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||||
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||||
|
* more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License along
|
||||||
|
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.isoron.uhabits.models.sqlite;
|
||||||
|
|
||||||
|
import android.support.test.runner.*;
|
||||||
|
import android.test.suitebuilder.annotation.*;
|
||||||
|
|
||||||
|
import com.activeandroid.query.*;
|
||||||
|
|
||||||
|
import org.isoron.uhabits.*;
|
||||||
|
import org.isoron.uhabits.models.*;
|
||||||
|
import org.isoron.uhabits.models.sqlite.records.*;
|
||||||
|
import org.isoron.uhabits.utils.*;
|
||||||
|
import org.junit.*;
|
||||||
|
import org.junit.runner.*;
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
import static org.hamcrest.MatcherAssert.*;
|
||||||
|
import static org.hamcrest.Matchers.*;
|
||||||
|
|
||||||
|
@RunWith(AndroidJUnit4.class)
|
||||||
|
@MediumTest
|
||||||
|
public class SQLiteCheckmarkListTest extends BaseAndroidTest
|
||||||
|
{
|
||||||
|
private Habit habit;
|
||||||
|
|
||||||
|
private CheckmarkList checkmarks;
|
||||||
|
|
||||||
|
private long today;
|
||||||
|
|
||||||
|
private long day;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setUp()
|
||||||
|
{
|
||||||
|
super.setUp();
|
||||||
|
|
||||||
|
habit = fixtures.createLongHabit();
|
||||||
|
checkmarks = habit.getCheckmarks();
|
||||||
|
checkmarks.getToday(); // compute checkmarks
|
||||||
|
|
||||||
|
today = DateUtils.getStartOfToday();
|
||||||
|
day = DateUtils.millisecondsInOneDay;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testAdd()
|
||||||
|
{
|
||||||
|
checkmarks.invalidateNewerThan(0);
|
||||||
|
|
||||||
|
List<Checkmark> list = new LinkedList<>();
|
||||||
|
list.add(new Checkmark(habit, 0, 0));
|
||||||
|
list.add(new Checkmark(habit, 1, 1));
|
||||||
|
list.add(new Checkmark(habit, 2, 2));
|
||||||
|
|
||||||
|
checkmarks.add(list);
|
||||||
|
|
||||||
|
List<CheckmarkRecord> records = getAllRecords();
|
||||||
|
assertThat(records.size(), equalTo(3));
|
||||||
|
assertThat(records.get(0).timestamp, equalTo(2L));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetByInterval()
|
||||||
|
{
|
||||||
|
long from = today - 10 * day;
|
||||||
|
long to = today - 3 * day;
|
||||||
|
|
||||||
|
List<Checkmark> list = checkmarks.getByInterval(from, to);
|
||||||
|
assertThat(list.size(), equalTo(8));
|
||||||
|
|
||||||
|
assertThat(list.get(0).getTimestamp(), equalTo(today - 3 * day));
|
||||||
|
assertThat(list.get(3).getTimestamp(), equalTo(today - 6 * day));
|
||||||
|
assertThat(list.get(7).getTimestamp(), equalTo(today - 10 * day));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testInvalidateNewerThan()
|
||||||
|
{
|
||||||
|
List<CheckmarkRecord> records = getAllRecords();
|
||||||
|
assertThat(records.size(), equalTo(121));
|
||||||
|
|
||||||
|
checkmarks.invalidateNewerThan(today - 20 * day);
|
||||||
|
|
||||||
|
records = getAllRecords();
|
||||||
|
assertThat(records.size(), equalTo(100));
|
||||||
|
assertThat(records.get(0).timestamp, equalTo(today - 21 * day));
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<CheckmarkRecord> getAllRecords()
|
||||||
|
{
|
||||||
|
return new Select()
|
||||||
|
.from(CheckmarkRecord.class)
|
||||||
|
.where("habit = ?", habit.getId())
|
||||||
|
.orderBy("timestamp desc")
|
||||||
|
.execute();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,229 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2016 Álinson Santos Xavier <isoron@gmail.com>
|
||||||
|
*
|
||||||
|
* This file is part of Loop Habit Tracker.
|
||||||
|
*
|
||||||
|
* Loop Habit Tracker is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by the
|
||||||
|
* Free Software Foundation, either version 3 of the License, or (at your
|
||||||
|
* option) any later version.
|
||||||
|
*
|
||||||
|
* Loop Habit Tracker is distributed in the hope that it will be useful, but
|
||||||
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||||
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||||
|
* more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License along
|
||||||
|
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.isoron.uhabits.models.sqlite;
|
||||||
|
|
||||||
|
import android.support.test.runner.*;
|
||||||
|
import android.test.suitebuilder.annotation.*;
|
||||||
|
|
||||||
|
import com.activeandroid.query.*;
|
||||||
|
|
||||||
|
import org.isoron.uhabits.*;
|
||||||
|
import org.isoron.uhabits.models.*;
|
||||||
|
import org.isoron.uhabits.models.sqlite.records.*;
|
||||||
|
import org.junit.*;
|
||||||
|
import org.junit.rules.*;
|
||||||
|
import org.junit.runner.*;
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
import static junit.framework.Assert.*;
|
||||||
|
import static org.hamcrest.MatcherAssert.*;
|
||||||
|
import static org.hamcrest.core.IsEqual.*;
|
||||||
|
|
||||||
|
@SuppressWarnings("JavaDoc")
|
||||||
|
@RunWith(AndroidJUnit4.class)
|
||||||
|
@MediumTest
|
||||||
|
public class SQLiteHabitListTest extends BaseAndroidTest
|
||||||
|
{
|
||||||
|
@Rule
|
||||||
|
public ExpectedException exception = ExpectedException.none();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setUp()
|
||||||
|
{
|
||||||
|
super.setUp();
|
||||||
|
fixtures.purgeHabits(habitList);
|
||||||
|
|
||||||
|
for (int i = 0; i < 10; i++)
|
||||||
|
{
|
||||||
|
Habit h = new Habit();
|
||||||
|
h.setName("habit " + i);
|
||||||
|
h.setId((long) i);
|
||||||
|
if (i % 2 == 0) h.setArchived(1);
|
||||||
|
|
||||||
|
HabitRecord record = new HabitRecord();
|
||||||
|
record.copyFrom(h);
|
||||||
|
record.position = i;
|
||||||
|
record.save(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testAdd_withDuplicate()
|
||||||
|
{
|
||||||
|
Habit habit = new Habit();
|
||||||
|
habitList.add(habit);
|
||||||
|
exception.expect(IllegalArgumentException.class);
|
||||||
|
habitList.add(habit);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testAdd_withId()
|
||||||
|
{
|
||||||
|
Habit habit = new Habit();
|
||||||
|
habit.setName("Hello world with id");
|
||||||
|
habit.setId(12300L);
|
||||||
|
|
||||||
|
habitList.add(habit);
|
||||||
|
assertThat(habit.getId(), equalTo(12300L));
|
||||||
|
|
||||||
|
HabitRecord record = getRecord(12300L);
|
||||||
|
assertNotNull(record);
|
||||||
|
assertThat(record.name, equalTo(habit.getName()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testAdd_withoutId()
|
||||||
|
{
|
||||||
|
Habit habit = new Habit();
|
||||||
|
habit.setName("Hello world");
|
||||||
|
assertNull(habit.getId());
|
||||||
|
|
||||||
|
habitList.add(habit);
|
||||||
|
assertNotNull(habit.getId());
|
||||||
|
|
||||||
|
HabitRecord record = getRecord(habit.getId());
|
||||||
|
assertNotNull(record);
|
||||||
|
assertThat(record.name, equalTo(habit.getName()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testCountActive()
|
||||||
|
{
|
||||||
|
assertThat(habitList.countActive(), equalTo(5));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testCountWithArchived()
|
||||||
|
{
|
||||||
|
assertThat(habitList.countWithArchived(), equalTo(10));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetAll_withArchived()
|
||||||
|
{
|
||||||
|
List<Habit> habits = habitList.getAll(true);
|
||||||
|
assertThat(habits.size(), equalTo(10));
|
||||||
|
assertThat(habits.get(3).getName(), equalTo("habit 3"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetAll_withoutArchived()
|
||||||
|
{
|
||||||
|
List<Habit> habits = habitList.getAll(false);
|
||||||
|
assertThat(habits.size(), equalTo(5));
|
||||||
|
assertThat(habits.get(3).getName(), equalTo("habit 7"));
|
||||||
|
|
||||||
|
List<Habit> another = habitList.getAll(false);
|
||||||
|
assertThat(habits, equalTo(another));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetById()
|
||||||
|
{
|
||||||
|
Habit h1 = habitList.getById(0);
|
||||||
|
assertNotNull(h1);
|
||||||
|
assertThat(h1.getName(), equalTo("habit 0"));
|
||||||
|
|
||||||
|
Habit h2 = habitList.getById(0);
|
||||||
|
assertNotNull(h2);
|
||||||
|
assertThat(h1, equalTo(h2));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetById_withInvalid()
|
||||||
|
{
|
||||||
|
long invalidId = 9183792001L;
|
||||||
|
Habit h1 = habitList.getById(invalidId);
|
||||||
|
assertNull(h1);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetByPosition()
|
||||||
|
{
|
||||||
|
Habit h = habitList.getByPosition(5);
|
||||||
|
assertNotNull(h);
|
||||||
|
assertThat(h.getName(), equalTo("habit 5"));
|
||||||
|
|
||||||
|
h = habitList.getByPosition(5000);
|
||||||
|
assertNull(h);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testIndexOf()
|
||||||
|
{
|
||||||
|
Habit h1 = habitList.getByPosition(5);
|
||||||
|
assertNotNull(h1);
|
||||||
|
assertThat(habitList.indexOf(h1), equalTo(5));
|
||||||
|
|
||||||
|
Habit h2 = new Habit();
|
||||||
|
assertThat(habitList.indexOf(h2), equalTo(-1));
|
||||||
|
|
||||||
|
h2.setId(1000L);
|
||||||
|
assertThat(habitList.indexOf(h2), equalTo(-1));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void test_reorder()
|
||||||
|
{
|
||||||
|
// Same as HabitListTest.java
|
||||||
|
// TODO: remove duplication
|
||||||
|
|
||||||
|
int operations[][] = {
|
||||||
|
{5, 2}, {3, 7}, {4, 4}, {3, 2}
|
||||||
|
};
|
||||||
|
|
||||||
|
int expectedPosition[][] = {
|
||||||
|
{0, 1, 3, 4, 5, 2, 6, 7, 8, 9},
|
||||||
|
{0, 1, 7, 3, 4, 2, 5, 6, 8, 9},
|
||||||
|
{0, 1, 7, 3, 4, 2, 5, 6, 8, 9},
|
||||||
|
{0, 1, 7, 2, 4, 3, 5, 6, 8, 9},
|
||||||
|
};
|
||||||
|
|
||||||
|
for (int i = 0; i < operations.length; i++)
|
||||||
|
{
|
||||||
|
int from = operations[i][0];
|
||||||
|
int to = operations[i][1];
|
||||||
|
|
||||||
|
Habit fromHabit = habitList.getByPosition(from);
|
||||||
|
Habit toHabit = habitList.getByPosition(to);
|
||||||
|
habitList.reorder(fromHabit, toHabit);
|
||||||
|
|
||||||
|
int actualPositions[] = new int[10];
|
||||||
|
|
||||||
|
for (int j = 0; j < 10; j++)
|
||||||
|
{
|
||||||
|
Habit h = habitList.getById(j);
|
||||||
|
assertNotNull(h);
|
||||||
|
actualPositions[j] = habitList.indexOf(h);
|
||||||
|
}
|
||||||
|
|
||||||
|
assertThat(actualPositions, equalTo(expectedPosition[i]));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private HabitRecord getRecord(long id)
|
||||||
|
{
|
||||||
|
return new Select()
|
||||||
|
.from(HabitRecord.class)
|
||||||
|
.where("id = ?", id)
|
||||||
|
.executeSingle();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,145 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2016 Álinson Santos Xavier <isoron@gmail.com>
|
||||||
|
*
|
||||||
|
* This file is part of Loop Habit Tracker.
|
||||||
|
*
|
||||||
|
* Loop Habit Tracker is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by the
|
||||||
|
* Free Software Foundation, either version 3 of the License, or (at your
|
||||||
|
* option) any later version.
|
||||||
|
*
|
||||||
|
* Loop Habit Tracker is distributed in the hope that it will be useful, but
|
||||||
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||||
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||||
|
* more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License along
|
||||||
|
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.isoron.uhabits.models.sqlite;
|
||||||
|
|
||||||
|
import android.support.annotation.*;
|
||||||
|
import android.support.test.runner.*;
|
||||||
|
import android.test.suitebuilder.annotation.*;
|
||||||
|
|
||||||
|
import com.activeandroid.query.*;
|
||||||
|
|
||||||
|
import org.isoron.uhabits.*;
|
||||||
|
import org.isoron.uhabits.models.*;
|
||||||
|
import org.isoron.uhabits.models.sqlite.records.*;
|
||||||
|
import org.isoron.uhabits.utils.*;
|
||||||
|
import org.junit.*;
|
||||||
|
import org.junit.runner.*;
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
import static org.hamcrest.MatcherAssert.*;
|
||||||
|
import static org.hamcrest.Matchers.*;
|
||||||
|
import static org.hamcrest.core.IsNot.not;
|
||||||
|
|
||||||
|
@RunWith(AndroidJUnit4.class)
|
||||||
|
@MediumTest
|
||||||
|
public class SQLiteRepetitionListTest extends BaseAndroidTest
|
||||||
|
{
|
||||||
|
private Habit habit;
|
||||||
|
|
||||||
|
private long today;
|
||||||
|
|
||||||
|
private RepetitionList repetitions;
|
||||||
|
|
||||||
|
private long day;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setUp()
|
||||||
|
{
|
||||||
|
super.setUp();
|
||||||
|
|
||||||
|
habit = fixtures.createLongHabit();
|
||||||
|
repetitions = habit.getRepetitions();
|
||||||
|
today = DateUtils.getStartOfToday();
|
||||||
|
day = DateUtils.millisecondsInOneDay;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testAdd()
|
||||||
|
{
|
||||||
|
RepetitionRecord record = getByTimestamp(today + day);
|
||||||
|
assertThat(record, is(nullValue()));
|
||||||
|
|
||||||
|
Repetition rep = new Repetition(habit, today + day);
|
||||||
|
habit.getRepetitions().add(rep);
|
||||||
|
|
||||||
|
record = getByTimestamp(today + day);
|
||||||
|
assertThat(record, is(not(nullValue())));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetByInterval()
|
||||||
|
{
|
||||||
|
List<Repetition> reps =
|
||||||
|
repetitions.getByInterval(today - 10 * day, today);
|
||||||
|
|
||||||
|
assertThat(reps.size(), equalTo(8));
|
||||||
|
assertThat(reps.get(0).getTimestamp(), equalTo(today - 10 * day));
|
||||||
|
assertThat(reps.get(4).getTimestamp(), equalTo(today - 5 * day));
|
||||||
|
assertThat(reps.get(5).getTimestamp(), equalTo(today - 3 * day));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetByTimestamp()
|
||||||
|
{
|
||||||
|
Repetition rep = repetitions.getByTimestamp(today);
|
||||||
|
assertThat(rep, is(not(nullValue())));
|
||||||
|
assertThat(rep.getHabit(), equalTo(habit));
|
||||||
|
assertThat(rep.getTimestamp(), equalTo(today));
|
||||||
|
|
||||||
|
rep = repetitions.getByTimestamp(today - 2 * day);
|
||||||
|
assertThat(rep, is(nullValue()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetOldest()
|
||||||
|
{
|
||||||
|
Repetition rep = repetitions.getOldest();
|
||||||
|
assertThat(rep, is(not(nullValue())));
|
||||||
|
assertThat(rep.getHabit(), equalTo(habit));
|
||||||
|
assertThat(rep.getTimestamp(), equalTo(today - 120 * day));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetOldest_withEmptyHabit()
|
||||||
|
{
|
||||||
|
Habit empty = fixtures.createEmptyHabit();
|
||||||
|
Repetition rep = empty.getRepetitions().getOldest();
|
||||||
|
assertThat(rep, is(nullValue()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testRemove()
|
||||||
|
{
|
||||||
|
RepetitionRecord record = getByTimestamp(today);
|
||||||
|
assertThat(record, is(not(nullValue())));
|
||||||
|
|
||||||
|
Repetition rep = record.toRepetition();
|
||||||
|
repetitions.remove(rep);
|
||||||
|
|
||||||
|
record = getByTimestamp(today);
|
||||||
|
assertThat(record, is(nullValue()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
private RepetitionRecord getByTimestamp(long timestamp)
|
||||||
|
{
|
||||||
|
return selectByTimestamp(timestamp).executeSingle();
|
||||||
|
}
|
||||||
|
|
||||||
|
@NonNull
|
||||||
|
private From selectByTimestamp(long timestamp)
|
||||||
|
{
|
||||||
|
return new Select()
|
||||||
|
.from(RepetitionRecord.class)
|
||||||
|
.where("habit = ?", habit.getId())
|
||||||
|
.and("timestamp = ?", timestamp);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,126 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2016 Álinson Santos Xavier <isoron@gmail.com>
|
||||||
|
*
|
||||||
|
* This file is part of Loop Habit Tracker.
|
||||||
|
*
|
||||||
|
* Loop Habit Tracker is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by the
|
||||||
|
* Free Software Foundation, either version 3 of the License, or (at your
|
||||||
|
* option) any later version.
|
||||||
|
*
|
||||||
|
* Loop Habit Tracker is distributed in the hope that it will be useful, but
|
||||||
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||||
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||||
|
* more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License along
|
||||||
|
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.isoron.uhabits.models.sqlite;
|
||||||
|
|
||||||
|
import android.support.test.runner.*;
|
||||||
|
import android.test.suitebuilder.annotation.*;
|
||||||
|
|
||||||
|
import com.activeandroid.query.*;
|
||||||
|
|
||||||
|
import org.isoron.uhabits.*;
|
||||||
|
import org.isoron.uhabits.models.*;
|
||||||
|
import org.isoron.uhabits.models.sqlite.records.*;
|
||||||
|
import org.isoron.uhabits.utils.*;
|
||||||
|
import org.junit.*;
|
||||||
|
import org.junit.runner.*;
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
import static junit.framework.Assert.assertNotNull;
|
||||||
|
import static junit.framework.Assert.assertNull;
|
||||||
|
import static org.hamcrest.MatcherAssert.*;
|
||||||
|
import static org.hamcrest.Matchers.*;
|
||||||
|
|
||||||
|
@SuppressWarnings("JavaDoc")
|
||||||
|
@RunWith(AndroidJUnit4.class)
|
||||||
|
@MediumTest
|
||||||
|
public class SQLiteScoreListTest extends BaseAndroidTest
|
||||||
|
{
|
||||||
|
private Habit habit;
|
||||||
|
|
||||||
|
private ScoreList scores;
|
||||||
|
|
||||||
|
private long today;
|
||||||
|
|
||||||
|
private long day;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setUp()
|
||||||
|
{
|
||||||
|
super.setUp();
|
||||||
|
|
||||||
|
habit = fixtures.createLongHabit();
|
||||||
|
scores = habit.getScores();
|
||||||
|
|
||||||
|
today = DateUtils.getStartOfToday();
|
||||||
|
day = DateUtils.millisecondsInOneDay;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetAll()
|
||||||
|
{
|
||||||
|
List<Score> list = scores.getAll();
|
||||||
|
assertThat(list.size(), equalTo(121));
|
||||||
|
assertThat(list.get(0).getTimestamp(), equalTo(today));
|
||||||
|
assertThat(list.get(10).getTimestamp(), equalTo(today - 10 * day));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testInvalidateNewerThan()
|
||||||
|
{
|
||||||
|
scores.getTodayValue(); // force recompute
|
||||||
|
List<ScoreRecord> records = getAllRecords();
|
||||||
|
assertThat(records.size(), equalTo(121));
|
||||||
|
|
||||||
|
scores.invalidateNewerThan(today - 10 * day);
|
||||||
|
|
||||||
|
records = getAllRecords();
|
||||||
|
assertThat(records.size(), equalTo(110));
|
||||||
|
assertThat(records.get(0).timestamp, equalTo(today - 11 * day));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testAdd()
|
||||||
|
{
|
||||||
|
new Delete().from(ScoreRecord.class).execute();
|
||||||
|
|
||||||
|
List<Score> list = new LinkedList<>();
|
||||||
|
list.add(new Score(habit, today, 0));
|
||||||
|
list.add(new Score(habit, today - day, 0));
|
||||||
|
list.add(new Score(habit, today - 2 * day, 0));
|
||||||
|
|
||||||
|
scores.add(list);
|
||||||
|
|
||||||
|
List<ScoreRecord> records = getAllRecords();
|
||||||
|
assertThat(records.size(), equalTo(3));
|
||||||
|
assertThat(records.get(0).timestamp, equalTo(today));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetByTimestamp()
|
||||||
|
{
|
||||||
|
Score s = scores.getByTimestamp(today);
|
||||||
|
assertNotNull(s);
|
||||||
|
assertThat(s.getTimestamp(), equalTo(today));
|
||||||
|
|
||||||
|
s = scores.getByTimestamp(today - 200 * day);
|
||||||
|
assertNull(s);
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<ScoreRecord> getAllRecords()
|
||||||
|
{
|
||||||
|
return new Select()
|
||||||
|
.from(ScoreRecord.class)
|
||||||
|
.where("habit = ?", habit.getId())
|
||||||
|
.orderBy("timestamp desc")
|
||||||
|
.execute();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,33 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (C) 2016 Álinson Santos Xavier <isoron@gmail.com>
|
|
||||||
*
|
|
||||||
* This file is part of Loop Habit Tracker.
|
|
||||||
*
|
|
||||||
* Loop Habit Tracker is free software: you can redistribute it and/or modify
|
|
||||||
* it under the terms of the GNU General Public License as published by the
|
|
||||||
* Free Software Foundation, either version 3 of the License, or (at your
|
|
||||||
* option) any later version.
|
|
||||||
*
|
|
||||||
* Loop Habit Tracker is distributed in the hope that it will be useful, but
|
|
||||||
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
||||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
||||||
* more details.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU General Public License along
|
|
||||||
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package org.isoron.uhabits.unit.models;
|
|
||||||
|
|
||||||
import android.support.test.runner.AndroidJUnit4;
|
|
||||||
import android.test.suitebuilder.annotation.SmallTest;
|
|
||||||
|
|
||||||
import org.isoron.uhabits.BaseAndroidTest;
|
|
||||||
import org.junit.runner.RunWith;
|
|
||||||
|
|
||||||
@RunWith(AndroidJUnit4.class)
|
|
||||||
@SmallTest
|
|
||||||
public class ScoreListTest extends BaseAndroidTest
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
Loading…
Reference in new issue