Search in sources :

Example 1 with RandomPrimaryKey

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

the class RealmTests method copyToRealm_defaultValuesAreIgnored.

@Test
public void copyToRealm_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;
    final DefaultValueOfField managedObj;
    realm.beginTransaction();
    {
        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);
        obj.setFieldStringList(new RealmList<>("2", "3"));
        obj.setFieldBooleanList(new RealmList<>(true, false));
        obj.setFieldBinaryList(new RealmList<>(new byte[] { 2 }, new byte[] { 3 }));
        obj.setFieldLongList(new RealmList<>(2L, 3L));
        obj.setFieldIntegerList(new RealmList<>(2, 3));
        obj.setFieldShortList(new RealmList<>((short) 2, (short) 3));
        obj.setFieldByteList(new RealmList<>((byte) 2, (byte) 3));
        obj.setFieldDoubleList(new RealmList<>(2D, 3D));
        obj.setFieldFloatList(new RealmList<>(2F, 3F));
        obj.setFieldDateList(new RealmList<>(new Date(2L), new Date(3L)));
        managedObj = realm.copyToRealm(obj);
    }
    realm.commitTransaction();
    assertEquals(DefaultValueOfField.FIELD_IGNORED_DEFAULT_VALUE, /*not fieldIgnoredValue*/
    managedObj.getFieldIgnored());
    assertEquals(fieldStringValue, managedObj.getFieldString());
    assertEquals(fieldRandomStringValue, managedObj.getFieldRandomString());
    assertEquals(fieldShortValue, managedObj.getFieldShort());
    assertEquals(fieldIntValue, managedObj.getFieldInt());
    assertEquals(fieldLongPrimaryKeyValue, managedObj.getFieldLongPrimaryKey());
    assertEquals(fieldLongValue, managedObj.getFieldLong());
    assertEquals(fieldByteValue, managedObj.getFieldByte());
    assertEquals(fieldFloatValue, managedObj.getFieldFloat(), 0F);
    assertEquals(fieldDoubleValue, managedObj.getFieldDouble(), 0D);
    assertEquals(fieldBooleanValue, managedObj.isFieldBoolean());
    assertEquals(fieldDateValue, managedObj.getFieldDate());
    assertArrayEquals(fieldBinaryValue, managedObj.getFieldBinary());
    assertEquals(fieldObjectIntValue, managedObj.getFieldObject().getFieldInt());
    assertEquals(1, managedObj.getFieldList().size());
    assertEquals(fieldListIntValue, managedObj.getFieldList().first().getFieldInt());
    assertEquals(2, managedObj.getFieldStringList().size());
    assertEquals("2", managedObj.getFieldStringList().get(0));
    assertEquals("3", managedObj.getFieldStringList().get(1));
    assertEquals(2, managedObj.getFieldBooleanList().size());
    assertEquals(true, managedObj.getFieldBooleanList().get(0));
    assertEquals(false, managedObj.getFieldBooleanList().get(1));
    assertEquals(2, managedObj.getFieldBinaryList().size());
    assertArrayEquals(new byte[] { 2 }, managedObj.getFieldBinaryList().get(0));
    assertArrayEquals(new byte[] { 3 }, managedObj.getFieldBinaryList().get(1));
    assertEquals(2, managedObj.getFieldLongList().size());
    assertEquals((Long) 2L, managedObj.getFieldLongList().get(0));
    assertEquals((Long) 3L, managedObj.getFieldLongList().get(1));
    assertEquals(2, managedObj.getFieldIntegerList().size());
    assertEquals((Integer) 2, managedObj.getFieldIntegerList().get(0));
    assertEquals((Integer) 3, managedObj.getFieldIntegerList().get(1));
    assertEquals(2, managedObj.getFieldShortList().size());
    assertEquals((Short) (short) 2, managedObj.getFieldShortList().get(0));
    assertEquals((Short) (short) 3, managedObj.getFieldShortList().get(1));
    assertEquals(2, managedObj.getFieldByteList().size());
    assertEquals((Byte) (byte) 2, managedObj.getFieldByteList().get(0));
    assertEquals((Byte) (byte) 3, managedObj.getFieldByteList().get(1));
    assertEquals(2, managedObj.getFieldDoubleList().size());
    assertEquals((Double) 2D, managedObj.getFieldDoubleList().get(0));
    assertEquals((Double) 3D, managedObj.getFieldDoubleList().get(1));
    assertEquals(2, managedObj.getFieldFloatList().size());
    assertEquals((Float) 2F, managedObj.getFieldFloatList().get(0));
    assertEquals((Float) 3F, managedObj.getFieldFloatList().get(1));
    assertEquals(2, managedObj.getFieldDateList().size());
    assertEquals(new Date(2L), managedObj.getFieldDateList().get(0));
    assertEquals(new Date(3L), managedObj.getFieldDateList().get(1));
    // Makes sure that excess object by default value is not created.
    assertEquals(2, realm.where(RandomPrimaryKey.class).count());
}
Also used : PrimaryKeyRequiredAsString(io.realm.entities.PrimaryKeyRequiredAsString) PrimaryKeyAsString(io.realm.entities.PrimaryKeyAsString) Date(java.util.Date) RandomPrimaryKey(io.realm.entities.RandomPrimaryKey) DefaultValueOfField(io.realm.entities.DefaultValueOfField) Test(org.junit.Test) UiThreadTest(androidx.test.annotation.UiThreadTest)

