Search in sources :

Example 76 with AllTypes

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."));
    }
}
Also used : AllTypes(io.realm.entities.AllTypes) DictionaryAllTypes(io.realm.entities.DictionaryAllTypes) Test(org.junit.Test) UiThreadTest(androidx.test.annotation.UiThreadTest)

Example 77 with AllTypes

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());
}
Also used : AllTypes(io.realm.entities.AllTypes) DictionaryAllTypes(io.realm.entities.DictionaryAllTypes) Date(java.util.Date) Test(org.junit.Test) UiThreadTest(androidx.test.annotation.UiThreadTest)

Example 78 with AllTypes

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());
}
Also used : AllTypes(io.realm.entities.AllTypes) DictionaryAllTypes(io.realm.entities.DictionaryAllTypes) Test(org.junit.Test) UiThreadTest(androidx.test.annotation.UiThreadTest)

Example 79 with AllTypes

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) {
    }
}
Also used : AllTypes(io.realm.entities.AllTypes) DictionaryAllTypes(io.realm.entities.DictionaryAllTypes) PrimaryKeyAsString(io.realm.entities.PrimaryKeyAsString) List(java.util.List) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet)

Example 80 with AllTypes

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());
}
Also used : AllTypes(io.realm.entities.AllTypes) DictionaryAllTypes(io.realm.entities.DictionaryAllTypes) Date(java.util.Date) Test(org.junit.Test) UiThreadTest(androidx.test.annotation.UiThreadTest)

Aggregations

AllTypes (io.realm.entities.AllTypes)225 Test (org.junit.Test)210 RunTestInLooperThread (io.realm.rule.RunTestInLooperThread)70 UiThreadTest (androidx.test.annotation.UiThreadTest)54 Date (java.util.Date)36 DictionaryAllTypes (io.realm.entities.DictionaryAllTypes)32 Dog (io.realm.entities.Dog)27 JSONObject (org.json.JSONObject)26 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)22 RunInLooperThread (io.realm.rule.RunInLooperThread)20 InputStream (java.io.InputStream)19 Decimal128 (org.bson.types.Decimal128)19 ByteArrayInputStream (java.io.ByteArrayInputStream)17 ObjectId (org.bson.types.ObjectId)15 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)11 UiThreadTest (android.support.test.annotation.UiThreadTest)10 AtomicLong (java.util.concurrent.atomic.AtomicLong)10 OsSharedRealm (io.realm.internal.OsSharedRealm)9 BigDecimal (java.math.BigDecimal)9 NonLatinFieldNames (io.realm.entities.NonLatinFieldNames)8