Setting properties outside transaction should throw

Resolves #40
This commit is contained in:
Scott Kyle 2015-10-12 02:02:23 -07:00
parent 37d5a12094
commit 33e0f5d8b1
3 changed files with 15 additions and 1 deletions

View File

@ -79,7 +79,7 @@ bool ObjectSetProperty(JSContextRef ctx, JSObjectRef jsObject, JSStringRef jsPro
Object *obj = RJSGetInternal<Object *>(jsObject); Object *obj = RJSGetInternal<Object *>(jsObject);
obj->set_property_value(ctx, RJSStringForJSString(jsPropertyName), value, true); obj->set_property_value(ctx, RJSStringForJSString(jsPropertyName), value, true);
} catch (std::exception &ex) { } catch (std::exception &ex) {
if (*exception) { if (exception) {
*exception = RJSMakeError(ctx, ex); *exception = RJSMakeError(ctx, ex);
} }
return false; return false;

View File

@ -98,6 +98,10 @@ namespace realm {
{ {
using Accessor = NativeAccessor<ValueType, ContextType>; using Accessor = NativeAccessor<ValueType, ContextType>;
if (!realm->is_in_transaction()) {
throw std::runtime_error("Can only set property values within a transaction.");
}
size_t column = property.table_column; size_t column = property.table_column;
switch (property.type) { switch (property.type) {
case PropertyTypeBool: case PropertyTypeBool:

View File

@ -44,6 +44,7 @@ var ObjectTests = {
var basicTypesValues = [true, 1, 1.1, 1.11, 'string', new Date(1), 'DATA']; var basicTypesValues = [true, 1, 1.1, 1.11, 'string', new Date(1), 'DATA'];
var realm = new Realm({schema: [BasicTypesObjectSchema]}); var realm = new Realm({schema: [BasicTypesObjectSchema]});
var obj = null; var obj = null;
realm.write(function() { realm.write(function() {
obj = realm.create('BasicTypesObject', basicTypesValues); obj = realm.create('BasicTypesObject', basicTypesValues);
obj.boolCol = false; obj.boolCol = false;
@ -54,6 +55,11 @@ var ObjectTests = {
obj.dateCol = new Date(2); obj.dateCol = new Date(2);
obj.dataCol = 'b'; obj.dataCol = 'b';
}); });
TestCase.assertThrows(function() {
obj.boolCol = true;
}, 'can only set property values in a write transaction');
TestCase.assertEqual(obj.boolCol, false, 'wrong bool value'); TestCase.assertEqual(obj.boolCol, false, 'wrong bool value');
TestCase.assertEqual(obj.intCol, 2, 'wrong int value'); TestCase.assertEqual(obj.intCol, 2, 'wrong int value');
TestCase.assertEqualWithTolerance(obj.floatCol, 2.2, 0.000001, 'wrong float value'); TestCase.assertEqualWithTolerance(obj.floatCol, 2.2, 0.000001, 'wrong float value');
@ -90,6 +96,10 @@ var ObjectTests = {
}); });
TestCase.assertEqual(realm.objects('TestObject').length, 2); TestCase.assertEqual(realm.objects('TestObject').length, 2);
TestCase.assertThrows(function() {
obj.objectCol1 = obj.objectCol;
}, 'can only set property values in a write transaction');
// set/reuse object property // set/reuse object property
realm.write(function() { realm.write(function() {
obj.objectCol1 = obj.objectCol; obj.objectCol1 = obj.objectCol;