Search in sources :

Example 6 with DefaultValueOfField

use of io.realm.entities.DefaultValueOfField in project realm-java by realm.

the class RealmTests method createObject_defaultValueFromModelField.

@Test
public void createObject_defaultValueFromModelField() {
    realm.executeTransaction(new Realm.Transaction() {

        @Override
        public void execute(Realm realm) {
            // Creates a DefaultValueOfField with non-default primary key value.
            realm.createObject(DefaultValueOfField.class, DefaultValueOfField.FIELD_LONG_PRIMARY_KEY_DEFAULT_VALUE * 3);
        }
    });
    final String createdRandomString = DefaultValueOfField.lastRandomStringValue;
    testOneObjectFound(realm, DefaultValueOfField.class, DefaultValueOfField.FIELD_STRING, DefaultValueOfField.FIELD_STRING_DEFAULT_VALUE);
    testOneObjectFound(realm, DefaultValueOfField.class, DefaultValueOfField.FIELD_RANDOM_STRING, createdRandomString);
    testOneObjectFound(realm, DefaultValueOfField.class, DefaultValueOfField.FIELD_SHORT, DefaultValueOfField.FIELD_SHORT_DEFAULT_VALUE);
    testOneObjectFound(realm, DefaultValueOfField.class, DefaultValueOfField.FIELD_INT, DefaultValueOfField.FIELD_INT_DEFAULT_VALUE);
    // Default value for pk must be ignored.
    testNoObjectFound(realm, DefaultValueOfField.class, DefaultValueOfField.FIELD_LONG_PRIMARY_KEY, DefaultValueOfField.FIELD_LONG_PRIMARY_KEY_DEFAULT_VALUE);
    testOneObjectFound(realm, DefaultValueOfField.class, DefaultValueOfField.FIELD_LONG_PRIMARY_KEY, DefaultValueOfField.FIELD_LONG_PRIMARY_KEY_DEFAULT_VALUE * 3);
    testOneObjectFound(realm, DefaultValueOfField.class, DefaultValueOfField.FIELD_LONG, DefaultValueOfField.FIELD_LONG_DEFAULT_VALUE);
    testOneObjectFound(realm, DefaultValueOfField.class, DefaultValueOfField.FIELD_BYTE, DefaultValueOfField.FIELD_BYTE_DEFAULT_VALUE);
    testOneObjectFound(realm, DefaultValueOfField.class, DefaultValueOfField.FIELD_FLOAT, DefaultValueOfField.FIELD_FLOAT_DEFAULT_VALUE);
    testOneObjectFound(realm, DefaultValueOfField.class, DefaultValueOfField.FIELD_DOUBLE, DefaultValueOfField.FIELD_DOUBLE_DEFAULT_VALUE);
    testOneObjectFound(realm, DefaultValueOfField.class, DefaultValueOfField.FIELD_BOOLEAN, DefaultValueOfField.FIELD_BOOLEAN_DEFAULT_VALUE);
    testOneObjectFound(realm, DefaultValueOfField.class, DefaultValueOfField.FIELD_DATE, DefaultValueOfField.FIELD_DATE_DEFAULT_VALUE);
    testOneObjectFound(realm, DefaultValueOfField.class, DefaultValueOfField.FIELD_BINARY, DefaultValueOfField.FIELD_BINARY_DEFAULT_VALUE);
    testOneObjectFound(realm, DefaultValueOfField.class, DefaultValueOfField.FIELD_OBJECT + "." + RandomPrimaryKey.FIELD_INT, RandomPrimaryKey.FIELD_INT_DEFAULT_VALUE);
    testOneObjectFound(realm, DefaultValueOfField.class, DefaultValueOfField.FIELD_LIST + "." + RandomPrimaryKey.FIELD_INT, RandomPrimaryKey.FIELD_INT_DEFAULT_VALUE);
}
Also used : PrimaryKeyRequiredAsString(io.realm.entities.PrimaryKeyRequiredAsString) PrimaryKeyAsString(io.realm.entities.PrimaryKeyAsString) DefaultValueOfField(io.realm.entities.DefaultValueOfField) SharedRealm(io.realm.internal.SharedRealm) Test(org.junit.Test)

Example 7 with DefaultValueOfField

use of io.realm.entities.DefaultValueOfField in project realm-java by realm.

the class RealmResultsTests method syncQuery_defaultValuesAreIgnored.

