Search in sources :

Example 1 with NullPrimaryKey

use of io.realm.objectid.NullPrimaryKey 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());
    // PrimaryKeyAsUUID
    } else if (clazz.equals(PrimaryKeyAsUUID.class)) {
        RealmResults<PrimaryKeyAsUUID> results = realm.where(PrimaryKeyAsUUID.class).findAll();
        assertEquals(1, results.size());
        assertEquals(null, results.first().getId());
        assertEquals(secondaryFieldValue, results.first().getName());
    // PrimaryKeyAsNumber
    } else {
        // Other types
        RealmResults results = realm.where(clazz).findAll();
        assertEquals(1, results.size());
        assertEquals(null, ((NullPrimaryKey) results.first()).getId());
        assertEquals(secondaryFieldValue, ((NullPrimaryKey) results.first()).getName());
    }
}
Also used : NullPrimaryKey(io.realm.objectid.NullPrimaryKey) JSONObject(org.json.JSONObject) PrimaryKeyAsString(io.realm.entities.PrimaryKeyAsString) PrimaryKeyAsUUID(io.realm.entities.PrimaryKeyAsUUID) Test(org.junit.Test)

Example 2 with NullPrimaryKey

use of io.realm.objectid.NullPrimaryKey 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());
        assertNull(results.first().getName());
    // PrimaryKeyAsObjectId
    } else if (clazz.equals(PrimaryKeyAsUUID.class)) {
        RealmResults<PrimaryKeyAsUUID> results = realm.where(PrimaryKeyAsUUID.class).findAll();
        assertEquals(1, results.size());
        assertEquals(null, results.first().getId());
        assertEquals(secondaryFieldValue, results.first().getName());
    // PrimaryKeyAsNumber
    } else {
        // Other types
        RealmResults results = realm.where(clazz).findAll();
        assertEquals(1, results.size());
        assertNull(((NullPrimaryKey) results.first()).getId());
        assertEquals(secondaryFieldValue, ((NullPrimaryKey) results.first()).getName());
    }
}
Also used : NullPrimaryKey(io.realm.objectid.NullPrimaryKey) JSONObject(org.json.JSONObject) PrimaryKeyAsString(io.realm.entities.PrimaryKeyAsString) PrimaryKeyAsUUID(io.realm.entities.PrimaryKeyAsUUID) Test(org.junit.Test)

Example 3 with NullPrimaryKey

use of io.realm.objectid.NullPrimaryKey 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());
    // PrimaryKeyAsUUID
    } else if (clazz.equals(PrimaryKeyAsUUID.class)) {
        RealmResults<PrimaryKeyAsUUID> results = realm.where(PrimaryKeyAsUUID.class).findAll();
        assertEquals(1, results.size());
        assertEquals(null, results.first().getId());
        assertEquals(secondaryFieldValue, results.first().getName());
    // PrimaryKeyAsNumber
    } else {
        // Other types
        RealmResults results = realm.where(clazz).findAll();
        assertEquals(1, results.size());
        assertEquals(null, ((NullPrimaryKey) results.first()).getId());
        assertEquals(secondaryFieldValue, ((NullPrimaryKey) results.first()).getName());
    }
}
Also used : NullPrimaryKey(io.realm.objectid.NullPrimaryKey) JSONObject(org.json.JSONObject) PrimaryKeyAsString(io.realm.entities.PrimaryKeyAsString) PrimaryKeyAsUUID(io.realm.entities.PrimaryKeyAsUUID) Test(org.junit.Test)

Aggregations

PrimaryKeyAsString (io.realm.entities.PrimaryKeyAsString)3 PrimaryKeyAsUUID (io.realm.entities.PrimaryKeyAsUUID)3 NullPrimaryKey (io.realm.objectid.NullPrimaryKey)3 JSONObject (org.json.JSONObject)3 Test (org.junit.Test)3