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-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');
|
|
|
|
var util = require('./util');
|
|
|
|
|
2015-10-14 22:25:58 +00:00
|
|
|
module.exports = BaseTest.extend({
|
2015-08-13 16:12:48 +00:00
|
|
|
testRealmConstructorPath: function() {
|
|
|
|
TestCase.assertThrows(function() { new Realm('/invalidpath'); });
|
2015-10-06 07:57:35 +00:00
|
|
|
TestCase.assertThrows(function() { new Realm(util.realmPathForFile('test1.realm'), 'invalidArgument'); });
|
2015-08-13 16:12:48 +00:00
|
|
|
|
|
|
|
var defaultRealm = new Realm({schema: []});
|
|
|
|
TestCase.assertEqual(defaultRealm.path, Realm.defaultPath);
|
|
|
|
|
|
|
|
var defaultRealm2 = new Realm();
|
|
|
|
TestCase.assertEqual(defaultRealm2.path, Realm.defaultPath);
|
|
|
|
|
2015-10-06 07:57:35 +00:00
|
|
|
var testPath = util.realmPathForFile('test1.realm');
|
2015-08-13 16:12:48 +00:00
|
|
|
var realm = new Realm({schema: [], path: testPath});
|
|
|
|
//TestCase.assertTrue(realm instanceof Realm);
|
|
|
|
TestCase.assertEqual(realm.path, testPath);
|
|
|
|
|
2015-10-06 07:57:35 +00:00
|
|
|
var testPath2 = util.realmPathForFile('test2.realm');
|
2015-08-13 16:12:48 +00:00
|
|
|
var realm2 = new Realm({schema: [], path: testPath2});
|
|
|
|
//TestCase.assertTrue(realm2 instanceof Realm);
|
|
|
|
TestCase.assertEqual(realm2.path, testPath2);
|
|
|
|
},
|
|
|
|
|
|
|
|
testRealmConstructorSchemaVersion: function() {
|
|
|
|
var defaultRealm = new Realm({schema: []});
|
|
|
|
TestCase.assertEqual(defaultRealm.schemaVersion, 0);
|
|
|
|
|
|
|
|
TestCase.assertThrows(function() {
|
|
|
|
new Realm({schemaVersion: 1});
|
|
|
|
}, "Realm already opened at a different schema version");
|
|
|
|
|
|
|
|
TestCase.assertEqual(new Realm().schemaVersion, 0);
|
|
|
|
TestCase.assertEqual(new Realm({schemaVersion: 0}).schemaVersion, 0);
|
|
|
|
|
2015-10-06 07:57:35 +00:00
|
|
|
var testPath = util.realmPathForFile('test1.realm');
|
2015-08-13 16:12:48 +00:00
|
|
|
var realm = new Realm({path: testPath, schema: [], schemaVersion: 1});
|
|
|
|
TestCase.assertEqual(realm.schemaVersion, 1);
|
2015-10-13 00:23:47 +00:00
|
|
|
// FIXME - enable once Realm exposes a schema object
|
2015-10-13 00:01:51 +00:00
|
|
|
//TestCase.assertEqual(realm.schema.length, 0);
|
|
|
|
|
|
|
|
realm.close();
|
2015-10-13 00:23:47 +00:00
|
|
|
// FIXME - enable once realm initialization supports schema comparison
|
|
|
|
// TestCase.assertThrows(function() {
|
2015-10-14 09:12:50 +00:00
|
|
|
// realm = new Realm({path: testPath, schema: [schemas.TestObject], schemaVersion: 1});
|
2015-10-13 00:23:47 +00:00
|
|
|
// }, "schema changes require updating the schema version");
|
2015-10-13 00:01:51 +00:00
|
|
|
|
2015-10-14 09:12:50 +00:00
|
|
|
realm = new Realm({path: testPath, schema: [schemas.TestObject], schemaVersion: 2});
|
2015-10-13 00:01:51 +00:00
|
|
|
realm.write(function() {
|
|
|
|
realm.create('TestObject', [1]);
|
|
|
|
});
|
|
|
|
TestCase.assertEqual(realm.objects('TestObject')[0].doubleCol, 1)
|
2015-08-13 16:12:48 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
testDefaultPath: function() {
|
|
|
|
var defaultRealm = new Realm({schema: []});
|
|
|
|
TestCase.assertEqual(defaultRealm.path, Realm.defaultPath);
|
|
|
|
|
2015-10-06 07:57:35 +00:00
|
|
|
var newPath = util.realmPathForFile('default2.realm');
|
2015-08-13 16:12:48 +00:00
|
|
|
Realm.defaultPath = newPath;
|
|
|
|
defaultRealm = new Realm({schema: []});
|
|
|
|
TestCase.assertEqual(defaultRealm.path, newPath);
|
|
|
|
TestCase.assertEqual(Realm.defaultPath, newPath);
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
testRealmCreate: function() {
|
2015-10-06 07:57:35 +00:00
|
|
|
var realm = new Realm({schema: [schemas.IntPrimary, schemas.AllTypes, schemas.TestObject]});
|
2015-10-12 09:01:29 +00:00
|
|
|
|
|
|
|
TestCase.assertThrows(function() {
|
|
|
|
realm.create('TestObject', [1]);
|
|
|
|
}, 'can only create inside a write transaction');
|
|
|
|
|
2015-08-13 16:12:48 +00:00
|
|
|
realm.write(function() {
|
|
|
|
realm.create('TestObject', [1]);
|
|
|
|
realm.create('TestObject', {'doubleCol': 2});
|
|
|
|
});
|
|
|
|
|
|
|
|
var objects = realm.objects('TestObject');
|
|
|
|
TestCase.assertEqual(objects.length, 2, 'wrong object count');
|
|
|
|
TestCase.assertEqual(objects[0].doubleCol, 1, 'wrong object property value');
|
|
|
|
TestCase.assertEqual(objects[1].doubleCol, 2, 'wrong object property value');
|
2015-09-03 21:05:56 +00:00
|
|
|
|
2015-09-04 19:41:17 +00:00
|
|
|
// test int primary object
|
2015-09-03 21:05:56 +00:00
|
|
|
realm.write(function() {
|
|
|
|
var obj0 = realm.create('IntPrimaryObject', [0, 'val0']);
|
|
|
|
|
|
|
|
TestCase.assertThrows(function() {
|
|
|
|
realm.create('IntPrimaryObject', [0, 'val0']);
|
|
|
|
});
|
|
|
|
realm.create('IntPrimaryObject', [1, 'val1'], true);
|
|
|
|
var objects = realm.objects('IntPrimaryObject');
|
|
|
|
TestCase.assertEqual(objects.length, 2);
|
|
|
|
|
|
|
|
realm.create('IntPrimaryObject', [0, 'newVal0'], true);
|
|
|
|
TestCase.assertEqual(obj0.valueCol, 'newVal0');
|
|
|
|
TestCase.assertEqual(objects.length, 2);
|
2015-09-03 21:37:22 +00:00
|
|
|
|
|
|
|
realm.create('IntPrimaryObject', {primaryCol: 0}, true);
|
|
|
|
TestCase.assertEqual(obj0.valueCol, 'newVal0');
|
|
|
|
});
|
|
|
|
|
2015-09-04 19:41:17 +00:00
|
|
|
// test upsert with all type and string primary object
|
2015-09-03 21:37:22 +00:00
|
|
|
realm.write(function() {
|
2015-09-04 19:41:17 +00:00
|
|
|
var values = ['0', true, 1, 1.1, 1.11, '1', new Date(1), '1', [1], []];
|
|
|
|
var obj0 = realm.create('AllTypesObject', values);
|
2015-09-03 21:37:22 +00:00
|
|
|
|
|
|
|
TestCase.assertThrows(function() {
|
2015-09-04 19:41:17 +00:00
|
|
|
realm.create('AllTypesObject', values);
|
2015-09-03 21:37:22 +00:00
|
|
|
});
|
2015-09-04 19:41:17 +00:00
|
|
|
realm.create('AllTypesObject', ['1', false, 2, 2.2, 2.11, '2', new Date(2), '2', null, [[2]]], true);
|
|
|
|
var objects = realm.objects('AllTypesObject');
|
2015-09-03 21:37:22 +00:00
|
|
|
TestCase.assertEqual(objects.length, 2);
|
|
|
|
|
2015-09-04 19:41:17 +00:00
|
|
|
realm.create('AllTypesObject', ['0', false, 2, 2.2, 2.22, '2', new Date(2), '2', null, [[2]]], true);
|
2015-09-03 21:37:22 +00:00
|
|
|
TestCase.assertEqual(objects.length, 2);
|
2015-09-04 19:28:57 +00:00
|
|
|
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.assertEqual(obj0.dateCol.getTime(), 2);
|
|
|
|
TestCase.assertEqual(obj0.dataCol, '2');
|
2015-09-04 19:41:17 +00:00
|
|
|
TestCase.assertEqual(obj0.objectCol, null);
|
|
|
|
TestCase.assertEqual(obj0.arrayCol.length, 1);
|
2015-09-04 19:28:57 +00:00
|
|
|
|
2015-09-04 19:41:17 +00:00
|
|
|
realm.create('AllTypesObject', { primaryCol: '0' }, true);
|
2015-09-04 19:28:57 +00:00
|
|
|
TestCase.assertEqual(obj0.stringCol, '2');
|
|
|
|
|
2015-09-04 19:41:17 +00:00
|
|
|
realm.create('AllTypesObject', { primaryCol: '0', stringCol: '3', objectCol: [0] }, true);
|
2015-09-04 19:28:57 +00:00
|
|
|
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.assertEqual(obj0.dateCol.getTime(), 2);
|
|
|
|
TestCase.assertEqual(obj0.dataCol, '2');
|
2015-09-04 19:41:17 +00:00
|
|
|
TestCase.assertEqual(obj0.objectCol.doubleCol, 0);
|
|
|
|
TestCase.assertEqual(obj0.arrayCol.length, 1);
|
2015-09-03 21:05:56 +00:00
|
|
|
});
|
2015-08-13 16:12:48 +00:00
|
|
|
},
|
|
|
|
|
2015-09-04 22:43:26 +00:00
|
|
|
testRealmCreateWithDefaults: function() {
|
2015-10-06 07:57:35 +00:00
|
|
|
var realm = new Realm({schema: [schemas.DefaultValues, schemas.TestObject]});
|
2015-09-04 22:43:26 +00:00
|
|
|
realm.write(function() {
|
|
|
|
var obj = realm.create('DefaultValuesObject', {});
|
2015-10-06 07:57:35 +00:00
|
|
|
TestCase.assertEqual(obj.boolCol, schemas.DefaultValues.properties[0].default);
|
|
|
|
TestCase.assertEqual(obj.intCol, schemas.DefaultValues.properties[1].default);
|
|
|
|
TestCase.assertEqualWithTolerance(obj.floatCol, schemas.DefaultValues.properties[2].default, 0.000001);
|
|
|
|
TestCase.assertEqual(obj.doubleCol, schemas.DefaultValues.properties[3].default);
|
|
|
|
TestCase.assertEqual(obj.stringCol, schemas.DefaultValues.properties[4].default);
|
|
|
|
TestCase.assertEqual(obj.dateCol.getTime(), schemas.DefaultValues.properties[5].default.getTime());
|
|
|
|
TestCase.assertEqual(obj.dataCol, schemas.DefaultValues.properties[6].default);
|
|
|
|
TestCase.assertEqual(obj.objectCol.doubleCol, schemas.DefaultValues.properties[7].default[0]);
|
2015-09-04 22:43:26 +00:00
|
|
|
TestCase.assertEqual(obj.nullObjectCol, null);
|
2015-10-06 07:57:35 +00:00
|
|
|
TestCase.assertEqual(obj.arrayCol.length, schemas.DefaultValues.properties[9].default.length);
|
|
|
|
TestCase.assertEqual(obj.arrayCol[0].doubleCol, schemas.DefaultValues.properties[9].default[0][0]);
|
2015-09-04 22:43:26 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2015-08-14 16:47:33 +00:00
|
|
|
testRealmDelete: function() {
|
2015-10-06 07:57:35 +00:00
|
|
|
var realm = new Realm({schema: [schemas.TestObject]});
|
2015-08-14 16:47:33 +00:00
|
|
|
realm.write(function() {
|
2015-09-03 04:31:29 +00:00
|
|
|
for (var i = 0; i < 10; i++) {
|
|
|
|
realm.create('TestObject', [i]);
|
|
|
|
}
|
2015-08-14 16:47:33 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
var objects = realm.objects('TestObject');
|
|
|
|
TestCase.assertThrows(function() {
|
|
|
|
realm.delete(objects[0]);
|
2015-10-12 09:01:29 +00:00
|
|
|
}, 'can only delete in a write transaction');
|
2015-08-14 16:47:33 +00:00
|
|
|
|
|
|
|
realm.write(function() {
|
|
|
|
TestCase.assertThrows(function() {
|
|
|
|
realm.delete();
|
|
|
|
});
|
|
|
|
|
|
|
|
realm.delete(objects[0]);
|
2015-09-03 04:31:29 +00:00
|
|
|
TestCase.assertEqual(objects.length, 9, 'wrong object count');
|
|
|
|
TestCase.assertEqual(objects[0].doubleCol, 9, "wrong property value");
|
|
|
|
TestCase.assertEqual(objects[1].doubleCol, 1, "wrong property value");
|
2015-08-14 16:47:33 +00:00
|
|
|
|
|
|
|
realm.delete([objects[0], objects[1]]);
|
2015-09-03 04:31:29 +00:00
|
|
|
TestCase.assertEqual(objects.length, 7, 'wrong object count');
|
|
|
|
TestCase.assertEqual(objects[0].doubleCol, 7, "wrong property value");
|
|
|
|
TestCase.assertEqual(objects[1].doubleCol, 8, "wrong property value");
|
|
|
|
|
|
|
|
var threeObjects = realm.objects('TestObject', "doubleCol < 5");
|
|
|
|
TestCase.assertEqual(threeObjects.length, 3, "wrong results count");
|
|
|
|
realm.delete(threeObjects);
|
|
|
|
TestCase.assertEqual(objects.length, 4, 'wrong object count');
|
|
|
|
TestCase.assertEqual(threeObjects.length, 0, 'threeObject should have been deleted');
|
2015-08-14 16:47:33 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2015-09-08 21:07:14 +00:00
|
|
|
testDeleteAll: function() {
|
2015-10-06 07:57:35 +00:00
|
|
|
var realm = new Realm({schema: [schemas.TestObject, schemas.IntPrimary]});
|
2015-09-08 21:07:14 +00:00
|
|
|
realm.write(function() {
|
|
|
|
realm.create('TestObject', [1]);
|
|
|
|
realm.create('TestObject', [2]);
|
|
|
|
realm.create('IntPrimaryObject', [2, 'value']);
|
|
|
|
});
|
|
|
|
TestCase.assertEqual(realm.objects('TestObject').length, 2);
|
|
|
|
TestCase.assertEqual(realm.objects('IntPrimaryObject').length, 1);
|
|
|
|
|
|
|
|
TestCase.assertThrows(function() {
|
|
|
|
realm.deleteAll();
|
2015-10-12 09:01:29 +00:00
|
|
|
}, 'can only deleteAll in a write transaction');
|
2015-09-08 21:07:14 +00:00
|
|
|
|
|
|
|
realm.write(function() {
|
|
|
|
realm.deleteAll();
|
|
|
|
});
|
|
|
|
|
|
|
|
TestCase.assertEqual(realm.objects('TestObject').length, 0);
|
|
|
|
TestCase.assertEqual(realm.objects('IntPrimaryObject').length, 0);
|
|
|
|
},
|
|
|
|
|
2015-08-13 16:12:48 +00:00
|
|
|
testRealmObjects: 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() {
|
2015-08-17 18:46:08 +00:00
|
|
|
realm.create('PersonObject', ['Ari', 10]);
|
2015-08-17 18:40:13 +00:00
|
|
|
realm.create('PersonObject', ['Tim', 11]);
|
|
|
|
realm.create('PersonObject', {'name': 'Bjarne', 'age': 12});
|
|
|
|
realm.create('PersonObject', {'name': 'Alex', 'age': 12});
|
2015-08-13 16:12:48 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
TestCase.assertThrows(function() {
|
|
|
|
realm.objects();
|
|
|
|
});
|
|
|
|
TestCase.assertThrows(function() {
|
|
|
|
realm.objects([]);
|
|
|
|
});
|
|
|
|
TestCase.assertThrows(function() {
|
|
|
|
realm.objects('InvalidClass');
|
|
|
|
});
|
|
|
|
TestCase.assertThrows(function() {
|
2015-08-17 18:40:13 +00:00
|
|
|
realm.objects('PersonObject', 'invalid query');
|
|
|
|
});
|
|
|
|
TestCase.assertThrows(function() {
|
|
|
|
realm.objects('PersonObject', []);
|
2015-08-13 16:12:48 +00:00
|
|
|
});
|
|
|
|
|
2015-09-04 17:08:13 +00:00
|
|
|
TestCase.assertEqual(realm.objects('PersonObject', "truepredicate").length, 4);
|
2015-08-17 18:46:08 +00:00
|
|
|
TestCase.assertEqual(realm.objects('PersonObject').length, 4);
|
2015-08-17 18:40:13 +00:00
|
|
|
TestCase.assertEqual(realm.objects('PersonObject', 'age = 11').length, 1);
|
|
|
|
TestCase.assertEqual(realm.objects('PersonObject', 'age = 11')[0].name, 'Tim');
|
|
|
|
TestCase.assertEqual(realm.objects('PersonObject', 'age = 12').length, 2);
|
|
|
|
TestCase.assertEqual(realm.objects('PersonObject', 'age = 13').length, 0);
|
2015-08-17 18:46:08 +00:00
|
|
|
TestCase.assertEqual(realm.objects('PersonObject', 'age < 12').length, 2);
|
|
|
|
TestCase.assertEqual(realm.objects('PersonObject', 'age > 10 && age < 13').length, 3);
|
|
|
|
TestCase.assertEqual(realm.objects('PersonObject', 'age >= 11 && age < 13').length, 3);
|
2015-08-17 18:40:13 +00:00
|
|
|
TestCase.assertEqual(realm.objects('PersonObject', 'name = \'Tim\'').length, 1);
|
2015-08-13 16:12:48 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
testNotifications: function() {
|
|
|
|
var realm = new Realm({schema: []});
|
2015-10-13 06:19:54 +00:00
|
|
|
var notificationCount = 0;
|
|
|
|
var notificationName;
|
|
|
|
|
2015-10-27 02:18:24 +00:00
|
|
|
realm.addListener('change', function(realm, name) {
|
2015-10-13 06:19:54 +00:00
|
|
|
notificationCount++;
|
|
|
|
notificationName = name;
|
2015-08-13 16:12:48 +00:00
|
|
|
});
|
2015-10-13 06:19:54 +00:00
|
|
|
|
2015-08-13 16:12:48 +00:00
|
|
|
TestCase.assertEqual(notificationCount, 0);
|
|
|
|
realm.write(function() {});
|
|
|
|
TestCase.assertEqual(notificationCount, 1);
|
2015-10-26 23:19:12 +00:00
|
|
|
TestCase.assertEqual(notificationName, 'change');
|
2015-10-26 23:15:46 +00:00
|
|
|
|
|
|
|
var secondNotificationCount = 0;
|
|
|
|
function secondNotification(realm, name) {
|
|
|
|
secondNotificationCount++;
|
|
|
|
};
|
2015-10-27 02:18:24 +00:00
|
|
|
realm.addListener('change', secondNotification)
|
2015-10-26 23:15:46 +00:00
|
|
|
|
|
|
|
realm.write(function() {});
|
|
|
|
TestCase.assertEqual(notificationCount, 2);
|
|
|
|
TestCase.assertEqual(secondNotificationCount, 1);
|
|
|
|
|
2015-10-27 02:18:24 +00:00
|
|
|
realm.removeListener('change', secondNotification);
|
2015-10-26 23:15:46 +00:00
|
|
|
realm.write(function() {});
|
|
|
|
TestCase.assertEqual(notificationCount, 3);
|
|
|
|
TestCase.assertEqual(secondNotificationCount, 1);
|
|
|
|
|
|
|
|
realm.removeAllListeners();
|
|
|
|
realm.write(function() {});
|
|
|
|
TestCase.assertEqual(notificationCount, 3);
|
|
|
|
TestCase.assertEqual(secondNotificationCount, 1);
|
2015-10-27 14:48:11 +00:00
|
|
|
|
|
|
|
TestCase.assertThrows(function() {
|
|
|
|
realm.addListener('invalid', function() {});
|
|
|
|
});
|
|
|
|
|
|
|
|
realm.addListener('change', function() {
|
|
|
|
throw new Error('error');
|
|
|
|
});
|
|
|
|
|
|
|
|
TestCase.assertThrows(function() {
|
|
|
|
realm.write(function() {});
|
|
|
|
});
|
2015-08-13 16:12:48 +00:00
|
|
|
},
|
2015-10-14 22:25:58 +00:00
|
|
|
});
|