diff --git a/collect_app/src/main/java/org/odk/collect/android/database/entities/DatabaseEntitiesRepository.kt b/collect_app/src/main/java/org/odk/collect/android/database/entities/DatabaseEntitiesRepository.kt index 273c89f44c1..a3b8051d97e 100644 --- a/collect_app/src/main/java/org/odk/collect/android/database/entities/DatabaseEntitiesRepository.kt +++ b/collect_app/src/main/java/org/odk/collect/android/database/entities/DatabaseEntitiesRepository.kt @@ -284,6 +284,7 @@ class DatabaseEntitiesRepository(context: Context, dbPath: String) : EntitiesRep SELECT *, i.$ROW_ID FROM $list e, ${getRowIdTableName(list)} i WHERE e._id = i._id + ORDER BY i.$ROW_ID """.trimIndent(), null ) @@ -301,6 +302,7 @@ class DatabaseEntitiesRepository(context: Context, dbPath: String) : EntitiesRep SELECT *, i.$ROW_ID FROM $list e, ${getRowIdTableName(list)} i WHERE e._id = i._id AND $selectionColumn = ? + ORDER BY i.$ROW_ID """.trimIndent(), arrayOf(selectionArg) ) @@ -325,7 +327,7 @@ class DatabaseEntitiesRepository(context: Context, dbPath: String) : EntitiesRep writableDatabase.execSQL( """ - CREATE TABLE ${getRowIdTableName(it)} AS SELECT _id FROM $it; + CREATE TABLE ${getRowIdTableName(it)} AS SELECT _id FROM $it ORDER BY _id; """.trimIndent() ) } diff --git a/collect_app/src/test/java/org/odk/collect/android/entities/EntitiesRepositoryTest.kt b/collect_app/src/test/java/org/odk/collect/android/entities/EntitiesRepositoryTest.kt index 5024e58c12b..6b9d3d27c72 100644 --- a/collect_app/src/test/java/org/odk/collect/android/entities/EntitiesRepositoryTest.kt +++ b/collect_app/src/test/java/org/odk/collect/android/entities/EntitiesRepositoryTest.kt @@ -309,15 +309,21 @@ abstract class EntitiesRepositoryTest { @Test fun `#save assigns an index to each entity in insert order when saving multiple entities`() { - val first = Entity.New("1", "LĂ©oville Barton 2008") - val second = Entity.New("2", "Pontet Canet 2014") + /** + * first and second have alphabetically out of order IDs/names here so that any indexing on + * them is tested. We'd likely never see this fail if they were ordered. + */ + val first = Entity.New("2", "B") + val second = Entity.New("1", "A") val repository = buildSubject() repository.save("wines", first, second) val entities = repository.getEntities("wines") assertThat(entities[0].index, equalTo(0)) + assertThat(entities[0].id, equalTo(first.id)) assertThat(entities[1].index, equalTo(1)) + assertThat(entities[1].id, equalTo(second.id)) } @Test