Search in sources :

Example 1 with CyclicTypePrimaryKey

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

the class RealmListTests method set_unmanagedPrimaryKeyObjectToManagedList.

// Tests that set correctly uses Realm.copyToRealmOrUpdate() on unmanaged objects with a primary key.
@Test
public void set_unmanagedPrimaryKeyObjectToManagedList() {
    realm.beginTransaction();
    CyclicTypePrimaryKey parent = realm.copyToRealm(new CyclicTypePrimaryKey(1, "Parent"));
    RealmList<CyclicTypePrimaryKey> children = parent.getObjects();
    children.add(new CyclicTypePrimaryKey(2));
    children.add(new CyclicTypePrimaryKey(3, "original"));
    children.add(new CyclicTypePrimaryKey(4));
    children.set(1, new CyclicTypePrimaryKey(3, "updated"));
    realm.commitTransaction();
    RealmList<CyclicTypePrimaryKey> list = realm.where(CyclicTypePrimaryKey.class).findFirst().getObjects();
    assertEquals(3, list.size());
    assertEquals("updated", list.get(1).getName());
}
Also used : CyclicTypePrimaryKey(io.realm.entities.CyclicTypePrimaryKey) Test(org.junit.Test)

Example 2 with CyclicTypePrimaryKey

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

the class RealmTests method copyToRealm_doNotCopyReferencedObjectIfManaged.

@Test
public void copyToRealm_doNotCopyReferencedObjectIfManaged() {
    realm.beginTransaction();
    // Child object is managed by Realm.
    CyclicTypePrimaryKey childObj = realm.createObject(CyclicTypePrimaryKey.class, 1);
    childObj.setName("Child");
    // Parent object is an unmanaged object.
    CyclicTypePrimaryKey parentObj = new CyclicTypePrimaryKey(2);
    parentObj.setObject(childObj);
    realm.copyToRealm(parentObj);
    realm.commitTransaction();
    assertEquals(2, realm.where(CyclicTypePrimaryKey.class).count());
}
Also used : CyclicTypePrimaryKey(io.realm.entities.CyclicTypePrimaryKey) Test(org.junit.Test)

Example 3 with CyclicTypePrimaryKey

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

the class RealmTests method copyToRealm_cyclicObjectReferencesWithPK.

@Test
public void copyToRealm_cyclicObjectReferencesWithPK() {
    CyclicTypePrimaryKey oneCyclicType = new CyclicTypePrimaryKey(1, "One");
    CyclicTypePrimaryKey anotherCyclicType = new CyclicTypePrimaryKey(2, "Two");
    oneCyclicType.setObject(anotherCyclicType);
    anotherCyclicType.setObject(oneCyclicType);
    realm.beginTransaction();
    CyclicTypePrimaryKey realmObject = realm.copyToRealm(oneCyclicType);
    realm.commitTransaction();
    assertEquals("One", realmObject.getName());
    assertEquals("Two", realmObject.getObject().getName());
    assertEquals(2, realm.where(CyclicTypePrimaryKey.class).count());
    // Tests copyToRealm overload that uses the Iterator.
    // Makes sure we reuse the same graph cache Map to avoid duplicates.
    realm.beginTransaction();
    realm.deleteAll();
    realm.commitTransaction();
    assertEquals(0, realm.where(CyclicTypePrimaryKey.class).count());
    realm.beginTransaction();
    List<CyclicTypePrimaryKey> cyclicTypes = realm.copyToRealm(Arrays.asList(oneCyclicType, anotherCyclicType));
    realm.commitTransaction();
    assertEquals(2, cyclicTypes.size());
    assertEquals("One", cyclicTypes.get(0).getName());
    assertEquals("Two", cyclicTypes.get(1).getName());
    assertEquals(2, realm.where(CyclicTypePrimaryKey.class).count());
}
Also used : CyclicTypePrimaryKey(io.realm.entities.CyclicTypePrimaryKey) Test(org.junit.Test)

Example 4 with CyclicTypePrimaryKey

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

the class RealmListTests method add_unmanagedPrimaryKeyObjectToManagedList.

// Makes sure that unmanaged objects with a primary key are added using copyToRealmOrUpdate.
@Test
public void add_unmanagedPrimaryKeyObjectToManagedList() {
    realm.beginTransaction();
    realm.copyToRealm(new CyclicTypePrimaryKey(2, "original"));
    RealmList<CyclicTypePrimaryKey> children = realm.copyToRealm(new CyclicTypePrimaryKey(1)).getObjects();
    children.add(new CyclicTypePrimaryKey(2, "new"));
    realm.commitTransaction();
    assertEquals(1, realm.where(CyclicTypePrimaryKey.class).equalTo("id", 1).findFirst().getObjects().size());
    assertEquals("new", realm.where(CyclicTypePrimaryKey.class).equalTo("id", 2).findFirst().getName());
}
Also used : CyclicTypePrimaryKey(io.realm.entities.CyclicTypePrimaryKey) Test(org.junit.Test)

Example 5 with CyclicTypePrimaryKey

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

the class BulkInsertTests method insertOrUpdate_cyclicType.

@Test
public void insertOrUpdate_cyclicType() {
    CyclicTypePrimaryKey oneCyclicType = new CyclicTypePrimaryKey(1, "One");
    CyclicTypePrimaryKey anotherCyclicType = new CyclicTypePrimaryKey(2, "Two");
    oneCyclicType.setObject(anotherCyclicType);
    anotherCyclicType.setObject(oneCyclicType);
    realm.beginTransaction();
    realm.insertOrUpdate(Arrays.asList(oneCyclicType, anotherCyclicType));
    realm.commitTransaction();
    RealmResults<CyclicTypePrimaryKey> realmObjects = realm.where(CyclicTypePrimaryKey.class).findAllSorted("name");
    assertNotNull(realmObjects);
    assertEquals(2, realmObjects.size());
    assertEquals("One", realmObjects.get(0).getName());
    assertEquals("Two", realmObjects.get(0).getObject().getName());
    CyclicTypePrimaryKey updatedCyclicType = new CyclicTypePrimaryKey(2, "updated");
    realm.beginTransaction();
    realm.insertOrUpdate(updatedCyclicType);
    realm.commitTransaction();
    assertEquals("One", realmObjects.get(0).getName());
    assertEquals("updated", realmObjects.get(0).getObject().getName());
    assertEquals(2, realm.where(CyclicTypePrimaryKey.class).count());
}
Also used : CyclicTypePrimaryKey(io.realm.entities.CyclicTypePrimaryKey) Test(org.junit.Test)

Aggregations

CyclicTypePrimaryKey (io.realm.entities.CyclicTypePrimaryKey)6 Test (org.junit.Test)6