don't require optional or link properties when creating objects

This commit is contained in:
Ari Lazier 2016-04-01 11:00:53 -07:00
parent 34ece60e69
commit 4ac022ecc9
2 changed files with 31 additions and 7 deletions

View File

@ -208,10 +208,12 @@ namespace realm {
case PropertyTypeArray: {
realm::LinkViewRef link_view = m_row.get_linklist(column);
link_view->clear();
size_t count = Accessor::list_size(ctx, value);
for (size_t i = 0; i < count; i++) {
ValueType element = Accessor::list_value_at_index(ctx, value, i);
link_view->add(Accessor::to_object_index(ctx, m_realm, element, property.object_type, try_update));
if (!Accessor::is_null(ctx, value)) {
size_t count = Accessor::list_size(ctx, value);
for (size_t i = 0; i < count; i++) {
ValueType element = Accessor::list_value_at_index(ctx, value, i);
link_view->add(Accessor::to_object_index(ctx, m_realm, element, property.object_type, try_update));
}
}
break;
}
@ -310,6 +312,9 @@ namespace realm {
if (Accessor::has_default_value_for_property(ctx, realm.get(), object_schema, prop.name)) {
object.set_property_value_impl(ctx, prop, Accessor::default_value_for_property(ctx, realm.get(), object_schema, prop.name), try_update);
}
else if (prop.is_nullable || prop.type == PropertyTypeArray) {
object.set_property_value_impl(ctx, prop, Accessor::null_value(ctx), try_update);
}
else {
throw MissingPropertyValueException(object_schema.name, prop.name,
"Missing property value for property " + prop.name);

View File

@ -163,7 +163,7 @@ module.exports = BaseTest.extend({
},
testRealmCreate: function() {
var realm = new Realm({schema: [schemas.IntPrimary, schemas.AllTypes, schemas.TestObject]});
var realm = new Realm({schema: [schemas.TestObject]});
TestCase.assertThrows(function() {
realm.create('TestObject', {doubleCol: 1});
@ -178,8 +178,11 @@ module.exports = BaseTest.extend({
TestCase.assertEqual(objects.length, 2, 'wrong object count');
TestCase.assertEqual(objects[0].doubleCol, 1, 'wrong object property value');
TestCase.assertEqual(objects[1].doubleCol, 2, 'wrong object property value');
},
testRealmCreatePrimaryKey: function() {
var realm = new Realm({schema: [schemas.IntPrimary]});
// test int primary object
realm.write(function() {
var obj0 = realm.create('IntPrimaryObject', {
primaryCol: 0,
@ -212,8 +215,24 @@ module.exports = BaseTest.extend({
realm.create('IntPrimaryObject', {primaryCol: 0}, true);
TestCase.assertEqual(obj0.valueCol, 'newVal0');
});
},
// test upsert with all type and string primary object
testRealmCreateOptionals: function() {
var realm = new Realm({schema: [schemas.NullableBasicTypes, schemas.LinkTypes, schemas.TestObject]});
var basic, links;
realm.write(function() {
basic = realm.create('NullableBasicTypesObject', {});
links = realm.create('LinkTypesObject', {});
});
for (var name in schemas.NullableBasicTypes.properties) {
TestCase.assertEqual(basic[name], null);
}
TestCase.assertEqual(links.objectCol, null);
TestCase.assertEqual(links.arrayCol.length, 0);
},
testRealmCreateUpsert: function() {
var realm = new Realm({schema: [schemas.IntPrimary, schemas.AllTypes, schemas.TestObject]});
realm.write(function() {
var values = {
primaryCol: '0',