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 15:18:49 +00:00
|
|
|
|
2015-08-13 16:12:48 +00:00
|
|
|
'use strict';
|
|
|
|
|
2015-10-08 23:19:19 +00:00
|
|
|
var Realm = require('realm');
|
2015-10-14 22:25:58 +00:00
|
|
|
var BaseTest = require('./base-test');
|
2015-10-06 07:57:35 +00:00
|
|
|
var TestCase = require('./asserts');
|
|
|
|
var schemas = require('./schemas');
|
|
|
|
|
2015-10-14 22:25:58 +00:00
|
|
|
module.exports = BaseTest.extend({
|
2016-02-28 21:07:05 +00:00
|
|
|
testResultsConstructor: function() {
|
|
|
|
var realm = new Realm({schema: [schemas.TestObject]});
|
|
|
|
var objects = realm.objects('TestObject');
|
|
|
|
|
|
|
|
TestCase.assertTrue(objects instanceof Realm.Results);
|
2016-03-09 07:47:08 +00:00
|
|
|
TestCase.assertTrue(objects instanceof Realm.Collection);
|
2016-02-28 21:07:05 +00:00
|
|
|
|
|
|
|
TestCase.assertThrows(function() {
|
|
|
|
new Realm.Results();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2015-08-13 16:12:48 +00:00
|
|
|
testResultsLength: function() {
|
2015-10-06 07:57:35 +00:00
|
|
|
var realm = new Realm({schema: [schemas.TestObject]});
|
2015-08-13 16:12:48 +00:00
|
|
|
var objects = realm.objects('TestObject');
|
|
|
|
TestCase.assertEqual(objects.length, 0);
|
|
|
|
|
|
|
|
realm.write(function() {
|
2015-11-02 20:02:53 +00:00
|
|
|
realm.create('TestObject', {doubleCol: 1});
|
2015-08-13 16:12:48 +00:00
|
|
|
TestCase.assertEqual(objects.length, 1);
|
|
|
|
});
|
|
|
|
TestCase.assertEqual(objects.length, 1);
|
|
|
|
},
|
2016-02-28 21:07:05 +00:00
|
|
|
|
2015-08-13 16:12:48 +00:00
|
|
|
testResultsSubscript: function() {
|
2015-10-06 07:57:35 +00:00
|
|
|
var realm = new Realm({schema: [schemas.PersonObject]});
|
2016-04-19 23:03:18 +00:00
|
|
|
TestCase.assertEqual(realm.objects('PersonObject')[0], undefined);
|
2016-04-27 21:01:01 +00:00
|
|
|
|
2015-08-13 16:12:48 +00:00
|
|
|
realm.write(function() {
|
2015-11-02 20:02:53 +00:00
|
|
|
realm.create('PersonObject', {name: 'name1', age: 1});
|
|
|
|
realm.create('PersonObject', {name: 'name2', age: 2});
|
2015-08-13 16:12:48 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
var people = realm.objects('PersonObject');
|
|
|
|
TestCase.assertEqual(people[0].age, 1);
|
|
|
|
TestCase.assertEqual(people[1].age, 2);
|
2015-10-12 22:35:13 +00:00
|
|
|
TestCase.assertEqual(people[2], undefined);
|
|
|
|
TestCase.assertEqual(people[-1], undefined);
|
2015-10-06 07:57:35 +00:00
|
|
|
TestCase.assertTrue(Object.getPrototypeOf(people[0]) === schemas.PersonObject.prototype);
|
2015-10-21 22:17:55 +00:00
|
|
|
TestCase.assertTrue(people[0] instanceof schemas.PersonObject);
|
2015-08-13 16:12:48 +00:00
|
|
|
},
|
2016-02-28 21:07:05 +00:00
|
|
|
|
2015-10-21 22:25:15 +00:00
|
|
|
testResultsReadonly: function() {
|
|
|
|
var realm = new Realm({schema: [schemas.TestObject]});
|
|
|
|
var objects = realm.objects('TestObject');
|
|
|
|
|
|
|
|
realm.write(function() {
|
2015-11-02 20:02:53 +00:00
|
|
|
realm.create('TestObject', {doubleCol: 1});
|
2015-10-21 22:25:15 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
TestCase.assertThrows(function() {
|
2015-11-02 20:02:53 +00:00
|
|
|
objects[-1] = {doubleCol: 0};
|
2015-10-21 22:25:15 +00:00
|
|
|
});
|
|
|
|
TestCase.assertThrows(function() {
|
2015-11-02 20:02:53 +00:00
|
|
|
objects[0] = {doubleCol: 0};
|
2015-10-21 22:25:15 +00:00
|
|
|
});
|
|
|
|
TestCase.assertThrows(function() {
|
2015-11-02 20:02:53 +00:00
|
|
|
objects[1] = {doubleCol: 0};
|
2015-10-21 22:25:15 +00:00
|
|
|
});
|
|
|
|
TestCase.assertThrows(function() {
|
|
|
|
objects.length = 0;
|
|
|
|
});
|
|
|
|
},
|
2016-02-28 21:07:05 +00:00
|
|
|
|
2015-08-13 16:12:48 +00:00
|
|
|
testResultsInvalidProperty: function() {
|
2015-10-06 07:57:35 +00:00
|
|
|
var realm = new Realm({schema: [schemas.TestObject]});
|
2015-08-13 16:12:48 +00:00
|
|
|
var objects = realm.objects('TestObject');
|
|
|
|
TestCase.assertEqual(undefined, objects.ablasdf);
|
|
|
|
},
|
2016-02-28 21:07:05 +00:00
|
|
|
|
2015-09-01 23:23:42 +00:00
|
|
|
testResultsInvalidObjectType: function() {
|
2015-10-06 07:57:35 +00:00
|
|
|
var realm = new Realm({schema: [schemas.TestObject]});
|
2015-09-01 23:23:42 +00:00
|
|
|
TestCase.assertThrows(function() {
|
|
|
|
var objects = realm.objects('NotTestObject');
|
|
|
|
});
|
|
|
|
},
|
2016-02-28 21:07:05 +00:00
|
|
|
|
2015-08-14 17:47:56 +00:00
|
|
|
testResultsEnumerate: function() {
|
2015-10-06 07:57:35 +00:00
|
|
|
var realm = new Realm({schema: [schemas.TestObject]});
|
2015-08-13 16:12:48 +00:00
|
|
|
var objects = realm.objects('TestObject');
|
2015-11-02 20:02:53 +00:00
|
|
|
|
|
|
|
for (var index in objects) {
|
2015-08-13 16:12:48 +00:00
|
|
|
TestCase.assertTrue(false, "No objects should have been enumerated");
|
|
|
|
}
|
|
|
|
|
|
|
|
realm.write(function() {
|
2015-11-02 20:02:53 +00:00
|
|
|
realm.create('TestObject', {doubleCol: 1});
|
2015-08-13 16:12:48 +00:00
|
|
|
TestCase.assertEqual(objects.length, 1);
|
|
|
|
});
|
|
|
|
|
|
|
|
var count = 0;
|
2015-10-21 22:21:17 +00:00
|
|
|
var keys = Object.keys(objects);
|
|
|
|
for (var index in objects) {
|
|
|
|
TestCase.assertEqual(count++, +index);
|
|
|
|
TestCase.assertEqual(keys[index], index);
|
2016-04-27 21:01:01 +00:00
|
|
|
}
|
2015-10-21 22:21:17 +00:00
|
|
|
|
|
|
|
TestCase.assertEqual(count, 1);
|
|
|
|
TestCase.assertEqual(keys.length, 1);
|
2015-08-13 16:12:48 +00:00
|
|
|
},
|
2016-02-28 21:07:05 +00:00
|
|
|
|
2016-02-18 04:06:42 +00:00
|
|
|
testResultsFiltered: function() {
|
|
|
|
var realm = new Realm({schema: [schemas.PersonObject, schemas.DefaultValues, schemas.TestObject]});
|
|
|
|
realm.write(function() {
|
|
|
|
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.assertEqual(realm.objects('PersonObject').filtered("truepredicate").length, 4);
|
|
|
|
TestCase.assertEqual(realm.objects('PersonObject').length, 4);
|
|
|
|
TestCase.assertEqual(realm.objects('PersonObject').filtered('age = 11').length, 1);
|
|
|
|
TestCase.assertEqual(realm.objects('PersonObject').filtered('age = 11')[0].name, 'Tim');
|
|
|
|
TestCase.assertEqual(realm.objects('PersonObject').filtered('age = 12').length, 2);
|
|
|
|
TestCase.assertEqual(realm.objects('PersonObject').filtered('age = 13').length, 0);
|
|
|
|
TestCase.assertEqual(realm.objects('PersonObject').filtered('age < 12').length, 2);
|
|
|
|
TestCase.assertEqual(realm.objects('PersonObject').filtered('age > 10 && age < 13').length, 3);
|
2016-02-18 04:09:39 +00:00
|
|
|
TestCase.assertEqual(realm.objects('PersonObject').filtered('age > 10').filtered('age < 13').length, 3);
|
|
|
|
|
2016-02-18 04:06:42 +00:00
|
|
|
TestCase.assertEqual(realm.objects('PersonObject').filtered('age >= 11 && age < 13').length, 3);
|
|
|
|
TestCase.assertEqual(realm.objects('PersonObject').filtered('name = "Tim"').length, 1);
|
|
|
|
TestCase.assertEqual(realm.objects('PersonObject').filtered('name = \'Tim\'').length, 1);
|
|
|
|
TestCase.assertEqual(realm.objects('PersonObject').filtered('married == TRUE').length, 1);
|
|
|
|
TestCase.assertEqual(realm.objects('PersonObject').filtered('married == false').length, 3);
|
|
|
|
|
|
|
|
TestCase.assertEqual(realm.objects('PersonObject').filtered('name = $0', 'Tim').length, 1);
|
|
|
|
TestCase.assertEqual(realm.objects('PersonObject').filtered('age > $1 && age < $0', 13, 10).length, 3);
|
|
|
|
TestCase.assertThrows(function() {
|
|
|
|
realm.objects('PersonObject').filtered('age > $2 && age < $0', 13, 10)
|
|
|
|
});
|
|
|
|
|
|
|
|
realm.write(function() {
|
|
|
|
realm.create('DefaultValuesObject', {'dateCol': new Date(3)});
|
|
|
|
realm.create('DefaultValuesObject', {'dateCol': new Date(4)});
|
|
|
|
realm.create('DefaultValuesObject', {'dateCol': new Date(5)});
|
|
|
|
});
|
|
|
|
|
|
|
|
TestCase.assertEqual(realm.objects('DefaultValuesObject').filtered('dateCol > $0', new Date(4)).length, 1);
|
|
|
|
TestCase.assertEqual(realm.objects('DefaultValuesObject').filtered('dateCol <= $0', new Date(4)).length, 2);
|
2016-02-18 04:51:03 +00:00
|
|
|
|
|
|
|
TestCase.assertThrows(function() {
|
|
|
|
realm.objects('PersonObject').filtered("invalidQuery");
|
|
|
|
});
|
2016-02-18 04:06:42 +00:00
|
|
|
},
|
2016-02-28 21:07:05 +00:00
|
|
|
|
2016-02-18 21:41:11 +00:00
|
|
|
testResultsSorted: function() {
|
2016-01-13 10:40:26 +00:00
|
|
|
var realm = new Realm({schema: [schemas.IntPrimary]});
|
|
|
|
var objects = realm.objects('IntPrimaryObject');
|
2015-11-02 20:02:53 +00:00
|
|
|
|
2015-09-03 22:46:31 +00:00
|
|
|
realm.write(function() {
|
2016-01-13 10:40:26 +00:00
|
|
|
realm.create('IntPrimaryObject', {primaryCol: 2, valueCol: 'a'});
|
|
|
|
realm.create('IntPrimaryObject', {primaryCol: 3, valueCol: 'a'});
|
|
|
|
realm.create('IntPrimaryObject', {primaryCol: 1, valueCol: 'b'});
|
|
|
|
realm.create('IntPrimaryObject', {primaryCol: 4, valueCol: 'c'});
|
|
|
|
realm.create('IntPrimaryObject', {primaryCol: 0, valueCol: 'c'});
|
|
|
|
});
|
|
|
|
|
|
|
|
var primaries = function(results, prop) {
|
2016-02-29 05:40:27 +00:00
|
|
|
return results.map(function(object) {
|
2016-01-13 10:40:26 +00:00
|
|
|
return object.primaryCol;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2016-02-18 20:47:18 +00:00
|
|
|
objects = objects.sorted('primaryCol');
|
2016-01-13 10:40:26 +00:00
|
|
|
TestCase.assertArraysEqual(primaries(objects), [0, 1, 2, 3, 4]);
|
|
|
|
|
2016-02-18 20:47:18 +00:00
|
|
|
objects = objects.sorted('primaryCol', true);
|
2016-01-13 10:40:26 +00:00
|
|
|
TestCase.assertArraysEqual(primaries(objects), [4, 3, 2, 1, 0]);
|
|
|
|
|
2016-02-18 20:47:18 +00:00
|
|
|
objects = objects.sorted(['primaryCol', 'valueCol']);
|
2016-01-13 10:40:26 +00:00
|
|
|
TestCase.assertArraysEqual(primaries(objects), [0, 1, 2, 3, 4]);
|
|
|
|
|
2016-02-18 20:47:18 +00:00
|
|
|
objects = objects.sorted([['primaryCol', true], ['valueCol', true]]);
|
2016-01-13 10:40:26 +00:00
|
|
|
TestCase.assertArraysEqual(primaries(objects), [4, 3, 2, 1, 0]);
|
|
|
|
|
2016-02-18 20:47:18 +00:00
|
|
|
objects = objects.sorted([['primaryCol', false], 'valueCol']);
|
2016-01-13 10:40:26 +00:00
|
|
|
TestCase.assertArraysEqual(primaries(objects), [0, 1, 2, 3, 4]);
|
|
|
|
|
2016-02-18 20:47:18 +00:00
|
|
|
objects = objects.sorted(['valueCol', 'primaryCol']);
|
2016-01-13 10:40:26 +00:00
|
|
|
TestCase.assertArraysEqual(primaries(objects), [2, 3, 1, 0, 4]);
|
|
|
|
|
2016-02-18 20:47:18 +00:00
|
|
|
objects = objects.sorted([['valueCol', false], ['primaryCol', true]]);
|
2016-01-13 10:40:26 +00:00
|
|
|
TestCase.assertArraysEqual(primaries(objects), [3, 2, 1, 4, 0]);
|
|
|
|
|
2016-02-18 20:47:18 +00:00
|
|
|
objects = objects.sorted([['valueCol', true], ['primaryCol', false]]);
|
2016-01-13 10:40:26 +00:00
|
|
|
TestCase.assertArraysEqual(primaries(objects), [0, 4, 1, 2, 3]);
|
|
|
|
|
2016-02-18 20:47:18 +00:00
|
|
|
objects = objects.sorted([['valueCol', true], ['primaryCol', true]]);
|
2016-01-13 10:40:26 +00:00
|
|
|
TestCase.assertArraysEqual(primaries(objects), [4, 0, 1, 3, 2]);
|
|
|
|
|
2016-02-18 20:55:54 +00:00
|
|
|
TestCase.assertThrows(function() {
|
|
|
|
objects.sorted();
|
|
|
|
});
|
2016-01-13 10:40:26 +00:00
|
|
|
TestCase.assertThrows(function() {
|
2016-02-18 20:47:18 +00:00
|
|
|
objects.sorted(1);
|
2016-01-13 10:40:26 +00:00
|
|
|
});
|
|
|
|
TestCase.assertThrows(function() {
|
2016-02-18 20:47:18 +00:00
|
|
|
objects.sorted([1]);
|
2016-01-13 10:40:26 +00:00
|
|
|
});
|
2016-02-18 20:55:54 +00:00
|
|
|
TestCase.assertThrows(function() {
|
|
|
|
objects.sorted([]);
|
|
|
|
});
|
2016-01-13 10:40:26 +00:00
|
|
|
TestCase.assertThrows(function() {
|
2016-02-18 20:47:18 +00:00
|
|
|
objects.sorted('fish');
|
2016-01-13 10:40:26 +00:00
|
|
|
});
|
|
|
|
TestCase.assertThrows(function() {
|
2016-02-18 20:47:18 +00:00
|
|
|
objects.sorted(['valueCol', 'fish']);
|
|
|
|
});
|
|
|
|
TestCase.assertThrows(function() {
|
|
|
|
objects.sorted(['valueCol', 'primaryCol'], true);
|
2016-01-13 10:40:26 +00:00
|
|
|
});
|
2015-09-03 22:46:31 +00:00
|
|
|
},
|
2016-03-04 15:55:05 +00:00
|
|
|
|
|
|
|
testResultsSortedAllTypes: function() {
|
|
|
|
var realm = new Realm({schema: [schemas.BasicTypes]});
|
|
|
|
var objects = realm.objects('BasicTypesObject');
|
|
|
|
|
|
|
|
realm.write(function() {
|
|
|
|
realm.create('BasicTypesObject', [false, 0, 0, 0, '0', new Date(0), new ArrayBuffer()]);
|
|
|
|
realm.create('BasicTypesObject', [true, 2, 2, 2, '2', new Date(2), new ArrayBuffer()]);
|
|
|
|
realm.create('BasicTypesObject', [false, 1, 1, 1, '1', new Date(1), new ArrayBuffer()]);
|
|
|
|
});
|
|
|
|
|
2016-03-04 15:58:25 +00:00
|
|
|
var numberProps = ['intCol', 'floatCol', 'doubleCol', 'stringCol'];
|
2016-03-04 15:55:05 +00:00
|
|
|
for (var i = 0; i < numberProps.length; i++) {
|
|
|
|
var prop = numberProps[i];
|
|
|
|
|
|
|
|
objects = objects.sorted(prop, false);
|
|
|
|
TestCase.assertEqual('' + objects[0][prop], '0', 'first element ascending for ' + prop);
|
|
|
|
TestCase.assertEqual('' + objects[2][prop], '2', 'second element ascending for ' + prop);
|
|
|
|
|
|
|
|
objects = objects.sorted(prop, true);
|
|
|
|
TestCase.assertEqual('' + objects[0][prop], '2', 'first element descending for ' + prop);
|
|
|
|
TestCase.assertEqual('' + objects[2][prop], '0', 'second element descending for ' + prop);
|
|
|
|
}
|
|
|
|
|
|
|
|
objects = objects.sorted('dateCol', false);
|
|
|
|
TestCase.assertEqual(objects[0].dateCol.getTime(), 0);
|
|
|
|
TestCase.assertEqual(objects[2].dateCol.getTime(), 2);
|
|
|
|
|
|
|
|
objects = objects.sorted('dateCol', true);
|
|
|
|
TestCase.assertEqual(objects[0].dateCol.getTime(), 2);
|
|
|
|
TestCase.assertEqual(objects[2].dateCol.getTime(), 0);
|
|
|
|
|
|
|
|
objects = objects.sorted('boolCol', false);
|
|
|
|
TestCase.assertEqual(objects[0].boolCol, false, 'first element ascending for boolCol');
|
|
|
|
TestCase.assertEqual(objects[0].boolCol, false, 'second element ascending for boolCol');
|
|
|
|
TestCase.assertEqual(objects[2].boolCol, true, 'third element ascending for boolCol');
|
|
|
|
|
|
|
|
objects = objects.sorted('boolCol', true);
|
|
|
|
TestCase.assertEqual(objects[0].boolCol, true, 'first element descending for boolCol');
|
|
|
|
TestCase.assertEqual(objects[1].boolCol, false, 'second element descending for boolCol');
|
|
|
|
TestCase.assertEqual(objects[2].boolCol, false, 'third element descending for boolCol');
|
2016-04-27 21:01:01 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
testResultsInvalidation: function() {
|
|
|
|
var realm = new Realm({schema: [schemas.TestObject]});
|
|
|
|
var testObjects = realm.objects('TestObject');
|
|
|
|
var filteredObjects = testObjects.filtered('doubleCol > 1');
|
|
|
|
var sortedObjects = testObjects.sorted('doubleCol');
|
|
|
|
var snapshotObjects = testObjects.snapshot();
|
|
|
|
|
|
|
|
realm.close();
|
|
|
|
|
|
|
|
realm = new Realm({
|
|
|
|
schemaVersion: 1,
|
|
|
|
schema: [schemas.TestObject, schemas.BasicTypes]
|
|
|
|
});
|
|
|
|
|
|
|
|
[
|
|
|
|
testObjects,
|
|
|
|
filteredObjects,
|
|
|
|
sortedObjects,
|
|
|
|
snapshotObjects,
|
|
|
|
].forEach(function(objects) {
|
|
|
|
TestCase.assertThrows(function() { objects[0]; });
|
|
|
|
TestCase.assertThrows(function() { objects.filtered('doubleCol < 42'); });
|
|
|
|
TestCase.assertThrows(function() { objects.sorted('doubleCol', true); });
|
|
|
|
TestCase.assertThrows(function() { objects.snapshot(); });
|
|
|
|
});
|
2016-03-04 15:55:05 +00:00
|
|
|
}
|
2015-10-14 22:25:58 +00:00
|
|
|
});
|