use of io.realm.entities.NullTypes in project realm-java by realm.
the class TestHelper method populatePartialNullRowsForNumericTesting.
public static void populatePartialNullRowsForNumericTesting(Realm realm) {
// Id values are [1, 2, 3]
// IntegerNull values are [3, null, 4]
// FloatNull values are [4F, null, 5F]
// DoubleNull values are [5D, null, 6F]
NullTypes nullTypes1 = new NullTypes();
nullTypes1.setId(1);
nullTypes1.setFieldIntegerNull(3);
nullTypes1.setFieldFloatNull(4F);
nullTypes1.setFieldDoubleNull(5D);
nullTypes1.setFieldBooleanNull(true);
nullTypes1.setFieldStringNull("4");
nullTypes1.setFieldDateNull(new Date(12345));
NullTypes nullTypes2 = new NullTypes();
nullTypes2.setId(2);
NullTypes nullTypes3 = new NullTypes();
nullTypes3.setId(3);
nullTypes3.setFieldIntegerNull(4);
nullTypes3.setFieldFloatNull(5F);
nullTypes3.setFieldDoubleNull(6D);
nullTypes3.setFieldBooleanNull(false);
nullTypes3.setFieldStringNull("0");
nullTypes3.setFieldDateNull(new Date(0));
realm.beginTransaction();
realm.copyToRealm(nullTypes1);
realm.copyToRealm(nullTypes2);
realm.copyToRealm(nullTypes3);
realm.commitTransaction();
}
use of io.realm.entities.NullTypes in project realm-java by realm.
the class TestHelper method populateAllNonNullRowsForNumericTesting.
public static void populateAllNonNullRowsForNumericTesting(Realm realm) {
NullTypes nullTypes1 = new NullTypes();
nullTypes1.setId(1);
nullTypes1.setFieldIntegerNull(3);
nullTypes1.setFieldFloatNull(4F);
nullTypes1.setFieldDoubleNull(5D);
nullTypes1.setFieldBooleanNull(true);
nullTypes1.setFieldStringNull("4");
nullTypes1.setFieldDateNull(new Date(12345));
NullTypes nullTypes2 = new NullTypes();
nullTypes2.setId(2);
nullTypes2.setFieldIntegerNull(-1);
nullTypes2.setFieldFloatNull(-2F);
nullTypes2.setFieldDoubleNull(-3D);
nullTypes2.setFieldBooleanNull(false);
nullTypes2.setFieldStringNull("str");
nullTypes2.setFieldDateNull(new Date(-2000));
NullTypes nullTypes3 = new NullTypes();
nullTypes3.setId(3);
nullTypes3.setFieldIntegerNull(4);
nullTypes3.setFieldFloatNull(5F);
nullTypes3.setFieldDoubleNull(6D);
nullTypes3.setFieldBooleanNull(false);
nullTypes3.setFieldStringNull("0");
nullTypes3.setFieldDateNull(new Date(0));
realm.beginTransaction();
realm.copyToRealm(nullTypes1);
realm.copyToRealm(nullTypes2);
realm.copyToRealm(nullTypes3);
realm.commitTransaction();
}
use of io.realm.entities.NullTypes in project realm-java by realm.
the class ManagedRealmCollectionTests method createEmptyCollection.
private OrderedRealmCollection<NullTypes> createEmptyCollection(Realm realm, ManagedCollection collectionClass) {
OrderedRealmCollection<NullTypes> orderedCollection;
switch(collectionClass) {
case REALMRESULTS_SNAPSHOT_LIST_BASE:
case MANAGED_REALMLIST:
realm.beginTransaction();
NullTypes obj = realm.createObject(NullTypes.class, 0);
realm.commitTransaction();
orderedCollection = obj.getFieldListNull();
break;
case REALMRESULTS_SNAPSHOT_RESULTS_BASE:
case REALMRESULTS:
orderedCollection = realm.where(NullTypes.class).findAll();
break;
default:
throw new AssertionError("Unknown collection: " + collectionClass);
}
if (isSnapshot(collectionClass)) {
orderedCollection = orderedCollection.createSnapshot();
}
return orderedCollection;
}
use of io.realm.entities.NullTypes in project realm-java by realm.
the class RealmJsonTests method createObjectFromJson_updateNullTypesJSONWithNulls.
/**
* Tests a nullable field already has a non-null value, update it through JSON with null value
* of the corresponding field.
*/
@Test
public void createObjectFromJson_updateNullTypesJSONWithNulls() throws IOException, JSONException {
String json = TestHelper.streamToString(TestHelper.loadJsonFromAssets(context, "nulltypes.json"));
// Nullable fields with values
JSONArray jsonArray = new JSONArray(json);
JSONObject jsonObject = jsonArray.getJSONObject(1);
jsonObject.put("id", 1);
// Now object with id 1 has values for all nullable fields.
realm.beginTransaction();
realm.createObjectFromJson(NullTypes.class, jsonObject);
realm.commitTransaction();
RealmResults<NullTypes> nullTypesRealmResults = realm.where(NullTypes.class).findAll();
assertEquals(2, nullTypesRealmResults.size());
checkNullableValuesAreNotNull(nullTypesRealmResults.where().equalTo("id", 1).findFirst());
// Updates object with id 1, nullable fields should have null values.
JSONArray array = new JSONArray(json);
realm.beginTransaction();
realm.createOrUpdateAllFromJson(NullTypes.class, array);
realm.commitTransaction();
nullTypesRealmResults = realm.where(NullTypes.class).findAll();
assertEquals(3, nullTypesRealmResults.size());
NullTypes nullTypes1 = nullTypesRealmResults.where().equalTo("id", 1).findFirst();
checkNullableValuesAreNull(nullTypes1);
}
use of io.realm.entities.NullTypes in project realm-java by realm.
the class LinkingObjectsQueryTests method populateTestRealmForNullTests.
// Similar to the version in TestHelper, but with more Backlinks
// Creates 3 NullTypes objects. The objects are self-referenced (link) in
// order to test link queries.
//
// +-+--------+------+---------+--------+--------------------+----------+
// | | string | link | numeric | binary | numeric (not null) | linklist |
// +-+--------+------+---------+--------+--------------------+----------+
// |0| Fish | 0 | 1 | {0} | 1 | [0] |
// |1| null | 2 | null | null | 0 | [2] |
// |2| Horse | null | 3 | {1,2} | 3 | null |
// +-+--------+------+---------+--------+--------------------+----------+
private void populateTestRealmForNullTests(Realm testRealm) {
// 1 String
String[] words = { "Fish", null, "Horse" };
// 2 Bytes
byte[][] binaries = { new byte[] { 0 }, null, new byte[] { 1, 2 } };
// 3 Boolean
Boolean[] booleans = { false, null, true };
// Numeric fields will be 1, 0/null, 3
// 10 Date
Date[] dates = { new Date(0), null, new Date(10000) };
NullTypes[] nullTypesArray = new NullTypes[3];
Decimal128[] decimals = { new Decimal128(BigDecimal.TEN), null, new Decimal128(BigDecimal.ONE) };
ObjectId[] ids = { new ObjectId(TestHelper.generateObjectIdHexString(10)), null, new ObjectId(TestHelper.generateObjectIdHexString(1)) };
testRealm.beginTransaction();
for (int i = 0; i < 3; i++) {
NullTypes nullTypes = new NullTypes();
nullTypes.setId(i + 1);
// 1 String
nullTypes.setFieldStringNull(words[i]);
if (words[i] != null) {
nullTypes.setFieldStringNotNull(words[i]);
}
// 2 Bytes
nullTypes.setFieldBytesNull(binaries[i]);
if (binaries[i] != null) {
nullTypes.setFieldBytesNotNull(binaries[i]);
}
// 3 Boolean
nullTypes.setFieldBooleanNull(booleans[i]);
if (booleans[i] != null) {
nullTypes.setFieldBooleanNotNull(booleans[i]);
}
if (i != 1) {
int n = i + 1;
// 4 Byte
nullTypes.setFieldByteNull((byte) n);
nullTypes.setFieldByteNotNull((byte) n);
// 5 Short
nullTypes.setFieldShortNull((short) n);
nullTypes.setFieldShortNotNull((short) n);
// 6 Integer
nullTypes.setFieldIntegerNull(n);
nullTypes.setFieldIntegerNotNull(n);
// 7 Long
nullTypes.setFieldLongNull((long) n);
nullTypes.setFieldLongNotNull((long) n);
// 8 Float
nullTypes.setFieldFloatNull((float) n);
nullTypes.setFieldFloatNotNull((float) n);
// 9 Double
nullTypes.setFieldDoubleNull((double) n);
nullTypes.setFieldDoubleNotNull((double) n);
}
// 10 Date
nullTypes.setFieldDateNull(dates[i]);
if (dates[i] != null) {
nullTypes.setFieldDateNotNull(dates[i]);
}
nullTypes.setFieldDecimal128Null(decimals[i]);
nullTypes.setFieldObjectIdNull(ids[i]);
nullTypesArray[i] = testRealm.copyToRealm(nullTypes);
}
nullTypesArray[0].setFieldObjectNull(nullTypesArray[0]);
nullTypesArray[1].setFieldObjectNull(nullTypesArray[2]);
nullTypesArray[2].setFieldObjectNull(null);
nullTypesArray[0].getFieldListNull().add(nullTypesArray[1]);
nullTypesArray[1].getFieldListNull().add(nullTypesArray[2]);
// just to be sure
nullTypesArray[2].getFieldListNull().clear();
testRealm.commitTransaction();
}
Aggregations