use of io.realm.entities.PrimaryKeyAsLong in project realm-java by realm.
the class RealmAnnotationTests method primaryKey_migration_string.
// Tests migrating primary key from long to str with existing data.
@Test
public void primaryKey_migration_string() {
realm.beginTransaction();
for (int i = 1; i <= 2; i++) {
PrimaryKeyAsLong obj = realm.createObject(PrimaryKeyAsLong.class, i);
obj.setName("String" + i);
}
Table table = realm.getTable(PrimaryKeyAsLong.class);
table.setPrimaryKey("name");
assertEquals(1, table.getPrimaryKey());
realm.cancelTransaction();
}
use of io.realm.entities.PrimaryKeyAsLong in project realm-java by realm.
the class BulkInsertTests method insertOrUpdate_list.
@Test
public void insertOrUpdate_list() {
realm.executeTransaction(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
PrimaryKeyAsLong obj = new PrimaryKeyAsLong();
obj.setId(1);
obj.setName("Foo");
realm.copyToRealm(obj);
PrimaryKeyAsLong obj2 = new PrimaryKeyAsLong();
obj2.setId(1);
obj2.setName("Bar");
PrimaryKeyAsLong obj3 = new PrimaryKeyAsLong();
obj3.setId(1);
obj3.setName("Baz");
realm.insertOrUpdate(Arrays.asList(obj2, obj3));
}
});
assertEquals(1, realm.where(PrimaryKeyAsLong.class).count());
PrimaryKeyAsLong first = realm.where(PrimaryKeyAsLong.class).findFirst();
assertNotNull(first);
assertEquals("Baz", first.getName());
}
use of io.realm.entities.PrimaryKeyAsLong in project realm-java by realm.
the class RealmTests method copyToRealmOrUpdate_iterable.
@Test
public void copyToRealmOrUpdate_iterable() {
realm.executeTransaction(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
PrimaryKeyAsLong obj = new PrimaryKeyAsLong();
obj.setId(1);
obj.setName("Foo");
realm.copyToRealm(obj);
PrimaryKeyAsLong obj2 = new PrimaryKeyAsLong();
obj2.setId(1);
obj2.setName("Bar");
PrimaryKeyAsLong obj3 = new PrimaryKeyAsLong();
obj3.setId(1);
obj3.setName("Baz");
realm.copyToRealmOrUpdate(Arrays.asList(obj2, obj3));
}
});
assertEquals(1, realm.where(PrimaryKeyAsLong.class).count());
assertEquals("Baz", realm.where(PrimaryKeyAsLong.class).findFirst().getName());
}
use of io.realm.entities.PrimaryKeyAsLong in project realm-java by realm.
the class RealmTests method copyToRealmOrUpdate_addNewObjects.
@Test
public void copyToRealmOrUpdate_addNewObjects() {
realm.executeTransaction(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
PrimaryKeyAsLong obj = new PrimaryKeyAsLong();
obj.setId(1);
obj.setName("Foo");
realm.copyToRealm(obj);
PrimaryKeyAsLong obj2 = new PrimaryKeyAsLong();
obj2.setId(2);
obj2.setName("Bar");
realm.copyToRealmOrUpdate(obj2);
}
});
assertEquals(2, realm.where(PrimaryKeyAsLong.class).count());
}
use of io.realm.entities.PrimaryKeyAsLong in project realm-java by realm.
the class RealmResultsTests method asJSON_withEscaping.
@Test
public void asJSON_withEscaping() throws JSONException {
realm.beginTransaction();
PrimaryKeyAsLong element = realm.createObject(PrimaryKeyAsLong.class, 1);
String value = "\"something\"";
element.setName(value);
realm.commitTransaction();
RealmResults<PrimaryKeyAsLong> all = realm.where(PrimaryKeyAsLong.class).equalTo(PrimaryKeyAsString.FIELD_ID, element.getId()).findAll();
assertEquals(1, all.size());
String json = all.asJSON();
final String expectedJSON = "[{\"_key\":0,\"id\":1,\"name\":\"\\\"something\\\"\"}]";
JSONAssert.assertEquals(expectedJSON, json, false);
}
Aggregations