Example 2 with RandomPrimaryKey

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

the class RealmTests method copyFromRealm_defaultValuesAreIgnored.

@Test
public void copyFromRealm_defaultValuesAreIgnored() {
    final DefaultValueOfField managedObj;
    realm.beginTransaction();
    {
        final DefaultValueOfField obj = new DefaultValueOfField();
        obj.setFieldIgnored(DefaultValueOfField.FIELD_IGNORED_DEFAULT_VALUE + ".modified");
        obj.setFieldString(DefaultValueOfField.FIELD_STRING_DEFAULT_VALUE + ".modified");
        obj.setFieldRandomString("non-random");
        obj.setFieldShort((short) (DefaultValueOfField.FIELD_SHORT_DEFAULT_VALUE + 1));
        obj.setFieldInt(DefaultValueOfField.FIELD_INT_DEFAULT_VALUE + 1);
        obj.setFieldLongPrimaryKey(DefaultValueOfField.FIELD_LONG_PRIMARY_KEY_DEFAULT_VALUE + 1);
        obj.setFieldLong(DefaultValueOfField.FIELD_LONG_DEFAULT_VALUE + 1);
        obj.setFieldByte((byte) (DefaultValueOfField.FIELD_BYTE_DEFAULT_VALUE + 1));
        obj.setFieldFloat(DefaultValueOfField.FIELD_FLOAT_DEFAULT_VALUE + 1);
        obj.setFieldDouble(DefaultValueOfField.FIELD_DOUBLE_DEFAULT_VALUE + 1);
        obj.setFieldBoolean(!DefaultValueOfField.FIELD_BOOLEAN_DEFAULT_VALUE);
        obj.setFieldDate(new Date(DefaultValueOfField.FIELD_DATE_DEFAULT_VALUE.getTime() + 1));
        obj.setFieldBinary(new byte[] { (byte) (DefaultValueOfField.FIELD_BINARY_DEFAULT_VALUE[0] - 1) });
        final RandomPrimaryKey fieldObjectValue = new RandomPrimaryKey();
        fieldObjectValue.setFieldInt(RandomPrimaryKey.FIELD_INT_DEFAULT_VALUE + 1);
        obj.setFieldObject(fieldObjectValue);
        final RealmList<RandomPrimaryKey> list = new RealmList<RandomPrimaryKey>();
        final RandomPrimaryKey listItem = new RandomPrimaryKey();
        listItem.setFieldInt(RandomPrimaryKey.FIELD_INT_DEFAULT_VALUE + 2);
        list.add(listItem);
        obj.setFieldList(list);
        obj.setFieldStringList(new RealmList<>("2", "3"));
        obj.setFieldBooleanList(new RealmList<>(true, false));
        obj.setFieldBinaryList(new RealmList<>(new byte[] { 2 }, new byte[] { 3 }));
        obj.setFieldLongList(new RealmList<>(2L, 3L));
        obj.setFieldIntegerList(new RealmList<>(2, 3));
        obj.setFieldShortList(new RealmList<>((short) 2, (short) 3));
        obj.setFieldByteList(new RealmList<>((byte) 2, (byte) 3));
        obj.setFieldDoubleList(new RealmList<>(2D, 3D));
        obj.setFieldFloatList(new RealmList<>(2F, 3F));
        obj.setFieldDateList(new RealmList<>(new Date(2L), new Date(3L)));
        managedObj = realm.copyToRealm(obj);
    }
    realm.commitTransaction();
    final DefaultValueOfField copy = realm.copyFromRealm(managedObj);
    assertEquals(DefaultValueOfField.FIELD_IGNORED_DEFAULT_VALUE, copy.getFieldIgnored());
    assertEquals(managedObj.getFieldString(), copy.getFieldString());
    assertEquals(managedObj.getFieldRandomString(), copy.getFieldRandomString());
    assertEquals(managedObj.getFieldShort(), copy.getFieldShort());
    assertEquals(managedObj.getFieldInt(), copy.getFieldInt());
    assertEquals(managedObj.getFieldLongPrimaryKey(), copy.getFieldLongPrimaryKey());
    assertEquals(managedObj.getFieldLong(), copy.getFieldLong());
    assertEquals(managedObj.getFieldByte(), copy.getFieldByte());
    assertEquals(managedObj.getFieldFloat(), copy.getFieldFloat(), 0F);
    assertEquals(managedObj.getFieldDouble(), copy.getFieldDouble(), 0D);
    assertEquals(managedObj.isFieldBoolean(), copy.isFieldBoolean());
    assertEquals(managedObj.getFieldDate(), copy.getFieldDate());
    assertArrayEquals(managedObj.getFieldBinary(), copy.getFieldBinary());
    assertEquals(managedObj.getFieldObject().getFieldInt(), copy.getFieldObject().getFieldInt());
    assertEquals(1, copy.getFieldList().size());
    // noinspection ConstantConditions
    assertEquals(managedObj.getFieldList().first().getFieldInt(), copy.getFieldList().first().getFieldInt());
    assertEquals(2, managedObj.getFieldStringList().size());
    assertEquals("2", managedObj.getFieldStringList().get(0));
    assertEquals("3", managedObj.getFieldStringList().get(1));
    assertEquals(2, managedObj.getFieldBooleanList().size());
    assertEquals(true, managedObj.getFieldBooleanList().get(0));
    assertEquals(false, managedObj.getFieldBooleanList().get(1));
    assertEquals(2, managedObj.getFieldBinaryList().size());
    assertArrayEquals(new byte[] { 2 }, managedObj.getFieldBinaryList().get(0));
    assertArrayEquals(new byte[] { 3 }, managedObj.getFieldBinaryList().get(1));
    assertEquals(2, managedObj.getFieldLongList().size());
    assertEquals((Long) 2L, managedObj.getFieldLongList().get(0));
    assertEquals((Long) 3L, managedObj.getFieldLongList().get(1));
    assertEquals(2, managedObj.getFieldIntegerList().size());
    assertEquals((Integer) 2, managedObj.getFieldIntegerList().get(0));
    assertEquals((Integer) 3, managedObj.getFieldIntegerList().get(1));
    assertEquals(2, managedObj.getFieldShortList().size());
    assertEquals((Short) (short) 2, managedObj.getFieldShortList().get(0));
    assertEquals((Short) (short) 3, managedObj.getFieldShortList().get(1));
    assertEquals(2, managedObj.getFieldByteList().size());
    assertEquals((Byte) (byte) 2, managedObj.getFieldByteList().get(0));
    assertEquals((Byte) (byte) 3, managedObj.getFieldByteList().get(1));
    assertEquals(2, managedObj.getFieldDoubleList().size());
    assertEquals((Double) 2D, managedObj.getFieldDoubleList().get(0));
    assertEquals((Double) 3D, managedObj.getFieldDoubleList().get(1));
    assertEquals(2, managedObj.getFieldFloatList().size());
    assertEquals((Float) 2F, managedObj.getFieldFloatList().get(0));
    assertEquals((Float) 3F, managedObj.getFieldFloatList().get(1));
    assertEquals(2, managedObj.getFieldDateList().size());
    assertEquals(new Date(2L), managedObj.getFieldDateList().get(0));
    assertEquals(new Date(3L), managedObj.getFieldDateList().get(1));
}
Also used : RandomPrimaryKey(io.realm.entities.RandomPrimaryKey) DefaultValueOfField(io.realm.entities.DefaultValueOfField) Date(java.util.Date) Test(org.junit.Test) UiThreadTest(androidx.test.annotation.UiThreadTest)

Example 3 with RandomPrimaryKey

use of io.realm.entities.RandomPrimaryKey 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 : PrimaryKeyAsString(io.realm.entities.PrimaryKeyAsString) Date(java.util.Date) RandomPrimaryKey(io.realm.entities.RandomPrimaryKey) DefaultValueOfField(io.realm.entities.DefaultValueOfField) Test(org.junit.Test) UiThreadTest(androidx.test.annotation.UiThreadTest)

Aggregations

UiThreadTest (androidx.test.annotation.UiThreadTest)3 DefaultValueOfField (io.realm.entities.DefaultValueOfField)3 RandomPrimaryKey (io.realm.entities.RandomPrimaryKey)3 Date (java.util.Date)3 Test (org.junit.Test)3 PrimaryKeyAsString (io.realm.entities.PrimaryKeyAsString)2 PrimaryKeyRequiredAsString (io.realm.entities.PrimaryKeyRequiredAsString)1