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());
}
}
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());
}
}
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());
}
}
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();
}
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());
}
Aggregations