mirror of
https://github.com/status-im/realm-js.git
synced 2025-02-17 00:56:22 +00:00
Convert tests to create objects with property objects
This makes it more readable and will be the preferred syntax in the documentation (and is much less likely to be deprecated in the future!).
This commit is contained in:
parent
09752846e5
commit
9be1331dbf
@ -12,20 +12,30 @@ var schemas = require('./schemas');
|
||||
module.exports = BaseTest.extend({
|
||||
testArrayLength: function() {
|
||||
var realm = new Realm({schema: [schemas.LinkTypes, schemas.TestObject]});
|
||||
var array;
|
||||
|
||||
realm.write(function() {
|
||||
var obj = realm.create('LinkTypesObject', [[1], [2], [[3]]]);
|
||||
TestCase.assertEqual(obj.arrayCol.length, 1);
|
||||
var obj = realm.create('LinkTypesObject', {
|
||||
objectCol: {doubleCol: 1},
|
||||
objectCol1: {doubleCol: 2},
|
||||
arrayCol: [{doubleCol: 3}],
|
||||
});
|
||||
|
||||
array = obj.arrayCol;
|
||||
TestCase.assertEqual(array.length, 1);
|
||||
|
||||
obj.arrayCol = [];
|
||||
TestCase.assertEqual(obj.arrayCol.length, 0);
|
||||
TestCase.assertEqual(array.length, 0);
|
||||
|
||||
obj.arrayCol = [[1], [2]];
|
||||
TestCase.assertEqual(obj.arrayCol.length, 2);
|
||||
obj.arrayCol = [{doubleCol: 1}, {doubleCol: 2}];
|
||||
TestCase.assertEqual(array.length, 2);
|
||||
|
||||
TestCase.assertThrows(function() {
|
||||
obj.arrayCol.length = 0;
|
||||
array.length = 0;
|
||||
}, 'cannot set length property on lists');
|
||||
});
|
||||
|
||||
TestCase.assertEqual(array.length, 2);
|
||||
},
|
||||
|
||||
testArraySubscriptGetters: function() {
|
||||
@ -33,7 +43,12 @@ module.exports = BaseTest.extend({
|
||||
var array;
|
||||
|
||||
realm.write(function() {
|
||||
var obj = realm.create('LinkTypesObject', [[1], [2], [[3], [4]]]);
|
||||
var obj = realm.create('LinkTypesObject', {
|
||||
objectCol: {doubleCol: 1},
|
||||
objectCol1: {doubleCol: 2},
|
||||
arrayCol: [{doubleCol: 3}, {doubleCol: 4}],
|
||||
});
|
||||
|
||||
array = obj.arrayCol;
|
||||
});
|
||||
|
||||
@ -48,26 +63,36 @@ module.exports = BaseTest.extend({
|
||||
var array;
|
||||
|
||||
realm.write(function() {
|
||||
var obj = realm.create('LinkTypesObject', [[1], [2], [[3], [4]]]);
|
||||
array = obj.arrayCol;
|
||||
var obj = realm.create('LinkTypesObject', {
|
||||
objectCol: {doubleCol: 1},
|
||||
objectCol1: {doubleCol: 2},
|
||||
arrayCol: [{doubleCol: 3}, {doubleCol: 4}],
|
||||
});
|
||||
|
||||
array[0] = [5];
|
||||
array[1] = [6];
|
||||
array = obj.arrayCol;
|
||||
array[0] = {doubleCol: 5};
|
||||
array[1] = {doubleCol: 6};
|
||||
|
||||
TestCase.assertEqual(array[0].doubleCol, 5);
|
||||
TestCase.assertEqual(array[1].doubleCol, 6);
|
||||
|
||||
array[0] = obj.objectCol;
|
||||
array[1] = obj.objectCol1;
|
||||
|
||||
TestCase.assertEqual(array[0].doubleCol, 1);
|
||||
TestCase.assertEqual(array[1].doubleCol, 2);
|
||||
|
||||
TestCase.assertThrows(function() {
|
||||
array[2] = [1];
|
||||
array[2] = {doubleCol: 1};
|
||||
}, 'cannot set list item beyond its bounds');
|
||||
|
||||
TestCase.assertThrows(function() {
|
||||
array[-1] = [1];
|
||||
array[-1] = {doubleCol: 1};
|
||||
}, 'cannot set list item with negative index');
|
||||
});
|
||||
|
||||
TestCase.assertThrows(function() {
|
||||
array[0] = [3];
|
||||
array[0] = {doubleCol: 1};
|
||||
}, 'cannot set list item outside write transaction');
|
||||
},
|
||||
|
||||
@ -76,7 +101,12 @@ module.exports = BaseTest.extend({
|
||||
var array;
|
||||
|
||||
realm.write(function() {
|
||||
var obj = realm.create('LinkTypesObject', [[1], [2], [[3], [4]]]);
|
||||
var obj = realm.create('LinkTypesObject', {
|
||||
objectCol: {doubleCol: 1},
|
||||
objectCol1: {doubleCol: 2},
|
||||
arrayCol: [{doubleCol: 3}, {doubleCol: 4}],
|
||||
});
|
||||
|
||||
array = obj.arrayCol;
|
||||
});
|
||||
|
||||
@ -88,21 +118,26 @@ module.exports = BaseTest.extend({
|
||||
var obj;
|
||||
|
||||
realm.write(function() {
|
||||
obj = realm.create('LinkTypesObject', [[1], [2], []]);
|
||||
obj = realm.create('LinkTypesObject', {
|
||||
objectCol: {doubleCol: 1},
|
||||
objectCol1: {doubleCol: 2},
|
||||
arrayCol: [],
|
||||
});
|
||||
});
|
||||
|
||||
for (var index in obj.arrayCol) {
|
||||
var index;
|
||||
for (index in obj.arrayCol) {
|
||||
TestCase.assertTrue(false, "No objects should have been enumerated: " + index);
|
||||
}
|
||||
|
||||
realm.write(function() {
|
||||
obj.arrayCol = [[0], [1]];
|
||||
obj.arrayCol = [{doubleCol: 0}, {doubleCol: 1}];
|
||||
TestCase.assertEqual(obj.arrayCol.length, 2);
|
||||
});
|
||||
|
||||
var count = 0;
|
||||
var keys = Object.keys(obj.arrayCol);
|
||||
for (var index in obj.arrayCol) {
|
||||
for (index in obj.arrayCol) {
|
||||
TestCase.assertEqual(count++, +index);
|
||||
TestCase.assertEqual(keys[index], index);
|
||||
}
|
||||
@ -116,11 +151,16 @@ module.exports = BaseTest.extend({
|
||||
var array;
|
||||
|
||||
realm.write(function() {
|
||||
var obj = realm.create('LinkTypesObject', [[1], [2], [[3]]]);
|
||||
TestCase.assertEqual(obj.arrayCol.length, 1);
|
||||
var obj = realm.create('LinkTypesObject', {
|
||||
objectCol: {doubleCol: 1},
|
||||
objectCol1: {doubleCol: 2},
|
||||
arrayCol: [{doubleCol: 3}],
|
||||
});
|
||||
|
||||
array = obj.arrayCol;
|
||||
TestCase.assertEqual(array.push([4]), 2);
|
||||
TestCase.assertEqual(array.length, 1);
|
||||
|
||||
TestCase.assertEqual(array.push({doubleCol: 4}), 2);
|
||||
TestCase.assertEqual(array.length, 2);
|
||||
TestCase.assertEqual(array[1].doubleCol, 4);
|
||||
|
||||
@ -145,9 +185,13 @@ module.exports = BaseTest.extend({
|
||||
var array;
|
||||
|
||||
realm.write(function() {
|
||||
var obj = realm.create('LinkTypesObject', [[1], [2], [[3], [4]]]);
|
||||
array = obj.arrayCol;
|
||||
var obj = realm.create('LinkTypesObject', {
|
||||
objectCol: {doubleCol: 1},
|
||||
objectCol1: {doubleCol: 2},
|
||||
arrayCol: [{doubleCol: 3}, {doubleCol: 4}],
|
||||
});
|
||||
|
||||
array = obj.arrayCol;
|
||||
TestCase.assertEqual(array.pop().doubleCol, 4);
|
||||
TestCase.assertEqual(array.pop().doubleCol, 3);
|
||||
TestCase.assertEqual(array.length, 0);
|
||||
@ -169,11 +213,16 @@ module.exports = BaseTest.extend({
|
||||
var array;
|
||||
|
||||
realm.write(function() {
|
||||
var obj = realm.create('LinkTypesObject', [[1], [2], [[3]]]);
|
||||
TestCase.assertEqual(obj.arrayCol.length, 1);
|
||||
var obj = realm.create('LinkTypesObject', {
|
||||
objectCol: {doubleCol: 1},
|
||||
objectCol1: {doubleCol: 2},
|
||||
arrayCol: [{doubleCol: 3}],
|
||||
});
|
||||
|
||||
array = obj.arrayCol;
|
||||
TestCase.assertEqual(array.unshift([5]), 2);
|
||||
TestCase.assertEqual(array.length, 1);
|
||||
|
||||
TestCase.assertEqual(array.unshift({doubleCol: 5}), 2);
|
||||
TestCase.assertEqual(array.length, 2);
|
||||
TestCase.assertEqual(array[0].doubleCol, 5);
|
||||
|
||||
@ -185,7 +234,7 @@ module.exports = BaseTest.extend({
|
||||
|
||||
TestCase.assertEqual(array.length, 4);
|
||||
TestCase.assertThrows(function() {
|
||||
array.unshift([1]);
|
||||
array.unshift({doubleCol: 1});
|
||||
}, 'can only unshift in a write transaction');
|
||||
},
|
||||
|
||||
@ -194,9 +243,13 @@ module.exports = BaseTest.extend({
|
||||
var array;
|
||||
|
||||
realm.write(function() {
|
||||
var obj = realm.create('LinkTypesObject', [[1], [2], [[3], [4]]]);
|
||||
array = obj.arrayCol;
|
||||
var obj = realm.create('LinkTypesObject', {
|
||||
objectCol: {doubleCol: 1},
|
||||
objectCol1: {doubleCol: 2},
|
||||
arrayCol: [{doubleCol: 3}, {doubleCol: 4}],
|
||||
});
|
||||
|
||||
array = obj.arrayCol;
|
||||
TestCase.assertEqual(array.shift().doubleCol, 3);
|
||||
TestCase.assertEqual(array.shift().doubleCol, 4);
|
||||
TestCase.assertEqual(array.length, 0);
|
||||
@ -218,10 +271,14 @@ module.exports = BaseTest.extend({
|
||||
var array;
|
||||
|
||||
realm.write(function() {
|
||||
var obj = realm.create('LinkTypesObject', [[1], [2], [[3], [4]]]);
|
||||
var removed;
|
||||
var obj = realm.create('LinkTypesObject', {
|
||||
objectCol: {doubleCol: 1},
|
||||
objectCol1: {doubleCol: 2},
|
||||
arrayCol: [{doubleCol: 3}, {doubleCol: 4}],
|
||||
});
|
||||
|
||||
array = obj.arrayCol;
|
||||
var removed;
|
||||
|
||||
removed = array.splice(0, 0, obj.objectCol, obj.objectCol1);
|
||||
TestCase.assertEqual(removed.length, 0);
|
||||
@ -229,7 +286,7 @@ module.exports = BaseTest.extend({
|
||||
TestCase.assertEqual(array[0].doubleCol, 1);
|
||||
TestCase.assertEqual(array[1].doubleCol, 2);
|
||||
|
||||
removed = array.splice(2, 2, [5], [6]);
|
||||
removed = array.splice(2, 2, {doubleCol: 5}, {doubleCol: 6});
|
||||
TestCase.assertEqual(removed.length, 2);
|
||||
TestCase.assertEqual(removed[0].doubleCol, 3);
|
||||
TestCase.assertEqual(removed[1].doubleCol, 4);
|
||||
@ -270,7 +327,7 @@ module.exports = BaseTest.extend({
|
||||
});
|
||||
|
||||
TestCase.assertThrows(function() {
|
||||
array.splice(0, 0, [1]);
|
||||
array.splice(0, 0, {doubleCol: 1});
|
||||
}, 'can only splice in a write transaction');
|
||||
},
|
||||
|
||||
@ -280,7 +337,12 @@ module.exports = BaseTest.extend({
|
||||
var array;
|
||||
|
||||
realm.write(function() {
|
||||
object = realm.create('LinkTypesObject', [[1], [2], [[3], [4]]]);
|
||||
object = realm.create('LinkTypesObject', {
|
||||
objectCol: {doubleCol: 1},
|
||||
objectCol1: {doubleCol: 2},
|
||||
arrayCol: [{doubleCol: 3}, {doubleCol: 4}],
|
||||
});
|
||||
|
||||
array = object.arrayCol;
|
||||
});
|
||||
|
||||
@ -313,21 +375,27 @@ module.exports = BaseTest.extend({
|
||||
var array;
|
||||
|
||||
realm.write(function() {
|
||||
var obj = realm.create('LinkTypesObject', [[1], [2], [[3], [4]]]);
|
||||
var obj = realm.create('LinkTypesObject', {
|
||||
objectCol: {doubleCol: 1},
|
||||
objectCol1: {doubleCol: 2},
|
||||
arrayCol: [{doubleCol: 3}, {doubleCol: 4}],
|
||||
});
|
||||
|
||||
array = obj.arrayCol;
|
||||
});
|
||||
|
||||
TestCase.assertEqual(array.length, 2);
|
||||
TestCase.assertEqual(objects.length, 4);
|
||||
|
||||
try {
|
||||
realm.write(function() {
|
||||
array.push([5]);
|
||||
array.push({doubleCol: 5});
|
||||
TestCase.assertEqual(objects.length, 5);
|
||||
|
||||
array.unshift([2]);
|
||||
array.unshift({doubleCol: 2});
|
||||
TestCase.assertEqual(objects.length, 6);
|
||||
|
||||
array.splice(0, 0, [1]);
|
||||
array.splice(0, 0, {doubleCol: 1});
|
||||
TestCase.assertEqual(objects.length, 7);
|
||||
|
||||
array.push(objects[0], objects[1]);
|
||||
|
@ -16,67 +16,105 @@ var RANDOM_DATA = new Uint8Array([
|
||||
|
||||
module.exports = BaseTest.extend({
|
||||
testBasicTypesPropertyGetters: function() {
|
||||
var basicTypesValues = [true, 1, 1.1, 1.11, 'string', new Date(1), RANDOM_DATA];
|
||||
var realm = new Realm({schema: [schemas.BasicTypes]});
|
||||
var object;
|
||||
|
||||
var basicTypesValues = {
|
||||
boolCol: true,
|
||||
intCol: 1,
|
||||
floatCol: 1.1,
|
||||
doubleCol: 1.11,
|
||||
stringCol: 'string',
|
||||
dateCol: new Date(1),
|
||||
dataCol: RANDOM_DATA,
|
||||
};
|
||||
|
||||
realm.write(function() {
|
||||
object = realm.create('BasicTypesObject', basicTypesValues);
|
||||
});
|
||||
|
||||
for (var i = 0; i < schemas.BasicTypes.properties.length; i++) {
|
||||
var prop = schemas.BasicTypes.properties[i];
|
||||
if (prop.type == Realm.Types.FLOAT) {
|
||||
TestCase.assertEqualWithTolerance(object[prop.name], basicTypesValues[i], 0.000001);
|
||||
for (var name in schemas.BasicTypes.properties) {
|
||||
var prop = schemas.BasicTypes.properties[name];
|
||||
var type = typeof prop == 'object' ? prop.type : prop;
|
||||
|
||||
if (type == Realm.Types.FLOAT || type == Realm.Types.DOUBLE) {
|
||||
TestCase.assertEqualWithTolerance(object[name], basicTypesValues[name], 0.000001);
|
||||
}
|
||||
else if (prop.type == Realm.Types.DATA) {
|
||||
TestCase.assertArraysEqual(new Uint8Array(object[prop.name]), RANDOM_DATA);
|
||||
else if (type == Realm.Types.DATA) {
|
||||
TestCase.assertArraysEqual(new Uint8Array(object[name]), RANDOM_DATA);
|
||||
}
|
||||
else if (prop.type == Realm.Types.DATE) {
|
||||
TestCase.assertEqual(object[prop.name].getTime(), basicTypesValues[i].getTime());
|
||||
else if (type == Realm.Types.DATE) {
|
||||
TestCase.assertEqual(object[name].getTime(), basicTypesValues[name].getTime());
|
||||
}
|
||||
else {
|
||||
TestCase.assertEqual(object[prop.name], basicTypesValues[i]);
|
||||
TestCase.assertEqual(object[name], basicTypesValues[name]);
|
||||
}
|
||||
}
|
||||
|
||||
TestCase.assertEqual(object.nonexistent, undefined);
|
||||
},
|
||||
testNullableBasicTypesPropertyGetters: function() {
|
||||
var nullValues = [null, null, null, null, null, null, null];
|
||||
var basicTypesValues = [true, 1, 1.1, 1.11, 'string', new Date(1), RANDOM_DATA];
|
||||
|
||||
var realm = new Realm({schema: [schemas.NullableBasicTypes]});
|
||||
var nullObject = null;
|
||||
var object = null;
|
||||
var object, nullObject;
|
||||
|
||||
var basicTypesValues = {
|
||||
boolCol: true,
|
||||
intCol: 1,
|
||||
floatCol: 1.1,
|
||||
doubleCol: 1.11,
|
||||
stringCol: 'string',
|
||||
dateCol: new Date(1),
|
||||
dataCol: RANDOM_DATA,
|
||||
};
|
||||
|
||||
realm.write(function() {
|
||||
nullObject = realm.create('NullableBasicTypesObject', nullValues);
|
||||
object = realm.create('NullableBasicTypesObject', basicTypesValues);
|
||||
|
||||
nullObject = realm.create('NullableBasicTypesObject', {
|
||||
boolCol: null,
|
||||
intCol: null,
|
||||
floatCol: null,
|
||||
doubleCol: null,
|
||||
stringCol: null,
|
||||
dateCol: null,
|
||||
dataCol: null,
|
||||
});
|
||||
});
|
||||
|
||||
for (var i = 0; i < schemas.BasicTypes.properties.length; i++) {
|
||||
var prop = schemas.BasicTypes.properties[i];
|
||||
TestCase.assertEqual(nullObject[prop.name], null);
|
||||
for (var name in schemas.BasicTypes.properties) {
|
||||
var prop = schemas.BasicTypes.properties[name];
|
||||
var type = typeof prop == 'object' ? prop.type : prop;
|
||||
|
||||
if (prop.type == Realm.Types.FLOAT) {
|
||||
TestCase.assertEqualWithTolerance(object[prop.name], basicTypesValues[i], 0.000001);
|
||||
TestCase.assertEqual(nullObject[name], null);
|
||||
|
||||
if (type == Realm.Types.FLOAT || type == Realm.Types.DOUBLE) {
|
||||
TestCase.assertEqualWithTolerance(object[name], basicTypesValues[name], 0.000001);
|
||||
}
|
||||
else if (prop.type == Realm.Types.DATA) {
|
||||
TestCase.assertArraysEqual(new Uint8Array(object[prop.name]), RANDOM_DATA);
|
||||
else if (type == Realm.Types.DATA) {
|
||||
TestCase.assertArraysEqual(new Uint8Array(object[name]), RANDOM_DATA);
|
||||
}
|
||||
else if (prop.type == Realm.Types.DATE) {
|
||||
TestCase.assertEqual(object[prop.name].getTime(), basicTypesValues[i].getTime());
|
||||
else if (type == Realm.Types.DATE) {
|
||||
TestCase.assertEqual(object[name].getTime(), basicTypesValues[name].getTime());
|
||||
}
|
||||
else {
|
||||
TestCase.assertEqual(object[prop.name], basicTypesValues[i]);
|
||||
TestCase.assertEqual(object[name], basicTypesValues[name]);
|
||||
}
|
||||
}
|
||||
|
||||
},
|
||||
testBasicTypesPropertySetters: function() {
|
||||
var basicTypesValues = [true, 1, 1.1, 1.11, 'string', new Date(1), new ArrayBuffer()];
|
||||
var realm = new Realm({schema: [schemas.BasicTypes]});
|
||||
var obj = null;
|
||||
var obj;
|
||||
|
||||
var basicTypesValues = {
|
||||
boolCol: true,
|
||||
intCol: 1,
|
||||
floatCol: 1.1,
|
||||
doubleCol: 1.11,
|
||||
stringCol: 'string',
|
||||
dateCol: new Date(1),
|
||||
dataCol: new ArrayBuffer(),
|
||||
};
|
||||
|
||||
realm.write(function() {
|
||||
obj = realm.create('BasicTypesObject', basicTypesValues);
|
||||
@ -92,7 +130,7 @@ module.exports = BaseTest.extend({
|
||||
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.assertEqualWithTolerance(obj.doubleCol, 2.22, 0.000001, 'wrong double value');
|
||||
TestCase.assertEqual(obj.stringCol, 'STRING', 'wrong string value');
|
||||
TestCase.assertEqual(obj.dateCol.getTime(), 2, 'wrong date value');
|
||||
TestCase.assertArraysEqual(new Uint8Array(obj.dataCol), RANDOM_DATA, 'wrong data value');
|
||||
@ -156,13 +194,23 @@ module.exports = BaseTest.extend({
|
||||
TestCase.assertEqual(obj.boolCol, false, 'bool value changed outside transaction');
|
||||
},
|
||||
testNullableBasicTypesPropertySetters: function() {
|
||||
var basicTypesValues = [true, 1, 1.1, 1.11, 'string', new Date(1), RANDOM_DATA];
|
||||
var realm = new Realm({schema: [schemas.NullableBasicTypes]});
|
||||
var obj, obj1;
|
||||
|
||||
var basicTypesValues = {
|
||||
boolCol: true,
|
||||
intCol: 1,
|
||||
floatCol: 1.1,
|
||||
doubleCol: 1.11,
|
||||
stringCol: 'string',
|
||||
dateCol: new Date(1),
|
||||
dataCol: RANDOM_DATA,
|
||||
};
|
||||
|
||||
realm.write(function() {
|
||||
obj = realm.create('NullableBasicTypesObject', basicTypesValues);
|
||||
obj1 = realm.create('NullableBasicTypesObject', basicTypesValues);
|
||||
|
||||
for (var name in schemas.NullableBasicTypes.properties) {
|
||||
obj[name] = null;
|
||||
obj1[name] = undefined;
|
||||
@ -190,8 +238,13 @@ module.exports = BaseTest.extend({
|
||||
testLinkTypesPropertyGetters: function() {
|
||||
var realm = new Realm({schema: [schemas.LinkTypes, schemas.TestObject]});
|
||||
var obj = null;
|
||||
|
||||
realm.write(function() {
|
||||
obj = realm.create('LinkTypesObject', [[1], null, [[3]]]);
|
||||
obj = realm.create('LinkTypesObject', {
|
||||
objectCol: {doubleCol: 1},
|
||||
objectCol1: null,
|
||||
arrayCol: [{doubleCol: 3}],
|
||||
});
|
||||
});
|
||||
|
||||
var objVal = obj.objectCol;
|
||||
@ -210,11 +263,16 @@ module.exports = BaseTest.extend({
|
||||
testLinkTypesPropertySetters: function() {
|
||||
var realm = new Realm({schema: [schemas.LinkTypes, schemas.TestObject]});
|
||||
var objects = realm.objects('TestObject');
|
||||
var obj = null;
|
||||
var obj;
|
||||
|
||||
realm.write(function() {
|
||||
obj = realm.create('LinkTypesObject', [[1], null, [[3]]]);
|
||||
obj = realm.create('LinkTypesObject', {
|
||||
objectCol: {doubleCol: 1},
|
||||
objectCol1: null,
|
||||
arrayCol: [{doubleCol: 3}],
|
||||
});
|
||||
});
|
||||
|
||||
TestCase.assertEqual(objects.length, 2);
|
||||
|
||||
TestCase.assertThrows(function() {
|
||||
@ -238,15 +296,20 @@ module.exports = BaseTest.extend({
|
||||
|
||||
// set object as JSON
|
||||
realm.write(function() {
|
||||
obj.objectCol = { doubleCol: 1 };
|
||||
obj.objectCol = {doubleCol: 1};
|
||||
});
|
||||
TestCase.assertEqual(obj.objectCol.doubleCol, 1);
|
||||
TestCase.assertEqual(objects.length, 3);
|
||||
|
||||
// set array property
|
||||
realm.write(function() {
|
||||
obj.arrayCol = [obj.arrayCol[0], obj.objectCol, realm.create('TestObject', [2])];
|
||||
obj.arrayCol = [
|
||||
obj.arrayCol[0],
|
||||
obj.objectCol,
|
||||
realm.create('TestObject', {doubleCol: 2}),
|
||||
];
|
||||
});
|
||||
|
||||
TestCase.assertEqual(objects.length, 4);
|
||||
TestCase.assertEqual(obj.arrayCol.length, 3);
|
||||
TestCase.assertEqual(obj.arrayCol[0].doubleCol, 3);
|
||||
@ -254,12 +317,19 @@ module.exports = BaseTest.extend({
|
||||
TestCase.assertEqual(obj.arrayCol[2].doubleCol, 2);
|
||||
},
|
||||
testEnumerablePropertyNames: function() {
|
||||
var basicTypesValues = [true, 1, 1.1, 1.11, 'string', new Date(1), new ArrayBuffer()];
|
||||
var realm = new Realm({schema: [schemas.BasicTypes]});
|
||||
var object;
|
||||
|
||||
realm.write(function() {
|
||||
object = realm.create('BasicTypesObject', basicTypesValues);
|
||||
object = realm.create('BasicTypesObject', {
|
||||
boolCol: true,
|
||||
intCol: 1,
|
||||
floatCol: 1.1,
|
||||
doubleCol: 1.11,
|
||||
stringCol: 'string',
|
||||
dateCol: new Date(1),
|
||||
dataCol: RANDOM_DATA,
|
||||
});
|
||||
});
|
||||
|
||||
var propNames = Object.keys(schemas.BasicTypes.properties);
|
||||
|
@ -12,8 +12,12 @@ var util = require('./util');
|
||||
|
||||
module.exports = BaseTest.extend({
|
||||
testRealmConstructorPath: function() {
|
||||
TestCase.assertThrows(function() { new Realm('/invalidpath'); });
|
||||
TestCase.assertThrows(function() { new Realm(util.realmPathForFile('test1.realm'), 'invalidArgument'); });
|
||||
TestCase.assertThrows(function() {
|
||||
new Realm('/invalidpath');
|
||||
}, 'Realm cannot be created with an invalid path');
|
||||
TestCase.assertThrows(function() {
|
||||
new Realm(util.realmPathForFile('test1.realm'), 'invalidArgument');
|
||||
}, 'Realm constructor can only have 0 or 1 argument(s)');
|
||||
|
||||
var defaultRealm = new Realm({schema: []});
|
||||
TestCase.assertEqual(defaultRealm.path, Realm.defaultPath);
|
||||
@ -57,7 +61,7 @@ module.exports = BaseTest.extend({
|
||||
|
||||
realm = new Realm({path: testPath, schema: [schemas.TestObject], schemaVersion: 2});
|
||||
realm.write(function() {
|
||||
realm.create('TestObject', [1]);
|
||||
realm.create('TestObject', {doubleCol: 1});
|
||||
});
|
||||
TestCase.assertEqual(realm.objects('TestObject')[0].doubleCol, 1)
|
||||
},
|
||||
@ -78,12 +82,12 @@ module.exports = BaseTest.extend({
|
||||
var realm = new Realm({schema: [schemas.IntPrimary, schemas.AllTypes, schemas.TestObject]});
|
||||
|
||||
TestCase.assertThrows(function() {
|
||||
realm.create('TestObject', [1]);
|
||||
realm.create('TestObject', {doubleCol: 1});
|
||||
}, 'can only create inside a write transaction');
|
||||
|
||||
realm.write(function() {
|
||||
realm.create('TestObject', [1]);
|
||||
realm.create('TestObject', {'doubleCol': 2});
|
||||
realm.create('TestObject', {doubleCol: 1});
|
||||
realm.create('TestObject', {doubleCol: 2});
|
||||
});
|
||||
|
||||
var objects = realm.objects('TestObject');
|
||||
@ -93,16 +97,31 @@ module.exports = BaseTest.extend({
|
||||
|
||||
// test int primary object
|
||||
realm.write(function() {
|
||||
var obj0 = realm.create('IntPrimaryObject', [0, 'val0']);
|
||||
var obj0 = realm.create('IntPrimaryObject', {
|
||||
primaryCol: 0,
|
||||
valueCol: 'val0',
|
||||
});
|
||||
|
||||
TestCase.assertThrows(function() {
|
||||
realm.create('IntPrimaryObject', [0, 'val0']);
|
||||
});
|
||||
realm.create('IntPrimaryObject', [1, 'val1'], true);
|
||||
realm.create('IntPrimaryObject', {
|
||||
primaryCol: 0,
|
||||
valueCol: 'val0',
|
||||
});
|
||||
}, 'cannot create object with conflicting primary key');
|
||||
|
||||
realm.create('IntPrimaryObject', {
|
||||
primaryCol: 1,
|
||||
valueCol: 'val1',
|
||||
}, true);
|
||||
|
||||
var objects = realm.objects('IntPrimaryObject');
|
||||
TestCase.assertEqual(objects.length, 2);
|
||||
|
||||
realm.create('IntPrimaryObject', [0, 'newVal0'], true);
|
||||
realm.create('IntPrimaryObject', {
|
||||
primaryCol: 0,
|
||||
valueCol: 'newVal0',
|
||||
}, true);
|
||||
|
||||
TestCase.assertEqual(obj0.valueCol, 'newVal0');
|
||||
TestCase.assertEqual(objects.length, 2);
|
||||
|
||||
@ -112,47 +131,89 @@ module.exports = BaseTest.extend({
|
||||
|
||||
// test upsert with all type and string primary object
|
||||
realm.write(function() {
|
||||
var values = ['0', true, 1, 1.1, 1.11, '1', new Date(1), new ArrayBuffer(1), [1], []];
|
||||
var values = {
|
||||
primaryCol: '0',
|
||||
boolCol: true,
|
||||
intCol: 1,
|
||||
floatCol: 1.1,
|
||||
doubleCol: 1.11,
|
||||
stringCol: '1',
|
||||
dateCol: new Date(1),
|
||||
dataCol: new ArrayBuffer(1),
|
||||
objectCol: {doubleCol: 1},
|
||||
arrayCol: [],
|
||||
};
|
||||
|
||||
var obj0 = realm.create('AllTypesObject', values);
|
||||
|
||||
TestCase.assertThrows(function() {
|
||||
realm.create('AllTypesObject', values);
|
||||
});
|
||||
var obj1 = realm.create('AllTypesObject', ['1', false, 2, 2.2, 2.11, '2', new Date(2), new ArrayBuffer(2), [0], [[2]]], true);
|
||||
}, 'cannot create object with conflicting primary key');
|
||||
|
||||
var obj1 = realm.create('AllTypesObject', {
|
||||
primaryCol: '1',
|
||||
boolCol: false,
|
||||
intCol: 2,
|
||||
floatCol: 2.2,
|
||||
doubleCol: 2.22,
|
||||
stringCol: '2',
|
||||
dateCol: new Date(2),
|
||||
dataCol: new ArrayBuffer(2),
|
||||
objectCol: {doubleCol: 0},
|
||||
arrayCol: [{doubleCol: 2}],
|
||||
}, true);
|
||||
|
||||
var objects = realm.objects('AllTypesObject');
|
||||
TestCase.assertEqual(objects.length, 2);
|
||||
|
||||
realm.create('AllTypesObject', ['0', false, 2, 2.2, 2.22, '2', new Date(2), new ArrayBuffer(2), null, [[2]]], true);
|
||||
realm.create('AllTypesObject', {
|
||||
primaryCol: '0',
|
||||
boolCol: false,
|
||||
intCol: 2,
|
||||
floatCol: 2.2,
|
||||
doubleCol: 2.22,
|
||||
stringCol: '2',
|
||||
dateCol: new Date(2),
|
||||
dataCol: new ArrayBuffer(2),
|
||||
objectCol: null,
|
||||
arrayCol: [{doubleCol: 2}],
|
||||
}, true);
|
||||
|
||||
TestCase.assertEqual(objects.length, 2);
|
||||
TestCase.assertEqual(obj0.stringCol, '2');
|
||||
TestCase.assertEqual(obj0.boolCol, false);
|
||||
TestCase.assertEqual(obj0.intCol, 2);
|
||||
TestCase.assertEqualWithTolerance(obj0.floatCol, 2.2, 0.000001);
|
||||
TestCase.assertEqual(obj0.doubleCol, 2.22);
|
||||
TestCase.assertEqualWithTolerance(obj0.doubleCol, 2.22, 0.000001);
|
||||
TestCase.assertEqual(obj0.dateCol.getTime(), 2);
|
||||
TestCase.assertEqual(obj0.dataCol.byteLength, 2);
|
||||
TestCase.assertEqual(obj0.objectCol, null);
|
||||
TestCase.assertEqual(obj0.arrayCol.length, 1);
|
||||
|
||||
realm.create('AllTypesObject', { primaryCol: '0' }, true);
|
||||
realm.create('AllTypesObject', { primaryCol: '1' }, true);
|
||||
realm.create('AllTypesObject', {primaryCol: '0'}, true);
|
||||
realm.create('AllTypesObject', {primaryCol: '1'}, true);
|
||||
TestCase.assertEqual(obj0.stringCol, '2');
|
||||
TestCase.assertEqual(obj0.objectCol, null);
|
||||
TestCase.assertEqual(obj1.objectCol.doubleCol, 0);
|
||||
|
||||
realm.create('AllTypesObject', { primaryCol: '0', stringCol: '3', objectCol: [0] }, true);
|
||||
realm.create('AllTypesObject', {
|
||||
primaryCol: '0',
|
||||
stringCol: '3',
|
||||
objectCol: {doubleCol: 0},
|
||||
}, true);
|
||||
|
||||
TestCase.assertEqual(obj0.stringCol, '3');
|
||||
TestCase.assertEqual(obj0.boolCol, false);
|
||||
TestCase.assertEqual(obj0.intCol, 2);
|
||||
TestCase.assertEqualWithTolerance(obj0.floatCol, 2.2, 0.000001);
|
||||
TestCase.assertEqual(obj0.doubleCol, 2.22);
|
||||
TestCase.assertEqualWithTolerance(obj0.doubleCol, 2.22, 0.000001);
|
||||
TestCase.assertEqual(obj0.dateCol.getTime(), 2);
|
||||
TestCase.assertEqual(obj0.dataCol.byteLength, 2);
|
||||
TestCase.assertEqual(obj0.objectCol.doubleCol, 0);
|
||||
TestCase.assertEqual(obj0.arrayCol.length, 1);
|
||||
|
||||
realm.create('AllTypesObject', { primaryCol: '0', objectCol: undefined }, true);
|
||||
realm.create('AllTypesObject', { primaryCol: '1', objectCol: null }, true);
|
||||
realm.create('AllTypesObject', {primaryCol: '0', objectCol: undefined}, true);
|
||||
realm.create('AllTypesObject', {primaryCol: '1', objectCol: null}, true);
|
||||
TestCase.assertEqual(obj0.objectCol, null);
|
||||
TestCase.assertEqual(obj1.objectCol, null);
|
||||
});
|
||||
@ -160,6 +221,7 @@ module.exports = BaseTest.extend({
|
||||
|
||||
testRealmCreateWithDefaults: function() {
|
||||
var realm = new Realm({schema: [schemas.DefaultValues, schemas.TestObject]});
|
||||
|
||||
realm.write(function() {
|
||||
var obj = realm.create('DefaultValuesObject', {});
|
||||
var properties = schemas.DefaultValues.properties;
|
||||
@ -167,22 +229,23 @@ module.exports = BaseTest.extend({
|
||||
TestCase.assertEqual(obj.boolCol, properties.boolCol.default);
|
||||
TestCase.assertEqual(obj.intCol, properties.intCol.default);
|
||||
TestCase.assertEqualWithTolerance(obj.floatCol, properties.floatCol.default, 0.000001);
|
||||
TestCase.assertEqual(obj.doubleCol, properties.doubleCol.default);
|
||||
TestCase.assertEqualWithTolerance(obj.doubleCol, properties.doubleCol.default, 0.000001);
|
||||
TestCase.assertEqual(obj.stringCol, properties.stringCol.default);
|
||||
TestCase.assertEqual(obj.dateCol.getTime(), properties.dateCol.default.getTime());
|
||||
TestCase.assertEqual(obj.dataCol.byteLength, properties.dataCol.default.byteLength);
|
||||
TestCase.assertEqual(obj.objectCol.doubleCol, properties.objectCol.default[0]);
|
||||
TestCase.assertEqual(obj.objectCol.doubleCol, properties.objectCol.default.doubleCol);
|
||||
TestCase.assertEqual(obj.nullObjectCol, null);
|
||||
TestCase.assertEqual(obj.arrayCol.length, properties.arrayCol.default.length);
|
||||
TestCase.assertEqual(obj.arrayCol[0].doubleCol, properties.arrayCol.default[0][0]);
|
||||
TestCase.assertEqual(obj.arrayCol[0].doubleCol, properties.arrayCol.default[0].doubleCol);
|
||||
});
|
||||
},
|
||||
|
||||
testRealmDelete: function() {
|
||||
var realm = new Realm({schema: [schemas.TestObject]});
|
||||
|
||||
realm.write(function() {
|
||||
for (var i = 0; i < 10; i++) {
|
||||
realm.create('TestObject', [i]);
|
||||
realm.create('TestObject', {doubleCol: i});
|
||||
}
|
||||
});
|
||||
|
||||
@ -216,11 +279,13 @@ module.exports = BaseTest.extend({
|
||||
|
||||
testDeleteAll: function() {
|
||||
var realm = new Realm({schema: [schemas.TestObject, schemas.IntPrimary]});
|
||||
|
||||
realm.write(function() {
|
||||
realm.create('TestObject', [1]);
|
||||
realm.create('TestObject', [2]);
|
||||
realm.create('IntPrimaryObject', [2, 'value']);
|
||||
realm.create('TestObject', {doubleCol: 1});
|
||||
realm.create('TestObject', {doubleCol: 2});
|
||||
realm.create('IntPrimaryObject', {primaryCol: 2, valueCol: 'value'});
|
||||
});
|
||||
|
||||
TestCase.assertEqual(realm.objects('TestObject').length, 2);
|
||||
TestCase.assertEqual(realm.objects('IntPrimaryObject').length, 1);
|
||||
|
||||
@ -239,10 +304,10 @@ module.exports = BaseTest.extend({
|
||||
testRealmObjects: function() {
|
||||
var realm = new Realm({schema: [schemas.PersonObject, schemas.DefaultValues, schemas.TestObject]});
|
||||
realm.write(function() {
|
||||
realm.create('PersonObject', ['Ari', 10, false]);
|
||||
realm.create('PersonObject', ['Tim', 11, false]);
|
||||
realm.create('PersonObject', {'name': 'Bjarne', 'age': 12});
|
||||
realm.create('PersonObject', {'name': 'Alex', 'age': 12, 'married': true});
|
||||
realm.create('PersonObject', {name: 'Ari', age: 10});
|
||||
realm.create('PersonObject', {name: 'Tim', age: 11});
|
||||
realm.create('PersonObject', {name: 'Bjarne', age: 12});
|
||||
realm.create('PersonObject', {name: 'Alex', age: 12, married: true});
|
||||
});
|
||||
|
||||
TestCase.assertThrows(function() {
|
||||
|
@ -16,7 +16,7 @@ module.exports = BaseTest.extend({
|
||||
TestCase.assertEqual(objects.length, 0);
|
||||
|
||||
realm.write(function() {
|
||||
realm.create('TestObject', [1]);
|
||||
realm.create('TestObject', {doubleCol: 1});
|
||||
TestCase.assertEqual(objects.length, 1);
|
||||
});
|
||||
TestCase.assertEqual(objects.length, 1);
|
||||
@ -24,8 +24,8 @@ module.exports = BaseTest.extend({
|
||||
testResultsSubscript: function() {
|
||||
var realm = new Realm({schema: [schemas.PersonObject]});
|
||||
realm.write(function() {
|
||||
realm.create('PersonObject', ['name1', 1, false]);
|
||||
realm.create('PersonObject', ['name2', 2, false]);
|
||||
realm.create('PersonObject', {name: 'name1', age: 1});
|
||||
realm.create('PersonObject', {name: 'name2', age: 2});
|
||||
});
|
||||
|
||||
var people = realm.objects('PersonObject');
|
||||
@ -41,17 +41,17 @@ module.exports = BaseTest.extend({
|
||||
var objects = realm.objects('TestObject');
|
||||
|
||||
realm.write(function() {
|
||||
realm.create('TestObject', [1]);
|
||||
realm.create('TestObject', {doubleCol: 1});
|
||||
});
|
||||
|
||||
TestCase.assertThrows(function() {
|
||||
objects[-1] = [0];
|
||||
objects[-1] = {doubleCol: 0};
|
||||
});
|
||||
TestCase.assertThrows(function() {
|
||||
objects[0] = [0];
|
||||
objects[0] = {doubleCol: 0};
|
||||
});
|
||||
TestCase.assertThrows(function() {
|
||||
objects[1] = [0];
|
||||
objects[1] = {doubleCol: 0};
|
||||
});
|
||||
TestCase.assertThrows(function() {
|
||||
objects.length = 0;
|
||||
@ -71,12 +71,13 @@ module.exports = BaseTest.extend({
|
||||
testResultsEnumerate: function() {
|
||||
var realm = new Realm({schema: [schemas.TestObject]});
|
||||
var objects = realm.objects('TestObject');
|
||||
for (var object in objects) {
|
||||
|
||||
for (var index in objects) {
|
||||
TestCase.assertTrue(false, "No objects should have been enumerated");
|
||||
}
|
||||
|
||||
realm.write(function() {
|
||||
realm.create('TestObject', [1]);
|
||||
realm.create('TestObject', {doubleCol: 1});
|
||||
TestCase.assertEqual(objects.length, 1);
|
||||
});
|
||||
|
||||
@ -93,12 +94,13 @@ module.exports = BaseTest.extend({
|
||||
testSort: function() {
|
||||
var realm = new Realm({schema: [schemas.TestObject]});
|
||||
var objects = realm.objects('TestObject');
|
||||
|
||||
realm.write(function() {
|
||||
realm.create('TestObject', [2]);
|
||||
realm.create('TestObject', [3]);
|
||||
realm.create('TestObject', [1]);
|
||||
realm.create('TestObject', [4]);
|
||||
realm.create('TestObject', [0]);
|
||||
realm.create('TestObject', {doubleCol: 2});
|
||||
realm.create('TestObject', {doubleCol: 3});
|
||||
realm.create('TestObject', {doubleCol: 1});
|
||||
realm.create('TestObject', {doubleCol: 4});
|
||||
realm.create('TestObject', {doubleCol: 0});
|
||||
});
|
||||
|
||||
objects.sortByProperty('doubleCol');
|
||||
|
@ -98,9 +98,9 @@ exports.DefaultValues = {
|
||||
stringCol: {type: Realm.Types.STRING, default: 'defaultString'},
|
||||
dateCol: {type: Realm.Types.DATE, default: new Date(1.111)},
|
||||
dataCol: {type: Realm.Types.DATA, default: new ArrayBuffer(1)},
|
||||
objectCol: {type: 'TestObject', default: [1]},
|
||||
objectCol: {type: 'TestObject', default: {doubleCol: 1}},
|
||||
nullObjectCol: {type: 'TestObject', default: null},
|
||||
arrayCol: {type: Realm.Types.LIST, objectType: 'TestObject', default: [[2]]},
|
||||
arrayCol: {type: Realm.Types.LIST, objectType: 'TestObject', default: [{doubleCol: 2}]},
|
||||
}
|
||||
};
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user