@Test
public void syncQuery_defaultValuesAreIgnored() {
    final String fieldIgnoredValue = DefaultValueOfField.FIELD_IGNORED_DEFAULT_VALUE + ".modified";
    final String fieldStringValue = DefaultValueOfField.FIELD_STRING_DEFAULT_VALUE + ".modified";
    final String fieldRandomStringValue = "non-random";
    final short fieldShortValue = (short) (DefaultValueOfField.FIELD_SHORT_DEFAULT_VALUE + 1);
    final int fieldIntValue = DefaultValueOfField.FIELD_INT_DEFAULT_VALUE + 1;
    final long fieldLongPrimaryKeyValue = DefaultValueOfField.FIELD_LONG_PRIMARY_KEY_DEFAULT_VALUE + 1;
    final long fieldLongValue = DefaultValueOfField.FIELD_LONG_DEFAULT_VALUE + 1;
    final byte fieldByteValue = (byte) (DefaultValueOfField.FIELD_BYTE_DEFAULT_VALUE + 1);
    final float fieldFloatValue = DefaultValueOfField.FIELD_FLOAT_DEFAULT_VALUE + 1;
    final double fieldDoubleValue = DefaultValueOfField.FIELD_DOUBLE_DEFAULT_VALUE + 1;
    final boolean fieldBooleanValue = !DefaultValueOfField.FIELD_BOOLEAN_DEFAULT_VALUE;
    final Date fieldDateValue = new Date(DefaultValueOfField.FIELD_DATE_DEFAULT_VALUE.getTime() + 1);
    final byte[] fieldBinaryValue = { (byte) (DefaultValueOfField.FIELD_BINARY_DEFAULT_VALUE[0] - 1) };
    final int fieldObjectIntValue = RandomPrimaryKey.FIELD_INT_DEFAULT_VALUE + 1;
    final int fieldListIntValue = RandomPrimaryKey.FIELD_INT_DEFAULT_VALUE + 2;
    realm.executeTransaction(new Realm.Transaction() {

        @Override
        public void execute(Realm realm) {
            final DefaultValueOfField obj = new DefaultValueOfField();
            obj.setFieldIgnored(fieldIgnoredValue);
            obj.setFieldString(fieldStringValue);
            obj.setFieldRandomString(fieldRandomStringValue);
            obj.setFieldShort(fieldShortValue);
            obj.setFieldInt(fieldIntValue);
            obj.setFieldLongPrimaryKey(fieldLongPrimaryKeyValue);
            obj.setFieldLong(fieldLongValue);
            obj.setFieldByte(fieldByteValue);
            obj.setFieldFloat(fieldFloatValue);
            obj.setFieldDouble(fieldDoubleValue);
            obj.setFieldBoolean(fieldBooleanValue);
            obj.setFieldDate(fieldDateValue);
            obj.setFieldBinary(fieldBinaryValue);
            final RandomPrimaryKey fieldObjectValue = new RandomPrimaryKey();
            fieldObjectValue.setFieldInt(fieldObjectIntValue);
            obj.setFieldObject(fieldObjectValue);
            final RealmList<RandomPrimaryKey> list = new RealmList<RandomPrimaryKey>();
            final RandomPrimaryKey listItem = new RandomPrimaryKey();
            listItem.setFieldInt(fieldListIntValue);
            list.add(listItem);
            obj.setFieldList(list);
            realm.copyToRealm(obj);
        }
    });
    final RealmResults<DefaultValueOfField> result = realm.where(DefaultValueOfField.class).equalTo(DefaultValueOfField.FIELD_LONG_PRIMARY_KEY, fieldLongPrimaryKeyValue).findAll();
    final DefaultValueOfField obj = result.first();
    assertEquals(DefaultValueOfField.FIELD_IGNORED_DEFAULT_VALUE, /*not fieldIgnoredValue*/
    obj.getFieldIgnored());
    assertEquals(fieldStringValue, obj.getFieldString());
    assertEquals(fieldRandomStringValue, obj.getFieldRandomString());
    assertEquals(fieldShortValue, obj.getFieldShort());
    assertEquals(fieldIntValue, obj.getFieldInt());
    assertEquals(fieldLongPrimaryKeyValue, obj.getFieldLongPrimaryKey());
    assertEquals(fieldLongValue, obj.getFieldLong());
    assertEquals(fieldByteValue, obj.getFieldByte());
    assertEquals(fieldFloatValue, obj.getFieldFloat(), 0f);
    assertEquals(fieldDoubleValue, obj.getFieldDouble(), 0d);
    assertEquals(fieldBooleanValue, obj.isFieldBoolean());
    assertEquals(fieldDateValue, obj.getFieldDate());
    assertTrue(Arrays.equals(fieldBinaryValue, obj.getFieldBinary()));
    assertEquals(fieldObjectIntValue, obj.getFieldObject().getFieldInt());
    assertEquals(1, obj.getFieldList().size());
    assertEquals(fieldListIntValue, obj.getFieldList().first().getFieldInt());
}
Also used : Date(java.util.Date) RandomPrimaryKey(io.realm.entities.RandomPrimaryKey) DefaultValueOfField(io.realm.entities.DefaultValueOfField) UiThreadTest(android.support.test.annotation.UiThreadTest) Test(org.junit.Test)

Aggregations

DefaultValueOfField (io.realm.entities.DefaultValueOfField)7 Test (org.junit.Test)7 Date (java.util.Date)5 RandomPrimaryKey (io.realm.entities.RandomPrimaryKey)3 JSONObject (org.json.JSONObject)3 PrimaryKeyAsString (io.realm.entities.PrimaryKeyAsString)2 PrimaryKeyRequiredAsString (io.realm.entities.PrimaryKeyRequiredAsString)2 JSONArray (org.json.JSONArray)2 UiThreadTest (android.support.test.annotation.UiThreadTest)1 SharedRealm (io.realm.internal.SharedRealm)1