/** * Test to see if a particular item that does exist in the same Realm does not exist in the result * set of another query. */ public void testContainsSameRealmNotContained() { RealmResults<AllTypes> items = testRealm.where(AllTypes.class).lessThan(AllTypes.FIELD_LONG, 1000).findAll(); AllTypes anotherType = testRealm.where(AllTypes.class).greaterThan(AllTypes.FIELD_LONG, 1000).findFirst(); assertFalse( "Should not be able to find item in another result list.", items.contains(anotherType)); }
public void testShouldNotContainRemovedItem() { RealmQuery<AllTypes> query = testRealm.where(AllTypes.class).findAll().where(); AllTypes item = testRealm.where(AllTypes.class).findFirst(); testRealm.beginTransaction(); item.removeFromRealm(); testRealm.commitTransaction(); assertFalse("Should not contain a removed item.", query.findAll().contains(item)); }
public void testFindFirst() { AllTypes result = testRealm.where(AllTypes.class).findFirst(); assertEquals(0, result.getColumnLong()); assertEquals("test data 0", result.getColumnString()); AllTypes none = testRealm.where(AllTypes.class).equalTo(FIELD_STRING, "smurf").findFirst(); assertNull(none); }
public void testRemoveFromAdapter() { RealmResults<AllTypes> resultList = testRealm.where(AllTypes.class).findAll(); RealmAdapter realmAdapter = new RealmAdapter(getContext(), resultList, automaticUpdate); testRealm.beginTransaction(); realmAdapter.getRealmResults().remove(0); testRealm.commitTransaction(); assertEquals(TEST_DATA_SIZE - 1, realmAdapter.getCount()); resultList = testRealm.where(AllTypes.class).equalTo(FIELD_STRING, "test data 0").findAll(); assertEquals(0, resultList.size()); }
public void testNullToNonNullResults() { RealmResults<AllTypes> resultList = testRealm.where(AllTypes.class).findAll(); RealmAdapter realmAdapter = new RealmAdapter(getContext(), null, automaticUpdate); realmAdapter.updateRealmResults(resultList); assertEquals(TEST_DATA_SIZE, realmAdapter.getCount()); }
public void testGetItemId() { RealmResults<AllTypes> resultList = testRealm.where(AllTypes.class).findAll(); RealmAdapter realmAdapter = new RealmAdapter(getContext(), resultList, automaticUpdate); for (int i = 0; i < resultList.size(); i++) { assertEquals(i, realmAdapter.getItemId(i)); } }
// Test average on empty columns public void testAvgGivesCorrectValueForEmptyColumns() { RealmResults<NullTypes> resultList = testRealm.where(NullTypes.class).findAll(); assertEquals(0d, resultList.average(NullTypes.FIELD_INTEGER_NOT_NULL), 0d); assertEquals(0d, resultList.average(NullTypes.FIELD_FLOAT_NOT_NULL), 0d); assertEquals(0d, resultList.average(NullTypes.FIELD_DOUBLE_NOT_NULL), 0d); }
public void testAvgGivesCorrectValue() { RealmResults<AllTypes> resultList = testRealm.where(AllTypes.class).findAll(); double N = (double) TEST_DATA_SIZE; // Sum of numbers 1 to M: M*(M+1)/2 // See setUp() for values of fields // N = TEST_DATA_SIZE // Type: double; a = 3.1415 // a, a+1, ..., a+i, ..., a+N-1 // sum = 3.1415*N + N*(N-1)/2 // average = sum/N = 3.1415+(N-1)/2 double average = 3.1415 + (N - 1.0) * 0.5; assertEquals(average, resultList.average(FIELD_DOUBLE), 0.0001); // Type: long // 0, 1, ..., N-1 // sum = N*(N-1)/2 // average = sum/N = (N-1)/2 assertEquals(0.5 * (N - 1), resultList.average(FIELD_LONG), 0.0001); // Type: float; b = 1.234567 // b, b+1, ..., b+i, ..., b+N-1 // sum = b*N + N*(N-1)/2 // average = sum/N = b + (N-1)/2 assertEquals(1.234567 + 0.5 * (N - 1.0), resultList.average(FIELD_FLOAT), 0.0001); }
public void testEmptyRealmResult() { RealmResults<AllTypes> resultList = testRealm.where(AllTypes.class).equalTo(FIELD_STRING, "Not there").findAll(); RealmAdapter realmAdapter = new RealmAdapter(getContext(), resultList, automaticUpdate); assertEquals(0, realmAdapter.getRealmResults().size()); assertEquals(0, realmAdapter.getCount()); }
public void testSumGivesCorrectValue() { RealmResults<AllTypes> resultList = testRealm.where(AllTypes.class).findAll(); Number sum = resultList.sum(FIELD_LONG); // Sum of numbers 0 to M-1: (M-1)*M/2 assertEquals((TEST_DATA_SIZE - 1) * TEST_DATA_SIZE / 2, sum.intValue()); }
// Test max on empty columns public void testMaxValueForEmptyColumns() { RealmResults<NullTypes> results = testRealm.where(NullTypes.class).findAll(); assertNull(results.max(NullTypes.FIELD_INTEGER_NOT_NULL)); assertNull(results.max(NullTypes.FIELD_FLOAT_NOT_NULL)); assertNull(results.max(NullTypes.FIELD_DOUBLE_NOT_NULL)); assertNull(results.maxDate(NullTypes.FIELD_DATE_NOT_NULL)); }
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()); }
// Test sum on nullable rows with all null values public void testSumGivesCorrectValueForAllNullRows() { TestHelper.populateAllNullRowsForNumericTesting(testRealm); RealmResults<NullTypes> resultList = testRealm.where(NullTypes.class).findAll(); assertEquals(0, resultList.sum(NullTypes.FIELD_INTEGER_NULL).intValue()); assertEquals(0f, resultList.sum(NullTypes.FIELD_FLOAT_NULL).floatValue(), 0f); assertEquals(0d, resultList.sum(NullTypes.FIELD_DOUBLE_NULL).doubleValue(), 0d); }
// Test sum on nullable rows with partial null values public void testAvgGivesCorrectValueForPartialNullRows() { populatePartialNullRowsForNumericTesting(); RealmResults<NullTypes> resultList = testRealm.where(NullTypes.class).findAll(); assertEquals(1, resultList.sum(NullTypes.FIELD_INTEGER_NULL).intValue()); assertEquals(2f, resultList.sum(NullTypes.FIELD_FLOAT_NULL).floatValue(), 0f); assertEquals(3d, resultList.sum(NullTypes.FIELD_DOUBLE_NULL).doubleValue(), 0d); }
// Test min on nullable rows with partial null values public void testMinValueForPartialNullRows() { populatePartialNullRowsForNumericTesting(); RealmResults<NullTypes> results = testRealm.where(NullTypes.class).findAll(); assertEquals(0, results.min(NullTypes.FIELD_INTEGER_NULL).intValue()); assertEquals(0f, results.min(NullTypes.FIELD_FLOAT_NULL).floatValue(), 0f); assertEquals(0d, results.min(NullTypes.FIELD_DOUBLE_NULL).doubleValue(), 0d); }
public void testSortOnNonExistingColumn() { try { RealmResults<AllTypes> resultList = testRealm.where(AllTypes.class).findAll(); resultList.sort("Non-existing"); fail("Column should not exist"); } catch (IllegalArgumentException ignored) { } }
// Test average on nullable rows with all null values public void testAvgGivesCorrectValueForAllNullRows() { TestHelper.populateAllNullRowsForNumericTesting(testRealm); RealmResults<NullTypes> resultList = testRealm.where(NullTypes.class).findAll(); assertEquals(0d, resultList.average(NullTypes.FIELD_INTEGER_NULL), 0d); assertEquals(0d, resultList.average(NullTypes.FIELD_FLOAT_NULL), 0d); assertEquals(0d, resultList.average(NullTypes.FIELD_DOUBLE_NULL), 0d); }
public void testAdapterParameterExceptions() { RealmResults<AllTypes> resultList = testRealm.where(AllTypes.class).findAll(); try { RealmAdapter realmAdapter = new RealmAdapter(null, resultList, automaticUpdate); fail("Should throw exception if context is null"); } catch (IllegalArgumentException ignore) { } }
// Test average on nullable rows with partial null values public void testAvgForPartialNullRows() { populatePartialNullRowsForNumericTesting(); RealmResults<NullTypes> resultList = testRealm.where(NullTypes.class).findAll(); assertEquals(0.5, resultList.average(NullTypes.FIELD_INTEGER_NULL), 0d); assertEquals(1.0, resultList.average(NullTypes.FIELD_FLOAT_NULL), 0d); assertEquals(1.5, resultList.average(NullTypes.FIELD_DOUBLE_NULL), 0d); }
// Triggered an ARM bug public void testComparisons() { testRealm.beginTransaction(); testRealm.clear(AllTypes.class); long id = -1; for (int i = 0; i < 10; i++) { AllTypes allTypes = testRealm.createObject(AllTypes.class); allTypes.setColumnLong(id--); } testRealm.commitTransaction(); assertEquals( 10, testRealm.where(AllTypes.class).between(AllTypes.FIELD_LONG, -10, -1).findAll().size()); assertEquals( 10, testRealm.where(AllTypes.class).greaterThan(AllTypes.FIELD_LONG, -11).findAll().size()); assertEquals( 10, testRealm .where(AllTypes.class) .greaterThanOrEqualTo(AllTypes.FIELD_LONG, -10) .findAll() .size()); assertEquals( 10, testRealm.where(AllTypes.class).lessThan(AllTypes.FIELD_LONG, 128).findAll().size()); assertEquals( 10, testRealm.where(AllTypes.class).lessThan(AllTypes.FIELD_LONG, 127).findAll().size()); assertEquals( 10, testRealm .where(AllTypes.class) .lessThanOrEqualTo(AllTypes.FIELD_LONG, -1) .findAll() .size()); assertEquals( 10, testRealm.where(AllTypes.class).lessThan(AllTypes.FIELD_LONG, 0).findAll().size()); }
// Test max on nullable rows with all null values public void testMaxValueForAllNullRows() { TestHelper.populateAllNullRowsForNumericTesting(testRealm); RealmResults<NullTypes> results = testRealm.where(NullTypes.class).findAll(); assertNull(results.max(NullTypes.FIELD_INTEGER_NULL)); assertNull(results.max(NullTypes.FIELD_FLOAT_NULL)); assertNull(results.max(NullTypes.FIELD_DOUBLE_NULL)); assertNull(results.maxDate(NullTypes.FIELD_DATE_NULL)); }
public void testManyConditions() { RealmQuery<AllTypes> query = testRealm.where(AllTypes.class); query.equalTo(FIELD_LONG, 0); for (int i = 1; i < TEST_DATA_SIZE; i++) { query.or().equalTo(FIELD_LONG, i); } RealmResults<AllTypes> allTypesRealmResults = query.findAll(); assertEquals(TEST_DATA_SIZE, allTypesRealmResults.size()); }
public void testRemoveLast() { RealmResults<AllTypes> resultList = testRealm.where(AllTypes.class).findAll(); testRealm.beginTransaction(); resultList.removeLast(); testRealm.commitTransaction(); assertEquals( "ResultList.removeLast did not remove record", TEST_DATA_SIZE - 1, resultList.size()); AllTypes allTypes = resultList.get(resultList.size() - 1); assertEquals( "ResultList.removeLast unexpected last record", TEST_DATA_SIZE - 2, allTypes.getColumnLong()); RealmResults<AllTypes> resultListCheck = testRealm.where(AllTypes.class).findAll(); assertEquals("ResultList.removeLast not committed", TEST_DATA_SIZE - 1, resultListCheck.size()); }
public void testQueryDateField() { RealmQuery<AllTypes> query = testRealm.where(AllTypes.class).equalTo(FIELD_DATE, new Date(YEAR_MILLIS * 5)); RealmResults<AllTypes> all = query.findAll(); assertEquals(1, query.count()); assertEquals(1, all.size()); // before 1901 query = testRealm.where(AllTypes.class).equalTo(FIELD_DATE, new Date(YEAR_MILLIS * -100)); all = query.findAll(); assertEquals(1, query.count()); assertEquals(1, all.size()); // after 2038 query = testRealm.where(AllTypes.class).equalTo(FIELD_DATE, new Date(YEAR_MILLIS * 100)); all = query.findAll(); assertEquals(1, query.count()); assertEquals(1, all.size()); }
public void testWithEmptyRealmObjects() { testRealm.beginTransaction(); testRealm.clear(AllTypes.class); testRealm.commitTransaction(); try { testRealm.where(AllTypes.class).findAll().sort(FIELD_STRING); } catch (IllegalArgumentException e) { fail("Failed to sort an empty RealmResults"); } }
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) { } }
// void clear(Class<?> classSpec) public void testClearEmptiesTable() { RealmResults<AllTypes> resultList = testRealm.where(AllTypes.class).findAll(); assertEquals(TEST_DATA_SIZE, resultList.size()); testRealm.beginTransaction(); resultList.clear(); testRealm.commitTransaction(); assertEquals(0, resultList.size()); }
public void testRemoveLastEmptyList() { RealmResults<AllTypes> resultList = testRealm.where(AllTypes.class).findAll(); testRealm.beginTransaction(); resultList.clear(); assertEquals(0, resultList.size()); resultList.removeLast(); testRealm.commitTransaction(); assertEquals(0, resultList.size()); }
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()); }
public void testContainsDoesNotContainAnItem() { RealmConfiguration realmConfig = TestHelper.createConfiguration(getContext(), "contains_test.realm"); Realm.deleteRealm(realmConfig); Realm testRealmTwo = Realm.getInstance(realmConfig); try { testRealmTwo.beginTransaction(); testRealmTwo.allObjects(AllTypes.class).clear(); testRealmTwo.allObjects(NonLatinFieldNames.class).clear(); for (int i = 0; i < TEST_DATA_SIZE; ++i) { AllTypes allTypes = testRealmTwo.createObject(AllTypes.class); allTypes.setColumnBoolean((i % 2) == 0); allTypes.setColumnBinary(new byte[] {1, 2, 3}); allTypes.setColumnDate(new Date(YEAR_MILLIS * (i - TEST_DATA_SIZE / 2))); allTypes.setColumnDouble(3.1415 + i); allTypes.setColumnFloat(1.234567f + i); allTypes.setColumnString("test data " + i); allTypes.setColumnLong(i); Dog d = testRealmTwo.createObject(Dog.class); d.setName("Foo " + i); allTypes.setColumnRealmObject(d); allTypes.getColumnRealmList().add(d); NonLatinFieldNames nonLatinFieldNames = testRealmTwo.createObject(NonLatinFieldNames.class); nonLatinFieldNames.set델타(i); nonLatinFieldNames.setΔέλτα(i); } testRealmTwo.commitTransaction(); final AllTypes item = testRealmTwo.where(AllTypes.class).findFirst(); assertFalse( "Should not be able to find one object in another Realm via RealmResults#contains", testRealm.where(AllTypes.class).findAll().contains(item)); } finally { if (testRealmTwo != null && !testRealmTwo.isClosed()) { testRealmTwo.close(); } } }