2015-08-14 15:18:49 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// Copyright 2015 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-13 16:12:48 +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');
|
|
|
|
|
|
|
|
module.exports = {
|
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() {
|
|
|
|
realm.create('TestObject', [1]);
|
|
|
|
TestCase.assertEqual(objects.length, 1);
|
|
|
|
});
|
|
|
|
TestCase.assertEqual(objects.length, 1);
|
|
|
|
},
|
|
|
|
testResultsSubscript: function() {
|
2015-10-06 07:57:35 +00:00
|
|
|
var realm = new Realm({schema: [schemas.PersonObject]});
|
2015-08-13 16:12:48 +00:00
|
|
|
realm.write(function() {
|
|
|
|
realm.create('PersonObject', ['name1', 1]);
|
|
|
|
realm.create('PersonObject', ['name2', 2]);
|
|
|
|
});
|
|
|
|
|
|
|
|
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-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);
|
|
|
|
},
|
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');
|
|
|
|
});
|
|
|
|
},
|
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');
|
|
|
|
for (var object in objects) {
|
|
|
|
TestCase.assertTrue(false, "No objects should have been enumerated");
|
|
|
|
}
|
|
|
|
|
|
|
|
realm.write(function() {
|
|
|
|
realm.create('TestObject', [1]);
|
|
|
|
TestCase.assertEqual(objects.length, 1);
|
|
|
|
});
|
|
|
|
|
|
|
|
var count = 0;
|
|
|
|
for (var object in objects) {
|
|
|
|
count++;
|
|
|
|
//TestCase.assertTrue(object instanceof Object);
|
|
|
|
}
|
|
|
|
TestCase.assertEqual(1, count);
|
|
|
|
},
|
2015-09-03 22:46:31 +00:00
|
|
|
testSort: function() {
|
2015-10-06 07:57:35 +00:00
|
|
|
var realm = new Realm({schema: [schemas.TestObject]});
|
2015-09-03 22:46:31 +00:00
|
|
|
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]);
|
|
|
|
});
|
|
|
|
|
|
|
|
objects.sortByProperty('doubleCol');
|
|
|
|
TestCase.assertEqual(objects[0].doubleCol, 0);
|
|
|
|
TestCase.assertEqual(objects[1].doubleCol, 1);
|
|
|
|
TestCase.assertEqual(objects[2].doubleCol, 2);
|
|
|
|
TestCase.assertEqual(objects[3].doubleCol, 3);
|
|
|
|
TestCase.assertEqual(objects[4].doubleCol, 4);
|
|
|
|
|
|
|
|
objects.sortByProperty('doubleCol', false);
|
|
|
|
TestCase.assertEqual(objects[0].doubleCol, 4);
|
|
|
|
TestCase.assertEqual(objects[1].doubleCol, 3);
|
|
|
|
TestCase.assertEqual(objects[2].doubleCol, 2);
|
|
|
|
TestCase.assertEqual(objects[3].doubleCol, 1);
|
|
|
|
TestCase.assertEqual(objects[4].doubleCol, 0);
|
|
|
|
},
|
2015-10-06 07:57:35 +00:00
|
|
|
};
|