public void testSortByLong() { RealmResults<AllTypes> resultList = testRealm.where(AllTypes.class).findAll(); RealmResults<AllTypes> sortedList = testRealm.allObjects(AllTypes.class); sortedList.sort(FIELD_LONG, RealmResults.SORT_ORDER_DESCENDING); assertEquals("Should have same size", resultList.size(), sortedList.size()); assertEquals(TEST_DATA_SIZE, sortedList.size()); assertEquals( "First excepted to be last", resultList.first().getColumnLong(), sortedList.last().getColumnLong()); RealmResults<AllTypes> reverseList = sortedList; reverseList.sort(FIELD_LONG, RealmResults.SORT_ORDER_ASCENDING); assertEquals(TEST_DATA_SIZE, reverseList.size()); assertEquals( "First excepted to be first", resultList.first().getColumnLong(), reverseList.first().getColumnLong()); assertEquals( "Last excepted to be last", resultList.last().getColumnLong(), reverseList.last().getColumnLong()); RealmResults<AllTypes> reserveSortedList = reverseList; reverseList.sort(FIELD_LONG, RealmResults.SORT_ORDER_DESCENDING); assertEquals(TEST_DATA_SIZE, reserveSortedList.size()); }
public void testSortWithRussianCharacters() { testRealm.beginTransaction(); testRealm.clear(AllTypes.class); AllTypes at1 = testRealm.createObject(AllTypes.class); at1.setColumnString("Санкт-Петербург"); AllTypes at2 = testRealm.createObject(AllTypes.class); at2.setColumnString("Москва"); AllTypes at3 = testRealm.createObject(AllTypes.class); at3.setColumnString("Новороссийск"); testRealm.commitTransaction(); RealmResults<AllTypes> result = testRealm.allObjects(AllTypes.class); RealmResults<AllTypes> sortedResult = result.where().findAll(); sortedResult.sort(FIELD_STRING); assertEquals(3, sortedResult.size()); assertEquals("Москва", sortedResult.first().getColumnString()); assertEquals("Москва", sortedResult.get(0).getColumnString()); assertEquals("Новороссийск", sortedResult.get(1).getColumnString()); assertEquals("Санкт-Петербург", sortedResult.get(2).getColumnString()); RealmResults<AllTypes> reverseResult = result.where().findAll(); reverseResult.sort(FIELD_STRING, RealmResults.SORT_ORDER_DESCENDING); assertEquals(3, reverseResult.size()); assertEquals("Москва", reverseResult.last().getColumnString()); assertEquals("Санкт-Петербург", reverseResult.get(0).getColumnString()); assertEquals("Новороссийск", reverseResult.get(1).getColumnString()); assertEquals("Москва", reverseResult.get(2).getColumnString()); }
public void testSortWithDanishCharacters() { testRealm.beginTransaction(); testRealm.clear(AllTypes.class); AllTypes at1 = testRealm.createObject(AllTypes.class); at1.setColumnString("Æble"); AllTypes at2 = testRealm.createObject(AllTypes.class); at2.setColumnString("Øl"); AllTypes at3 = testRealm.createObject(AllTypes.class); at3.setColumnString("Århus"); testRealm.commitTransaction(); RealmResults<AllTypes> result = testRealm.allObjects(AllTypes.class); RealmResults<AllTypes> sortedResult = result.where().findAll(); sortedResult.sort(FIELD_STRING); assertEquals(3, sortedResult.size()); assertEquals("Æble", sortedResult.first().getColumnString()); assertEquals("Æble", sortedResult.get(0).getColumnString()); assertEquals("Øl", sortedResult.get(1).getColumnString()); assertEquals("Århus", sortedResult.get(2).getColumnString()); RealmResults<AllTypes> reverseResult = result.where().findAll(); reverseResult.sort(FIELD_STRING, Sort.DESCENDING); assertEquals(3, reverseResult.size()); assertEquals("Æble", reverseResult.last().getColumnString()); assertEquals("Århus", reverseResult.get(0).getColumnString()); assertEquals("Øl", reverseResult.get(1).getColumnString()); assertEquals("Æble", reverseResult.get(2).getColumnString()); }
public void testSortWithGreekCharacters() { testRealm.beginTransaction(); testRealm.clear(AllTypes.class); AllTypes at1 = testRealm.createObject(AllTypes.class); at1.setColumnString("αύριο"); AllTypes at2 = testRealm.createObject(AllTypes.class); at2.setColumnString("ημέρες"); AllTypes at3 = testRealm.createObject(AllTypes.class); at3.setColumnString("δοκιμές"); testRealm.commitTransaction(); RealmResults<AllTypes> result = testRealm.allObjects(AllTypes.class); RealmResults<AllTypes> sortedResult = result.where().findAll(); sortedResult.sort(FIELD_STRING); assertEquals(3, sortedResult.size()); assertEquals("αύριο", sortedResult.first().getColumnString()); assertEquals("αύριο", sortedResult.get(0).getColumnString()); assertEquals("δοκιμές", sortedResult.get(1).getColumnString()); assertEquals("ημέρες", sortedResult.get(2).getColumnString()); RealmResults<AllTypes> reverseResult = result.where().findAll(); reverseResult.sort(FIELD_STRING, RealmResults.SORT_ORDER_DESCENDING); assertEquals(3, reverseResult.size()); assertEquals("αύριο", reverseResult.last().getColumnString()); assertEquals("ημέρες", reverseResult.get(0).getColumnString()); assertEquals("δοκιμές", reverseResult.get(1).getColumnString()); assertEquals("αύριο", reverseResult.get(2).getColumnString()); }
public void testSortSingleField() { RealmResults<AllTypes> sortedList = testRealm.allObjects(AllTypes.class); sortedList.sort(new String[] {FIELD_LONG}, new boolean[] {RealmResults.SORT_ORDER_DESCENDING}); assertEquals(TEST_DATA_SIZE, sortedList.size()); assertEquals(TEST_DATA_SIZE - 1, sortedList.first().getColumnLong()); assertEquals(0, sortedList.last().getColumnLong()); }
private void doTestSortOnColumnWithPartialNullValues(String fieldName) { RealmResults<NullTypes> resultList = testRealm.where(NullTypes.class).findAll(); // Ascending RealmResults<NullTypes> sortedList = testRealm.allObjects(NullTypes.class); sortedList.sort(fieldName, RealmResults.SORT_ORDER_ASCENDING); assertEquals("Should have same size", resultList.size(), sortedList.size()); // Null should always be the first one in the ascending sorted list assertEquals(2, sortedList.first().getId()); assertEquals(1, sortedList.last().getId()); // Descending sortedList = testRealm.allObjects(NullTypes.class); sortedList.sort(fieldName, RealmResults.SORT_ORDER_DESCENDING); assertEquals("Should have same size", resultList.size(), sortedList.size()); assertEquals(1, sortedList.first().getId()); // Null should always be the last one in the descending sorted list assertEquals(2, sortedList.last().getId()); }
public void testSortByDouble() { RealmResults<AllTypes> resultList = testRealm.where(AllTypes.class).findAll(); RealmResults<AllTypes> sortedList = resultList.where().findAll(); sortedList.sort(FIELD_DOUBLE, Sort.DESCENDING); assertEquals(resultList.size(), sortedList.size()); assertEquals(TEST_DATA_SIZE, sortedList.size()); assertEquals(resultList.first().getColumnDouble(), sortedList.last().getColumnDouble()); RealmResults<AllTypes> reverseList = sortedList.where().findAll(); reverseList.sort(FIELD_DOUBLE, Sort.ASCENDING); assertEquals(TEST_DATA_SIZE, reverseList.size()); assertEquals(resultList.first().getColumnDouble(), reverseList.first().getColumnDouble()); assertEquals(resultList.last().getColumnDouble(), reverseList.last().getColumnDouble()); RealmResults<AllTypes> reverseSortedList = reverseList.where().findAll(); reverseSortedList.sort(FIELD_DOUBLE, Sort.DESCENDING); assertEquals(TEST_DATA_SIZE, reverseSortedList.size()); }
public void testSortByFloat() { RealmResults<AllTypes> resultList = testRealm.where(AllTypes.class).findAll(); RealmResults<AllTypes> sortedList = resultList.where().findAll(); sortedList.sort(FIELD_FLOAT, RealmResults.SORT_ORDER_DESCENDING); assertEquals(resultList.size(), sortedList.size()); assertEquals(TEST_DATA_SIZE, sortedList.size()); assertEquals(resultList.first().getColumnFloat(), sortedList.last().getColumnFloat()); RealmResults<AllTypes> reverseList = sortedList.where().findAll(); reverseList.sort(FIELD_FLOAT, RealmResults.SORT_ORDER_ASCENDING); assertEquals(TEST_DATA_SIZE, reverseList.size()); assertEquals(resultList.first().getColumnFloat(), reverseList.first().getColumnFloat()); assertEquals(resultList.last().getColumnFloat(), reverseList.last().getColumnFloat()); RealmResults<AllTypes> reverseSortedList = reverseList.where().findAll(); reverseSortedList.sort(FIELD_FLOAT, RealmResults.SORT_ORDER_DESCENDING); assertEquals(TEST_DATA_SIZE, reverseSortedList.size()); }
public void testFindAllSorted() { RealmResults<AllTypes> allTypes = testRealm.where(AllTypes.class).findAllSorted(FIELD_LONG, Sort.ASCENDING); assertEquals(TEST_DATA_SIZE, allTypes.size()); assertEquals(0, allTypes.first().getColumnLong()); assertEquals(TEST_DATA_SIZE - 1, allTypes.last().getColumnLong()); RealmResults<AllTypes> reverseList = testRealm.where(AllTypes.class).findAllSorted(FIELD_LONG, Sort.DESCENDING); assertEquals(TEST_DATA_SIZE, reverseList.size()); assertEquals(0, reverseList.last().getColumnLong()); assertEquals(TEST_DATA_SIZE - 1, reverseList.first().getColumnLong()); try { testRealm.where(AllTypes.class).findAllSorted("invalid", Sort.DESCENDING); fail(); } catch (IllegalArgumentException ignored) { } }
public void testGetItem() { RealmResults<AllTypes> resultList = testRealm.where(AllTypes.class).findAll(); RealmAdapter realmAdapter = new RealmAdapter(getContext(), resultList, automaticUpdate); assertEquals(resultList.get(0).getColumnString(), realmAdapter.getItem(0).getColumnString()); assertEquals(resultList.size(), realmAdapter.getRealmResults().size()); assertEquals( resultList.last().getColumnString(), realmAdapter.getRealmResults().last().getColumnString()); }
private static Integer getPhotoId() { Realm realm = RealmUtils.getRealm(); RealmResults<Photo> list = realm.where(Photo.class).findAll().sort("id", Sort.ASCENDING); realm.close(); if (list.size() == 0) { return 1; } return list.last().getId() + 1; }
public void testSortByBoolean() { RealmResults<AllTypes> resultList = testRealm.where(AllTypes.class).findAll(); RealmResults<AllTypes> sortedList = resultList.where().findAll(); sortedList.sort(FIELD_BOOLEAN, RealmResults.SORT_ORDER_DESCENDING); assertEquals(resultList.size(), sortedList.size()); assertEquals(TEST_DATA_SIZE, sortedList.size()); assertEquals(false, sortedList.last().isColumnBoolean()); assertEquals(true, sortedList.first().isColumnBoolean()); assertEquals(true, sortedList.get(TEST_DATA_FIRST_HALF).isColumnBoolean()); assertEquals(false, sortedList.get(TEST_DATA_LAST_HALF).isColumnBoolean()); RealmResults<AllTypes> reverseList = sortedList.where().findAll(); reverseList.sort(FIELD_BOOLEAN, RealmResults.SORT_ORDER_ASCENDING); assertEquals(TEST_DATA_SIZE, reverseList.size()); assertEquals(true, reverseList.last().isColumnBoolean()); assertEquals(false, reverseList.first().isColumnBoolean()); assertEquals(false, reverseList.get(TEST_DATA_FIRST_HALF).isColumnBoolean()); assertEquals(true, reverseList.get(TEST_DATA_LAST_HALF).isColumnBoolean()); RealmResults<AllTypes> reserveSortedList = reverseList.where().findAll(); reserveSortedList.sort(FIELD_BOOLEAN, RealmResults.SORT_ORDER_DESCENDING); assertEquals(TEST_DATA_SIZE, reserveSortedList.size()); assertEquals(reserveSortedList.first(), sortedList.first()); }
public void testSortByString() { RealmResults<AllTypes> resultList = testRealm.where(AllTypes.class).findAll(); RealmResults<AllTypes> sortedList = resultList.where().findAll(); sortedList.sort(FIELD_STRING, RealmResults.SORT_ORDER_DESCENDING); assertEquals(resultList.size(), sortedList.size()); assertEquals(TEST_DATA_SIZE, sortedList.size()); assertEquals(resultList.first().getColumnString(), sortedList.last().getColumnString()); RealmResults<AllTypes> reverseList = sortedList.where().findAll(); reverseList.sort(FIELD_STRING, RealmResults.SORT_ORDER_ASCENDING); assertEquals(TEST_DATA_SIZE, reverseList.size()); assertEquals(resultList.first().getColumnString(), reverseList.first().getColumnString()); int numberOfDigits = 1 + ((int) Math.log10(TEST_DATA_SIZE)); int largestNumber = 1; for (int i = 1; i < numberOfDigits; i++) largestNumber *= 10; // 10*10* ... *10 largestNumber = largestNumber - 1; assertEquals( resultList.get(largestNumber).getColumnString(), reverseList.last().getColumnString()); RealmResults<AllTypes> reverseSortedList = reverseList.where().findAll(); reverseList.sort(FIELD_STRING, RealmResults.SORT_ORDER_DESCENDING); assertEquals(TEST_DATA_SIZE, reverseSortedList.size()); }
// first() and last() will throw an exception when no element exist public void testResultListFirstLastThrowIfEmpty() { testRealm.beginTransaction(); testRealm.clear(AllTypes.class); testRealm.commitTransaction(); RealmResults<AllTypes> allTypes = testRealm.allObjects(AllTypes.class); assertEquals(0, allTypes.size()); try { allTypes.first(); fail(); } catch (ArrayIndexOutOfBoundsException ignored) { } try { allTypes.last(); fail(); } catch (ArrayIndexOutOfBoundsException ignored) { } }
public void testSortWithAdapter() { RealmResults<AllTypes> resultList = testRealm.where(AllTypes.class).findAll(); resultList.sort(FIELD_STRING, RealmResults.SORT_ORDER_DESCENDING); RealmAdapter realmAdapter = new RealmAdapter(getContext(), resultList, automaticUpdate); assertEquals( resultList.first().getColumnString(), realmAdapter.getRealmResults().first().getColumnString()); assertEquals(resultList.size(), realmAdapter.getRealmResults().size()); resultList.sort(FIELD_STRING); assertEquals( resultList.last().getColumnString(), realmAdapter.getRealmResults().last().getColumnString()); assertEquals( resultList.get(TEST_DATA_SIZE / 2).getColumnString(), realmAdapter.getRealmResults().get(TEST_DATA_SIZE / 2).getColumnString()); assertEquals(resultList.size(), realmAdapter.getRealmResults().size()); }
public void testUpdateRealmResultInAdapter() { RealmResults<AllTypes> resultList = testRealm.where(AllTypes.class).findAll(); resultList.sort(FIELD_STRING); RealmAdapter realmAdapter = new RealmAdapter(getContext(), resultList, false); assertEquals( resultList.first().getColumnString(), realmAdapter.getRealmResults().first().getColumnString()); assertEquals(resultList.size(), realmAdapter.getRealmResults().size()); testRealm.beginTransaction(); AllTypes allTypes = testRealm.createObject(AllTypes.class); allTypes.setColumnString("test data " + TEST_DATA_SIZE); testRealm.commitTransaction(); assertEquals( resultList.last().getColumnString(), realmAdapter.getRealmResults().last().getColumnString()); assertEquals(resultList.size(), realmAdapter.getRealmResults().size()); RealmResults<AllTypes> emptyResultList = testRealm.where(AllTypes.class).equalTo(FIELD_STRING, "Not there").findAll(); realmAdapter.updateRealmResults(emptyResultList); assertEquals(emptyResultList.size(), realmAdapter.getRealmResults().size()); }
public void testResultListLastIsLast() { RealmResults<AllTypes> resultList = testRealm.where(AllTypes.class).findAll(); AllTypes allTypes = resultList.last(); assertEquals((TEST_DATA_SIZE - 1), allTypes.getColumnLong()); }