2016-02-18 19:59:34 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// Copyright 2016 Realm Inc.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
//
|
|
|
|
////////////////////////////////////////////////////////////////////////////
|
2015-08-14 17:47:56 +00:00
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
2015-10-08 23:19:19 +00:00
|
|
|
var Realm = require('realm');
|
2015-10-06 07:57:35 +00:00
|
|
|
var TestCase = require('./asserts');
|
|
|
|
var schemas = require('./schemas');
|
|
|
|
|
2016-05-20 22:54:20 +00:00
|
|
|
module.exports = {
|
2016-02-28 20:35:54 +00:00
|
|
|
testListConstructor: function() {
|
|
|
|
var realm = new Realm({schema: [schemas.PersonObject, schemas.PersonList]});
|
|
|
|
|
|
|
|
realm.write(function() {
|
|
|
|
var obj = realm.create('PersonList', {list: []});
|
|
|
|
TestCase.assertTrue(obj.list instanceof Realm.List);
|
2016-03-09 07:47:08 +00:00
|
|
|
TestCase.assertTrue(obj.list instanceof Realm.Collection);
|
2016-02-28 20:35:54 +00:00
|
|
|
});
|
2016-02-28 21:07:05 +00:00
|
|
|
|
|
|
|
TestCase.assertThrows(function() {
|
|
|
|
new Realm.List();
|
|
|
|
});
|
2016-05-09 23:15:10 +00:00
|
|
|
|
|
|
|
TestCase.assertEqual(typeof Realm.List, 'function');
|
|
|
|
TestCase.assertTrue(Realm.List instanceof Function);
|
2016-02-28 20:35:54 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
testListLength: function() {
|
2015-10-06 07:57:35 +00:00
|
|
|
var realm = new Realm({schema: [schemas.LinkTypes, schemas.TestObject]});
|
2015-11-02 20:02:53 +00:00
|
|
|
var array;
|
|
|
|
|
2015-08-14 17:47:56 +00:00
|
|
|
realm.write(function() {
|
2015-11-02 20:02:53 +00:00
|
|
|
var obj = realm.create('LinkTypesObject', {
|
|
|
|
objectCol: {doubleCol: 1},
|
|
|
|
objectCol1: {doubleCol: 2},
|
|
|
|
arrayCol: [{doubleCol: 3}],
|
|
|
|
});
|
|
|
|
|
|
|
|
array = obj.arrayCol;
|
|
|
|
TestCase.assertEqual(array.length, 1);
|
2015-08-14 17:47:56 +00:00
|
|
|
|
|
|
|
obj.arrayCol = [];
|
2015-11-02 20:02:53 +00:00
|
|
|
TestCase.assertEqual(array.length, 0);
|
2015-08-14 17:47:56 +00:00
|
|
|
|
2015-11-02 20:02:53 +00:00
|
|
|
obj.arrayCol = [{doubleCol: 1}, {doubleCol: 2}];
|
|
|
|
TestCase.assertEqual(array.length, 2);
|
2015-10-12 22:25:23 +00:00
|
|
|
|
|
|
|
TestCase.assertThrows(function() {
|
2015-11-02 20:02:53 +00:00
|
|
|
array.length = 0;
|
2015-10-12 22:25:23 +00:00
|
|
|
}, 'cannot set length property on lists');
|
2015-10-12 09:55:37 +00:00
|
|
|
});
|
2015-11-02 20:02:53 +00:00
|
|
|
|
|
|
|
TestCase.assertEqual(array.length, 2);
|
2015-08-14 17:47:56 +00:00
|
|
|
},
|
|
|
|
|
2016-02-28 20:35:54 +00:00
|
|
|
testListSubscriptGetters: function() {
|
2015-10-06 07:57:35 +00:00
|
|
|
var realm = new Realm({schema: [schemas.LinkTypes, schemas.TestObject]});
|
2015-10-12 09:55:37 +00:00
|
|
|
var array;
|
|
|
|
|
|
|
|
realm.write(function() {
|
2015-11-02 20:02:53 +00:00
|
|
|
var obj = realm.create('LinkTypesObject', {
|
|
|
|
objectCol: {doubleCol: 1},
|
|
|
|
objectCol1: {doubleCol: 2},
|
|
|
|
arrayCol: [{doubleCol: 3}, {doubleCol: 4}],
|
|
|
|
});
|
|
|
|
|
2015-10-12 09:55:37 +00:00
|
|
|
array = obj.arrayCol;
|
|
|
|
});
|
2015-08-14 17:47:56 +00:00
|
|
|
|
|
|
|
TestCase.assertEqual(array[0].doubleCol, 3);
|
|
|
|
TestCase.assertEqual(array[1].doubleCol, 4);
|
2015-10-12 22:25:23 +00:00
|
|
|
TestCase.assertEqual(array[2], undefined);
|
|
|
|
TestCase.assertEqual(array[-1], undefined);
|
2015-08-14 17:47:56 +00:00
|
|
|
},
|
|
|
|
|
2016-02-28 20:35:54 +00:00
|
|
|
testListSubscriptSetters: function() {
|
2015-10-06 07:57:35 +00:00
|
|
|
var realm = new Realm({schema: [schemas.LinkTypes, schemas.TestObject]});
|
2015-10-12 09:55:37 +00:00
|
|
|
var array;
|
|
|
|
|
|
|
|
realm.write(function() {
|
2015-11-02 20:02:53 +00:00
|
|
|
var obj = realm.create('LinkTypesObject', {
|
|
|
|
objectCol: {doubleCol: 1},
|
|
|
|
objectCol1: {doubleCol: 2},
|
|
|
|
arrayCol: [{doubleCol: 3}, {doubleCol: 4}],
|
|
|
|
});
|
2015-10-12 09:55:37 +00:00
|
|
|
|
2015-11-02 20:02:53 +00:00
|
|
|
array = obj.arrayCol;
|
|
|
|
array[0] = {doubleCol: 5};
|
|
|
|
array[1] = {doubleCol: 6};
|
2015-10-12 09:55:37 +00:00
|
|
|
|
|
|
|
TestCase.assertEqual(array[0].doubleCol, 5);
|
|
|
|
TestCase.assertEqual(array[1].doubleCol, 6);
|
|
|
|
|
2015-11-02 20:02:53 +00:00
|
|
|
array[0] = obj.objectCol;
|
|
|
|
array[1] = obj.objectCol1;
|
|
|
|
|
|
|
|
TestCase.assertEqual(array[0].doubleCol, 1);
|
|
|
|
TestCase.assertEqual(array[1].doubleCol, 2);
|
|
|
|
|
2015-10-12 09:55:37 +00:00
|
|
|
TestCase.assertThrows(function() {
|
2015-11-02 20:02:53 +00:00
|
|
|
array[2] = {doubleCol: 1};
|
2015-10-12 09:55:37 +00:00
|
|
|
}, 'cannot set list item beyond its bounds');
|
2015-10-12 22:25:23 +00:00
|
|
|
|
|
|
|
TestCase.assertThrows(function() {
|
2015-11-02 20:02:53 +00:00
|
|
|
array[-1] = {doubleCol: 1};
|
2015-10-12 22:25:23 +00:00
|
|
|
}, 'cannot set list item with negative index');
|
2015-10-12 09:55:37 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
TestCase.assertThrows(function() {
|
2015-11-02 20:02:53 +00:00
|
|
|
array[0] = {doubleCol: 1};
|
2015-10-12 09:55:37 +00:00
|
|
|
}, 'cannot set list item outside write transaction');
|
|
|
|
},
|
|
|
|
|
2016-02-28 20:35:54 +00:00
|
|
|
testListInvalidProperty: function() {
|
2015-10-14 09:12:50 +00:00
|
|
|
var realm = new Realm({schema: [schemas.LinkTypes, schemas.TestObject]});
|
2015-10-12 09:55:37 +00:00
|
|
|
var array;
|
|
|
|
|
|
|
|
realm.write(function() {
|
2015-11-02 20:02:53 +00:00
|
|
|
var obj = realm.create('LinkTypesObject', {
|
|
|
|
objectCol: {doubleCol: 1},
|
|
|
|
objectCol1: {doubleCol: 2},
|
|
|
|
arrayCol: [{doubleCol: 3}, {doubleCol: 4}],
|
|
|
|
});
|
|
|
|
|
2015-10-12 09:55:37 +00:00
|
|
|
array = obj.arrayCol;
|
|
|
|
});
|
2015-08-14 17:47:56 +00:00
|
|
|
|
|
|
|
TestCase.assertEqual(undefined, array.ablasdf);
|
|
|
|
},
|
|
|
|
|
2016-02-28 20:35:54 +00:00
|
|
|
testListEnumerate: function() {
|
2015-10-06 07:57:35 +00:00
|
|
|
var realm = new Realm({schema: [schemas.LinkTypes, schemas.TestObject]});
|
2015-10-12 09:55:37 +00:00
|
|
|
var obj;
|
2015-08-14 17:47:56 +00:00
|
|
|
|
2015-10-12 09:55:37 +00:00
|
|
|
realm.write(function() {
|
2015-11-02 20:02:53 +00:00
|
|
|
obj = realm.create('LinkTypesObject', {
|
|
|
|
objectCol: {doubleCol: 1},
|
|
|
|
objectCol1: {doubleCol: 2},
|
|
|
|
arrayCol: [],
|
|
|
|
});
|
2015-10-12 09:55:37 +00:00
|
|
|
});
|
2015-08-14 17:47:56 +00:00
|
|
|
|
2015-11-02 20:02:53 +00:00
|
|
|
var index;
|
|
|
|
for (index in obj.arrayCol) {
|
2015-10-12 09:55:37 +00:00
|
|
|
TestCase.assertTrue(false, "No objects should have been enumerated: " + index);
|
2015-08-14 17:47:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
realm.write(function() {
|
2015-11-02 20:02:53 +00:00
|
|
|
obj.arrayCol = [{doubleCol: 0}, {doubleCol: 1}];
|
2015-08-14 17:47:56 +00:00
|
|
|
TestCase.assertEqual(obj.arrayCol.length, 2);
|
|
|
|
});
|
|
|
|
|
|
|
|
var count = 0;
|
2015-10-21 20:09:51 +00:00
|
|
|
var keys = Object.keys(obj.arrayCol);
|
2015-11-02 20:02:53 +00:00
|
|
|
for (index in obj.arrayCol) {
|
2015-10-21 20:09:51 +00:00
|
|
|
TestCase.assertEqual(count++, +index);
|
|
|
|
TestCase.assertEqual(keys[index], index);
|
2015-10-12 09:55:37 +00:00
|
|
|
}
|
2015-10-21 20:09:51 +00:00
|
|
|
|
|
|
|
TestCase.assertEqual(count, 2);
|
|
|
|
TestCase.assertEqual(keys.length, 2);
|
2015-08-14 17:47:56 +00:00
|
|
|
},
|
2015-08-24 16:22:37 +00:00
|
|
|
|
2016-02-28 20:35:54 +00:00
|
|
|
testListPush: function() {
|
2015-10-06 07:57:35 +00:00
|
|
|
var realm = new Realm({schema: [schemas.LinkTypes, schemas.TestObject]});
|
2015-08-27 20:33:20 +00:00
|
|
|
var array;
|
2015-10-12 09:23:09 +00:00
|
|
|
|
2015-08-27 20:33:20 +00:00
|
|
|
realm.write(function() {
|
2015-11-02 20:02:53 +00:00
|
|
|
var obj = realm.create('LinkTypesObject', {
|
|
|
|
objectCol: {doubleCol: 1},
|
|
|
|
objectCol1: {doubleCol: 2},
|
|
|
|
arrayCol: [{doubleCol: 3}],
|
|
|
|
});
|
2015-08-27 20:33:20 +00:00
|
|
|
|
|
|
|
array = obj.arrayCol;
|
2015-11-02 20:02:53 +00:00
|
|
|
TestCase.assertEqual(array.length, 1);
|
|
|
|
|
|
|
|
TestCase.assertEqual(array.push({doubleCol: 4}), 2);
|
2015-08-27 20:33:20 +00:00
|
|
|
TestCase.assertEqual(array.length, 2);
|
|
|
|
TestCase.assertEqual(array[1].doubleCol, 4);
|
|
|
|
|
2015-08-28 21:41:30 +00:00
|
|
|
TestCase.assertEqual(array.push(obj.objectCol, obj.objectCol1), 4);
|
|
|
|
TestCase.assertEqual(array.length, 4);
|
2015-08-27 20:33:20 +00:00
|
|
|
TestCase.assertEqual(array[2].doubleCol, 1);
|
2015-08-28 21:41:30 +00:00
|
|
|
TestCase.assertEqual(array[3].doubleCol, 2);
|
|
|
|
|
|
|
|
TestCase.assertThrows(function() {
|
|
|
|
array.push();
|
|
|
|
});
|
2015-10-12 22:25:23 +00:00
|
|
|
});
|
2015-08-27 20:33:20 +00:00
|
|
|
|
2015-08-28 21:41:30 +00:00
|
|
|
TestCase.assertEqual(array.length, 4);
|
2015-10-12 09:23:09 +00:00
|
|
|
TestCase.assertThrows(function() {
|
|
|
|
array.push([1]);
|
|
|
|
}, 'can only push in a write transaction');
|
2015-08-28 21:49:15 +00:00
|
|
|
},
|
|
|
|
|
2016-02-28 20:35:54 +00:00
|
|
|
testListPop: function() {
|
2015-10-06 07:57:35 +00:00
|
|
|
var realm = new Realm({schema: [schemas.LinkTypes, schemas.TestObject]});
|
2015-08-28 21:49:15 +00:00
|
|
|
var array;
|
2015-10-12 09:23:09 +00:00
|
|
|
|
2015-08-28 21:49:15 +00:00
|
|
|
realm.write(function() {
|
2015-11-02 20:02:53 +00:00
|
|
|
var obj = realm.create('LinkTypesObject', {
|
|
|
|
objectCol: {doubleCol: 1},
|
|
|
|
objectCol1: {doubleCol: 2},
|
|
|
|
arrayCol: [{doubleCol: 3}, {doubleCol: 4}],
|
|
|
|
});
|
2015-08-28 21:49:15 +00:00
|
|
|
|
2015-11-02 20:02:53 +00:00
|
|
|
array = obj.arrayCol;
|
2015-08-28 22:11:50 +00:00
|
|
|
TestCase.assertEqual(array.pop().doubleCol, 4);
|
2015-08-28 21:49:15 +00:00
|
|
|
TestCase.assertEqual(array.pop().doubleCol, 3);
|
|
|
|
TestCase.assertEqual(array.length, 0);
|
|
|
|
|
|
|
|
TestCase.assertEqual(array.pop(), undefined);
|
|
|
|
|
|
|
|
TestCase.assertThrows(function() {
|
|
|
|
array.pop(1);
|
|
|
|
});
|
2015-08-27 20:33:20 +00:00
|
|
|
});
|
2015-08-28 21:49:15 +00:00
|
|
|
|
2015-10-12 09:23:09 +00:00
|
|
|
TestCase.assertThrows(function() {
|
|
|
|
array.pop();
|
|
|
|
}, 'can only pop in a write transaction');
|
2015-08-28 21:41:30 +00:00
|
|
|
},
|
|
|
|
|
2016-02-28 20:35:54 +00:00
|
|
|
testListUnshift: function() {
|
2015-10-06 07:57:35 +00:00
|
|
|
var realm = new Realm({schema: [schemas.LinkTypes, schemas.TestObject]});
|
2015-08-28 21:41:30 +00:00
|
|
|
var array;
|
2015-10-12 09:23:09 +00:00
|
|
|
|
2015-08-28 21:41:30 +00:00
|
|
|
realm.write(function() {
|
2015-11-02 20:02:53 +00:00
|
|
|
var obj = realm.create('LinkTypesObject', {
|
|
|
|
objectCol: {doubleCol: 1},
|
|
|
|
objectCol1: {doubleCol: 2},
|
|
|
|
arrayCol: [{doubleCol: 3}],
|
|
|
|
});
|
2015-08-28 21:41:30 +00:00
|
|
|
|
|
|
|
array = obj.arrayCol;
|
2015-11-02 20:02:53 +00:00
|
|
|
TestCase.assertEqual(array.length, 1);
|
|
|
|
|
|
|
|
TestCase.assertEqual(array.unshift({doubleCol: 5}), 2);
|
2015-08-28 21:41:30 +00:00
|
|
|
TestCase.assertEqual(array.length, 2);
|
|
|
|
TestCase.assertEqual(array[0].doubleCol, 5);
|
|
|
|
|
|
|
|
TestCase.assertEqual(array.unshift(obj.objectCol, obj.objectCol1), 4);
|
|
|
|
TestCase.assertEqual(array.length, 4);
|
|
|
|
TestCase.assertEqual(array[0].doubleCol, 1);
|
|
|
|
TestCase.assertEqual(array[1].doubleCol, 2);
|
2016-12-20 18:56:51 +00:00
|
|
|
});
|
2015-08-28 21:41:30 +00:00
|
|
|
|
|
|
|
TestCase.assertEqual(array.length, 4);
|
2015-10-12 09:23:09 +00:00
|
|
|
TestCase.assertThrows(function() {
|
2015-11-02 20:02:53 +00:00
|
|
|
array.unshift({doubleCol: 1});
|
2015-10-12 09:23:09 +00:00
|
|
|
}, 'can only unshift in a write transaction');
|
2015-08-28 21:41:30 +00:00
|
|
|
},
|
|
|
|
|
2016-02-28 20:35:54 +00:00
|
|
|
testListShift: function() {
|
2015-10-06 07:57:35 +00:00
|
|
|
var realm = new Realm({schema: [schemas.LinkTypes, schemas.TestObject]});
|
2015-08-28 22:11:50 +00:00
|
|
|
var array;
|
2015-10-12 09:23:09 +00:00
|
|
|
|
2015-08-28 22:11:50 +00:00
|
|
|
realm.write(function() {
|
2015-11-02 20:02:53 +00:00
|
|
|
var obj = realm.create('LinkTypesObject', {
|
|
|
|
objectCol: {doubleCol: 1},
|
|
|
|
objectCol1: {doubleCol: 2},
|
|
|
|
arrayCol: [{doubleCol: 3}, {doubleCol: 4}],
|
|
|
|
});
|
2015-08-28 22:11:50 +00:00
|
|
|
|
2015-11-02 20:02:53 +00:00
|
|
|
array = obj.arrayCol;
|
2015-08-28 22:11:50 +00:00
|
|
|
TestCase.assertEqual(array.shift().doubleCol, 3);
|
|
|
|
TestCase.assertEqual(array.shift().doubleCol, 4);
|
|
|
|
TestCase.assertEqual(array.length, 0);
|
|
|
|
|
|
|
|
TestCase.assertEqual(array.shift(), undefined);
|
|
|
|
|
|
|
|
TestCase.assertThrows(function() {
|
|
|
|
array.shift(1);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-10-12 09:23:09 +00:00
|
|
|
TestCase.assertThrows(function() {
|
|
|
|
array.shift();
|
|
|
|
}, 'can only shift in a write transaction');
|
2015-08-28 22:11:50 +00:00
|
|
|
},
|
|
|
|
|
2016-02-28 20:35:54 +00:00
|
|
|
testListSplice: function() {
|
2015-10-06 07:57:35 +00:00
|
|
|
var realm = new Realm({schema: [schemas.LinkTypes, schemas.TestObject]});
|
2015-10-12 09:23:09 +00:00
|
|
|
var array;
|
2015-09-28 23:00:24 +00:00
|
|
|
|
2015-08-28 22:11:50 +00:00
|
|
|
realm.write(function() {
|
2015-11-02 20:02:53 +00:00
|
|
|
var obj = realm.create('LinkTypesObject', {
|
|
|
|
objectCol: {doubleCol: 1},
|
|
|
|
objectCol1: {doubleCol: 2},
|
|
|
|
arrayCol: [{doubleCol: 3}, {doubleCol: 4}],
|
|
|
|
});
|
2015-08-28 22:11:50 +00:00
|
|
|
|
2015-10-12 09:23:09 +00:00
|
|
|
array = obj.arrayCol;
|
2015-11-02 20:02:53 +00:00
|
|
|
var removed;
|
2015-10-12 09:23:09 +00:00
|
|
|
|
2015-09-28 23:00:24 +00:00
|
|
|
removed = array.splice(0, 0, obj.objectCol, obj.objectCol1);
|
|
|
|
TestCase.assertEqual(removed.length, 0);
|
2015-08-28 22:11:50 +00:00
|
|
|
TestCase.assertEqual(array.length, 4);
|
|
|
|
TestCase.assertEqual(array[0].doubleCol, 1);
|
|
|
|
TestCase.assertEqual(array[1].doubleCol, 2);
|
|
|
|
|
2015-11-02 20:02:53 +00:00
|
|
|
removed = array.splice(2, 2, {doubleCol: 5}, {doubleCol: 6});
|
2015-09-28 23:00:24 +00:00
|
|
|
TestCase.assertEqual(removed.length, 2);
|
|
|
|
TestCase.assertEqual(removed[0].doubleCol, 3);
|
|
|
|
TestCase.assertEqual(removed[1].doubleCol, 4);
|
2015-08-28 22:11:50 +00:00
|
|
|
TestCase.assertEqual(array.length, 4);
|
|
|
|
TestCase.assertEqual(array[2].doubleCol, 5);
|
|
|
|
TestCase.assertEqual(array[3].doubleCol, 6);
|
|
|
|
|
2015-09-28 23:00:24 +00:00
|
|
|
removed = array.splice(2, 2);
|
|
|
|
TestCase.assertEqual(removed.length, 2);
|
|
|
|
TestCase.assertEqual(removed[0].doubleCol, 5);
|
|
|
|
TestCase.assertEqual(removed[1].doubleCol, 6);
|
|
|
|
TestCase.assertEqual(array.length, 2);
|
2015-08-28 22:11:50 +00:00
|
|
|
TestCase.assertEqual(array[0].doubleCol, 1);
|
|
|
|
TestCase.assertEqual(array[1].doubleCol, 2);
|
|
|
|
|
2015-09-28 23:00:24 +00:00
|
|
|
removed = array.splice(-1, 1);
|
|
|
|
TestCase.assertEqual(removed.length, 1);
|
|
|
|
TestCase.assertEqual(removed[0].doubleCol, 2);
|
2015-08-28 22:11:50 +00:00
|
|
|
TestCase.assertEqual(array.length, 1);
|
|
|
|
TestCase.assertEqual(array[0].doubleCol, 1);
|
|
|
|
|
2015-09-28 23:00:24 +00:00
|
|
|
removed = array.splice(0, 2);
|
|
|
|
TestCase.assertEqual(removed.length, 1);
|
|
|
|
TestCase.assertEqual(removed[0].doubleCol, 1);
|
|
|
|
TestCase.assertEqual(array.length, 0);
|
2015-10-12 23:50:45 +00:00
|
|
|
|
|
|
|
removed = array.splice('0', '0', obj.objectCol);
|
|
|
|
TestCase.assertEqual(removed.length, 0);
|
|
|
|
TestCase.assertEqual(array.length, 1);
|
|
|
|
|
2016-03-17 21:53:42 +00:00
|
|
|
removed = array.splice(1);
|
|
|
|
TestCase.assertEqual(removed.length, 0);
|
|
|
|
TestCase.assertEqual(array.length, 1);
|
|
|
|
|
|
|
|
removed = array.splice(0);
|
|
|
|
TestCase.assertEqual(removed.length, 1);
|
|
|
|
TestCase.assertEqual(array.length, 0);
|
|
|
|
|
2015-10-12 23:50:45 +00:00
|
|
|
TestCase.assertThrows(function() {
|
|
|
|
array.splice('cat', 1);
|
|
|
|
});
|
|
|
|
|
2015-08-28 22:11:50 +00:00
|
|
|
TestCase.assertThrows(function() {
|
|
|
|
array.splice(0, 0, 0);
|
|
|
|
});
|
|
|
|
});
|
2015-10-12 09:23:09 +00:00
|
|
|
|
|
|
|
TestCase.assertThrows(function() {
|
2015-11-02 20:02:53 +00:00
|
|
|
array.splice(0, 0, {doubleCol: 1});
|
2015-10-12 09:23:09 +00:00
|
|
|
}, 'can only splice in a write transaction');
|
2015-08-28 22:11:50 +00:00
|
|
|
},
|
2015-10-28 17:21:32 +00:00
|
|
|
|
2016-02-28 20:35:54 +00:00
|
|
|
testListDeletions: function() {
|
2015-10-28 17:21:32 +00:00
|
|
|
var realm = new Realm({schema: [schemas.LinkTypes, schemas.TestObject]});
|
2015-12-01 22:05:33 +00:00
|
|
|
var object;
|
2015-10-28 17:21:32 +00:00
|
|
|
var array;
|
|
|
|
|
|
|
|
realm.write(function() {
|
2015-11-02 20:02:53 +00:00
|
|
|
object = realm.create('LinkTypesObject', {
|
|
|
|
objectCol: {doubleCol: 1},
|
|
|
|
objectCol1: {doubleCol: 2},
|
|
|
|
arrayCol: [{doubleCol: 3}, {doubleCol: 4}],
|
|
|
|
});
|
|
|
|
|
2015-12-01 22:05:33 +00:00
|
|
|
array = object.arrayCol;
|
2015-10-28 17:21:32 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
try {
|
|
|
|
realm.write(function() {
|
|
|
|
realm.delete(array[0]);
|
|
|
|
TestCase.assertEqual(array.length, 1);
|
|
|
|
TestCase.assertEqual(array[0].doubleCol, 4);
|
|
|
|
|
2015-10-28 17:45:42 +00:00
|
|
|
// This should cancel the transaction and cause the list to be reset.
|
2015-10-28 17:21:32 +00:00
|
|
|
throw new Error('Transaction FAIL');
|
|
|
|
});
|
|
|
|
} catch (e) {}
|
|
|
|
|
|
|
|
TestCase.assertEqual(array.length, 2);
|
|
|
|
TestCase.assertEqual(array[0].doubleCol, 3);
|
2015-12-01 22:05:33 +00:00
|
|
|
|
|
|
|
realm.write(function() {
|
|
|
|
realm.delete(object);
|
|
|
|
});
|
|
|
|
|
|
|
|
TestCase.assertThrows(function() {
|
|
|
|
array[0];
|
|
|
|
});
|
2015-10-28 17:21:32 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
testLiveUpdatingResults: function() {
|
|
|
|
var realm = new Realm({schema: [schemas.LinkTypes, schemas.TestObject]});
|
|
|
|
var objects = realm.objects('TestObject');
|
|
|
|
var array;
|
|
|
|
|
|
|
|
realm.write(function() {
|
2015-11-02 20:02:53 +00:00
|
|
|
var obj = realm.create('LinkTypesObject', {
|
|
|
|
objectCol: {doubleCol: 1},
|
|
|
|
objectCol1: {doubleCol: 2},
|
|
|
|
arrayCol: [{doubleCol: 3}, {doubleCol: 4}],
|
|
|
|
});
|
|
|
|
|
2015-10-28 17:21:32 +00:00
|
|
|
array = obj.arrayCol;
|
|
|
|
});
|
|
|
|
|
2015-11-02 20:02:53 +00:00
|
|
|
TestCase.assertEqual(array.length, 2);
|
2015-10-28 17:21:32 +00:00
|
|
|
TestCase.assertEqual(objects.length, 4);
|
|
|
|
|
|
|
|
try {
|
|
|
|
realm.write(function() {
|
2015-11-02 20:02:53 +00:00
|
|
|
array.push({doubleCol: 5});
|
2015-10-28 17:21:32 +00:00
|
|
|
TestCase.assertEqual(objects.length, 5);
|
|
|
|
|
2015-11-02 20:02:53 +00:00
|
|
|
array.unshift({doubleCol: 2});
|
2015-10-28 17:21:32 +00:00
|
|
|
TestCase.assertEqual(objects.length, 6);
|
|
|
|
|
2015-11-02 20:02:53 +00:00
|
|
|
array.splice(0, 0, {doubleCol: 1});
|
2015-10-28 17:21:32 +00:00
|
|
|
TestCase.assertEqual(objects.length, 7);
|
|
|
|
|
|
|
|
array.push(objects[0], objects[1]);
|
|
|
|
TestCase.assertEqual(objects.length, 7);
|
|
|
|
|
2015-10-28 17:45:42 +00:00
|
|
|
// This should cancel the transaction and cause the list and results to be reset.
|
2015-10-28 17:21:32 +00:00
|
|
|
throw new Error('Transaction FAIL');
|
|
|
|
});
|
|
|
|
} catch (e) {}
|
|
|
|
|
|
|
|
TestCase.assertEqual(array.length, 2);
|
|
|
|
TestCase.assertEqual(objects.length, 4);
|
|
|
|
},
|
2015-11-20 22:05:18 +00:00
|
|
|
|
2016-02-28 20:35:54 +00:00
|
|
|
testListSnapshot: function() {
|
2015-11-20 22:05:18 +00:00
|
|
|
var realm = new Realm({schema: [schemas.LinkTypes, schemas.TestObject]});
|
|
|
|
var objects = realm.objects('TestObject');
|
|
|
|
var array;
|
|
|
|
|
|
|
|
realm.write(function() {
|
|
|
|
var obj = realm.create('LinkTypesObject', [[1], [2], [[3], [4]]]);
|
|
|
|
array = obj.arrayCol;
|
|
|
|
});
|
|
|
|
|
|
|
|
var objectsCopy = objects.snapshot();
|
|
|
|
var arrayCopy = array.snapshot();
|
|
|
|
|
|
|
|
TestCase.assertEqual(objectsCopy.length, 4);
|
|
|
|
TestCase.assertEqual(arrayCopy.length, 2);
|
|
|
|
|
|
|
|
realm.write(function() {
|
|
|
|
array.push([5]);
|
|
|
|
TestCase.assertEqual(objectsCopy.length, 4);
|
|
|
|
TestCase.assertEqual(arrayCopy.length, 2);
|
|
|
|
|
|
|
|
TestCase.assertEqual(objectsCopy.snapshot().length, 4);
|
|
|
|
TestCase.assertEqual(arrayCopy.snapshot().length, 2);
|
|
|
|
|
|
|
|
TestCase.assertEqual(objects.snapshot().length, 5);
|
|
|
|
TestCase.assertEqual(array.snapshot().length, 3);
|
|
|
|
|
|
|
|
realm.delete(array[0]);
|
|
|
|
TestCase.assertEqual(objectsCopy.length, 4);
|
|
|
|
TestCase.assertEqual(arrayCopy.length, 2);
|
|
|
|
TestCase.assertEqual(arrayCopy[0], null);
|
2016-05-20 20:42:17 +00:00
|
|
|
|
|
|
|
realm.deleteAll();
|
|
|
|
TestCase.assertEqual(objectsCopy.length, 4);
|
|
|
|
TestCase.assertEqual(arrayCopy.length, 2);
|
|
|
|
TestCase.assertEqual(arrayCopy[1], null);
|
2015-11-20 22:05:18 +00:00
|
|
|
});
|
|
|
|
},
|
2016-02-18 04:24:08 +00:00
|
|
|
|
|
|
|
testListFiltered: function() {
|
2016-02-18 21:41:11 +00:00
|
|
|
var realm = new Realm({schema: [schemas.PersonObject, schemas.PersonList]});
|
|
|
|
var list;
|
|
|
|
|
2016-02-18 04:24:08 +00:00
|
|
|
realm.write(function() {
|
2016-02-18 21:41:11 +00:00
|
|
|
var object = realm.create('PersonList', {list: [
|
2016-02-18 04:24:08 +00:00
|
|
|
{name: 'Ari', age: 10},
|
|
|
|
{name: 'Tim', age: 11},
|
|
|
|
{name: 'Bjarne', age: 12},
|
|
|
|
{name: 'Alex', age: 12, married: true}
|
|
|
|
]});
|
|
|
|
realm.create('PersonObject', {name: 'NotInList', age: 10});
|
2016-02-18 21:41:11 +00:00
|
|
|
|
|
|
|
list = object.list;
|
2016-02-18 04:24:08 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
TestCase.assertEqual(list.filtered("truepredicate").length, 4);
|
|
|
|
TestCase.assertEqual(list.filtered('age = 11')[0].name, 'Tim');
|
|
|
|
TestCase.assertEqual(list.filtered('age = 12').length, 2);
|
|
|
|
TestCase.assertEqual(list.filtered('age > 10 && age < 13').length, 3);
|
|
|
|
TestCase.assertEqual(list.filtered('age > 10').filtered('age < 13').length, 3);
|
|
|
|
},
|
2016-02-18 21:41:11 +00:00
|
|
|
|
|
|
|
testListSorted: function() {
|
2017-09-21 17:12:10 +00:00
|
|
|
const schema = [
|
|
|
|
{name: 'Target', properties: {value: 'int'}},
|
|
|
|
{name: 'Mid', properties: {value: 'int', link: 'Target'}},
|
|
|
|
{name: 'List', properties: {list: {type: 'list', objectType: 'Mid'}}},
|
|
|
|
];
|
|
|
|
const realm = new Realm({schema: schema});
|
|
|
|
|
|
|
|
let list;
|
|
|
|
realm.write(() => {
|
|
|
|
list = realm.create('List', {list: [
|
|
|
|
{value: 3, link: {value: 1}},
|
|
|
|
{value: 1, link: {value: 3}},
|
|
|
|
{value: 2, link: {value: 2}},
|
|
|
|
]}).list;
|
|
|
|
realm.create('List', {list: [
|
|
|
|
{value: 4, link: {value: 4}},
|
2016-02-18 21:41:11 +00:00
|
|
|
]});
|
|
|
|
});
|
|
|
|
|
2017-09-21 17:12:10 +00:00
|
|
|
const values = (results) => results.map((o) => o.value);
|
|
|
|
|
|
|
|
TestCase.assertThrows(() => list.sorted());
|
|
|
|
TestCase.assertThrows(() => list.sorted('nonexistent property'));
|
|
|
|
TestCase.assertThrows(() => list.sorted('link'));
|
|
|
|
|
|
|
|
TestCase.assertArraysEqual(values(list.sorted([])), [3, 1, 2]);
|
2016-02-18 21:41:11 +00:00
|
|
|
|
2017-09-21 17:12:10 +00:00
|
|
|
TestCase.assertArraysEqual(values(list.sorted('value')), [1, 2, 3]);
|
|
|
|
TestCase.assertArraysEqual(values(list.sorted('value', false)), [1, 2, 3]);
|
|
|
|
TestCase.assertArraysEqual(values(list.sorted('value', true)), [3, 2, 1]);
|
|
|
|
TestCase.assertArraysEqual(values(list.sorted(['value'])), [1, 2, 3]);
|
|
|
|
TestCase.assertArraysEqual(values(list.sorted([['value', false]])), [1, 2, 3]);
|
|
|
|
TestCase.assertArraysEqual(values(list.sorted([['value', true]])), [3, 2, 1]);
|
2016-02-18 21:41:11 +00:00
|
|
|
|
2017-09-21 17:12:10 +00:00
|
|
|
TestCase.assertArraysEqual(values(list.sorted('link.value')), [3, 2, 1]);
|
|
|
|
TestCase.assertArraysEqual(values(list.sorted('link.value', false)), [3, 2, 1]);
|
|
|
|
TestCase.assertArraysEqual(values(list.sorted('link.value', true)), [1, 2, 3]);
|
|
|
|
TestCase.assertArraysEqual(values(list.sorted(['link.value'])), [3, 2, 1]);
|
|
|
|
TestCase.assertArraysEqual(values(list.sorted([['link.value', false]])), [3, 2, 1]);
|
|
|
|
TestCase.assertArraysEqual(values(list.sorted([['link.value', true]])), [1, 2, 3]);
|
2016-02-18 21:41:11 +00:00
|
|
|
},
|
2016-02-29 05:40:27 +00:00
|
|
|
|
|
|
|
testArrayMethods: function() {
|
|
|
|
var realm = new Realm({schema: [schemas.PersonObject, schemas.PersonList]});
|
|
|
|
var object;
|
|
|
|
|
|
|
|
realm.write(function() {
|
|
|
|
object = realm.create('PersonList', {list: [
|
|
|
|
{name: 'Ari', age: 10},
|
|
|
|
{name: 'Tim', age: 11},
|
|
|
|
{name: 'Bjarne', age: 12},
|
|
|
|
]});
|
|
|
|
});
|
|
|
|
|
|
|
|
[
|
|
|
|
object.list,
|
|
|
|
realm.objects('PersonObject'),
|
|
|
|
].forEach(function(list) {
|
|
|
|
TestCase.assertEqual(list.slice().length, 3);
|
|
|
|
TestCase.assertEqual(list.slice(-1).length, 1);
|
|
|
|
TestCase.assertEqual(list.slice(-1)[0].age, 12);
|
|
|
|
TestCase.assertEqual(list.slice(1, 3).length, 2);
|
|
|
|
TestCase.assertEqual(list.slice(1, 3)[1].age, 12);
|
|
|
|
|
2016-05-04 09:15:17 +00:00
|
|
|
// A Node 6 regression in v8 causes an error when converting our objects to strings:
|
|
|
|
// TypeError: Cannot convert a Symbol value to a string
|
2016-05-10 18:34:33 +00:00
|
|
|
if (!TestCase.isNode6()) {
|
2016-05-04 09:15:17 +00:00
|
|
|
TestCase.assertEqual(list.join(' '), 'Ari Tim Bjarne');
|
|
|
|
}
|
2016-02-29 05:40:27 +00:00
|
|
|
|
|
|
|
var count = 0;
|
|
|
|
list.forEach(function(p, i) {
|
|
|
|
TestCase.assertEqual(p.name, list[i].name);
|
|
|
|
count++;
|
|
|
|
});
|
|
|
|
TestCase.assertEqual(count, list.length);
|
|
|
|
|
|
|
|
TestCase.assertArraysEqual(list.map(function(p) {return p.age}), [10, 11, 12]);
|
|
|
|
TestCase.assertTrue(list.some(function(p) {return p.age > 10}));
|
|
|
|
TestCase.assertTrue(list.every(function(p) {return p.age > 0}));
|
|
|
|
|
|
|
|
var person = list.find(function(p) {return p.name == 'Tim'});
|
|
|
|
TestCase.assertEqual(person.name, 'Tim');
|
|
|
|
|
|
|
|
var index = list.findIndex(function(p) {return p.name == 'Tim'});
|
|
|
|
TestCase.assertEqual(index, 1);
|
2017-06-26 11:43:49 +00:00
|
|
|
TestCase.assertEqual(list.indexOf(list[index]), index);
|
2016-02-29 05:40:27 +00:00
|
|
|
|
|
|
|
TestCase.assertEqual(list.reduce(function(n, p) {return n + p.age}, 0), 33);
|
|
|
|
TestCase.assertEqual(list.reduceRight(function(n, p) {return n + p.age}, 0), 33);
|
|
|
|
|
2016-03-20 02:55:19 +00:00
|
|
|
// eslint-disable-next-line no-undef
|
|
|
|
var iteratorMethodNames = ['entries', 'keys', 'values'];
|
|
|
|
|
2016-12-20 18:56:51 +00:00
|
|
|
iteratorMethodNames.push(Symbol.iterator);
|
2016-03-03 07:00:53 +00:00
|
|
|
|
2016-03-20 02:55:19 +00:00
|
|
|
iteratorMethodNames.forEach(function(methodName) {
|
2016-03-03 06:44:11 +00:00
|
|
|
var iterator = list[methodName]();
|
|
|
|
var count = 0;
|
|
|
|
var result;
|
2016-03-20 02:55:19 +00:00
|
|
|
|
2016-12-20 18:56:51 +00:00
|
|
|
// This iterator should itself be iterable.
|
|
|
|
// TestCase.assertEqual(iterator[iteratorSymbol](), iterator);
|
|
|
|
TestCase.assertEqual(iterator[Symbol.iterator](), iterator);
|
2016-03-03 06:44:11 +00:00
|
|
|
|
|
|
|
while ((result = iterator.next()) && !result.done) {
|
|
|
|
var value = result.value;
|
|
|
|
|
|
|
|
switch (methodName) {
|
|
|
|
case 'entries':
|
|
|
|
TestCase.assertEqual(value.length, 2);
|
|
|
|
TestCase.assertEqual(value[0], count);
|
|
|
|
TestCase.assertEqual(value[1].name, list[count].name);
|
|
|
|
break;
|
|
|
|
case 'keys':
|
|
|
|
TestCase.assertEqual(value, count);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
TestCase.assertEqual(value.name, list[count].name);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
TestCase.assertEqual(result.done, true);
|
|
|
|
TestCase.assertEqual(result.value, undefined);
|
|
|
|
TestCase.assertEqual(count, list.length);
|
|
|
|
});
|
2016-02-29 05:40:27 +00:00
|
|
|
});
|
|
|
|
},
|
2016-05-16 23:01:14 +00:00
|
|
|
|
|
|
|
testIsValid: function() {
|
|
|
|
var realm = new Realm({schema: [schemas.PersonObject, schemas.PersonList]});
|
|
|
|
var object;
|
|
|
|
var list;
|
|
|
|
realm.write(function() {
|
|
|
|
object = realm.create('PersonList', {list: [
|
|
|
|
{name: 'Ari', age: 10},
|
|
|
|
{name: 'Tim', age: 11},
|
|
|
|
{name: 'Bjarne', age: 12},
|
|
|
|
]});
|
|
|
|
list = object.list;
|
|
|
|
TestCase.assertEqual(list.isValid(), true);
|
|
|
|
realm.delete(object);
|
|
|
|
});
|
|
|
|
|
|
|
|
TestCase.assertEqual(list.isValid(), false);
|
|
|
|
TestCase.assertThrows(function() {
|
|
|
|
list.length;
|
|
|
|
});
|
|
|
|
},
|
2016-05-20 22:54:20 +00:00
|
|
|
};
|