use of io.realm.entities.AllTypes in project realm-java by realm.
the class RealmQueryTests method rawPredicate_invalidRealmObjectThrows.
@Test
public void rawPredicate_invalidRealmObjectThrows() {
realm.beginTransaction();
AllTypes allTypes = realm.createObject(AllTypes.class);
realm.commitTransaction();
realm.executeTransaction(r -> allTypes.deleteFromRealm());
try {
realm.where(AllTypes.class).rawPredicate("columnRealmObject = $0", allTypes);
fail();
} catch (IllegalArgumentException e) {
assertTrue("Real message: " + e.getMessage(), e.getMessage().contains("RealmObject is not a valid managed object."));
}
try {
realm.where(AllTypes.class).rawPredicate("columnRealmObject = $0", new AllTypes());
fail();
} catch (IllegalArgumentException e) {
assertTrue("Real message: " + e.getMessage(), e.getMessage().contains("RealmObject is not a valid managed object."));
}
}
use of io.realm.entities.AllTypes in project realm-java by realm.
the class RealmQueryTests method lessThanOrEqualTo_date.
@Test
public void lessThanOrEqualTo_date() {
final int TEST_OBJECTS_COUNT = 200;
populateTestRealm(realm, TEST_OBJECTS_COUNT);
RealmResults<AllTypes> resultList;
resultList = realm.where(AllTypes.class).lessThanOrEqualTo(AllTypes.FIELD_DATE, new Date(Long.MIN_VALUE)).findAll();
assertEquals(0, resultList.size());
resultList = realm.where(AllTypes.class).lessThanOrEqualTo(AllTypes.FIELD_DATE, new Date(DECADE_MILLIS * -80)).findAll();
assertEquals(21, resultList.size());
resultList = realm.where(AllTypes.class).lessThanOrEqualTo(AllTypes.FIELD_DATE, new Date(0)).findAll();
assertEquals(TEST_OBJECTS_COUNT / 2 + 1, resultList.size());
resultList = realm.where(AllTypes.class).lessThanOrEqualTo(AllTypes.FIELD_DATE, new Date(DECADE_MILLIS * 80)).findAll();
assertEquals(181, resultList.size());
resultList = realm.where(AllTypes.class).lessThanOrEqualTo(AllTypes.FIELD_DATE, new Date(Long.MAX_VALUE)).findAll();
assertEquals(TEST_OBJECTS_COUNT, resultList.size());
}
use of io.realm.entities.AllTypes in project realm-java by realm.
the class RealmQueryTests method distinctCount.
// Verify that count correctly when using distinct.
// See https://github.com/realm/realm-java/issues/5958
@Test
public void distinctCount() {
realm.executeTransaction(r -> {
for (int i = 0; i < 5; i++) {
AllTypes obj = new AllTypes();
obj.setColumnString("Foo");
realm.copyToRealm(obj);
}
});
assertEquals(1, realm.where(AllTypes.class).distinct(AllTypes.FIELD_STRING).count());
}
use of io.realm.entities.AllTypes in project realm-java by realm.
the class RealmQueryTests method distinctAllFields.
// Helper method to verify distinct behavior an all fields of AllTypes, potentially following
// possible multiple indirection links as given by 'prefix'
private void distinctAllFields(Realm realm, String prefix) {
final long numberOfBlocks = 3;
final long numberOfObjects = 3;
populateForDistinctAllTypes(realm, numberOfBlocks, numberOfObjects);
// Dynamic realm for verifying distinct query result against naive manual implementation of
// distinct
DynamicRealm dynamicRealm = DynamicRealm.createInstance(realm.sharedRealm);
RealmResults<DynamicRealmObject> all = dynamicRealm.where(AllTypes.CLASS_NAME).findAll();
// Bookkeeping to ensure that we are actually testing all types
HashSet types = new HashSet(Arrays.asList(RealmFieldType.values()));
types.remove(RealmFieldType.TYPED_LINK);
types.remove(RealmFieldType.MIXED_LIST);
types.remove(RealmFieldType.STRING_TO_MIXED_MAP);
types.remove(RealmFieldType.STRING_TO_BOOLEAN_MAP);
types.remove(RealmFieldType.STRING_TO_STRING_MAP);
types.remove(RealmFieldType.STRING_TO_INTEGER_MAP);
types.remove(RealmFieldType.STRING_TO_FLOAT_MAP);
types.remove(RealmFieldType.STRING_TO_DOUBLE_MAP);
types.remove(RealmFieldType.STRING_TO_BINARY_MAP);
types.remove(RealmFieldType.STRING_TO_DATE_MAP);
types.remove(RealmFieldType.STRING_TO_OBJECT_ID_MAP);
types.remove(RealmFieldType.STRING_TO_UUID_MAP);
types.remove(RealmFieldType.STRING_TO_DECIMAL128_MAP);
types.remove(RealmFieldType.STRING_TO_LINK_MAP);
types.remove(RealmFieldType.BOOLEAN_SET);
types.remove(RealmFieldType.STRING_SET);
types.remove(RealmFieldType.INTEGER_SET);
types.remove(RealmFieldType.FLOAT_SET);
types.remove(RealmFieldType.DOUBLE_SET);
types.remove(RealmFieldType.BINARY_SET);
types.remove(RealmFieldType.DATE_SET);
types.remove(RealmFieldType.DECIMAL128_SET);
types.remove(RealmFieldType.OBJECT_ID_SET);
types.remove(RealmFieldType.UUID_SET);
types.remove(RealmFieldType.LINK_SET);
types.remove(RealmFieldType.MIXED_SET);
// Iterate all fields of AllTypes table and verify that distinct either:
// - Returns correct number of entries, or
// - Raises an error that distinct cannot be performed on the specific field types (lists)
RealmObjectSchema schema = realm.getSchema().getSchemaForClass(AllTypes.CLASS_NAME);
Set<String> fieldNames = schema.getFieldNames();
for (String fieldName : fieldNames) {
String field = prefix + fieldName;
RealmFieldType type = schema.getFieldType(fieldName);
if (supportDistinct(type)) {
// Actual query
RealmResults<AllTypes> distinct = realm.where(AllTypes.class).distinct(field).findAll();
// Assert query result
// Test against manual distinct implementation
Set<List<? super Object>> values = distinct(all, field);
assertEquals(field, values.size(), distinct.size());
// Test against expected numbers from setup
switch(type) {
case BOOLEAN:
assertEquals(field, 2, distinct.size());
break;
case OBJECT:
if (fieldName.equals("columnRealmObject")) {
assertEquals(field, numberOfBlocks, distinct.size());
} else if (fieldName.equals("columnRealmLink")) {
assertEquals(field, numberOfBlocks * numberOfObjects, distinct.size());
} else {
fail("Unknown object " + fieldName);
}
break;
default:
assertEquals(field, numberOfObjects, distinct.size());
break;
}
} else {
// Test that unsupported types throw exception as expected
try {
realm.where(AllTypes.class).distinct(field).findAll();
} catch (IllegalStateException ignore) {
// Not distinct not supported on lists
}
}
types.remove(type);
}
// Validate that backlinks are not supported by sort/distinct
assertEquals(types.toString(), Sets.newSet(RealmFieldType.LINKING_OBJECTS), types);
RealmQuery<AllTypes> query = realm.where(AllTypes.class);
try {
query.distinct(prefix + AllTypes.FIELD_REALMBACKLINK);
fail();
} catch (IllegalArgumentException ignore) {
}
}
use of io.realm.entities.AllTypes in project realm-java by realm.
the class RealmQueryTests method greaterThanOrEqualTo_date.
@Test
public void greaterThanOrEqualTo_date() {
final int TEST_OBJECTS_COUNT = 200;
populateTestRealm(realm, TEST_OBJECTS_COUNT);
RealmResults<AllTypes> resultList;
resultList = realm.where(AllTypes.class).greaterThanOrEqualTo(AllTypes.FIELD_DATE, new Date(Long.MIN_VALUE)).findAll();
assertEquals(TEST_OBJECTS_COUNT, resultList.size());
resultList = realm.where(AllTypes.class).greaterThanOrEqualTo(AllTypes.FIELD_DATE, new Date(DECADE_MILLIS * -80)).findAll();
assertEquals(180, resultList.size());
resultList = realm.where(AllTypes.class).greaterThanOrEqualTo(AllTypes.FIELD_DATE, new Date(0)).findAll();
assertEquals(TEST_OBJECTS_COUNT / 2, resultList.size());
resultList = realm.where(AllTypes.class).greaterThanOrEqualTo(AllTypes.FIELD_DATE, new Date(DECADE_MILLIS * 80)).findAll();
assertEquals(20, resultList.size());
resultList = realm.where(AllTypes.class).greaterThanOrEqualTo(AllTypes.FIELD_DATE, new Date(Long.MAX_VALUE)).findAll();
assertEquals(0, resultList.size());
}
Aggregations