diff --git a/uhabits-core/src/main/java/org/isoron/uhabits/core/models/sqlite/SQLiteHabitList.java b/uhabits-core/src/main/java/org/isoron/uhabits/core/models/sqlite/SQLiteHabitList.java index 23a266437..0960f2c97 100644 --- a/uhabits-core/src/main/java/org/isoron/uhabits/core/models/sqlite/SQLiteHabitList.java +++ b/uhabits-core/src/main/java/org/isoron/uhabits/core/models/sqlite/SQLiteHabitList.java @@ -201,24 +201,21 @@ public class SQLiteHabitList extends HabitList if (toRecord == null) throw new RuntimeException("habit not in database"); - Integer fromPos = fromRecord.position; - Integer toPos = toRecord.position; - if (toPos < fromPos) + if (toRecord.position < fromRecord.position) { repository.execSQL("update habits set position = position + 1 " + "where position >= ? and position < ?", - toPos, fromPos); + toRecord.position, fromRecord.position); } else { repository.execSQL("update habits set position = position - 1 " + "where position > ? and position <= ?", - fromPos, toPos); + fromRecord.position, toRecord.position); } - fromRecord.position = toPos; + fromRecord.position = toRecord.position; repository.save(fromRecord); - update(from); getObservable().notifyListeners(); } diff --git a/uhabits-core/src/test/java/org/isoron/uhabits/core/models/sqlite/SQLiteHabitListTest.java b/uhabits-core/src/test/java/org/isoron/uhabits/core/models/sqlite/SQLiteHabitListTest.java index 61d153ea8..72222413e 100644 --- a/uhabits-core/src/test/java/org/isoron/uhabits/core/models/sqlite/SQLiteHabitListTest.java +++ b/uhabits-core/src/test/java/org/isoron/uhabits/core/models/sqlite/SQLiteHabitListTest.java @@ -28,12 +28,10 @@ import org.isoron.uhabits.core.models.sqlite.records.*; import org.junit.*; import org.junit.rules.*; -import static org.hamcrest.MatcherAssert.*; -import static org.hamcrest.core.IsEqual.*; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; +import static org.hamcrest.CoreMatchers.*; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.*; +import static org.mockito.Mockito.*; public class SQLiteHabitListTest extends BaseUnitTest { @@ -183,4 +181,42 @@ public class SQLiteHabitListTest extends BaseUnitTest assertNotNull(rec); assertThat(rec.position, equalTo(2)); } + + @Test + public void testReorder() + { + // Same as HabitListTest#testReorder + 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); + habitList.reload(); + + 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])); + } + } } \ No newline at end of file