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-06 07:57:35 +00:00
|
|
|
var TestCase = require('./asserts');
|
|
|
|
var schemas = require('./schemas');
|
|
|
|
|
2017-03-07 22:24:30 +00:00
|
|
|
let pathSeparator = '/';
|
|
|
|
if (typeof process === 'object' && process.platform === 'win32') {
|
|
|
|
pathSeparator = '\\';
|
|
|
|
}
|
|
|
|
|
2016-05-20 22:54:20 +00:00
|
|
|
module.exports = {
|
2016-02-26 20:11:35 +00:00
|
|
|
testRealmConstructor: function() {
|
|
|
|
var realm = new Realm({schema: []});
|
|
|
|
TestCase.assertTrue(realm instanceof Realm);
|
2016-05-09 23:15:10 +00:00
|
|
|
|
|
|
|
TestCase.assertEqual(typeof Realm, 'function');
|
|
|
|
TestCase.assertTrue(Realm instanceof Function);
|
2016-02-26 20:11:35 +00:00
|
|
|
},
|
|
|
|
|
2015-08-13 16:12:48 +00:00
|
|
|
testRealmConstructorPath: function() {
|
2015-11-02 20:02:53 +00:00
|
|
|
TestCase.assertThrows(function() {
|
2016-10-04 22:02:51 +00:00
|
|
|
new Realm('');
|
2015-11-02 20:02:53 +00:00
|
|
|
}, 'Realm cannot be created with an invalid path');
|
|
|
|
TestCase.assertThrows(function() {
|
2016-03-03 21:33:49 +00:00
|
|
|
new Realm('test1.realm', 'invalidArgument');
|
2015-11-02 20:02:53 +00:00
|
|
|
}, 'Realm constructor can only have 0 or 1 argument(s)');
|
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);
|
|
|
|
|
2017-03-07 22:24:30 +00:00
|
|
|
var defaultDir = Realm.defaultPath.substring(0, Realm.defaultPath.lastIndexOf(pathSeparator) + 1)
|
2016-03-03 21:33:49 +00:00
|
|
|
var testPath = 'test1.realm';
|
2015-08-13 16:12:48 +00:00
|
|
|
var realm = new Realm({schema: [], path: testPath});
|
2016-03-03 21:33:49 +00:00
|
|
|
TestCase.assertEqual(realm.path, defaultDir + testPath);
|
2015-08-13 16:12:48 +00:00
|
|
|
|
2016-03-03 21:33:49 +00:00
|
|
|
var testPath2 = 'test2.realm';
|
2015-08-13 16:12:48 +00:00
|
|
|
var realm2 = new Realm({schema: [], path: testPath2});
|
2016-03-03 21:33:49 +00:00
|
|
|
TestCase.assertEqual(realm2.path, defaultDir + testPath2);
|
2015-08-13 16:12:48 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
testRealmConstructorSchemaVersion: function() {
|
|
|
|
var defaultRealm = new Realm({schema: []});
|
|
|
|
TestCase.assertEqual(defaultRealm.schemaVersion, 0);
|
|
|
|
|
|
|
|
TestCase.assertThrows(function() {
|
2017-01-05 10:56:11 +00:00
|
|
|
new Realm({schemaVersion: 1, schema: []});
|
2015-08-13 16:12:48 +00:00
|
|
|
}, "Realm already opened at a different schema version");
|
|
|
|
|
|
|
|
TestCase.assertEqual(new Realm().schemaVersion, 0);
|
|
|
|
TestCase.assertEqual(new Realm({schemaVersion: 0}).schemaVersion, 0);
|
|
|
|
|
2016-03-03 21:33:49 +00:00
|
|
|
var realm = new Realm({path: 'test1.realm', schema: [], schemaVersion: 1});
|
2015-08-13 16:12:48 +00:00
|
|
|
TestCase.assertEqual(realm.schemaVersion, 1);
|
2016-05-06 22:41:17 +00:00
|
|
|
TestCase.assertEqual(realm.schema.length, 0);
|
2015-10-13 00:01:51 +00:00
|
|
|
realm.close();
|
2016-05-06 22:41:17 +00:00
|
|
|
|
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
|
|
|
|
2016-03-03 21:33:49 +00:00
|
|
|
realm = new Realm({path: 'test1.realm', schema: [schemas.TestObject], schemaVersion: 2});
|
2015-10-13 00:01:51 +00:00
|
|
|
realm.write(function() {
|
2015-11-02 20:02:53 +00:00
|
|
|
realm.create('TestObject', {doubleCol: 1});
|
2015-10-13 00:01:51 +00:00
|
|
|
});
|
2016-05-06 22:41:17 +00:00
|
|
|
TestCase.assertEqual(realm.objects('TestObject')[0].doubleCol, 1);
|
|
|
|
TestCase.assertEqual(realm.schemaVersion, 2);
|
|
|
|
TestCase.assertEqual(realm.schema.length, 1);
|
2015-08-13 16:12:48 +00:00
|
|
|
},
|
|
|
|
|
2016-04-18 22:50:49 +00:00
|
|
|
testRealmConstructorDynamicSchema: function() {
|
|
|
|
var realm = new Realm({schema: [schemas.TestObject]});
|
|
|
|
realm.write(function() {
|
|
|
|
realm.create('TestObject', [1])
|
|
|
|
});
|
|
|
|
realm.close();
|
|
|
|
|
|
|
|
realm = new Realm();
|
|
|
|
var objects = realm.objects('TestObject');
|
|
|
|
TestCase.assertEqual(objects.length, 1);
|
|
|
|
TestCase.assertEqual(objects[0].doubleCol, 1.0);
|
|
|
|
},
|
|
|
|
|
2016-01-05 00:10:48 +00:00
|
|
|
testRealmConstructorSchemaValidation: function() {
|
|
|
|
TestCase.assertThrows(function() {
|
|
|
|
new Realm({schema: schemas.AllTypes});
|
|
|
|
}, 'The schema should be an array');
|
|
|
|
|
|
|
|
TestCase.assertThrows(function() {
|
|
|
|
new Realm({schema: ['SomeType']});
|
|
|
|
}, 'The schema should be an array of objects');
|
|
|
|
|
|
|
|
TestCase.assertThrows(function() {
|
|
|
|
new Realm({schema: [{}]});
|
|
|
|
}, 'The schema should be an array of ObjectSchema objects');
|
|
|
|
|
|
|
|
TestCase.assertThrows(function() {
|
|
|
|
new Realm({schema: [{name: 'SomeObject'}]});
|
|
|
|
}, 'The schema should be an array of ObjectSchema objects');
|
|
|
|
|
|
|
|
TestCase.assertThrows(function() {
|
2016-07-13 16:53:47 +00:00
|
|
|
new Realm({schema: [{properties: {intCol: 'int'}}]});
|
2016-01-05 00:10:48 +00:00
|
|
|
}, 'The schema should be an array of ObjectSchema objects');
|
|
|
|
},
|
|
|
|
|
2016-04-28 20:56:49 +00:00
|
|
|
testRealmConstructorReadOnly: function() {
|
|
|
|
var realm = new Realm({schema: [schemas.TestObject]});
|
|
|
|
realm.write(function() {
|
|
|
|
realm.create('TestObject', [1])
|
|
|
|
});
|
2016-04-28 22:50:43 +00:00
|
|
|
TestCase.assertEqual(realm.readOnly, false);
|
2016-04-28 20:56:49 +00:00
|
|
|
realm.close();
|
|
|
|
|
|
|
|
realm = new Realm({readOnly: true, schema: [schemas.TestObject]});
|
|
|
|
var objects = realm.objects('TestObject');
|
|
|
|
TestCase.assertEqual(objects.length, 1);
|
|
|
|
TestCase.assertEqual(objects[0].doubleCol, 1.0);
|
2016-04-28 22:50:43 +00:00
|
|
|
TestCase.assertEqual(realm.readOnly, true);
|
2016-04-28 20:56:49 +00:00
|
|
|
|
|
|
|
TestCase.assertThrows(function() {
|
|
|
|
realm.write(function() {});
|
|
|
|
});
|
|
|
|
realm.close();
|
|
|
|
|
|
|
|
realm = new Realm({readOnly: true});
|
|
|
|
TestCase.assertEqual(realm.schema.length, 1);
|
2016-04-28 22:50:43 +00:00
|
|
|
TestCase.assertEqual(realm.readOnly, true);
|
2016-04-28 20:56:49 +00:00
|
|
|
},
|
|
|
|
|
2015-08-13 16:12:48 +00:00
|
|
|
testDefaultPath: function() {
|
2016-05-04 22:38:29 +00:00
|
|
|
var defaultPath = Realm.defaultPath;
|
2015-08-13 16:12:48 +00:00
|
|
|
var defaultRealm = new Realm({schema: []});
|
|
|
|
TestCase.assertEqual(defaultRealm.path, Realm.defaultPath);
|
|
|
|
|
2016-05-04 22:38:29 +00:00
|
|
|
try {
|
2017-03-07 22:24:30 +00:00
|
|
|
var newPath = Realm.defaultPath.substring(0, defaultPath.lastIndexOf(pathSeparator) + 1) + 'default2.realm';
|
2016-05-04 22:38:29 +00:00
|
|
|
Realm.defaultPath = newPath;
|
|
|
|
defaultRealm = new Realm({schema: []});
|
|
|
|
TestCase.assertEqual(defaultRealm.path, newPath, "should use updated default realm path");
|
|
|
|
TestCase.assertEqual(Realm.defaultPath, newPath, "defaultPath should have been updated");
|
|
|
|
} finally {
|
|
|
|
Realm.defaultPath = defaultPath;
|
|
|
|
}
|
2015-08-13 16:12:48 +00:00
|
|
|
},
|
|
|
|
|
2016-03-17 22:50:10 +00:00
|
|
|
testRealmSchemaVersion: function() {
|
2016-03-18 16:51:04 +00:00
|
|
|
TestCase.assertEqual(Realm.schemaVersion(Realm.defaultPath), -1);
|
2016-03-17 22:50:10 +00:00
|
|
|
|
|
|
|
var realm = new Realm({schema: []});
|
2016-05-20 23:33:16 +00:00
|
|
|
TestCase.assertEqual(realm.schemaVersion, 0);
|
2016-03-17 22:50:10 +00:00
|
|
|
TestCase.assertEqual(Realm.schemaVersion(Realm.defaultPath), 0);
|
|
|
|
|
|
|
|
realm = new Realm({schema: [], schemaVersion: 2, path: 'another.realm'});
|
2016-05-20 23:33:16 +00:00
|
|
|
TestCase.assertEqual(realm.schemaVersion, 2);
|
2016-03-17 22:50:10 +00:00
|
|
|
TestCase.assertEqual(Realm.schemaVersion('another.realm'), 2);
|
|
|
|
},
|
|
|
|
|
2016-03-29 16:02:36 +00:00
|
|
|
testRealmWrite: function() {
|
|
|
|
var realm = new Realm({schema: [schemas.IntPrimary, schemas.AllTypes, schemas.TestObject]});
|
|
|
|
|
|
|
|
// exceptions should be propogated
|
|
|
|
TestCase.assertThrows(function() {
|
|
|
|
realm.write(function() {
|
|
|
|
realm.invalid();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// writes should be possible after caught exception
|
|
|
|
realm.write(function() {
|
|
|
|
realm.create('TestObject', {doubleCol: 1});
|
|
|
|
});
|
|
|
|
TestCase.assertEqual(1, realm.objects('TestObject').length);
|
|
|
|
|
|
|
|
realm.write(function() {
|
|
|
|
// nested transactions not supported
|
|
|
|
TestCase.assertThrows(function() {
|
|
|
|
realm.write(function() {});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2015-08-13 16:12:48 +00:00
|
|
|
testRealmCreate: function() {
|
2016-04-01 18:00:53 +00:00
|
|
|
var realm = new Realm({schema: [schemas.TestObject]});
|
2015-10-12 09:01:29 +00:00
|
|
|
|
|
|
|
TestCase.assertThrows(function() {
|
2015-11-02 20:02:53 +00:00
|
|
|
realm.create('TestObject', {doubleCol: 1});
|
2015-10-12 09:01:29 +00:00
|
|
|
}, 'can only create inside a write transaction');
|
|
|
|
|
2015-08-13 16:12:48 +00:00
|
|
|
realm.write(function() {
|
2015-11-02 20:02:53 +00:00
|
|
|
realm.create('TestObject', {doubleCol: 1});
|
|
|
|
realm.create('TestObject', {doubleCol: 2});
|
2015-08-13 16:12:48 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
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');
|
2016-04-01 18:00:53 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
testRealmCreatePrimaryKey: function() {
|
|
|
|
var realm = new Realm({schema: [schemas.IntPrimary]});
|
2015-09-03 21:05:56 +00:00
|
|
|
|
|
|
|
realm.write(function() {
|
2015-11-02 20:02:53 +00:00
|
|
|
var obj0 = realm.create('IntPrimaryObject', {
|
|
|
|
primaryCol: 0,
|
|
|
|
valueCol: 'val0',
|
|
|
|
});
|
2015-09-03 21:05:56 +00:00
|
|
|
|
|
|
|
TestCase.assertThrows(function() {
|
2015-11-02 20:02:53 +00:00
|
|
|
realm.create('IntPrimaryObject', {
|
|
|
|
primaryCol: 0,
|
|
|
|
valueCol: 'val0',
|
|
|
|
});
|
|
|
|
}, 'cannot create object with conflicting primary key');
|
|
|
|
|
|
|
|
realm.create('IntPrimaryObject', {
|
|
|
|
primaryCol: 1,
|
|
|
|
valueCol: 'val1',
|
|
|
|
}, true);
|
|
|
|
|
2015-09-03 21:05:56 +00:00
|
|
|
var objects = realm.objects('IntPrimaryObject');
|
|
|
|
TestCase.assertEqual(objects.length, 2);
|
|
|
|
|
2015-11-02 20:02:53 +00:00
|
|
|
realm.create('IntPrimaryObject', {
|
|
|
|
primaryCol: 0,
|
|
|
|
valueCol: 'newVal0',
|
|
|
|
}, true);
|
|
|
|
|
2015-09-03 21:05:56 +00:00
|
|
|
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');
|
|
|
|
});
|
2016-04-01 18:00:53 +00:00
|
|
|
},
|
2015-09-03 21:37:22 +00:00
|
|
|
|
2016-04-01 18:00:53 +00:00
|
|
|
testRealmCreateOptionals: function() {
|
|
|
|
var realm = new Realm({schema: [schemas.NullableBasicTypes, schemas.LinkTypes, schemas.TestObject]});
|
|
|
|
var basic, links;
|
|
|
|
realm.write(function() {
|
|
|
|
basic = realm.create('NullableBasicTypesObject', {});
|
|
|
|
links = realm.create('LinkTypesObject', {});
|
|
|
|
});
|
|
|
|
for (var name in schemas.NullableBasicTypes.properties) {
|
|
|
|
TestCase.assertEqual(basic[name], null);
|
|
|
|
}
|
|
|
|
TestCase.assertEqual(links.objectCol, null);
|
|
|
|
TestCase.assertEqual(links.arrayCol.length, 0);
|
|
|
|
},
|
|
|
|
|
|
|
|
testRealmCreateUpsert: function() {
|
2016-06-13 21:46:26 +00:00
|
|
|
var realm = new Realm({schema: [schemas.IntPrimary, schemas.StringPrimary, schemas.AllTypes, schemas.TestObject]});
|
2015-09-03 21:37:22 +00:00
|
|
|
realm.write(function() {
|
2015-11-02 20:02:53 +00:00
|
|
|
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: [],
|
|
|
|
};
|
|
|
|
|
2015-09-04 19:41:17 +00:00
|
|
|
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-11-02 20:02:53 +00:00
|
|
|
}, '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);
|
|
|
|
|
2015-09-04 19:41:17 +00:00
|
|
|
var objects = realm.objects('AllTypesObject');
|
2015-09-03 21:37:22 +00:00
|
|
|
TestCase.assertEqual(objects.length, 2);
|
|
|
|
|
2015-11-02 20:02:53 +00:00
|
|
|
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);
|
|
|
|
|
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);
|
2015-11-02 20:02:53 +00:00
|
|
|
TestCase.assertEqualWithTolerance(obj0.doubleCol, 2.22, 0.000001);
|
2015-09-04 19:28:57 +00:00
|
|
|
TestCase.assertEqual(obj0.dateCol.getTime(), 2);
|
2015-11-16 11:26:33 +00:00
|
|
|
TestCase.assertEqual(obj0.dataCol.byteLength, 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-11-02 20:02:53 +00:00
|
|
|
realm.create('AllTypesObject', {primaryCol: '0'}, true);
|
|
|
|
realm.create('AllTypesObject', {primaryCol: '1'}, true);
|
2015-09-04 19:28:57 +00:00
|
|
|
TestCase.assertEqual(obj0.stringCol, '2');
|
2015-11-02 21:30:42 +00:00
|
|
|
TestCase.assertEqual(obj0.objectCol, null);
|
|
|
|
TestCase.assertEqual(obj1.objectCol.doubleCol, 0);
|
2015-09-04 19:28:57 +00:00
|
|
|
|
2015-11-02 20:02:53 +00:00
|
|
|
realm.create('AllTypesObject', {
|
|
|
|
primaryCol: '0',
|
|
|
|
stringCol: '3',
|
|
|
|
objectCol: {doubleCol: 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);
|
2015-11-02 20:02:53 +00:00
|
|
|
TestCase.assertEqualWithTolerance(obj0.doubleCol, 2.22, 0.000001);
|
2015-09-04 19:28:57 +00:00
|
|
|
TestCase.assertEqual(obj0.dateCol.getTime(), 2);
|
2015-11-16 11:26:33 +00:00
|
|
|
TestCase.assertEqual(obj0.dataCol.byteLength, 2);
|
2015-09-04 19:41:17 +00:00
|
|
|
TestCase.assertEqual(obj0.objectCol.doubleCol, 0);
|
|
|
|
TestCase.assertEqual(obj0.arrayCol.length, 1);
|
2015-11-02 21:30:42 +00:00
|
|
|
|
2015-11-02 20:02:53 +00:00
|
|
|
realm.create('AllTypesObject', {primaryCol: '0', objectCol: undefined}, true);
|
|
|
|
realm.create('AllTypesObject', {primaryCol: '1', objectCol: null}, true);
|
2015-11-02 21:30:42 +00:00
|
|
|
TestCase.assertEqual(obj0.objectCol, null);
|
|
|
|
TestCase.assertEqual(obj1.objectCol, null);
|
2016-06-13 21:46:26 +00:00
|
|
|
|
|
|
|
// test with string primaries
|
|
|
|
var obj =realm.create('StringPrimaryObject', {
|
|
|
|
primaryCol: '0',
|
|
|
|
valueCol: 0
|
|
|
|
});
|
|
|
|
TestCase.assertEqual(obj.valueCol, 0);
|
|
|
|
|
|
|
|
realm.create('StringPrimaryObject', {
|
|
|
|
primaryCol: '0',
|
|
|
|
valueCol: 1
|
|
|
|
}, true);
|
|
|
|
TestCase.assertEqual(obj.valueCol, 1);
|
2015-09-03 21:05:56 +00:00
|
|
|
});
|
2015-08-13 16:12:48 +00:00
|
|
|
},
|
|
|
|
|
2016-03-18 23:50:55 +00:00
|
|
|
testRealmWithIndexedProperties: function() {
|
2016-04-28 19:16:16 +00:00
|
|
|
var realm = new Realm({schema: [schemas.IndexedTypes]});
|
2016-03-18 23:50:55 +00:00
|
|
|
realm.write(function() {
|
|
|
|
realm.create('IndexedTypesObject', {boolCol: true, intCol: 1, stringCol: '1', dateCol: new Date(1)});
|
|
|
|
});
|
|
|
|
|
|
|
|
var NotIndexed = {
|
|
|
|
name: 'NotIndexedObject',
|
|
|
|
properties: {
|
|
|
|
floatCol: {type: 'float', indexed: false}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-04-26 07:10:46 +00:00
|
|
|
new Realm({schema: [NotIndexed], path: '1.realm'});
|
2016-03-18 23:50:55 +00:00
|
|
|
|
2016-04-28 21:08:54 +00:00
|
|
|
var IndexedSchema = {
|
|
|
|
name: 'IndexedSchema',
|
|
|
|
};
|
2016-03-18 23:50:55 +00:00
|
|
|
TestCase.assertThrows(function() {
|
2016-04-28 21:08:54 +00:00
|
|
|
IndexedSchema.properties = { floatCol: {type: 'float', indexed: true} };
|
2016-04-26 07:10:46 +00:00
|
|
|
new Realm({schema: [IndexedSchema], path: '2.realm'});
|
2016-03-18 23:50:55 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
TestCase.assertThrows(function() {
|
2016-04-28 21:08:54 +00:00
|
|
|
IndexedSchema.properties = { doubleCol: {type: 'double', indexed: true} }
|
2016-04-26 07:10:46 +00:00
|
|
|
new Realm({schema: [IndexedSchema], path: '3.realm'});
|
2016-03-18 23:50:55 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
TestCase.assertThrows(function() {
|
2016-04-28 21:08:54 +00:00
|
|
|
IndexedSchema.properties = { dataCol: {type: 'data', indexed: true} }
|
2016-04-26 07:10:46 +00:00
|
|
|
new Realm({schema: [IndexedSchema], path: '4.realm'});
|
2016-03-18 23:50:55 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// primary key
|
2016-04-28 21:08:54 +00:00
|
|
|
IndexedSchema.properties = { boolCol: {type: 'bool', indexed: true} };
|
|
|
|
IndexedSchema.primaryKey = 'boolCol';
|
2016-03-21 18:46:34 +00:00
|
|
|
|
2016-04-28 21:08:54 +00:00
|
|
|
// Test this doesn't throw
|
2016-04-26 07:10:46 +00:00
|
|
|
new Realm({schema: [IndexedSchema], path: '5.realm'});
|
2016-03-18 23:50:55 +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-11-02 20:02:53 +00:00
|
|
|
|
2016-05-10 18:37:09 +00:00
|
|
|
var createAndTestObject = function() {
|
2015-09-04 22:43:26 +00:00
|
|
|
var obj = realm.create('DefaultValuesObject', {});
|
2015-10-30 19:08:31 +00:00
|
|
|
var properties = schemas.DefaultValues.properties;
|
|
|
|
|
|
|
|
TestCase.assertEqual(obj.boolCol, properties.boolCol.default);
|
|
|
|
TestCase.assertEqual(obj.intCol, properties.intCol.default);
|
|
|
|
TestCase.assertEqualWithTolerance(obj.floatCol, properties.floatCol.default, 0.000001);
|
2015-11-02 20:02:53 +00:00
|
|
|
TestCase.assertEqualWithTolerance(obj.doubleCol, properties.doubleCol.default, 0.000001);
|
2015-10-30 19:08:31 +00:00
|
|
|
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);
|
2015-11-02 20:02:53 +00:00
|
|
|
TestCase.assertEqual(obj.objectCol.doubleCol, properties.objectCol.default.doubleCol);
|
2015-09-04 22:43:26 +00:00
|
|
|
TestCase.assertEqual(obj.nullObjectCol, null);
|
2015-10-30 19:08:31 +00:00
|
|
|
TestCase.assertEqual(obj.arrayCol.length, properties.arrayCol.default.length);
|
2015-11-02 20:02:53 +00:00
|
|
|
TestCase.assertEqual(obj.arrayCol[0].doubleCol, properties.arrayCol.default[0].doubleCol);
|
2016-05-06 22:41:17 +00:00
|
|
|
};
|
|
|
|
|
2016-05-10 18:37:09 +00:00
|
|
|
realm.write(createAndTestObject);
|
2016-05-06 22:41:17 +00:00
|
|
|
|
|
|
|
// Defaults should still work when creating another Realm instance.
|
|
|
|
realm = new Realm();
|
2016-05-10 18:37:09 +00:00
|
|
|
realm.write(createAndTestObject);
|
2016-05-06 22:41:17 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
testRealmCreateWithChangingDefaults: function() {
|
|
|
|
var objectSchema = {
|
|
|
|
name: 'IntObject',
|
|
|
|
properties: {
|
|
|
|
intCol: {type: 'int', default: 1},
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
var realm = new Realm({schema: [objectSchema]});
|
|
|
|
|
2016-05-10 18:37:09 +00:00
|
|
|
var createAndTestObject = function() {
|
2016-05-06 22:41:17 +00:00
|
|
|
var object = realm.create('IntObject', {});
|
|
|
|
TestCase.assertEqual(object.intCol, objectSchema.properties.intCol.default);
|
|
|
|
};
|
|
|
|
|
2016-05-10 18:37:09 +00:00
|
|
|
realm.write(createAndTestObject);
|
2016-05-06 22:41:17 +00:00
|
|
|
|
|
|
|
objectSchema.properties.intCol.default++;
|
|
|
|
|
|
|
|
realm = new Realm({schema: [objectSchema]});
|
2016-05-10 18:37:09 +00:00
|
|
|
realm.write(createAndTestObject);
|
2015-09-04 22:43:26 +00:00
|
|
|
},
|
|
|
|
|
2016-02-18 19:09:51 +00:00
|
|
|
testRealmCreateWithConstructor: function() {
|
|
|
|
var customCreated = 0;
|
|
|
|
|
|
|
|
function CustomObject() {
|
|
|
|
customCreated++;
|
|
|
|
}
|
|
|
|
CustomObject.schema = {
|
|
|
|
name: 'CustomObject',
|
|
|
|
properties: {
|
|
|
|
intCol: 'int'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function InvalidObject() {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
TestCase.assertThrows(function() {
|
|
|
|
new Realm({schema: [InvalidObject]});
|
|
|
|
});
|
|
|
|
|
|
|
|
InvalidObject.schema = {
|
|
|
|
name: 'InvalidObject',
|
|
|
|
properties: {
|
|
|
|
intCol: 'int'
|
|
|
|
}
|
2016-05-06 22:41:17 +00:00
|
|
|
};
|
2016-02-18 19:09:51 +00:00
|
|
|
|
|
|
|
var realm = new Realm({schema: [CustomObject, InvalidObject]});
|
|
|
|
|
|
|
|
realm.write(function() {
|
|
|
|
var object = realm.create('CustomObject', {intCol: 1});
|
|
|
|
TestCase.assertTrue(object instanceof CustomObject);
|
|
|
|
TestCase.assertTrue(Object.getPrototypeOf(object) == CustomObject.prototype);
|
|
|
|
TestCase.assertEqual(customCreated, 1);
|
2016-02-18 21:48:07 +00:00
|
|
|
|
2016-02-25 01:34:20 +00:00
|
|
|
// Should be able to create object by passing in constructor.
|
|
|
|
object = realm.create(CustomObject, {intCol: 2});
|
|
|
|
TestCase.assertTrue(object instanceof CustomObject);
|
|
|
|
TestCase.assertTrue(Object.getPrototypeOf(object) == CustomObject.prototype);
|
|
|
|
TestCase.assertEqual(customCreated, 2);
|
2016-02-18 19:09:51 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
TestCase.assertThrows(function() {
|
|
|
|
realm.write(function() {
|
|
|
|
realm.create('InvalidObject', {intCol: 1});
|
|
|
|
});
|
|
|
|
});
|
2016-02-25 01:34:20 +00:00
|
|
|
|
|
|
|
// Only the original constructor should be valid.
|
2016-03-03 10:49:37 +00:00
|
|
|
function InvalidCustomObject() {}
|
|
|
|
InvalidCustomObject.schema = CustomObject.schema;
|
2016-02-25 01:34:20 +00:00
|
|
|
|
|
|
|
TestCase.assertThrows(function() {
|
|
|
|
realm.write(function() {
|
2016-03-03 10:49:37 +00:00
|
|
|
realm.create(InvalidCustomObject, {intCol: 1});
|
2016-02-25 01:34:20 +00:00
|
|
|
});
|
|
|
|
});
|
2016-05-06 22:41:17 +00:00
|
|
|
|
|
|
|
// The constructor should still work when creating another Realm instance.
|
|
|
|
realm = new Realm();
|
|
|
|
TestCase.assertTrue(realm.objects('CustomObject')[0] instanceof CustomObject);
|
|
|
|
TestCase.assertTrue(realm.objects(CustomObject).length > 0);
|
|
|
|
},
|
|
|
|
|
|
|
|
testRealmCreateWithChangingConstructor: function() {
|
|
|
|
function CustomObject() {}
|
|
|
|
CustomObject.schema = {
|
|
|
|
name: 'CustomObject',
|
|
|
|
properties: {
|
|
|
|
intCol: 'int'
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
var realm = new Realm({schema: [CustomObject]});
|
|
|
|
realm.write(function() {
|
|
|
|
var object = realm.create('CustomObject', {intCol: 1});
|
|
|
|
TestCase.assertTrue(object instanceof CustomObject);
|
|
|
|
});
|
|
|
|
|
|
|
|
function NewCustomObject() {}
|
|
|
|
NewCustomObject.schema = CustomObject.schema;
|
|
|
|
|
|
|
|
realm = new Realm({schema: [NewCustomObject]});
|
|
|
|
realm.write(function() {
|
|
|
|
var object = realm.create('CustomObject', {intCol: 1});
|
|
|
|
TestCase.assertTrue(object instanceof NewCustomObject);
|
|
|
|
});
|
2016-02-18 19:09:51 +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-11-02 20:02:53 +00:00
|
|
|
|
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++) {
|
2015-11-02 20:02:53 +00:00
|
|
|
realm.create('TestObject', {doubleCol: i});
|
2015-09-03 04:31:29 +00:00
|
|
|
}
|
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");
|
|
|
|
|
2016-02-18 04:06:42 +00:00
|
|
|
var threeObjects = realm.objects('TestObject').filtered("doubleCol < 5");
|
2015-09-03 04:31:29 +00:00
|
|
|
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');
|
2016-12-20 15:28:02 +00:00
|
|
|
|
|
|
|
var o = objects[0];
|
|
|
|
realm.delete(o);
|
|
|
|
TestCase.assertThrows(function() {
|
|
|
|
realm.delete(o);
|
|
|
|
});
|
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-11-02 20:02:53 +00:00
|
|
|
|
2015-09-08 21:07:14 +00:00
|
|
|
realm.write(function() {
|
2015-11-02 20:02:53 +00:00
|
|
|
realm.create('TestObject', {doubleCol: 1});
|
|
|
|
realm.create('TestObject', {doubleCol: 2});
|
|
|
|
realm.create('IntPrimaryObject', {primaryCol: 2, valueCol: 'value'});
|
2015-09-08 21:07:14 +00:00
|
|
|
});
|
2015-11-02 20:02:53 +00:00
|
|
|
|
2015-09-08 21:07:14 +00:00
|
|
|
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-11-11 00:11:57 +00:00
|
|
|
var realm = new Realm({schema: [schemas.PersonObject, schemas.DefaultValues, schemas.TestObject]});
|
2016-02-25 01:34:20 +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: '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});
|
2015-08-13 16:12:48 +00:00
|
|
|
});
|
|
|
|
|
2016-02-25 01:34:20 +00:00
|
|
|
// Should be able to pass constructor for getting objects.
|
|
|
|
var objects = realm.objects(schemas.PersonObject);
|
|
|
|
TestCase.assertTrue(objects[0] instanceof schemas.PersonObject);
|
|
|
|
|
2016-03-03 10:49:37 +00:00
|
|
|
function InvalidPerson() {}
|
|
|
|
InvalidPerson.schema = schemas.PersonObject.schema;
|
2016-02-25 01:34:20 +00:00
|
|
|
|
|
|
|
TestCase.assertThrows(function() {
|
2015-08-13 16:12:48 +00:00
|
|
|
realm.objects();
|
|
|
|
});
|
2016-02-25 01:34:20 +00:00
|
|
|
TestCase.assertThrows(function() {
|
2015-08-13 16:12:48 +00:00
|
|
|
realm.objects([]);
|
|
|
|
});
|
2016-02-25 01:34:20 +00:00
|
|
|
TestCase.assertThrows(function() {
|
2015-08-13 16:12:48 +00:00
|
|
|
realm.objects('InvalidClass');
|
|
|
|
});
|
2016-02-25 01:34:20 +00:00
|
|
|
TestCase.assertThrows(function() {
|
2016-02-18 04:06:42 +00:00
|
|
|
realm.objects('PersonObject', 'truepredicate');
|
2015-08-17 18:40:13 +00:00
|
|
|
});
|
2016-02-25 01:34:20 +00:00
|
|
|
TestCase.assertThrows(function() {
|
2016-03-03 10:49:37 +00:00
|
|
|
realm.objects(InvalidPerson);
|
2016-02-25 01:34:20 +00:00
|
|
|
});
|
2016-12-05 15:57:36 +00:00
|
|
|
|
|
|
|
var person = realm.objects('PersonObject')[0];
|
2016-12-07 14:34:53 +00:00
|
|
|
var listenerCallback = () => {};
|
|
|
|
realm.addListener('change', listenerCallback);
|
2016-12-05 15:57:36 +00:00
|
|
|
|
2016-12-07 14:28:28 +00:00
|
|
|
// The tests below assert that everthing throws when
|
|
|
|
// operating on a closed realm
|
2016-12-05 15:57:36 +00:00
|
|
|
realm.close();
|
|
|
|
|
|
|
|
TestCase.assertThrows(function() {
|
|
|
|
console.log("Name: ", person.name);
|
|
|
|
});
|
|
|
|
|
|
|
|
TestCase.assertThrows(function() {
|
|
|
|
realm.objects('PersonObject');
|
|
|
|
});
|
2016-12-07 14:28:28 +00:00
|
|
|
|
|
|
|
TestCase.assertThrows(function() {
|
|
|
|
realm.addListener('change', () => {});
|
|
|
|
});
|
|
|
|
|
|
|
|
TestCase.assertThrows(function() {
|
|
|
|
realm.create('PersonObject', {name: 'Ari', age: 10});
|
|
|
|
});
|
|
|
|
|
|
|
|
TestCase.assertThrows(function() {
|
|
|
|
realm.delete(person);
|
|
|
|
});
|
|
|
|
|
|
|
|
TestCase.assertThrows(function() {
|
|
|
|
realm.deleteAll();
|
|
|
|
});
|
|
|
|
|
|
|
|
TestCase.assertThrows(function() {
|
|
|
|
realm.write(() => {});
|
|
|
|
});
|
|
|
|
|
2016-12-07 14:34:53 +00:00
|
|
|
TestCase.assertThrows(function() {
|
|
|
|
realm.removeListener('change', listenerCallback);
|
|
|
|
});
|
2016-12-07 14:28:28 +00:00
|
|
|
|
2016-12-07 14:34:53 +00:00
|
|
|
TestCase.assertThrows(function() {
|
|
|
|
realm.removeAllListeners();
|
|
|
|
});
|
2015-08-13 16:12:48 +00:00
|
|
|
},
|
|
|
|
|
2016-06-03 23:43:36 +00:00
|
|
|
testRealmObjectForPrimaryKey: function() {
|
|
|
|
var realm = new Realm({schema: [schemas.IntPrimary, schemas.StringPrimary, schemas.TestObject]});
|
|
|
|
|
|
|
|
realm.write(function() {
|
|
|
|
realm.create('IntPrimaryObject', {primaryCol: 0, valueCol: 'val0'});
|
|
|
|
realm.create('IntPrimaryObject', {primaryCol: 1, valueCol: 'val1'});
|
|
|
|
|
|
|
|
realm.create('StringPrimaryObject', {primaryCol: '', valueCol: -1});
|
|
|
|
realm.create('StringPrimaryObject', {primaryCol: 'val0', valueCol: 0});
|
|
|
|
realm.create('StringPrimaryObject', {primaryCol: 'val1', valueCol: 1});
|
|
|
|
|
|
|
|
realm.create('TestObject', {doubleCol: 0});
|
|
|
|
});
|
|
|
|
|
|
|
|
TestCase.assertEqual(realm.objectForPrimaryKey('IntPrimaryObject', -1), undefined);
|
|
|
|
TestCase.assertEqual(realm.objectForPrimaryKey('IntPrimaryObject', 0).valueCol, 'val0');
|
|
|
|
TestCase.assertEqual(realm.objectForPrimaryKey('IntPrimaryObject', 1).valueCol, 'val1');
|
|
|
|
|
|
|
|
TestCase.assertEqual(realm.objectForPrimaryKey('StringPrimaryObject', 'invalid'), undefined);
|
|
|
|
TestCase.assertEqual(realm.objectForPrimaryKey('StringPrimaryObject', '').valueCol, -1);
|
|
|
|
TestCase.assertEqual(realm.objectForPrimaryKey('StringPrimaryObject', 'val0').valueCol, 0);
|
|
|
|
TestCase.assertEqual(realm.objectForPrimaryKey('StringPrimaryObject', 'val1').valueCol, 1);
|
|
|
|
|
|
|
|
TestCase.assertThrows(function() {
|
|
|
|
realm.objectForPrimaryKey('TestObject', 0);
|
|
|
|
});
|
|
|
|
TestCase.assertThrows(function() {
|
|
|
|
realm.objectForPrimaryKey();
|
|
|
|
});
|
|
|
|
TestCase.assertThrows(function() {
|
|
|
|
realm.objectForPrimaryKey('IntPrimary');
|
|
|
|
});
|
|
|
|
TestCase.assertThrows(function() {
|
|
|
|
realm.objectForPrimaryKey('InvalidClass', 0);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
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;
|
2016-05-20 23:33:16 +00:00
|
|
|
function secondNotification() {
|
2015-10-26 23:15:46 +00:00
|
|
|
secondNotificationCount++;
|
2015-11-03 22:45:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// The listener should only be added once.
|
|
|
|
realm.addListener('change', secondNotification);
|
|
|
|
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
|
|
|
},
|
2016-04-28 19:16:16 +00:00
|
|
|
|
|
|
|
testSchema: function() {
|
2016-04-28 19:44:48 +00:00
|
|
|
var originalSchema = [schemas.TestObject, schemas.BasicTypes, schemas.NullableBasicTypes, schemas.IndexedTypes, schemas.IntPrimary,
|
|
|
|
schemas.PersonObject, schemas.LinkTypes];
|
2016-04-28 19:16:16 +00:00
|
|
|
|
|
|
|
var schemaMap = {};
|
2016-12-28 16:16:37 +00:00
|
|
|
originalSchema.forEach(function(objectSchema) {
|
|
|
|
if (objectSchema.schema) { // for PersonObject
|
|
|
|
schemaMap[objectSchema.schema.name] = objectSchema;
|
|
|
|
} else {
|
|
|
|
schemaMap[objectSchema.name] = objectSchema;
|
|
|
|
}
|
|
|
|
});
|
2016-04-28 19:16:16 +00:00
|
|
|
|
|
|
|
var realm = new Realm({schema: originalSchema});
|
|
|
|
|
|
|
|
var schema = realm.schema;
|
|
|
|
TestCase.assertEqual(schema.length, originalSchema.length);
|
|
|
|
|
|
|
|
function isString(val) {
|
|
|
|
return typeof val === 'string' || val instanceof String;
|
|
|
|
}
|
|
|
|
|
|
|
|
function verifyObjectSchema(returned) {
|
|
|
|
var original = schemaMap[returned.name];
|
2016-04-28 19:44:48 +00:00
|
|
|
if (original.schema) {
|
|
|
|
original = original.schema;
|
|
|
|
}
|
|
|
|
|
2016-04-28 19:16:16 +00:00
|
|
|
TestCase.assertEqual(returned.primaryKey, original.primaryKey);
|
|
|
|
for (var propName in returned.properties) {
|
|
|
|
var prop1 = returned.properties[propName];
|
|
|
|
var prop2 = original.properties[propName];
|
|
|
|
if (prop1.type == 'object') {
|
2016-04-28 19:46:59 +00:00
|
|
|
TestCase.assertEqual(prop1.objectType, isString(prop2) ? prop2 : prop2.objectType);
|
2016-04-28 19:16:16 +00:00
|
|
|
TestCase.assertEqual(prop1.optional, true);
|
|
|
|
}
|
|
|
|
else if (prop1.type == 'list') {
|
2016-04-28 19:46:59 +00:00
|
|
|
TestCase.assertEqual(prop1.objectType, prop2.objectType);
|
2016-04-28 19:44:48 +00:00
|
|
|
TestCase.assertEqual(prop1.optional, undefined);
|
2016-04-28 19:16:16 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
TestCase.assertEqual(prop1.type, isString(prop2) ? prop2 : prop2.type);
|
|
|
|
TestCase.assertEqual(prop1.optional, prop2.optional || undefined);
|
|
|
|
}
|
|
|
|
|
2016-04-28 19:44:48 +00:00
|
|
|
TestCase.assertEqual(prop1.indexed, prop2.indexed || undefined);
|
2016-04-28 19:16:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (var i = 0; i < originalSchema.length; i++) {
|
|
|
|
verifyObjectSchema(schema[i]);
|
|
|
|
}
|
|
|
|
},
|
2016-05-13 22:29:59 +00:00
|
|
|
|
|
|
|
testCopyBundledRealmFiles: function() {
|
|
|
|
Realm.copyBundledRealmFiles();
|
|
|
|
|
|
|
|
var realm = new Realm({path: 'dates-v5.realm', schema: [schemas.DateObject]});
|
2016-05-26 20:17:19 +00:00
|
|
|
TestCase.assertEqual(realm.objects('Date').length, 2);
|
2016-05-13 22:29:59 +00:00
|
|
|
TestCase.assertEqual(realm.objects('Date')[0].currentDate.getTime(), 1462500087955);
|
|
|
|
|
|
|
|
var newDate = new Date(1);
|
|
|
|
realm.write(function() {
|
|
|
|
realm.objects('Date')[0].currentDate = newDate;
|
|
|
|
});
|
|
|
|
realm.close();
|
|
|
|
|
|
|
|
// copy should not overwrite existing files
|
|
|
|
Realm.copyBundledRealmFiles();
|
2016-05-20 23:33:16 +00:00
|
|
|
realm = new Realm({path: 'dates-v5.realm', schema: [schemas.DateObject]});
|
2016-05-13 22:29:59 +00:00
|
|
|
TestCase.assertEqual(realm.objects('Date')[0].currentDate.getTime(), 1);
|
|
|
|
},
|
2017-01-10 12:41:56 +00:00
|
|
|
|
|
|
|
testErrorMessageFromInvalidWrite: function() {
|
|
|
|
var realm = new Realm({schema: [schemas.PersonObject]});
|
|
|
|
|
|
|
|
TestCase.assertThrowsException(function() {
|
|
|
|
realm.write(function () {
|
|
|
|
var p1 = realm.create('PersonObject', { name: 'Ari', age: 10 });
|
|
|
|
p1.age = "Ten";
|
|
|
|
});
|
|
|
|
}, new Error("PersonObject.age must be of type: number"));
|
|
|
|
}
|
2016-05-20 22:54:20 +00:00
|
|
|
};
|