Search in sources :

Example 1 with PrimaryKeyAsLong

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();
}
Also used : Table(io.realm.internal.Table) PrimaryKeyAsLong(io.realm.entities.PrimaryKeyAsLong) Test(org.junit.Test)

Example 2 with PrimaryKeyAsLong

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());
}
Also used : PrimaryKeyAsLong(io.realm.entities.PrimaryKeyAsLong) Test(org.junit.Test)

Example 3 with PrimaryKeyAsLong

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());
}
Also used : PrimaryKeyAsLong(io.realm.entities.PrimaryKeyAsLong) OsSharedRealm(io.realm.internal.OsSharedRealm) Test(org.junit.Test) UiThreadTest(androidx.test.annotation.UiThreadTest)

Example 4 with PrimaryKeyAsLong

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());
}
Also used : PrimaryKeyAsLong(io.realm.entities.PrimaryKeyAsLong) OsSharedRealm(io.realm.internal.OsSharedRealm) Test(org.junit.Test) UiThreadTest(androidx.test.annotation.UiThreadTest)

Example 5 with PrimaryKeyAsLong

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);
}
Also used : PrimaryKeyAsLong(io.realm.entities.PrimaryKeyAsLong) PrimaryKeyAsString(io.realm.entities.PrimaryKeyAsString) Test(org.junit.Test) UiThreadTest(androidx.test.annotation.UiThreadTest)

Aggregations

PrimaryKeyAsLong (io.realm.entities.PrimaryKeyAsLong)10 Test (org.junit.Test)10 UiThreadTest (androidx.test.annotation.UiThreadTest)6 PrimaryKeyAsString (io.realm.entities.PrimaryKeyAsString)3 OsSharedRealm (io.realm.internal.OsSharedRealm)3 Table (io.realm.internal.Table)2 PrimaryKeyAsBoxedLong (io.realm.entities.PrimaryKeyAsBoxedLong)1 PrimaryKeyRequiredAsString (io.realm.entities.PrimaryKeyRequiredAsString)1 RealmPrimaryKeyConstraintException (io.realm.exceptions.RealmPrimaryKeyConstraintException)1 RunTestInLooperThread (io.realm.rule.RunTestInLooperThread)1 ObjectId (org.bson.types.ObjectId)1