Search in sources :

Example 6 with DogPrimaryKey

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

the class BulkInsertTests method insertOrUpdate.

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

        @Override
        public void execute(Realm realm) {
            AllTypesPrimaryKey obj = new AllTypesPrimaryKey();
            obj.setColumnString("Foo");
            obj.setColumnLong(1);
            obj.setColumnFloat(1.23F);
            obj.setColumnDouble(1.234D);
            obj.setColumnBoolean(false);
            obj.setColumnBinary(new byte[] { 1, 2, 3 });
            obj.setColumnDate(new Date(1000));
            obj.setColumnRealmObject(new DogPrimaryKey(1, "Dog1"));
            obj.setColumnRealmList(new RealmList<DogPrimaryKey>(new DogPrimaryKey(2, "Dog2")));
            obj.setColumnBoxedBoolean(true);
            realm.insert(obj);
            AllTypesPrimaryKey obj2 = new AllTypesPrimaryKey();
            obj2.setColumnString("Bar");
            obj2.setColumnLong(1);
            obj2.setColumnFloat(2.23F);
            obj2.setColumnDouble(2.234D);
            obj2.setColumnBoolean(true);
            obj2.setColumnBinary(new byte[] { 2, 3, 4 });
            obj2.setColumnDate(new Date(2000));
            obj2.setColumnRealmObject(new DogPrimaryKey(3, "Dog3"));
            obj2.setColumnRealmList(new RealmList<DogPrimaryKey>(new DogPrimaryKey(4, "Dog4")));
            obj2.setColumnBoxedBoolean(false);
            realm.insertOrUpdate(obj2);
        }
    });
    assertEquals(1, realm.where(AllTypesPrimaryKey.class).count());
    AllTypesPrimaryKey obj = realm.where(AllTypesPrimaryKey.class).findFirst();
    // Checks that the only element has all its properties updated.
    assertNotNull(obj);
    assertEquals("Bar", obj.getColumnString());
    assertEquals(1, obj.getColumnLong());
    assertEquals(2.23F, obj.getColumnFloat(), 0);
    assertEquals(2.234D, obj.getColumnDouble(), 0);
    assertEquals(true, obj.isColumnBoolean());
    assertArrayEquals(new byte[] { 2, 3, 4 }, obj.getColumnBinary());
    assertEquals(new Date(2000), obj.getColumnDate());
    assertEquals("Dog3", obj.getColumnRealmObject().getName());
    assertEquals(1, obj.getColumnRealmList().size());
    assertEquals("Dog4", obj.getColumnRealmList().get(0).getName());
    assertEquals(4, realm.where(DogPrimaryKey.class).findAll().size());
    assertFalse(obj.getColumnBoxedBoolean());
}
Also used : DogPrimaryKey(io.realm.entities.DogPrimaryKey) Date(java.util.Date) AllTypesPrimaryKey(io.realm.entities.AllTypesPrimaryKey) Test(org.junit.Test)

Aggregations

DogPrimaryKey (io.realm.entities.DogPrimaryKey)6 Test (org.junit.Test)6 AllTypesPrimaryKey (io.realm.entities.AllTypesPrimaryKey)5 SharedRealm (io.realm.internal.SharedRealm)3 Date (java.util.Date)3 Cat (io.realm.entities.Cat)1 OwnerPrimaryKey (io.realm.entities.OwnerPrimaryKey)1 PrimaryKeyMix (io.realm.entities.PrimaryKeyMix)1