mirror of
https://github.com/status-im/realm-js.git
synced 2025-01-11 23:04:29 +00:00
Merge pull request #43 from realm/sk-transaction-fail
Throw exceptions when not inside a transaction
This commit is contained in:
commit
f5a1c3fdb3
@ -49,6 +49,14 @@ static inline ObjectArray * RJSVerifiedArray(JSObjectRef object) {
|
||||
return array;
|
||||
}
|
||||
|
||||
static inline ObjectArray * RJSVerifiedMutableArray(JSObjectRef object) {
|
||||
ObjectArray *array = RJSVerifiedArray(object);
|
||||
if (!array->realm->is_in_transaction()) {
|
||||
throw std::runtime_error("Can only mutate lists within a transaction.");
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
JSValueRef ArrayGetProperty(JSContextRef ctx, JSObjectRef object, JSStringRef propertyName, JSValueRef* jsException) {
|
||||
try {
|
||||
// index subscripting
|
||||
@ -89,7 +97,7 @@ void ArrayPropertyNames(JSContextRef ctx, JSObjectRef object, JSPropertyNameAccu
|
||||
|
||||
JSValueRef ArrayPush(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* jsException) {
|
||||
try {
|
||||
ObjectArray *array = RJSVerifiedArray(thisObject);
|
||||
ObjectArray *array = RJSVerifiedMutableArray(thisObject);
|
||||
RJSValidateArgumentCountIsAtLeast(argumentCount, 1);
|
||||
for (size_t i = 0; i < argumentCount; i++) {
|
||||
array->link_view->add(RJSAccessor::to_object_index(ctx, array->realm, const_cast<JSValueRef &>(arguments[i]), array->object_schema.name, false));
|
||||
@ -106,7 +114,7 @@ JSValueRef ArrayPush(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObj
|
||||
|
||||
JSValueRef ArrayPop(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* jsException) {
|
||||
try {
|
||||
ObjectArray *array = RJSVerifiedArray(thisObject);
|
||||
ObjectArray *array = RJSVerifiedMutableArray(thisObject);
|
||||
RJSValidateArgumentCount(argumentCount, 0);
|
||||
|
||||
size_t size = array->size();
|
||||
@ -128,7 +136,7 @@ JSValueRef ArrayPop(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObje
|
||||
|
||||
JSValueRef ArrayUnshift(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* jsException) {
|
||||
try {
|
||||
ObjectArray *array = RJSVerifiedArray(thisObject);
|
||||
ObjectArray *array = RJSVerifiedMutableArray(thisObject);
|
||||
RJSValidateArgumentCountIsAtLeast(argumentCount, 1);
|
||||
for (size_t i = 0; i < argumentCount; i++) {
|
||||
array->link_view->insert(i, RJSAccessor::to_object_index(ctx, array->realm, const_cast<JSValueRef &>(arguments[i]), array->object_schema.name, false));
|
||||
@ -145,7 +153,7 @@ JSValueRef ArrayUnshift(JSContextRef ctx, JSObjectRef function, JSObjectRef this
|
||||
|
||||
JSValueRef ArrayShift(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* jsException) {
|
||||
try {
|
||||
ObjectArray *array = RJSVerifiedArray(thisObject);
|
||||
ObjectArray *array = RJSVerifiedMutableArray(thisObject);
|
||||
RJSValidateArgumentCount(argumentCount, 0);
|
||||
if (array->size() == 0) {
|
||||
return JSValueMakeUndefined(ctx);
|
||||
@ -164,7 +172,7 @@ JSValueRef ArrayShift(JSContextRef ctx, JSObjectRef function, JSObjectRef thisOb
|
||||
|
||||
JSValueRef ArraySplice(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* jsException) {
|
||||
try {
|
||||
ObjectArray *array = RJSVerifiedArray(thisObject);
|
||||
ObjectArray *array = RJSVerifiedMutableArray(thisObject);
|
||||
size_t size = array->size();
|
||||
|
||||
RJSValidateArgumentCountIsAtLeast(argumentCount, 2);
|
||||
|
@ -79,7 +79,7 @@ bool ObjectSetProperty(JSContextRef ctx, JSObjectRef jsObject, JSStringRef jsPro
|
||||
Object *obj = RJSGetInternal<Object *>(jsObject);
|
||||
obj->set_property_value(ctx, RJSStringForJSString(jsPropertyName), value, true);
|
||||
} catch (std::exception &ex) {
|
||||
if (*exception) {
|
||||
if (exception) {
|
||||
*exception = RJSMakeError(ctx, ex);
|
||||
}
|
||||
return false;
|
||||
|
@ -98,6 +98,10 @@ namespace realm {
|
||||
{
|
||||
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;
|
||||
switch (property.type) {
|
||||
case PropertyTypeBool:
|
||||
|
@ -108,6 +108,7 @@ var ArrayTests = {
|
||||
testPush: function() {
|
||||
var realm = new Realm({schema: [LinkTypesObjectSchema, TestObjectSchema]});
|
||||
var array;
|
||||
|
||||
realm.write(function() {
|
||||
var obj = realm.create('LinkTypesObject', [[1], [2], [[3]]]);
|
||||
TestCase.assertEqual(obj.arrayCol.length, 1);
|
||||
@ -128,14 +129,15 @@ var ArrayTests = {
|
||||
});
|
||||
|
||||
TestCase.assertEqual(array.length, 4);
|
||||
// TestCase.assertThrows(function() {
|
||||
// array.push([1]);
|
||||
// });
|
||||
TestCase.assertThrows(function() {
|
||||
array.push([1]);
|
||||
}, 'can only push in a write transaction');
|
||||
},
|
||||
|
||||
testPop: function() {
|
||||
var realm = new Realm({schema: [LinkTypesObjectSchema, TestObjectSchema]});
|
||||
var array;
|
||||
|
||||
realm.write(function() {
|
||||
var obj = realm.create('LinkTypesObject', [[1], [2], [[3], [4]]]);
|
||||
array = obj.arrayCol;
|
||||
@ -151,14 +153,15 @@ var ArrayTests = {
|
||||
});
|
||||
});
|
||||
|
||||
// TestCase.assertThrows(function() {
|
||||
// array.pop();
|
||||
// });
|
||||
TestCase.assertThrows(function() {
|
||||
array.pop();
|
||||
}, 'can only pop in a write transaction');
|
||||
},
|
||||
|
||||
testUnshift: function() {
|
||||
var realm = new Realm({schema: [LinkTypesObjectSchema, TestObjectSchema]});
|
||||
var array;
|
||||
|
||||
realm.write(function() {
|
||||
var obj = realm.create('LinkTypesObject', [[1], [2], [[3]]]);
|
||||
TestCase.assertEqual(obj.arrayCol.length, 1);
|
||||
@ -175,14 +178,15 @@ var ArrayTests = {
|
||||
});
|
||||
|
||||
TestCase.assertEqual(array.length, 4);
|
||||
// TestCase.assertThrows(function() {
|
||||
// array.unshift([1]);
|
||||
// });
|
||||
TestCase.assertThrows(function() {
|
||||
array.unshift([1]);
|
||||
}, 'can only unshift in a write transaction');
|
||||
},
|
||||
|
||||
testShift: function() {
|
||||
var realm = new Realm({schema: [LinkTypesObjectSchema, TestObjectSchema]});
|
||||
var array;
|
||||
|
||||
realm.write(function() {
|
||||
var obj = realm.create('LinkTypesObject', [[1], [2], [[3], [4]]]);
|
||||
array = obj.arrayCol;
|
||||
@ -198,19 +202,21 @@ var ArrayTests = {
|
||||
});
|
||||
});
|
||||
|
||||
// TestCase.assertThrows(function() {
|
||||
// array.shift();
|
||||
// });
|
||||
TestCase.assertThrows(function() {
|
||||
array.shift();
|
||||
}, 'can only shift in a write transaction');
|
||||
},
|
||||
|
||||
testSplice: function() {
|
||||
var realm = new Realm({schema: [LinkTypesObjectSchema, TestObjectSchema]});
|
||||
var array;
|
||||
|
||||
realm.write(function() {
|
||||
var obj = realm.create('LinkTypesObject', [[1], [2], [[3], [4]]]);
|
||||
var array = obj.arrayCol;
|
||||
var removed;
|
||||
|
||||
array = obj.arrayCol;
|
||||
|
||||
removed = array.splice(0, 0, obj.objectCol, obj.objectCol1);
|
||||
TestCase.assertEqual(removed.length, 0);
|
||||
TestCase.assertEqual(array.length, 4);
|
||||
@ -248,5 +254,9 @@ var ArrayTests = {
|
||||
array.splice(0, 0, 0);
|
||||
});
|
||||
});
|
||||
|
||||
TestCase.assertThrows(function() {
|
||||
obj.arrayCol.splice(0, 0, obj.objectCol);
|
||||
}, 'can only splice in a write transaction');
|
||||
},
|
||||
};
|
||||
|
@ -20,67 +20,75 @@
|
||||
|
||||
var ObjectTests = {
|
||||
testBasicTypesPropertyGetters: function() {
|
||||
var basicTypesValues = [true, 1, 1.1, 1.11, 'string', new Date(1), 'DATA'];
|
||||
var realm = new Realm({schema: [BasicTypesObjectSchema]});
|
||||
var object = null;
|
||||
realm.write(function() {
|
||||
object = realm.create('BasicTypesObject', basicTypesValues);
|
||||
});
|
||||
var basicTypesValues = [true, 1, 1.1, 1.11, 'string', new Date(1), 'DATA'];
|
||||
var realm = new Realm({schema: [BasicTypesObjectSchema]});
|
||||
var object = null;
|
||||
realm.write(function() {
|
||||
object = realm.create('BasicTypesObject', basicTypesValues);
|
||||
});
|
||||
|
||||
for (var i = 0; i < BasicTypesObjectSchema.properties.length; i++) {
|
||||
var prop = BasicTypesObjectSchema.properties[i];
|
||||
if (prop.type == Realm.Types.FLOAT) {
|
||||
TestCase.assertEqualWithTolerance(object[prop.name], basicTypesValues[i], 0.000001);
|
||||
}
|
||||
else if (prop.type == Realm.Types.DATE) {
|
||||
TestCase.assertEqual(object[prop.name].getTime(), basicTypesValues[i].getTime());
|
||||
}
|
||||
else {
|
||||
TestCase.assertEqual(object[prop.name], basicTypesValues[i]);
|
||||
}
|
||||
}
|
||||
for (var i = 0; i < BasicTypesObjectSchema.properties.length; i++) {
|
||||
var prop = BasicTypesObjectSchema.properties[i];
|
||||
if (prop.type == Realm.Types.FLOAT) {
|
||||
TestCase.assertEqualWithTolerance(object[prop.name], basicTypesValues[i], 0.000001);
|
||||
}
|
||||
else if (prop.type == Realm.Types.DATE) {
|
||||
TestCase.assertEqual(object[prop.name].getTime(), basicTypesValues[i].getTime());
|
||||
}
|
||||
else {
|
||||
TestCase.assertEqual(object[prop.name], basicTypesValues[i]);
|
||||
}
|
||||
}
|
||||
},
|
||||
testBasicTypesPropertySetters: function() {
|
||||
var basicTypesValues = [true, 1, 1.1, 1.11, 'string', new Date(1), 'DATA'];
|
||||
var realm = new Realm({schema: [BasicTypesObjectSchema]});
|
||||
var obj = null;
|
||||
realm.write(function() {
|
||||
obj = realm.create('BasicTypesObject', basicTypesValues);
|
||||
obj.boolCol = false;
|
||||
obj.intCol = 2;
|
||||
obj.floatCol = 2.2;
|
||||
obj.doubleCol = 2.22;
|
||||
obj.stringCol = 'STRING';
|
||||
obj.dateCol = new Date(2);
|
||||
obj.dataCol = 'b';
|
||||
});
|
||||
TestCase.assertEqual(obj.boolCol, false, 'wrong bool value');
|
||||
TestCase.assertEqual(obj.intCol, 2, 'wrong int value');
|
||||
TestCase.assertEqualWithTolerance(obj.floatCol, 2.2, 0.000001, 'wrong float value');
|
||||
TestCase.assertEqual(obj.doubleCol, 2.22, 'wrong double value');
|
||||
TestCase.assertEqual(obj.stringCol, 'STRING', 'wrong string value');
|
||||
TestCase.assertEqual(obj.dateCol.getTime(), 2, 'wrong date value');
|
||||
TestCase.assertEqual(obj.dataCol, 'b', 'wrong data value');
|
||||
var basicTypesValues = [true, 1, 1.1, 1.11, 'string', new Date(1), 'DATA'];
|
||||
var realm = new Realm({schema: [BasicTypesObjectSchema]});
|
||||
var obj = null;
|
||||
|
||||
realm.write(function() {
|
||||
obj = realm.create('BasicTypesObject', basicTypesValues);
|
||||
obj.boolCol = false;
|
||||
obj.intCol = 2;
|
||||
obj.floatCol = 2.2;
|
||||
obj.doubleCol = 2.22;
|
||||
obj.stringCol = 'STRING';
|
||||
obj.dateCol = new Date(2);
|
||||
obj.dataCol = 'b';
|
||||
});
|
||||
|
||||
TestCase.assertEqual(obj.boolCol, false, 'wrong bool value');
|
||||
TestCase.assertEqual(obj.intCol, 2, 'wrong int value');
|
||||
TestCase.assertEqualWithTolerance(obj.floatCol, 2.2, 0.000001, 'wrong float value');
|
||||
TestCase.assertEqual(obj.doubleCol, 2.22, 'wrong double value');
|
||||
TestCase.assertEqual(obj.stringCol, 'STRING', 'wrong string value');
|
||||
TestCase.assertEqual(obj.dateCol.getTime(), 2, 'wrong date value');
|
||||
TestCase.assertEqual(obj.dataCol, 'b', 'wrong data value');
|
||||
|
||||
TestCase.assertThrows(function() {
|
||||
obj.boolCol = true;
|
||||
}, 'can only set property values in a write transaction');
|
||||
|
||||
TestCase.assertEqual(obj.boolCol, false, 'bool value changed outside transaction');
|
||||
},
|
||||
testLinkTypesPropertyGetters: function() {
|
||||
var realm = new Realm({schema: [LinkTypesObjectSchema, TestObjectSchema]});
|
||||
var obj = null;
|
||||
realm.write(function() {
|
||||
obj = realm.create('LinkTypesObject', [[1], null, [[3]]]);
|
||||
});
|
||||
var realm = new Realm({schema: [LinkTypesObjectSchema, TestObjectSchema]});
|
||||
var obj = null;
|
||||
realm.write(function() {
|
||||
obj = realm.create('LinkTypesObject', [[1], null, [[3]]]);
|
||||
});
|
||||
|
||||
var objVal = obj.objectCol;
|
||||
TestCase.assertEqual(typeof objVal, 'object');
|
||||
TestCase.assertNotEqual(objVal, null);
|
||||
TestCase.assertEqual(objVal.doubleCol, 1);
|
||||
var objVal = obj.objectCol;
|
||||
TestCase.assertEqual(typeof objVal, 'object');
|
||||
TestCase.assertNotEqual(objVal, null);
|
||||
TestCase.assertEqual(objVal.doubleCol, 1);
|
||||
|
||||
TestCase.assertEqual(obj.objectCol1, null);
|
||||
|
||||
var arrayVal = obj.arrayCol;
|
||||
TestCase.assertEqual(typeof arrayVal, 'object');
|
||||
TestCase.assertNotEqual(arrayVal, null);
|
||||
TestCase.assertEqual(arrayVal.length, 1);
|
||||
TestCase.assertEqual(arrayVal[0].doubleCol, 3);
|
||||
var arrayVal = obj.arrayCol;
|
||||
TestCase.assertEqual(typeof arrayVal, 'object');
|
||||
TestCase.assertNotEqual(arrayVal, null);
|
||||
TestCase.assertEqual(arrayVal.length, 1);
|
||||
TestCase.assertEqual(arrayVal[0].doubleCol, 3);
|
||||
},
|
||||
testLinkTypesPropertySetters: function() {
|
||||
var realm = new Realm({schema: [LinkTypesObjectSchema, TestObjectSchema]});
|
||||
@ -90,6 +98,10 @@ var ObjectTests = {
|
||||
});
|
||||
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
|
||||
realm.write(function() {
|
||||
obj.objectCol1 = obj.objectCol;
|
||||
|
@ -73,6 +73,11 @@ var RealmTests = {
|
||||
|
||||
testRealmCreate: function() {
|
||||
var realm = new Realm({schema: [IntPrimaryObjectSchema, AllTypesObjectSchema, TestObjectSchema]});
|
||||
|
||||
TestCase.assertThrows(function() {
|
||||
realm.create('TestObject', [1]);
|
||||
}, 'can only create inside a write transaction');
|
||||
|
||||
realm.write(function() {
|
||||
realm.create('TestObject', [1]);
|
||||
realm.create('TestObject', {'doubleCol': 2});
|
||||
@ -171,7 +176,7 @@ var RealmTests = {
|
||||
var objects = realm.objects('TestObject');
|
||||
TestCase.assertThrows(function() {
|
||||
realm.delete(objects[0]);
|
||||
}, "can only delete in a write transaction");
|
||||
}, 'can only delete in a write transaction');
|
||||
|
||||
realm.write(function() {
|
||||
TestCase.assertThrows(function() {
|
||||
@ -208,7 +213,7 @@ var RealmTests = {
|
||||
|
||||
TestCase.assertThrows(function() {
|
||||
realm.deleteAll();
|
||||
});
|
||||
}, 'can only deleteAll in a write transaction');
|
||||
|
||||
realm.write(function() {
|
||||
realm.deleteAll();
|
||||
|
Loading…
x
Reference in New Issue
Block a user