Search in sources :

Example 6 with PrimaryKeyAsString

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

the class RealmJsonNullPrimaryKeyTests method createOrUpdateObjectFromJson_primaryKey_isNull_updateFromJsonObject.

// Tests null primary key value for createObject() -> createOrUpdateObjectFromJson().
@Test
public void createOrUpdateObjectFromJson_primaryKey_isNull_updateFromJsonObject() throws JSONException {
    realm.beginTransaction();
    // name = null, id =null
    realm.createObject(clazz, null);
    realm.createOrUpdateObjectFromJson(clazz, new JSONObject(jsonString));
    realm.commitTransaction();
    // PrimaryKeyAsString
    if (clazz.equals(PrimaryKeyAsString.class)) {
        RealmResults<PrimaryKeyAsString> results = realm.where(PrimaryKeyAsString.class).findAll();
        assertEquals(1, results.size());
        assertEquals(Long.valueOf(secondaryFieldValue).longValue(), results.first().getId());
        assertEquals(null, results.first().getName());
    // PrimaryKeyAsNumber
    } else {
        RealmResults results = realm.where(clazz).findAll();
        assertEquals(1, results.size());
        assertEquals(null, ((NullPrimaryKey) results.first()).getId());
        assertEquals(secondaryFieldValue, ((NullPrimaryKey) results.first()).getName());
    }
}
Also used : JSONObject(org.json.JSONObject) PrimaryKeyAsString(io.realm.entities.PrimaryKeyAsString) Test(org.junit.Test)

Example 7 with PrimaryKeyAsString

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

the class RealmJsonNullPrimaryKeyTests method createOrUpdateObjectFromJson_primaryKey_isNull_fromJsonObject.

// Tests null primary key value for createOrUpdateObjectFromJson().
@Test
public void createOrUpdateObjectFromJson_primaryKey_isNull_fromJsonObject() throws JSONException {
    realm.beginTransaction();
    realm.createOrUpdateObjectFromJson(clazz, new JSONObject(jsonString));
    realm.commitTransaction();
    // PrimaryKeyAsString
    if (clazz.equals(PrimaryKeyAsString.class)) {
        RealmResults<PrimaryKeyAsString> results = realm.where(PrimaryKeyAsString.class).findAll();
        assertEquals(1, results.size());
        assertEquals(Long.valueOf(secondaryFieldValue).longValue(), results.first().getId());
        assertEquals(null, results.first().getName());
    // PrimaryKeyAsNumber
    } else {
        RealmResults results = realm.where(clazz).findAll();
        assertEquals(1, results.size());
        assertEquals(null, ((NullPrimaryKey) results.first()).getId());
        assertEquals(secondaryFieldValue, ((NullPrimaryKey) results.first()).getName());
    }
}
Also used : JSONObject(org.json.JSONObject) PrimaryKeyAsString(io.realm.entities.PrimaryKeyAsString) Test(org.junit.Test)

Example 8 with PrimaryKeyAsString

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

the class RealmJsonNullPrimaryKeyTests method createObjectFromJson_primaryKey_isNull_fromJsonObject.

// Tests null primary key value for createObjectFromJson().
@Test
public void createObjectFromJson_primaryKey_isNull_fromJsonObject() throws JSONException {
    realm.beginTransaction();
    realm.createObjectFromJson(clazz, new JSONObject(jsonString));
    realm.commitTransaction();
    // PrimaryKeyAsString
    if (clazz.equals(PrimaryKeyAsString.class)) {
        RealmResults<PrimaryKeyAsString> results = realm.where(PrimaryKeyAsString.class).findAll();
        assertEquals(1, results.size());
        assertEquals(Long.valueOf(secondaryFieldValue).longValue(), results.first().getId());
        assertEquals(null, results.first().getName());
    // PrimaryKeyAsNumber
    } else {
        RealmResults results = realm.where(clazz).findAll();
        assertEquals(1, results.size());
        assertEquals(null, ((NullPrimaryKey) results.first()).getId());
        assertEquals(secondaryFieldValue, ((NullPrimaryKey) results.first()).getName());
    }
}
Also used : JSONObject(org.json.JSONObject) PrimaryKeyAsString(io.realm.entities.PrimaryKeyAsString) Test(org.junit.Test)

Example 9 with PrimaryKeyAsString

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

the class RealmAnnotationTests method primaryKey_migration_long.

// Tests migrating primary key from string to long with existing data.
@Test
public void primaryKey_migration_long() {
    realm.beginTransaction();
    for (int i = 1; i <= 2; i++) {
        PrimaryKeyAsString obj = realm.createObject(PrimaryKeyAsString.class, "String" + i);
        obj.setId(i);
    }
    Table table = realm.getTable(PrimaryKeyAsString.class);
    table.setPrimaryKey("id");
    assertEquals(1, table.getPrimaryKey());
    realm.cancelTransaction();
}
Also used : Table(io.realm.internal.Table) PrimaryKeyAsString(io.realm.entities.PrimaryKeyAsString) Test(org.junit.Test)

Example 10 with PrimaryKeyAsString

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

the class RealmTests method createObjectWithPrimaryKey_null.

@Test
public void createObjectWithPrimaryKey_null() {
    // Byte
    realm.beginTransaction();
    PrimaryKeyAsBoxedByte primaryKeyAsBoxedByte = realm.createObject(PrimaryKeyAsBoxedByte.class, null);
    realm.commitTransaction();
    assertEquals(1, realm.where(PrimaryKeyAsBoxedByte.class).count());
    assertNull(primaryKeyAsBoxedByte.getId());
    // Short
    realm.beginTransaction();
    PrimaryKeyAsBoxedShort primaryKeyAsBoxedShort = realm.createObject(PrimaryKeyAsBoxedShort.class, null);
    realm.commitTransaction();
    assertEquals(1, realm.where(PrimaryKeyAsBoxedShort.class).count());
    assertNull(primaryKeyAsBoxedShort.getId());
    // Integer
    realm.beginTransaction();
    PrimaryKeyAsBoxedInteger primaryKeyAsBoxedInteger = realm.createObject(PrimaryKeyAsBoxedInteger.class, null);
    realm.commitTransaction();
    assertEquals(1, realm.where(PrimaryKeyAsBoxedInteger.class).count());
    assertNull(primaryKeyAsBoxedInteger.getId());
    // Long
    realm.beginTransaction();
    PrimaryKeyAsBoxedLong primaryKeyAsBoxedLong = realm.createObject(PrimaryKeyAsBoxedLong.class, null);
    realm.commitTransaction();
    assertEquals(1, realm.where(PrimaryKeyAsBoxedLong.class).count());
    assertNull(primaryKeyAsBoxedLong.getId());
    // String
    realm.beginTransaction();
    PrimaryKeyAsString primaryKeyAsString = realm.createObject(PrimaryKeyAsString.class, null);
    realm.commitTransaction();
    assertEquals(1, realm.where(PrimaryKeyAsString.class).count());
    assertNull(primaryKeyAsString.getName());
}
Also used : PrimaryKeyAsBoxedShort(io.realm.entities.PrimaryKeyAsBoxedShort) PrimaryKeyAsBoxedByte(io.realm.entities.PrimaryKeyAsBoxedByte) PrimaryKeyAsBoxedInteger(io.realm.entities.PrimaryKeyAsBoxedInteger) PrimaryKeyAsString(io.realm.entities.PrimaryKeyAsString) PrimaryKeyAsBoxedLong(io.realm.entities.PrimaryKeyAsBoxedLong) 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