use of io.realm.entities.AllTypesPrimaryKey in project realm-java by realm.
the class RealmJsonTests method createOrUpdateObjectFromJson_jsonObjectNullClass.
@Test
public void createOrUpdateObjectFromJson_jsonObjectNullClass() throws JSONException {
TestHelper.populateSimpleAllTypesPrimaryKey(realm);
realm.beginTransaction();
JSONObject json = new JSONObject();
json.put("columnLong", 1);
json.put("columnString", "bar");
assertNull(realm.createOrUpdateObjectFromJson(null, json));
realm.commitTransaction();
AllTypesPrimaryKey obj2 = realm.where(AllTypesPrimaryKey.class).findFirst();
assertEquals("Foo", obj2.getColumnString());
}
use of io.realm.entities.AllTypesPrimaryKey in project realm-java by realm.
the class RealmObjectTests method addChangeListener_returnedObjectOfCopyToRealmOrUpdate.
// Bug https://github.com/realm/realm-java/issues/2569
@Test
@RunTestInLooperThread
public void addChangeListener_returnedObjectOfCopyToRealmOrUpdate() {
Realm realm = looperThread.realm;
realm.beginTransaction();
realm.createObject(AllTypesPrimaryKey.class, 1);
AllTypesPrimaryKey allTypesPrimaryKey = new AllTypesPrimaryKey();
allTypesPrimaryKey.setColumnLong(1);
allTypesPrimaryKey.setColumnFloat(0f);
allTypesPrimaryKey = realm.copyToRealmOrUpdate(allTypesPrimaryKey);
realm.commitTransaction();
looperThread.keepStrongReference.add(allTypesPrimaryKey);
allTypesPrimaryKey.addChangeListener(new RealmChangeListener<AllTypesPrimaryKey>() {
@Override
public void onChange(AllTypesPrimaryKey element) {
assertEquals(42.0f, element.getColumnFloat(), 0f);
looperThread.testComplete();
}
});
// Change the object to trigger the listener.
realm.beginTransaction();
allTypesPrimaryKey.setColumnFloat(42f);
realm.commitTransaction();
}
use of io.realm.entities.AllTypesPrimaryKey in project realm-java by realm.
the class BulkInsertTests method insertOrUpdate_mixingNoPrimaryKeyAndPrimaryKeyModels.
@Test
public void insertOrUpdate_mixingNoPrimaryKeyAndPrimaryKeyModels() {
AllTypesPrimaryKey objB_pk = new AllTypesPrimaryKey();
objB_pk.setColumnLong(7);
objB_pk.setColumnString("B");
NoPrimaryKeyWithPrimaryKeyObjectRelation objA_no_pk = new NoPrimaryKeyWithPrimaryKeyObjectRelation();
objA_no_pk.setColumnRealmObjectPK(objB_pk);
objA_no_pk.setColumnString("A");
realm.beginTransaction();
realm.insert(objA_no_pk);
realm.commitTransaction();
RealmResults<NoPrimaryKeyWithPrimaryKeyObjectRelation> all = realm.where(NoPrimaryKeyWithPrimaryKeyObjectRelation.class).findAll();
assertEquals(1, all.size());
assertEquals("A", all.get(0).getColumnString());
assertEquals(8, all.get(0).getColumnInt());
assertNotNull(all.get(0).getColumnRealmObjectPK());
assertEquals(7, all.get(0).getColumnRealmObjectPK().getColumnLong());
assertEquals("B", all.get(0).getColumnRealmObjectPK().getColumnString());
assertEquals(1, realm.where(AllTypesPrimaryKey.class).findAll().size());
objA_no_pk.setColumnString("different A");
// Should insert a new instance
objA_no_pk.setColumnInt(42);
// Updates (since it has a PK) now both AllTypesPrimaryKey points to the same objB_pk instance.
objB_pk.setColumnString("updated B");
realm.beginTransaction();
realm.insertOrUpdate(objA_no_pk);
realm.commitTransaction();
all = realm.where(NoPrimaryKeyWithPrimaryKeyObjectRelation.class).findAllSorted("columnString");
assertEquals(2, all.size());
assertEquals("A", all.get(0).getColumnString());
assertEquals(8, all.get(0).getColumnInt());
assertEquals("different A", all.get(1).getColumnString());
assertEquals(42, all.get(1).getColumnInt());
assertNotNull(all.get(0).getColumnRealmObjectPK());
assertNotNull(all.get(1).getColumnRealmObjectPK());
assertEquals(7, all.get(0).getColumnRealmObjectPK().getColumnLong());
assertEquals(7, all.get(1).getColumnRealmObjectPK().getColumnLong());
assertEquals("updated B", all.get(0).getColumnRealmObjectPK().getColumnString());
assertEquals("updated B", all.get(1).getColumnRealmObjectPK().getColumnString());
assertEquals(1, realm.where(AllTypesPrimaryKey.class).findAll().size());
}
use of io.realm.entities.AllTypesPrimaryKey in project realm-java by realm.
the class BulkInsertTests method insertOrUpdate_collectionOfManagedObjects.
@Test
public void insertOrUpdate_collectionOfManagedObjects() {
realm.beginTransaction();
AllTypesPrimaryKey allTypes = realm.createObject(AllTypesPrimaryKey.class, 0);
allTypes.getColumnRealmList().add(realm.createObject(DogPrimaryKey.class, 0));
realm.commitTransaction();
assertEquals(1, allTypes.getColumnRealmList().size());
List<AllTypesPrimaryKey> list = new ArrayList<AllTypesPrimaryKey>();
// Although there are two same objects in the list, none of them should be saved to the db since they are managed
// already.
list.add(allTypes);
list.add(allTypes);
realm.beginTransaction();
realm.insertOrUpdate(list);
realm.commitTransaction();
allTypes = realm.where(AllTypesPrimaryKey.class).findFirst();
assertNotNull(allTypes);
assertEquals(1, allTypes.getColumnRealmList().size());
}
use of io.realm.entities.AllTypesPrimaryKey in project realm-java by realm.
the class RealmJsonTests method createOrUpdateObjectFromJson_inputString.
@Test
public void createOrUpdateObjectFromJson_inputString() throws IOException {
TestHelper.populateSimpleAllTypesPrimaryKey(realm);
realm.beginTransaction();
AllTypesPrimaryKey newObj = realm.createOrUpdateObjectFromJson(AllTypesPrimaryKey.class, "{ \"columnLong\" : 1, \"columnString\" : \"bar\" }");
realm.commitTransaction();
assertEquals(1, realm.where(AllTypesPrimaryKey.class).count());
assertEquals("bar", newObj.getColumnString());
}
Aggregations