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';
|
|
|
|
|
2017-09-14 22:50:11 +00:00
|
|
|
const Realm = require('realm');
|
|
|
|
const TestCase = require('./asserts');
|
|
|
|
const schemas = require('./schemas');
|
2015-10-06 07:57:35 +00:00
|
|
|
|
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() {
|
2017-09-14 22:50:11 +00:00
|
|
|
const realm = new Realm({schema: []});
|
2016-02-26 20:11:35 +00:00
|
|
|
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() {
|
2017-09-14 22:50:11 +00:00
|
|
|
TestCase.assertThrows(() => new Realm('')); // the message for this error is platform-specific
|
|
|
|
TestCase.assertThrowsContaining(() => new Realm('test1.realm', 'invalidArgument'),
|
|
|
|
"Invalid arguments when constructing 'Realm'");
|
|
|
|
|
|
|
|
const defaultRealm = new Realm({schema: []});
|
2015-08-13 16:12:48 +00:00
|
|
|
TestCase.assertEqual(defaultRealm.path, Realm.defaultPath);
|
|
|
|
|
2017-09-14 22:50:11 +00:00
|
|
|
const defaultRealm2 = new Realm();
|
2015-08-13 16:12:48 +00:00
|
|
|
TestCase.assertEqual(defaultRealm2.path, Realm.defaultPath);
|
|
|
|
|
2017-09-14 22:50:11 +00:00
|
|
|
const defaultDir = Realm.defaultPath.substring(0, Realm.defaultPath.lastIndexOf(pathSeparator) + 1);
|
|
|
|
const testPath = 'test1.realm';
|
|
|
|
const 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
|
|
|
|
2017-09-14 22:50:11 +00:00
|
|
|
const testPath2 = 'test2.realm';
|
|
|
|
const 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
|
|
|
},
|
|
|
|
|
2017-12-13 09:09:26 +00:00
|
|
|
testRealmIsClosed: function() {
|
|
|
|
const realm = new Realm({schema: []});
|
|
|
|
TestCase.assertFalse(realm.isClosed);
|
|
|
|
realm.close();
|
|
|
|
TestCase.assertTrue(realm.isClosed);
|
|
|
|
},
|
|
|
|
|
2015-08-13 16:12:48 +00:00
|
|
|
testRealmConstructorSchemaVersion: function() {
|
2017-09-14 22:50:11 +00:00
|
|
|
const defaultRealm = new Realm({schema: []});
|
2015-08-13 16:12:48 +00:00
|
|
|
TestCase.assertEqual(defaultRealm.schemaVersion, 0);
|
|
|
|
|
2017-09-14 22:50:11 +00:00
|
|
|
TestCase.assertThrowsContaining(() => new Realm({schemaVersion: 1, schema: []}),
|
|
|
|
"already opened with different schema version.");
|
|
|
|
|
2015-08-13 16:12:48 +00:00
|
|
|
TestCase.assertEqual(new Realm().schemaVersion, 0);
|
|
|
|
TestCase.assertEqual(new Realm({schemaVersion: 0}).schemaVersion, 0);
|
|
|
|
|
2017-09-14 22:50:11 +00:00
|
|
|
let 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
|
|
|
|
2016-03-03 21:33:49 +00:00
|
|
|
realm = new Realm({path: 'test1.realm', schema: [schemas.TestObject], schemaVersion: 2});
|
2017-09-14 22:50:11 +00:00
|
|
|
realm.write(() => {
|
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() {
|
2017-09-14 22:50:11 +00:00
|
|
|
let realm = new Realm({schema: [schemas.TestObject]});
|
|
|
|
realm.write(() => {
|
2016-04-18 22:50:49 +00:00
|
|
|
realm.create('TestObject', [1])
|
|
|
|
});
|
|
|
|
realm.close();
|
|
|
|
|
|
|
|
realm = new Realm();
|
2017-09-14 22:50:11 +00:00
|
|
|
const objects = realm.objects('TestObject');
|
2016-04-18 22:50:49 +00:00
|
|
|
TestCase.assertEqual(objects.length, 1);
|
|
|
|
TestCase.assertEqual(objects[0].doubleCol, 1.0);
|
|
|
|
},
|
|
|
|
|
2016-01-05 00:10:48 +00:00
|
|
|
testRealmConstructorSchemaValidation: function() {
|
2017-09-11 21:57:31 +00:00
|
|
|
TestCase.assertThrowsContaining(() => new Realm({schema: schemas.AllTypes}),
|
|
|
|
"schema must be of type 'array', got");
|
2017-09-14 22:50:11 +00:00
|
|
|
TestCase.assertThrowsContaining(() => new Realm({schema: ['SomeType']}),
|
|
|
|
"Failed to read ObjectSchema: JS value must be of type 'object', got (SomeType)");
|
|
|
|
TestCase.assertThrowsContaining(() => new Realm({schema: [{}]}),
|
|
|
|
"Failed to read ObjectSchema: name must be of type 'string', got (undefined)");
|
|
|
|
TestCase.assertThrowsContaining(() => new Realm({schema: [{name: 'SomeObject'}]}),
|
|
|
|
"Failed to read ObjectSchema: properties must be of type 'object', got (undefined)");
|
|
|
|
TestCase.assertThrowsContaining(() => new Realm({schema: [{properties: {intCol: 'int'}}]}),
|
|
|
|
"Failed to read ObjectSchema: name must be of type 'string', got (undefined)");
|
2017-06-29 09:59:10 +00:00
|
|
|
|
2017-09-26 21:43:44 +00:00
|
|
|
function assertPropertyInvalid(prop, message) {
|
|
|
|
TestCase.assertThrowsContaining(() => {
|
|
|
|
new Realm({schema: [{name: 'InvalidObject', properties: { int: 'int', bad: prop }}]});
|
|
|
|
}, message, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
assertPropertyInvalid({type:'list[]', objectType: 'InvalidObject'},
|
|
|
|
"List property 'InvalidObject.bad' must have a non-list value type");
|
|
|
|
assertPropertyInvalid({type:'list?', objectType: 'InvalidObject'},
|
|
|
|
"List property 'InvalidObject.bad' cannot be optional");
|
|
|
|
assertPropertyInvalid('', "Property 'InvalidObject.bad' must have a non-empty type");
|
|
|
|
assertPropertyInvalid({type:'linkingObjects', objectType: 'InvalidObject', property: 'nosuchproperty'},
|
|
|
|
"Property 'InvalidObject.nosuchproperty' declared as origin of linking objects property 'InvalidObject.bad' does not exist");
|
|
|
|
assertPropertyInvalid({type:'linkingObjects', objectType: 'InvalidObject', property: 'int'},
|
|
|
|
"Property 'InvalidObject.int' declared as origin of linking objects property 'InvalidObject.bad' is not a link");
|
2017-09-14 22:50:11 +00:00
|
|
|
|
2017-06-29 09:59:10 +00:00
|
|
|
// linkingObjects property where the source property links elsewhere
|
2017-09-14 22:50:11 +00:00
|
|
|
TestCase.assertThrowsContaining(() => {
|
2017-06-29 09:59:10 +00:00
|
|
|
new Realm({schema: [{
|
|
|
|
name: 'InvalidObject',
|
|
|
|
properties: {
|
|
|
|
link: 'IntObject',
|
|
|
|
linkingObjects: {type:'linkingObjects', objectType: 'InvalidObject', property: 'link'}
|
|
|
|
}
|
|
|
|
}, {
|
|
|
|
name: 'IntObject',
|
|
|
|
properties: {
|
|
|
|
integer: 'int'
|
|
|
|
}
|
|
|
|
}]});
|
|
|
|
}, "Property 'InvalidObject.link' declared as origin of linking objects property 'InvalidObject.linkingObjects' links to type 'IntObject'")
|
2017-09-26 21:43:44 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
new Realm({schema: [{
|
|
|
|
name: 'Object',
|
|
|
|
properties: {
|
|
|
|
// weird but valid
|
|
|
|
objectList: {type:'object[]', objectType: 'Object'}
|
|
|
|
}
|
|
|
|
}]});
|
|
|
|
}
|
2016-01-05 00:10:48 +00:00
|
|
|
},
|
2017-09-11 21:57:31 +00:00
|
|
|
|
2017-09-13 18:42:23 +00:00
|
|
|
testRealmConstructorInMemory: function() {
|
|
|
|
// open in-memory realm instance
|
|
|
|
const realm1 = new Realm({inMemory: true, schema: [schemas.TestObject]});
|
2017-09-14 22:50:11 +00:00
|
|
|
realm1.write(() => {
|
2017-09-13 18:42:23 +00:00
|
|
|
realm1.create('TestObject', [1])
|
|
|
|
});
|
|
|
|
TestCase.assertEqual(realm1.inMemory, true);
|
|
|
|
|
|
|
|
// open a second instance of the same realm and check that they share data
|
|
|
|
const realm2 = new Realm({inMemory: true});
|
|
|
|
const objects = realm2.objects('TestObject');
|
|
|
|
TestCase.assertEqual(objects.length, 1);
|
|
|
|
TestCase.assertEqual(objects[0].doubleCol, 1.0);
|
|
|
|
TestCase.assertEqual(realm2.inMemory, true);
|
|
|
|
|
|
|
|
// Close both realms (this should delete the realm since there are no more
|
|
|
|
// references to it.
|
|
|
|
realm1.close();
|
|
|
|
realm2.close();
|
|
|
|
|
|
|
|
// Open the same in-memory realm again and verify that it is now empty
|
|
|
|
const realm3 = new Realm({inMemory: true});
|
|
|
|
TestCase.assertEqual(realm3.schema.length, 0);
|
2017-09-11 21:57:31 +00:00
|
|
|
|
2017-09-13 18:42:23 +00:00
|
|
|
// try to open the same realm in persistent mode (should fail as you cannot mix modes)
|
2017-09-14 22:50:11 +00:00
|
|
|
TestCase.assertThrowsContaining(() => new Realm({}), 'already opened with different inMemory settings.');
|
2017-09-13 18:42:23 +00:00
|
|
|
},
|
2016-01-05 00:10:48 +00:00
|
|
|
|
2016-04-28 20:56:49 +00:00
|
|
|
testRealmConstructorReadOnly: function() {
|
2017-09-14 22:50:11 +00:00
|
|
|
let realm = new Realm({schema: [schemas.TestObject]});
|
|
|
|
realm.write(() => {
|
2016-04-28 20:56:49 +00:00
|
|
|
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]});
|
2017-09-14 22:50:11 +00:00
|
|
|
const objects = realm.objects('TestObject');
|
2016-04-28 20:56:49 +00:00
|
|
|
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
|
|
|
|
2017-09-14 22:50:11 +00:00
|
|
|
TestCase.assertThrowsContaining(() => realm.write(() => {}),
|
|
|
|
"Can't perform transactions on read-only Realms.");
|
2016-04-28 20:56:49 +00:00
|
|
|
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() {
|
2017-09-14 22:50:11 +00:00
|
|
|
const defaultPath = Realm.defaultPath;
|
|
|
|
let defaultRealm = new Realm({schema: []});
|
2015-08-13 16:12:48 +00:00
|
|
|
TestCase.assertEqual(defaultRealm.path, Realm.defaultPath);
|
|
|
|
|
2016-05-04 22:38:29 +00:00
|
|
|
try {
|
2017-09-14 22:50:11 +00:00
|
|
|
const 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);
|
2017-09-14 22:50:11 +00:00
|
|
|
|
|
|
|
let 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() {
|
2017-09-14 22:50:11 +00:00
|
|
|
const realm = new Realm({schema: [schemas.IntPrimary, schemas.AllTypes, schemas.TestObject, schemas.LinkToAllTypes]});
|
|
|
|
|
2016-03-29 16:02:36 +00:00
|
|
|
// exceptions should be propogated
|
2017-09-14 22:50:11 +00:00
|
|
|
TestCase.assertThrowsContaining(() => realm.write(() => { throw new Error('Inner exception message'); }),
|
|
|
|
'Inner exception message');
|
2016-03-29 16:02:36 +00:00
|
|
|
|
|
|
|
// writes should be possible after caught exception
|
2017-09-14 22:50:11 +00:00
|
|
|
realm.write(() => {
|
2016-03-29 16:02:36 +00:00
|
|
|
realm.create('TestObject', {doubleCol: 1});
|
|
|
|
});
|
|
|
|
TestCase.assertEqual(1, realm.objects('TestObject').length);
|
|
|
|
|
2017-09-14 22:50:11 +00:00
|
|
|
realm.write(() => {
|
2016-03-29 16:02:36 +00:00
|
|
|
// nested transactions not supported
|
2017-09-14 22:50:11 +00:00
|
|
|
TestCase.assertThrowsContaining(() => realm.write(() => {}),
|
|
|
|
'The Realm is already in a write transaction');
|
2016-03-29 16:02:36 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2015-08-13 16:12:48 +00:00
|
|
|
testRealmCreate: function() {
|
2017-09-14 22:50:11 +00:00
|
|
|
const realm = new Realm({schema: [schemas.TestObject]});
|
2015-10-12 09:01:29 +00:00
|
|
|
|
2017-09-14 22:50:11 +00:00
|
|
|
TestCase.assertThrowsContaining(() => realm.create('TestObject', {doubleCol: 1}),
|
|
|
|
"Cannot modify managed objects outside of a write transaction.");
|
2015-10-12 09:01:29 +00:00
|
|
|
|
2017-09-14 22:50:11 +00:00
|
|
|
realm.write(() => {
|
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
|
|
|
});
|
|
|
|
|
2017-09-14 22:50:11 +00:00
|
|
|
const objects = realm.objects('TestObject');
|
2015-08-13 16:12:48 +00:00
|
|
|
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() {
|
2017-09-14 22:50:11 +00:00
|
|
|
const realm = new Realm({schema: [schemas.IntPrimary]});
|
2015-09-03 21:05:56 +00:00
|
|
|
|
2017-09-14 22:50:11 +00:00
|
|
|
realm.write(() => {
|
|
|
|
const obj0 = realm.create('IntPrimaryObject', {
|
2015-11-02 20:02:53 +00:00
|
|
|
primaryCol: 0,
|
|
|
|
valueCol: 'val0',
|
|
|
|
});
|
2015-09-03 21:05:56 +00:00
|
|
|
|
2017-09-14 22:50:11 +00:00
|
|
|
TestCase.assertThrowsContaining(() => {
|
2015-11-02 20:02:53 +00:00
|
|
|
realm.create('IntPrimaryObject', {
|
|
|
|
primaryCol: 0,
|
|
|
|
valueCol: 'val0',
|
|
|
|
});
|
2017-09-14 22:50:11 +00:00
|
|
|
}, "Attempting to create an object of type 'IntPrimaryObject' with an existing primary key value '0'.");
|
2015-11-02 20:02:53 +00:00
|
|
|
|
|
|
|
realm.create('IntPrimaryObject', {
|
|
|
|
primaryCol: 1,
|
|
|
|
valueCol: 'val1',
|
|
|
|
}, true);
|
|
|
|
|
2017-09-14 22:50:11 +00:00
|
|
|
const objects = realm.objects('IntPrimaryObject');
|
2015-09-03 21:05:56 +00:00
|
|
|
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
|
|
|
testRealmCreateUpsert: function() {
|
2017-09-11 21:57:31 +00:00
|
|
|
const realm = new Realm({schema: [schemas.AllPrimaryTypes, schemas.TestObject,
|
|
|
|
schemas.StringPrimary]});
|
|
|
|
realm.write(function() {
|
2017-09-14 22:50:11 +00:00
|
|
|
const values = {
|
2015-11-02 20:02:53 +00:00
|
|
|
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: [],
|
|
|
|
};
|
|
|
|
|
2017-09-11 21:57:31 +00:00
|
|
|
const obj0 = realm.create('AllPrimaryTypesObject', values);
|
2015-09-03 21:37:22 +00:00
|
|
|
|
2017-09-11 21:57:31 +00:00
|
|
|
TestCase.assertThrowsContaining(() => realm.create('AllPrimaryTypesObject', values),
|
|
|
|
"Attempting to create an object of type 'AllPrimaryTypesObject' with an existing primary key value ''0''.");
|
2015-11-02 20:02:53 +00:00
|
|
|
|
2017-09-11 21:57:31 +00:00
|
|
|
const obj1 = realm.create('AllPrimaryTypesObject', {
|
2015-11-02 20:02:53 +00:00
|
|
|
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);
|
|
|
|
|
2017-09-11 21:57:31 +00:00
|
|
|
const objects = realm.objects('AllPrimaryTypesObject');
|
2015-09-03 21:37:22 +00:00
|
|
|
TestCase.assertEqual(objects.length, 2);
|
|
|
|
|
2017-09-11 21:57:31 +00:00
|
|
|
realm.create('AllPrimaryTypesObject', {
|
2015-11-02 20:02:53 +00:00
|
|
|
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
|
|
|
|
2017-09-11 21:57:31 +00:00
|
|
|
realm.create('AllPrimaryTypesObject', {primaryCol: '0'}, true);
|
|
|
|
realm.create('AllPrimaryTypesObject', {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
|
|
|
|
2017-09-11 21:57:31 +00:00
|
|
|
realm.create('AllPrimaryTypesObject', {
|
2015-11-02 20:02:53 +00:00
|
|
|
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
|
|
|
|
2017-09-11 21:57:31 +00:00
|
|
|
realm.create('AllPrimaryTypesObject', {primaryCol: '0', objectCol: undefined}, true);
|
|
|
|
realm.create('AllPrimaryTypesObject', {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
|
2017-09-11 21:57:31 +00:00
|
|
|
const obj = realm.create('StringPrimaryObject', {
|
2016-06-13 21:46:26 +00:00
|
|
|
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() {
|
2017-09-14 22:50:11 +00:00
|
|
|
const realm = new Realm({schema: [schemas.IndexedTypes]});
|
|
|
|
realm.write(() => {
|
2016-03-18 23:50:55 +00:00
|
|
|
realm.create('IndexedTypesObject', {boolCol: true, intCol: 1, stringCol: '1', dateCol: new Date(1)});
|
|
|
|
});
|
|
|
|
|
2017-09-14 22:50:11 +00:00
|
|
|
const NotIndexed = {
|
2016-03-18 23:50:55 +00:00
|
|
|
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
|
|
|
|
2017-09-14 22:50:11 +00:00
|
|
|
const IndexedSchema = {
|
2016-04-28 21:08:54 +00:00
|
|
|
name: 'IndexedSchema',
|
|
|
|
};
|
2017-09-14 22:50:11 +00:00
|
|
|
TestCase.assertThrowsContaining(() => {
|
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'});
|
2017-09-14 22:50:11 +00:00
|
|
|
}, "Property 'IndexedSchema.floatCol' of type 'float' cannot be indexed.");
|
2016-03-18 23:50:55 +00:00
|
|
|
|
2017-09-14 22:50:11 +00:00
|
|
|
TestCase.assertThrowsContaining(() => {
|
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'});
|
2017-09-14 22:50:11 +00:00
|
|
|
}, "Property 'IndexedSchema.doubleCol' of type 'double' cannot be indexed.");
|
2016-03-18 23:50:55 +00:00
|
|
|
|
2017-09-14 22:50:11 +00:00
|
|
|
TestCase.assertThrowsContaining(() => {
|
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'});
|
2017-09-14 22:50:11 +00:00
|
|
|
}, "Property 'IndexedSchema.dataCol' of type 'data' cannot be indexed.");
|
2016-03-18 23:50:55 +00:00
|
|
|
|
|
|
|
// primary key
|
2017-09-14 22:50:11 +00:00
|
|
|
IndexedSchema.properties = { intCol: {type: 'int', indexed: true} };
|
|
|
|
IndexedSchema.primaryKey = 'intCol';
|
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() {
|
2017-09-14 22:50:11 +00:00
|
|
|
let realm = new Realm({schema: [schemas.DefaultValues, schemas.TestObject]});
|
2015-11-02 20:02:53 +00:00
|
|
|
|
2017-09-14 22:50:11 +00:00
|
|
|
const createAndTestObject = () => {
|
|
|
|
const obj = realm.create('DefaultValuesObject', {});
|
|
|
|
const properties = schemas.DefaultValues.properties;
|
2015-10-30 19:08:31 +00:00
|
|
|
|
|
|
|
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() {
|
2017-09-14 22:50:11 +00:00
|
|
|
const objectSchema = {
|
2016-05-06 22:41:17 +00:00
|
|
|
name: 'IntObject',
|
|
|
|
properties: {
|
|
|
|
intCol: {type: 'int', default: 1},
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-09-14 22:50:11 +00:00
|
|
|
let realm = new Realm({schema: [objectSchema]});
|
2016-05-06 22:41:17 +00:00
|
|
|
|
2017-09-14 22:50:11 +00:00
|
|
|
const createAndTestObject = () => {
|
|
|
|
const object = realm.create('IntObject', {});
|
2016-05-06 22:41:17 +00:00
|
|
|
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() {
|
2017-09-14 22:50:11 +00:00
|
|
|
let customCreated = 0;
|
2016-02-18 19:09:51 +00:00
|
|
|
|
|
|
|
function CustomObject() {
|
|
|
|
customCreated++;
|
|
|
|
}
|
|
|
|
CustomObject.schema = {
|
|
|
|
name: 'CustomObject',
|
|
|
|
properties: {
|
|
|
|
intCol: 'int'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function InvalidObject() {
|
|
|
|
return {};
|
|
|
|
}
|
2017-09-14 22:50:11 +00:00
|
|
|
TestCase.assertThrowsContaining(() => new Realm({schema: [InvalidObject]}),
|
|
|
|
"Realm object constructor must have a 'schema' property.");
|
2016-02-18 19:09:51 +00:00
|
|
|
|
|
|
|
InvalidObject.schema = {
|
|
|
|
name: 'InvalidObject',
|
|
|
|
properties: {
|
|
|
|
intCol: 'int'
|
|
|
|
}
|
2016-05-06 22:41:17 +00:00
|
|
|
};
|
2016-02-18 19:09:51 +00:00
|
|
|
|
2017-09-14 22:50:11 +00:00
|
|
|
let realm = new Realm({schema: [CustomObject, InvalidObject]});
|
2016-02-18 19:09:51 +00:00
|
|
|
|
2017-09-14 22:50:11 +00:00
|
|
|
realm.write(() => {
|
|
|
|
let object = realm.create('CustomObject', {intCol: 1});
|
2016-02-18 19:09:51 +00:00
|
|
|
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
|
|
|
});
|
|
|
|
|
2017-09-14 22:50:11 +00:00
|
|
|
TestCase.assertThrowsContaining(() => {
|
|
|
|
realm.write(() => {
|
2016-02-18 19:09:51 +00:00
|
|
|
realm.create('InvalidObject', {intCol: 1});
|
|
|
|
});
|
2017-09-14 22:50:11 +00:00
|
|
|
}, 'Realm object constructor must not return another value');
|
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
|
|
|
|
2017-09-14 22:50:11 +00:00
|
|
|
TestCase.assertThrowsContaining(() => {
|
|
|
|
realm.write(() => {
|
2016-03-03 10:49:37 +00:00
|
|
|
realm.create(InvalidCustomObject, {intCol: 1});
|
2016-02-25 01:34:20 +00:00
|
|
|
});
|
2017-09-14 22:50:11 +00:00
|
|
|
}, 'Constructor was not registered in the schema for this Realm');
|
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'
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-09-14 22:50:11 +00:00
|
|
|
let realm = new Realm({schema: [CustomObject]});
|
|
|
|
realm.write(() => {
|
|
|
|
const object = realm.create('CustomObject', {intCol: 1});
|
2016-05-06 22:41:17 +00:00
|
|
|
TestCase.assertTrue(object instanceof CustomObject);
|
|
|
|
});
|
|
|
|
|
|
|
|
function NewCustomObject() {}
|
|
|
|
NewCustomObject.schema = CustomObject.schema;
|
|
|
|
|
|
|
|
realm = new Realm({schema: [NewCustomObject]});
|
2017-09-14 22:50:11 +00:00
|
|
|
realm.write(() => {
|
|
|
|
const object = realm.create('CustomObject', {intCol: 1});
|
2016-05-06 22:41:17 +00:00
|
|
|
TestCase.assertTrue(object instanceof NewCustomObject);
|
|
|
|
});
|
2016-02-18 19:09:51 +00:00
|
|
|
},
|
|
|
|
|
2015-08-14 16:47:33 +00:00
|
|
|
testRealmDelete: function() {
|
2017-09-14 22:50:11 +00:00
|
|
|
const realm = new Realm({schema: [schemas.TestObject]});
|
2015-11-02 20:02:53 +00:00
|
|
|
|
2017-09-14 22:50:11 +00:00
|
|
|
realm.write(() => {
|
|
|
|
for (let 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
|
|
|
});
|
|
|
|
|
2017-09-14 22:50:11 +00:00
|
|
|
const objects = realm.objects('TestObject');
|
|
|
|
TestCase.assertThrowsContaining(() => realm.delete(objects[0]),
|
|
|
|
"Can only delete objects within a transaction.");
|
2015-08-14 16:47:33 +00:00
|
|
|
|
2017-09-14 22:50:11 +00:00
|
|
|
realm.write(() => {
|
2017-09-15 20:36:04 +00:00
|
|
|
TestCase.assertThrowsContaining(() => realm.delete(),
|
|
|
|
"object must be of type 'object', got (undefined)");
|
2015-08-14 16:47:33 +00:00
|
|
|
|
|
|
|
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");
|
|
|
|
|
2017-09-14 22:50:11 +00:00
|
|
|
const 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
|
|
|
|
2017-09-14 22:50:11 +00:00
|
|
|
const o = objects[0];
|
2016-12-20 15:28:02 +00:00
|
|
|
realm.delete(o);
|
2017-09-14 22:50:11 +00:00
|
|
|
TestCase.assertThrowsContaining(() => realm.delete(o),
|
|
|
|
'Object is invalid. Either it has been previously deleted or the Realm it belongs to has been closed.');
|
2015-08-14 16:47:33 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2015-09-08 21:07:14 +00:00
|
|
|
testDeleteAll: function() {
|
2017-09-14 22:50:11 +00:00
|
|
|
const realm = new Realm({schema: [schemas.TestObject, schemas.IntPrimary]});
|
2015-11-02 20:02:53 +00:00
|
|
|
|
2017-09-14 22:50:11 +00:00
|
|
|
realm.write(() => {
|
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);
|
|
|
|
|
2017-09-14 22:50:11 +00:00
|
|
|
TestCase.assertThrowsContaining(() => realm.deleteAll(),
|
|
|
|
"Can only delete objects within a transaction.");
|
2015-09-08 21:07:14 +00:00
|
|
|
|
2017-09-14 22:50:11 +00:00
|
|
|
realm.write(() => {
|
2015-09-08 21:07:14 +00:00
|
|
|
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() {
|
2017-09-14 22:50:11 +00:00
|
|
|
const realm = new Realm({schema: [schemas.PersonObject, schemas.DefaultValues, schemas.TestObject]});
|
2016-02-25 01:34:20 +00:00
|
|
|
|
2017-09-14 22:50:11 +00:00
|
|
|
realm.write(() => {
|
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.
|
2017-09-14 22:50:11 +00:00
|
|
|
const objects = realm.objects(schemas.PersonObject);
|
2016-02-25 01:34:20 +00:00
|
|
|
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
|
|
|
|
2017-09-15 20:36:04 +00:00
|
|
|
TestCase.assertThrowsContaining(() => realm.objects(), "objectType must be of type 'string', got (undefined)");
|
2017-09-14 22:50:11 +00:00
|
|
|
TestCase.assertThrowsContaining(() => realm.objects([]), "objectType must be of type 'string', got ()");
|
|
|
|
TestCase.assertThrowsContaining(() => realm.objects('InvalidClass'), "Object type 'InvalidClass' not found in schema.");
|
2017-09-15 20:36:04 +00:00
|
|
|
TestCase.assertThrowsContaining(() => realm.objects('PersonObject', 'truepredicate'),
|
|
|
|
"Invalid arguments: at most 1 expected, but 2 supplied.");
|
2017-09-14 22:50:11 +00:00
|
|
|
TestCase.assertThrowsContaining(() => realm.objects(InvalidPerson),
|
|
|
|
'Constructor was not registered in the schema for this Realm');
|
2016-12-05 15:57:36 +00:00
|
|
|
|
2017-09-14 22:50:11 +00:00
|
|
|
const person = realm.objects('PersonObject')[0];
|
|
|
|
const listenerCallback = () => {};
|
2016-12-07 14:34:53 +00:00
|
|
|
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();
|
|
|
|
|
2017-09-14 22:50:11 +00:00
|
|
|
TestCase.assertThrowsContaining(() => console.log("Name: ", person.name),
|
|
|
|
'Accessing object of type PersonObject which has been invalidated or deleted');
|
|
|
|
|
|
|
|
TestCase.assertThrowsContaining(() => realm.objects('PersonObject'), 'Cannot access realm that has been closed');
|
|
|
|
TestCase.assertThrowsContaining(() => realm.addListener('change', () => {}), 'Cannot access realm that has been closed');
|
|
|
|
TestCase.assertThrowsContaining(() => realm.create('PersonObject', {name: 'Ari', age: 10}), 'Cannot access realm that has been closed');
|
|
|
|
TestCase.assertThrowsContaining(() => realm.delete(person), 'Cannot access realm that has been closed');
|
|
|
|
TestCase.assertThrowsContaining(() => realm.deleteAll(), 'Cannot access realm that has been closed');
|
|
|
|
TestCase.assertThrowsContaining(() => realm.write(() => {}), 'Cannot access realm that has been closed');
|
|
|
|
TestCase.assertThrowsContaining(() => realm.removeListener('change', listenerCallback), 'Cannot access realm that has been closed');
|
|
|
|
TestCase.assertThrowsContaining(() => realm.removeAllListeners(), 'Cannot access realm that has been closed');
|
2015-08-13 16:12:48 +00:00
|
|
|
},
|
|
|
|
|
2016-06-03 23:43:36 +00:00
|
|
|
testRealmObjectForPrimaryKey: function() {
|
2017-09-14 22:50:11 +00:00
|
|
|
const realm = new Realm({schema: [schemas.IntPrimary, schemas.StringPrimary, schemas.TestObject]});
|
2016-06-03 23:43:36 +00:00
|
|
|
|
2017-09-14 22:50:11 +00:00
|
|
|
realm.write(() => {
|
2016-06-03 23:43:36 +00:00
|
|
|
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);
|
|
|
|
|
2017-09-14 22:50:11 +00:00
|
|
|
TestCase.assertThrowsContaining(() => realm.objectForPrimaryKey('TestObject', 0),
|
|
|
|
"'TestObject' does not have a primary key defined");
|
2017-09-15 20:36:04 +00:00
|
|
|
TestCase.assertThrowsContaining(() => realm.objectForPrimaryKey(),
|
|
|
|
"objectType must be of type 'string', got (undefined)");
|
|
|
|
TestCase.assertThrowsContaining(() => realm.objectForPrimaryKey('IntPrimaryObject'),
|
|
|
|
"Invalid null value for non-nullable primary key.");
|
2017-09-14 22:50:11 +00:00
|
|
|
TestCase.assertThrowsContaining(() => realm.objectForPrimaryKey('InvalidClass', 0),
|
|
|
|
"Object type 'InvalidClass' not found in schema.");
|
2016-06-03 23:43:36 +00:00
|
|
|
},
|
|
|
|
|
2015-08-13 16:12:48 +00:00
|
|
|
testNotifications: function() {
|
2017-09-14 22:50:11 +00:00
|
|
|
const realm = new Realm({schema: []});
|
|
|
|
let notificationCount = 0;
|
|
|
|
let notificationName;
|
2015-10-13 06:19:54 +00:00
|
|
|
|
2017-09-14 22:50:11 +00:00
|
|
|
realm.addListener('change', (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);
|
2017-09-14 22:50:11 +00:00
|
|
|
realm.write(() => {});
|
2015-08-13 16:12:48 +00:00
|
|
|
TestCase.assertEqual(notificationCount, 1);
|
2015-10-26 23:19:12 +00:00
|
|
|
TestCase.assertEqual(notificationName, 'change');
|
2015-10-26 23:15:46 +00:00
|
|
|
|
2017-09-14 22:50:11 +00:00
|
|
|
let 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
|
|
|
|
2017-09-14 22:50:11 +00:00
|
|
|
realm.write(() => {});
|
2015-10-26 23:15:46 +00:00
|
|
|
TestCase.assertEqual(notificationCount, 2);
|
|
|
|
TestCase.assertEqual(secondNotificationCount, 1);
|
|
|
|
|
2015-10-27 02:18:24 +00:00
|
|
|
realm.removeListener('change', secondNotification);
|
2017-09-14 22:50:11 +00:00
|
|
|
realm.write(() => {});
|
2015-10-26 23:15:46 +00:00
|
|
|
TestCase.assertEqual(notificationCount, 3);
|
|
|
|
TestCase.assertEqual(secondNotificationCount, 1);
|
|
|
|
|
|
|
|
realm.removeAllListeners();
|
2017-09-14 22:50:11 +00:00
|
|
|
realm.write(() => {});
|
2015-10-26 23:15:46 +00:00
|
|
|
TestCase.assertEqual(notificationCount, 3);
|
|
|
|
TestCase.assertEqual(secondNotificationCount, 1);
|
2015-10-27 14:48:11 +00:00
|
|
|
|
2017-09-14 22:50:11 +00:00
|
|
|
TestCase.assertThrowsContaining(() => realm.addListener('invalid', () => {}),
|
|
|
|
"Only the 'change' notification name is supported.");
|
2015-10-27 14:48:11 +00:00
|
|
|
|
2017-09-14 22:50:11 +00:00
|
|
|
realm.addListener('change', () => {
|
|
|
|
throw new Error('expected error message');
|
2015-10-27 14:48:11 +00:00
|
|
|
});
|
|
|
|
|
2017-09-14 22:50:11 +00:00
|
|
|
TestCase.assertThrowsContaining(() => realm.write(() => {}),
|
|
|
|
'expected error message');
|
2015-08-13 16:12:48 +00:00
|
|
|
},
|
2016-04-28 19:16:16 +00:00
|
|
|
|
|
|
|
testSchema: function() {
|
2017-09-11 21:57:31 +00:00
|
|
|
const originalSchema = [schemas.TestObject, schemas.AllTypes, schemas.LinkToAllTypes,
|
|
|
|
schemas.IndexedTypes, schemas.IntPrimary, schemas.PersonObject,
|
|
|
|
schemas.LinkTypes, schemas.LinkingObjectsObject];
|
2017-09-14 22:50:11 +00:00
|
|
|
|
|
|
|
const schemaMap = {};
|
|
|
|
originalSchema.forEach(objectSchema => {
|
2016-12-28 16:16:37 +00:00
|
|
|
if (objectSchema.schema) { // for PersonObject
|
|
|
|
schemaMap[objectSchema.schema.name] = objectSchema;
|
|
|
|
} else {
|
|
|
|
schemaMap[objectSchema.name] = objectSchema;
|
|
|
|
}
|
|
|
|
});
|
2016-04-28 19:16:16 +00:00
|
|
|
|
2017-09-14 22:50:11 +00:00
|
|
|
const realm = new Realm({schema: originalSchema});
|
2016-04-28 19:16:16 +00:00
|
|
|
|
2017-09-14 22:50:11 +00:00
|
|
|
const schema = realm.schema;
|
2016-04-28 19:16:16 +00:00
|
|
|
TestCase.assertEqual(schema.length, originalSchema.length);
|
|
|
|
|
2017-09-11 21:57:31 +00:00
|
|
|
const normalizeProperty = (val) => {
|
|
|
|
let prop;
|
|
|
|
if (typeof val !== 'string' && !(val instanceof String)) {
|
|
|
|
prop = val;
|
|
|
|
prop.optional = val.optional || false;
|
|
|
|
prop.indexed = val.indexed || false;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
prop = {type: val, indexed: false, optional: false};
|
|
|
|
}
|
|
|
|
if (prop.type.includes('?')) {
|
|
|
|
prop.optional = true;
|
|
|
|
prop.type = prop.type.replace('?', '');
|
|
|
|
}
|
|
|
|
if (prop.type.includes('[]')) {
|
|
|
|
prop.objectType = prop.type.replace('[]', '');
|
|
|
|
prop.type = 'list';
|
|
|
|
}
|
|
|
|
return prop;
|
|
|
|
};
|
2016-04-28 19:16:16 +00:00
|
|
|
|
2017-09-11 21:57:31 +00:00
|
|
|
for (const objectSchema of schema) {
|
|
|
|
let original = schemaMap[objectSchema.name];
|
2016-04-28 19:44:48 +00:00
|
|
|
if (original.schema) {
|
|
|
|
original = original.schema;
|
|
|
|
}
|
|
|
|
|
2017-09-11 21:57:31 +00:00
|
|
|
TestCase.assertEqual(objectSchema.primaryKey, original.primaryKey);
|
|
|
|
for (const propName in objectSchema.properties) {
|
|
|
|
TestCase.assertDefined(original.properties[propName], `schema has unexpected property ${propName}`);
|
|
|
|
|
|
|
|
const actual = objectSchema.properties[propName];
|
|
|
|
const expected = normalizeProperty(original.properties[propName]);
|
|
|
|
TestCase.assertEqual(actual.name, propName);
|
|
|
|
TestCase.assertEqual(actual.indexed, expected.indexed);
|
|
|
|
|
|
|
|
if (actual.type == 'object') {
|
|
|
|
TestCase.assertEqual(actual.objectType, expected.type === 'object' ? expected.objectType : expected.type);
|
|
|
|
TestCase.assertEqual(actual.optional, true);
|
|
|
|
TestCase.assertUndefined(actual.property);
|
2016-04-28 19:16:16 +00:00
|
|
|
}
|
2017-09-11 21:57:31 +00:00
|
|
|
else if (actual.type == 'list') {
|
|
|
|
TestCase.assertEqual(actual.type, expected.type);
|
|
|
|
TestCase.assertEqual(actual.objectType, expected.objectType);
|
|
|
|
TestCase.assertEqual(actual.optional, expected.optional);
|
|
|
|
TestCase.assertUndefined(actual.property);
|
2017-06-29 09:59:10 +00:00
|
|
|
}
|
2017-09-11 21:57:31 +00:00
|
|
|
else if (actual.type == 'linkingObjects') {
|
|
|
|
TestCase.assertEqual(actual.type, expected.type);
|
|
|
|
TestCase.assertEqual(actual.objectType, expected.objectType);
|
|
|
|
TestCase.assertEqual(actual.property, expected.property);
|
|
|
|
TestCase.assertEqual(actual.optional, false);
|
2016-04-28 19:16:16 +00:00
|
|
|
}
|
|
|
|
else {
|
2017-09-11 21:57:31 +00:00
|
|
|
TestCase.assertEqual(actual.type, expected.type);
|
|
|
|
TestCase.assertEqual(actual.optional, expected.optional);
|
|
|
|
TestCase.assertUndefined(actual.property);
|
|
|
|
TestCase.assertUndefined(actual.objectType);
|
2016-04-28 19:16:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2016-05-13 22:29:59 +00:00
|
|
|
|
|
|
|
testCopyBundledRealmFiles: function() {
|
|
|
|
Realm.copyBundledRealmFiles();
|
|
|
|
|
2017-09-14 22:50:11 +00:00
|
|
|
let 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);
|
|
|
|
|
2017-09-14 22:50:11 +00:00
|
|
|
const newDate = new Date(1);
|
|
|
|
realm.write(() => {
|
2016-05-13 22:29:59 +00:00
|
|
|
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() {
|
2017-09-14 22:50:11 +00:00
|
|
|
const realm = new Realm({schema: [schemas.PersonObject]});
|
|
|
|
|
|
|
|
TestCase.assertThrowsException(() => {
|
|
|
|
realm.write(() => {
|
|
|
|
const p1 = realm.create('PersonObject', { name: 'Ari', age: 10 });
|
2017-01-10 12:41:56 +00:00
|
|
|
p1.age = "Ten";
|
|
|
|
});
|
2017-09-11 21:57:31 +00:00
|
|
|
}, new Error("PersonObject.age must be of type 'number', got 'string' ('Ten')"));
|
2017-06-20 12:40:01 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
testErrorMessageFromInvalidCreate: function() {
|
2017-09-14 22:50:11 +00:00
|
|
|
const realm = new Realm({schema: [schemas.PersonObject]});
|
2017-06-20 12:40:01 +00:00
|
|
|
|
2017-09-14 22:50:11 +00:00
|
|
|
TestCase.assertThrowsException(() => {
|
|
|
|
realm.write(() => {
|
|
|
|
const p1 = realm.create('PersonObject', { name: 'Ari', age: 'Ten' });
|
2017-06-20 12:40:01 +00:00
|
|
|
});
|
2017-09-11 21:57:31 +00:00
|
|
|
}, new Error("PersonObject.age must be of type 'number', got 'string' ('Ten')"));
|
2017-07-10 11:45:49 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
testValidTypesForListProperties: function() {
|
2017-09-14 22:50:11 +00:00
|
|
|
const realm = new Realm({schema: [schemas.PersonObject]});
|
|
|
|
realm.write(() => {
|
|
|
|
const p1 = realm.create('PersonObject', { name: 'Ari', age: 10 });
|
|
|
|
const p2 = realm.create('PersonObject', { name: 'Harold', age: 55, children: realm.objects('PersonObject').filtered('age < 15') });
|
2017-07-10 11:45:49 +00:00
|
|
|
TestCase.assertEqual(p2.children.length, 1);
|
2017-09-14 22:50:11 +00:00
|
|
|
const p3 = realm.create('PersonObject', { name: 'Wendy', age: 52, children: p2.children });
|
2017-07-10 11:45:49 +00:00
|
|
|
TestCase.assertEqual(p3.children.length, 1);
|
|
|
|
});
|
2017-07-12 11:02:21 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
testEmpty: function() {
|
|
|
|
const realm = new Realm({schema: [schemas.PersonObject]});
|
|
|
|
TestCase.assertTrue(realm.empty);
|
|
|
|
|
|
|
|
realm.write(() => realm.create('PersonObject', { name: 'Ari', age: 10 }));
|
|
|
|
TestCase.assertTrue(!realm.empty);
|
|
|
|
|
|
|
|
realm.write(() => realm.delete(realm.objects('PersonObject')));
|
|
|
|
TestCase.assertTrue(realm.empty);
|
2017-08-18 12:22:29 +00:00
|
|
|
},
|
|
|
|
|
2017-08-21 15:48:53 +00:00
|
|
|
testManualTransaction: function() {
|
|
|
|
const realm = new Realm({schema: [schemas.TestObject]});
|
|
|
|
TestCase.assertTrue(realm.empty);
|
|
|
|
|
|
|
|
realm.beginTransaction();
|
|
|
|
realm.create('TestObject', {doubleCol: 3.1415});
|
|
|
|
realm.commitTransaction();
|
|
|
|
|
|
|
|
TestCase.assertEqual(realm.objects('TestObject').length, 1);
|
|
|
|
},
|
|
|
|
|
|
|
|
testCancelTransaction: function() {
|
|
|
|
const realm = new Realm({schema: [schemas.TestObject]});
|
|
|
|
TestCase.assertTrue(realm.empty);
|
|
|
|
|
|
|
|
realm.beginTransaction();
|
|
|
|
realm.create('TestObject', {doubleCol: 3.1415});
|
|
|
|
realm.cancelTransaction();
|
|
|
|
|
|
|
|
TestCase.assertTrue(realm.empty);
|
|
|
|
},
|
|
|
|
|
|
|
|
testIsInTransaction: function() {
|
|
|
|
const realm = new Realm({schema: [schemas.TestObject]});
|
|
|
|
TestCase.assertTrue(!realm.isInTransaction);
|
|
|
|
realm.beginTransaction();
|
|
|
|
TestCase.assertTrue(realm.isInTransaction);
|
|
|
|
realm.cancelTransaction();
|
|
|
|
TestCase.assertTrue(!realm.isInTransaction);
|
|
|
|
},
|
2017-09-11 21:57:31 +00:00
|
|
|
|
2017-08-18 12:22:29 +00:00
|
|
|
testCompact: function() {
|
2017-09-14 22:50:11 +00:00
|
|
|
let wasCalled = false;
|
2017-08-18 12:22:29 +00:00
|
|
|
const count = 1000;
|
|
|
|
// create compactable Realm
|
|
|
|
const realm1 = new Realm({schema: [schemas.StringOnly]});
|
|
|
|
realm1.write(() => {
|
|
|
|
realm1.create('StringOnlyObject', { stringCol: 'A' });
|
2017-09-14 22:50:11 +00:00
|
|
|
for (let i = 0; i < count; i++) {
|
2017-08-18 12:22:29 +00:00
|
|
|
realm1.create('StringOnlyObject', { stringCol: 'ABCDEFG' });
|
|
|
|
}
|
|
|
|
realm1.create('StringOnlyObject', { stringCol: 'B' });
|
|
|
|
});
|
|
|
|
realm1.close();
|
|
|
|
|
|
|
|
// open Realm and see if it is compacted
|
2017-09-14 22:50:11 +00:00
|
|
|
const shouldCompact = (totalBytes, usedBytes) => {
|
2017-08-18 12:22:29 +00:00
|
|
|
wasCalled = true;
|
|
|
|
const fiveHundredKB = 500*1024;
|
|
|
|
return (totalBytes > fiveHundredKB) && (usedBytes / totalBytes) < 0.2;
|
|
|
|
};
|
|
|
|
const realm2 = new Realm({schema: [schemas.StringOnly], shouldCompactOnLaunch: shouldCompact});
|
|
|
|
TestCase.assertTrue(wasCalled);
|
|
|
|
TestCase.assertEqual(realm2.objects('StringOnlyObject').length, count + 2);
|
|
|
|
// we don't check if the file is smaller as we assume that Object Store does it
|
|
|
|
realm2.close();
|
|
|
|
},
|
|
|
|
|
|
|
|
testManualCompact: function() {
|
|
|
|
const realm1 = new Realm({schema: [schemas.StringOnly]});
|
|
|
|
realm1.write(() => {
|
|
|
|
realm1.create('StringOnlyObject', { stringCol: 'A' });
|
|
|
|
});
|
|
|
|
TestCase.assertTrue(realm1.compact());
|
|
|
|
realm1.close();
|
|
|
|
|
|
|
|
const realm2 = new Realm({schema: [schemas.StringOnly]});
|
|
|
|
TestCase.assertEqual(realm2.objects('StringOnlyObject').length, 1);
|
|
|
|
realm2.close();
|
|
|
|
},
|
|
|
|
|
|
|
|
testManualCompactInWrite: function() {
|
|
|
|
const realm = new Realm({schema: [schemas.StringOnly]});
|
|
|
|
realm.write(() => {
|
2017-09-14 22:50:11 +00:00
|
|
|
TestCase.assertThrowsContaining(() => {
|
2017-08-18 12:22:29 +00:00
|
|
|
realm.compact();
|
2017-09-14 22:50:11 +00:00
|
|
|
}, 'Cannot compact a Realm within a transaction.');
|
2017-08-18 12:22:29 +00:00
|
|
|
});
|
|
|
|
TestCase.assertTrue(realm.empty);
|
|
|
|
},
|
|
|
|
|
|
|
|
testManualCompactMultipleInstances: function() {
|
|
|
|
const realm1 = new Realm({schema: [schemas.StringOnly]});
|
|
|
|
const realm2 = new Realm({schema: [schemas.StringOnly]});
|
2017-09-14 22:50:11 +00:00
|
|
|
TestCase.assertTrue(realm1.compact());
|
2017-08-30 04:55:30 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
testRealmDeleteFileDefaultConfigPath: function() {
|
|
|
|
const config = {schema: [schemas.TestObject]};
|
|
|
|
const realm = new Realm(config);
|
|
|
|
|
2017-09-14 22:50:11 +00:00
|
|
|
realm.write(() => {
|
2017-08-30 04:55:30 +00:00
|
|
|
realm.create('TestObject', {doubleCol: 1});
|
|
|
|
});
|
|
|
|
|
|
|
|
TestCase.assertEqual(realm.objects('TestObject').length, 1);
|
|
|
|
realm.close();
|
|
|
|
|
|
|
|
Realm.deleteFile(config);
|
|
|
|
|
|
|
|
const realm2 = new Realm(config);
|
|
|
|
TestCase.assertEqual(realm2.objects('TestObject').length, 0);
|
|
|
|
realm.close();
|
|
|
|
},
|
|
|
|
|
|
|
|
testRealmDeleteFileCustomConfigPath: function() {
|
|
|
|
const config = {schema: [schemas.TestObject], path: 'test-realm-delete-file.realm'};
|
|
|
|
const realm = new Realm(config);
|
|
|
|
|
2017-09-14 22:50:11 +00:00
|
|
|
realm.write(() => {
|
2017-08-30 04:55:30 +00:00
|
|
|
realm.create('TestObject', {doubleCol: 1});
|
|
|
|
});
|
|
|
|
|
|
|
|
TestCase.assertEqual(realm.objects('TestObject').length, 1);
|
|
|
|
realm.close();
|
|
|
|
|
|
|
|
Realm.deleteFile(config);
|
|
|
|
|
|
|
|
const realm2 = new Realm(config);
|
|
|
|
TestCase.assertEqual(realm2.objects('TestObject').length, 0);
|
|
|
|
realm.close();
|
2017-10-03 11:37:21 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
testRealmDeleteRealmIfMigrationNeededVersionChanged: function() {
|
|
|
|
const schema = [{
|
|
|
|
name: 'TestObject',
|
|
|
|
properties: {
|
|
|
|
prop0: 'string',
|
|
|
|
prop1: 'int',
|
|
|
|
}
|
|
|
|
}];
|
|
|
|
|
|
|
|
var realm = new Realm({schema: schema});
|
|
|
|
|
|
|
|
realm.write(function() {
|
|
|
|
realm.create('TestObject', ['stringValue', 1]);
|
|
|
|
});
|
|
|
|
|
|
|
|
realm.close();
|
|
|
|
|
|
|
|
|
|
|
|
realm = new Realm({schema: schema, deleteRealmIfMigrationNeeded: true, schemaVersion: 1, migration: undefined });
|
|
|
|
|
|
|
|
// object should be gone as Realm should get deleted
|
|
|
|
TestCase.assertEqual(realm.objects('TestObject').length, 0);
|
|
|
|
|
|
|
|
// create a new object
|
|
|
|
realm.write(function() {
|
|
|
|
realm.create('TestObject', ['stringValue', 1]);
|
|
|
|
});
|
|
|
|
|
|
|
|
realm.close();
|
|
|
|
|
|
|
|
var migrationWasCalled = false;
|
|
|
|
realm = new Realm({schema: schema, deleteRealmIfMigrationNeeded: false, schemaVersion: 2, migration: function(oldRealm, newRealm) {
|
|
|
|
migrationWasCalled = true;
|
|
|
|
}});
|
|
|
|
|
|
|
|
// migration function should get called as deleteRealmIfMigrationNeeded is false
|
|
|
|
TestCase.assertEqual(migrationWasCalled, true);
|
|
|
|
|
|
|
|
// object should be there because Realm shouldn't get deleted
|
|
|
|
TestCase.assertEqual(realm.objects('TestObject').length, 1);
|
|
|
|
realm.close();
|
|
|
|
},
|
|
|
|
|
|
|
|
testRealmDeleteRealmIfMigrationNeededSchemaChanged: function() {
|
|
|
|
const schema = [{
|
|
|
|
name: 'TestObject',
|
|
|
|
properties: {
|
|
|
|
prop0: 'string',
|
|
|
|
prop1: 'int',
|
|
|
|
}
|
|
|
|
}];
|
|
|
|
|
|
|
|
const schema1 = [{
|
|
|
|
name: 'TestObject',
|
|
|
|
properties: {
|
|
|
|
prop0: 'string',
|
|
|
|
prop1: 'int',
|
|
|
|
prop2: 'float',
|
|
|
|
}
|
|
|
|
}];
|
|
|
|
|
|
|
|
const schema2 = [{
|
|
|
|
name: 'TestObject',
|
|
|
|
properties: {
|
|
|
|
prop0: 'string',
|
|
|
|
prop1: 'int',
|
|
|
|
prop2: 'float',
|
|
|
|
prop3: 'double'
|
|
|
|
}
|
|
|
|
}];
|
|
|
|
|
|
|
|
var realm = new Realm({schema: schema});
|
|
|
|
|
|
|
|
realm.write(function() {
|
|
|
|
realm.create('TestObject', {prop0: 'stringValue', prop1: 1});
|
|
|
|
});
|
|
|
|
|
|
|
|
realm.close();
|
|
|
|
|
|
|
|
|
|
|
|
// change schema
|
|
|
|
realm = new Realm({schema: schema1, deleteRealmIfMigrationNeeded: true, migration: undefined});
|
|
|
|
|
|
|
|
// object should be gone as Realm should get deleted
|
|
|
|
TestCase.assertEqual(realm.objects('TestObject').length, 0);
|
|
|
|
|
|
|
|
// create a new object
|
|
|
|
realm.write(function() {
|
|
|
|
realm.create('TestObject', {prop0: 'stringValue', prop1: 1, prop2: 1.0});
|
|
|
|
});
|
|
|
|
|
|
|
|
realm.close();
|
|
|
|
|
|
|
|
|
|
|
|
TestCase.assertThrows(function(e) {
|
|
|
|
// updating schema without changing schemaVersion OR setting deleteRealmIfMigrationNeeded = true should raise an error
|
|
|
|
new Realm({schema: schema2, deleteRealmIfMigrationNeeded: false, migration: function(oldRealm, newRealm) {}});
|
|
|
|
});
|
|
|
|
|
|
|
|
var migrationWasCalled = false;
|
|
|
|
|
|
|
|
// change schema again, but increment schemaVersion
|
|
|
|
realm = new Realm({schema: schema2, deleteRealmIfMigrationNeeded: false, schemaVersion: 1, migration: function(oldRealm, newRealm) {
|
|
|
|
migrationWasCalled = true;
|
|
|
|
}});
|
|
|
|
|
|
|
|
// migration function should get called as deleteRealmIfMigrationNeeded is false
|
|
|
|
TestCase.assertEqual(migrationWasCalled, true);
|
|
|
|
|
|
|
|
// object should be there because Realm shouldn't get deleted
|
|
|
|
TestCase.assertEqual(realm.objects('TestObject').length, 1);
|
|
|
|
realm.close();
|
|
|
|
},
|
|
|
|
|
|
|
|
testRealmDeleteRealmIfMigrationNeededIncompatibleConfig: function() {
|
|
|
|
const schema = [{
|
|
|
|
name: 'TestObject',
|
|
|
|
properties: {
|
|
|
|
prop0: 'string',
|
|
|
|
prop1: 'int',
|
|
|
|
}
|
|
|
|
}];
|
|
|
|
|
|
|
|
TestCase.assertThrows(function() {
|
|
|
|
new Realm({schema: schema, deleteRealmIfMigrationNeeded: true, readOnly: true});
|
|
|
|
}, "Cannot set 'deleteRealmIfMigrationNeeded' when 'readOnly' is set.")
|
|
|
|
|
|
|
|
TestCase.assertThrows(function() {
|
|
|
|
new Realm({schema: schema, deleteRealmIfMigrationNeeded: true, migration: function(oldRealm, newRealm) {}});
|
|
|
|
}, "Cannot include 'migration' when 'deleteRealmIfMigrationNeeded' is set.")
|
|
|
|
},
|
2017-12-14 06:53:03 +00:00
|
|
|
|
|
|
|
testDisableFileFormatUpgrade: function() {
|
|
|
|
Realm.copyBundledRealmFiles();
|
|
|
|
|
|
|
|
TestCase.assertThrowsContaining(() => {
|
|
|
|
new Realm({ path: 'dates-v3.realm', disableFormatUpgrade: true } );
|
|
|
|
}, 'The Realm file format must be allowed to be upgraded in order to proceed.');
|
|
|
|
}
|
2016-05-20 22:54:20 +00:00
|
|
|
};
|