From 4ac022ecc9d3bd05c3b8e541fdc46a37bd2ee1c2 Mon Sep 17 00:00:00 2001 From: Ari Lazier Date: Fri, 1 Apr 2016 11:00:53 -0700 Subject: [PATCH] don't require optional or link properties when creating objects --- src/object-store/src/object_accessor.hpp | 13 ++++++++---- tests/js/realm-tests.js | 25 +++++++++++++++++++++--- 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/src/object-store/src/object_accessor.hpp b/src/object-store/src/object_accessor.hpp index a97568dd..61ef0a1d 100644 --- a/src/object-store/src/object_accessor.hpp +++ b/src/object-store/src/object_accessor.hpp @@ -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); diff --git a/tests/js/realm-tests.js b/tests/js/realm-tests.js index f201c601..3e6aec72 100644 --- a/tests/js/realm-tests.js +++ b/tests/js/realm-tests.js @@ -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',