Search in sources :

Example 1 with PrimaryKeyAsString

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

the class RealmAnnotationTests method primaryKey_migration_longDuplicateValues.

// Tests migrating primary key from string to long with existing data.
@Test
public void primaryKey_migration_longDuplicateValues() {
    realm.beginTransaction();
    for (int i = 1; i <= 2; i++) {
        PrimaryKeyAsString obj = realm.createObject(PrimaryKeyAsString.class, "String" + i);
        // Creates duplicate values.
        obj.setId(1);
    }
    Table table = realm.getTable(PrimaryKeyAsString.class);
    try {
        table.setPrimaryKey("id");
        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) PrimaryKeyAsString(io.realm.entities.PrimaryKeyAsString) Test(org.junit.Test)

Example 2 with PrimaryKeyAsString

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

the class TestHelper method populateTestRealmWithStringPrimaryKey.

/**
     * Populates a realm with String type Primarykey objects for a number of numberOfPopulation - 1,
     * starting with iteratorBeginValue. One object is setup to have given values from parameters.
     */
public static void populateTestRealmWithStringPrimaryKey(Realm testRealm, String primaryFieldValue, long secondaryFieldValue, int numberOfPopulation, int iteratorBeginValue) {
    testRealm.beginTransaction();
    PrimaryKeyAsString userObj = new PrimaryKeyAsString();
    userObj.setName(primaryFieldValue);
    userObj.setId(secondaryFieldValue);
    testRealm.copyToRealm(userObj);
    int idValue = iteratorBeginValue;
    for (int i = 0; i < numberOfPopulation - 1; ++i, ++idValue) {
        PrimaryKeyAsString obj = new PrimaryKeyAsString();
        obj.setName(String.valueOf(idValue));
        obj.setId(idValue);
        testRealm.copyToRealm(obj);
    }
    testRealm.commitTransaction();
}
Also used : PrimaryKeyAsString(io.realm.entities.PrimaryKeyAsString)

Example 3 with PrimaryKeyAsString

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

the class TestHelper method addStringPrimaryKeyObjectToTestRealm.

/**
     * Adds a String type PrimaryKey object to a realm with values for name field (PrimaryKey) and id field
     */
public static PrimaryKeyAsString addStringPrimaryKeyObjectToTestRealm(Realm testRealm, String primaryFieldValue, long secondaryFieldValue) {
    testRealm.beginTransaction();
    PrimaryKeyAsString obj = new PrimaryKeyAsString();
    obj.setName(primaryFieldValue);
    obj.setId(secondaryFieldValue);
    testRealm.copyToRealm(obj);
    testRealm.commitTransaction();
    return obj;
}
Also used : PrimaryKeyAsString(io.realm.entities.PrimaryKeyAsString)

Example 4 with PrimaryKeyAsString

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

the class RealmTests method copyToRealmOrUpdate_stringPrimaryKeyFieldIsNull.

@Test
public void copyToRealmOrUpdate_stringPrimaryKeyFieldIsNull() {
    final long SECONDARY_FIELD_VALUE = 2192841L;
    final long SECONDARY_FIELD_UPDATED = 44887612L;
    PrimaryKeyAsString nullPrimaryKeyObj = TestHelper.addStringPrimaryKeyObjectToTestRealm(realm, (String) null, SECONDARY_FIELD_VALUE);
    RealmResults<PrimaryKeyAsString> result = realm.where(PrimaryKeyAsString.class).findAll();
    assertEquals(1, result.size());
    assertEquals(null, result.first().getName());
    assertEquals(SECONDARY_FIELD_VALUE, result.first().getId());
    // Updates objects.
    realm.beginTransaction();
    nullPrimaryKeyObj.setId(SECONDARY_FIELD_UPDATED);
    realm.copyToRealmOrUpdate(nullPrimaryKeyObj);
    realm.commitTransaction();
    assertEquals(SECONDARY_FIELD_UPDATED, realm.where(PrimaryKeyAsString.class).findFirst().getId());
}
Also used : PrimaryKeyAsString(io.realm.entities.PrimaryKeyAsString) Test(org.junit.Test)

Example 5 with PrimaryKeyAsString

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

the class BulkInsertTests method insert_nullPrimaryKey.

@Test
public void insert_nullPrimaryKey() {
    PrimaryKeyAsString primaryKeyAsString = new PrimaryKeyAsString();
    primaryKeyAsString.setId(19);
    realm.beginTransaction();
    realm.insertOrUpdate(primaryKeyAsString);
    realm.commitTransaction();
    primaryKeyAsString = realm.where(PrimaryKeyAsString.class).isNull("name").findFirst();
    assertNotNull(primaryKeyAsString);
    assertNull(primaryKeyAsString.getName());
    assertEquals(19, primaryKeyAsString.getId());
    PrimaryKeyAsBoxedShort primaryKeyAsShort = new PrimaryKeyAsBoxedShort();
    primaryKeyAsShort.setName("42");
    realm.beginTransaction();
    realm.insertOrUpdate(primaryKeyAsShort);
    realm.commitTransaction();
    primaryKeyAsShort = realm.where(PrimaryKeyAsBoxedShort.class).isNull("id").findFirst();
    assertNotNull(primaryKeyAsShort);
    assertNull(primaryKeyAsShort.getId());
    assertEquals("42", primaryKeyAsShort.getName());
}
Also used : PrimaryKeyAsBoxedShort(io.realm.entities.PrimaryKeyAsBoxedShort) PrimaryKeyAsString(io.realm.entities.PrimaryKeyAsString) Test(org.junit.Test)

Aggregations

PrimaryKeyAsString (io.realm.entities.PrimaryKeyAsString)11 Test (org.junit.Test)9 PrimaryKeyAsBoxedShort (io.realm.entities.PrimaryKeyAsBoxedShort)3 JSONObject (org.json.JSONObject)3 PrimaryKeyAsBoxedByte (io.realm.entities.PrimaryKeyAsBoxedByte)2 PrimaryKeyAsBoxedInteger (io.realm.entities.PrimaryKeyAsBoxedInteger)2 PrimaryKeyAsBoxedLong (io.realm.entities.PrimaryKeyAsBoxedLong)2 Table (io.realm.internal.Table)2 PrimaryKeyRequiredAsString (io.realm.entities.PrimaryKeyRequiredAsString)1 RealmPrimaryKeyConstraintException (io.realm.exceptions.RealmPrimaryKeyConstraintException)1