Search in sources :

Example 6 with PrimaryKeyAsLong

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

the class RealmAnnotationTests method primaryKey_migration_stringDuplicateValues.

// Tests migrating primary key from long to str with existing data.
@Test
public void primaryKey_migration_stringDuplicateValues() {
    realm.beginTransaction();
    for (int i = 1; i <= 2; i++) {
        PrimaryKeyAsLong obj = realm.createObject(PrimaryKeyAsLong.class, i);
        // Creates duplicate values.
        obj.setName("String");
    }
    Table table = realm.getTable(PrimaryKeyAsLong.class);
    try {
        table.setPrimaryKey("name");
        fail("It should not be possible to set a primary key column which already contains duplicate values.");
    } catch (IllegalArgumentException ignored) {
        assertEquals(0, table.getPrimaryKey());
    } finally {
        realm.cancelTransaction();
    }
}
Also used : Table(io.realm.internal.Table) PrimaryKeyAsLong(io.realm.entities.PrimaryKeyAsLong) Test(org.junit.Test)

Example 7 with PrimaryKeyAsLong

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

the class RealmTests method isEmpty.

@Test
public void isEmpty() {
    RealmConfiguration realmConfig = configFactory.createConfiguration("empty_test.realm");
    Realm emptyRealm = Realm.getInstance(realmConfig);
    assertTrue(emptyRealm.isEmpty());
    emptyRealm.beginTransaction();
    PrimaryKeyAsLong obj = new PrimaryKeyAsLong();
    obj.setId(1);
    obj.setName("Foo");
    emptyRealm.copyToRealm(obj);
    assertFalse(emptyRealm.isEmpty());
    emptyRealm.cancelTransaction();
    assertTrue(emptyRealm.isEmpty());
    emptyRealm.beginTransaction();
    obj = new PrimaryKeyAsLong();
    obj.setId(1);
    obj.setName("Foo");
    emptyRealm.copyToRealm(obj);
    emptyRealm.commitTransaction();
    assertFalse(emptyRealm.isEmpty());
    emptyRealm.close();
}
Also used : PrimaryKeyAsLong(io.realm.entities.PrimaryKeyAsLong) SharedRealm(io.realm.internal.SharedRealm) Test(org.junit.Test)

Aggregations

PrimaryKeyAsLong (io.realm.entities.PrimaryKeyAsLong)7 Test (org.junit.Test)7 SharedRealm (io.realm.internal.SharedRealm)3 Table (io.realm.internal.Table)2 RunTestInLooperThread (io.realm.rule.RunTestInLooperThread)1