Check the exception message in all Realm tests which assert an exception is thrown

This commit is contained in:
Thomas Goyne 2017-09-14 15:50:11 -07:00
parent 4ad75c9546
commit 53ca09839e
2 changed files with 238 additions and 296 deletions

View File

@ -120,6 +120,23 @@ module.exports = {
}
},
assertThrowsContaining: function(func, expectedMessage) {
var caught = false;
try {
func();
}
catch (e) {
caught = true;
if (!e.message.includes(expectedMessage)) {
throw new TestFailureError(`Expected exception "${expectedMessage}" not thrown - instead caught: "${e}"`);
}
}
if (!caught) {
throw new TestFailureError(`Expected exception "${expectedMessage}" not thrown`);
}
},
assertTrue: function(condition, errorMessage) {
if (!condition) {
throw new TestFailureError(errorMessage || `Condition ${condition} expected to be true`);

View File

@ -18,9 +18,9 @@
'use strict';
var Realm = require('realm');
var TestCase = require('./asserts');
var schemas = require('./schemas');
const Realm = require('realm');
const TestCase = require('./asserts');
const schemas = require('./schemas');
let pathSeparator = '/';
if (typeof process === 'object' && process.platform === 'win32') {
@ -29,7 +29,7 @@ if (typeof process === 'object' && process.platform === 'win32') {
module.exports = {
testRealmConstructor: function() {
var realm = new Realm({schema: []});
const realm = new Realm({schema: []});
TestCase.assertTrue(realm instanceof Realm);
TestCase.assertEqual(typeof Realm, 'function');
@ -37,52 +37,43 @@ module.exports = {
},
testRealmConstructorPath: function() {
TestCase.assertThrows(function() {
new Realm('');
}, 'Realm cannot be created with an invalid path');
TestCase.assertThrows(function() {
new Realm('test1.realm', 'invalidArgument');
}, 'Realm constructor can only have 0 or 1 argument(s)');
TestCase.assertThrows(() => new Realm('')); // the message for this error is platform-specific
TestCase.assertThrowsContaining(() => new Realm('test1.realm', 'invalidArgument'),
"Invalid arguments when constructing 'Realm'");
var defaultRealm = new Realm({schema: []});
const defaultRealm = new Realm({schema: []});
TestCase.assertEqual(defaultRealm.path, Realm.defaultPath);
var defaultRealm2 = new Realm();
const defaultRealm2 = new Realm();
TestCase.assertEqual(defaultRealm2.path, Realm.defaultPath);
var defaultDir = Realm.defaultPath.substring(0, Realm.defaultPath.lastIndexOf(pathSeparator) + 1)
var testPath = 'test1.realm';
var realm = new Realm({schema: [], path: testPath});
const defaultDir = Realm.defaultPath.substring(0, Realm.defaultPath.lastIndexOf(pathSeparator) + 1);
const testPath = 'test1.realm';
const realm = new Realm({schema: [], path: testPath});
TestCase.assertEqual(realm.path, defaultDir + testPath);
var testPath2 = 'test2.realm';
var realm2 = new Realm({schema: [], path: testPath2});
const testPath2 = 'test2.realm';
const realm2 = new Realm({schema: [], path: testPath2});
TestCase.assertEqual(realm2.path, defaultDir + testPath2);
},
testRealmConstructorSchemaVersion: function() {
var defaultRealm = new Realm({schema: []});
const defaultRealm = new Realm({schema: []});
TestCase.assertEqual(defaultRealm.schemaVersion, 0);
TestCase.assertThrows(function() {
new Realm({schemaVersion: 1, schema: []});
}, "Realm already opened at a different schema version");
TestCase.assertThrowsContaining(() => new Realm({schemaVersion: 1, schema: []}),
"already opened with different schema version.");
TestCase.assertEqual(new Realm().schemaVersion, 0);
TestCase.assertEqual(new Realm({schemaVersion: 0}).schemaVersion, 0);
var realm = new Realm({path: 'test1.realm', schema: [], schemaVersion: 1});
let realm = new Realm({path: 'test1.realm', schema: [], schemaVersion: 1});
TestCase.assertEqual(realm.schemaVersion, 1);
TestCase.assertEqual(realm.schema.length, 0);
realm.close();
// FIXME - enable once realm initialization supports schema comparison
// TestCase.assertThrows(function() {
// realm = new Realm({path: testPath, schema: [schemas.TestObject], schemaVersion: 1});
// }, "schema changes require updating the schema version");
realm = new Realm({path: 'test1.realm', schema: [schemas.TestObject], schemaVersion: 2});
realm.write(function() {
realm.write(() => {
realm.create('TestObject', {doubleCol: 1});
});
TestCase.assertEqual(realm.objects('TestObject')[0].doubleCol, 1);
@ -91,41 +82,31 @@ module.exports = {
},
testRealmConstructorDynamicSchema: function() {
var realm = new Realm({schema: [schemas.TestObject]});
realm.write(function() {
let realm = new Realm({schema: [schemas.TestObject]});
realm.write(() => {
realm.create('TestObject', [1])
});
realm.close();
realm = new Realm();
var objects = realm.objects('TestObject');
const objects = realm.objects('TestObject');
TestCase.assertEqual(objects.length, 1);
TestCase.assertEqual(objects[0].doubleCol, 1.0);
},
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() {
new Realm({schema: [{properties: {intCol: 'int'}}]});
}, 'The schema should be an array of ObjectSchema objects');
TestCase.assertThrowsContaining(() => new Realm({schema: schemas.AllTypes}), "schema must be of type 'array', got");
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)");
// linkingObjects property where the source property is missing
TestCase.assertThrows(function() {
TestCase.assertThrowsContaining(() => {
new Realm({schema: [{
name: 'InvalidObject',
properties: {
@ -135,7 +116,7 @@ module.exports = {
}, "Property 'InvalidObject.nosuchproperty' declared as origin of linking objects property 'InvalidObject.linkingObjects' does not exist");
// linkingObjects property where the source property is not a link
TestCase.assertThrows(function() {
TestCase.assertThrowsContaining(() => {
new Realm({schema: [{
name: 'InvalidObject',
properties: {
@ -146,7 +127,7 @@ module.exports = {
}, "Property 'InvalidObject.integer' declared as origin of linking objects property 'InvalidObject.linkingObjects' is not a link")
// linkingObjects property where the source property links elsewhere
TestCase.assertThrows(function() {
TestCase.assertThrowsContaining(() => {
new Realm({schema: [{
name: 'InvalidObject',
properties: {
@ -165,7 +146,7 @@ module.exports = {
testRealmConstructorInMemory: function() {
// open in-memory realm instance
const realm1 = new Realm({inMemory: true, schema: [schemas.TestObject]});
realm1.write(function() {
realm1.write(() => {
realm1.create('TestObject', [1])
});
TestCase.assertEqual(realm1.inMemory, true);
@ -187,28 +168,25 @@ module.exports = {
TestCase.assertEqual(realm3.schema.length, 0);
// try to open the same realm in persistent mode (should fail as you cannot mix modes)
TestCase.assertThrows(function() {
const realm4 = new Realm({});
});
TestCase.assertThrowsContaining(() => new Realm({}), 'already opened with different inMemory settings.');
},
testRealmConstructorReadOnly: function() {
var realm = new Realm({schema: [schemas.TestObject]});
realm.write(function() {
let realm = new Realm({schema: [schemas.TestObject]});
realm.write(() => {
realm.create('TestObject', [1])
});
TestCase.assertEqual(realm.readOnly, false);
realm.close();
realm = new Realm({readOnly: true, schema: [schemas.TestObject]});
var objects = realm.objects('TestObject');
const objects = realm.objects('TestObject');
TestCase.assertEqual(objects.length, 1);
TestCase.assertEqual(objects[0].doubleCol, 1.0);
TestCase.assertEqual(realm.readOnly, true);
TestCase.assertThrows(function() {
realm.write(function() {});
});
TestCase.assertThrowsContaining(() => realm.write(() => {}),
"Can't perform transactions on read-only Realms.");
realm.close();
realm = new Realm({readOnly: true});
@ -217,12 +195,12 @@ module.exports = {
},
testDefaultPath: function() {
var defaultPath = Realm.defaultPath;
var defaultRealm = new Realm({schema: []});
const defaultPath = Realm.defaultPath;
let defaultRealm = new Realm({schema: []});
TestCase.assertEqual(defaultRealm.path, Realm.defaultPath);
try {
var newPath = Realm.defaultPath.substring(0, defaultPath.lastIndexOf(pathSeparator) + 1) + 'default2.realm';
const newPath = `${Realm.defaultPath.substring(0, defaultPath.lastIndexOf(pathSeparator) + 1)}default2.realm`;
Realm.defaultPath = newPath;
defaultRealm = new Realm({schema: []});
TestCase.assertEqual(defaultRealm.path, newPath, "should use updated default realm path");
@ -235,7 +213,7 @@ module.exports = {
testRealmSchemaVersion: function() {
TestCase.assertEqual(Realm.schemaVersion(Realm.defaultPath), -1);
var realm = new Realm({schema: []});
let realm = new Realm({schema: []});
TestCase.assertEqual(realm.schemaVersion, 0);
TestCase.assertEqual(Realm.schemaVersion(Realm.defaultPath), 0);
@ -245,69 +223,64 @@ module.exports = {
},
testRealmWrite: function() {
var realm = new Realm({schema: [schemas.IntPrimary, schemas.AllTypes, schemas.TestObject, schemas.LinkToAllTypes]});
const realm = new Realm({schema: [schemas.IntPrimary, schemas.AllTypes, schemas.TestObject, schemas.LinkToAllTypes]});
// exceptions should be propogated
TestCase.assertThrows(function() {
realm.write(function() {
realm.invalid();
});
});
TestCase.assertThrowsContaining(() => realm.write(() => { throw new Error('Inner exception message'); }),
'Inner exception message');
// writes should be possible after caught exception
realm.write(function() {
realm.write(() => {
realm.create('TestObject', {doubleCol: 1});
});
TestCase.assertEqual(1, realm.objects('TestObject').length);
realm.write(function() {
realm.write(() => {
// nested transactions not supported
TestCase.assertThrows(function() {
realm.write(function() {});
});
TestCase.assertThrowsContaining(() => realm.write(() => {}),
'The Realm is already in a write transaction');
});
},
testRealmCreate: function() {
var realm = new Realm({schema: [schemas.TestObject]});
const realm = new Realm({schema: [schemas.TestObject]});
TestCase.assertThrows(function() {
realm.create('TestObject', {doubleCol: 1});
}, 'can only create inside a write transaction');
TestCase.assertThrowsContaining(() => realm.create('TestObject', {doubleCol: 1}),
"Cannot modify managed objects outside of a write transaction.");
realm.write(function() {
realm.write(() => {
realm.create('TestObject', {doubleCol: 1});
realm.create('TestObject', {doubleCol: 2});
});
var objects = realm.objects('TestObject');
const 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');
},
testRealmCreatePrimaryKey: function() {
var realm = new Realm({schema: [schemas.IntPrimary]});
const realm = new Realm({schema: [schemas.IntPrimary]});
realm.write(function() {
var obj0 = realm.create('IntPrimaryObject', {
realm.write(() => {
const obj0 = realm.create('IntPrimaryObject', {
primaryCol: 0,
valueCol: 'val0',
});
TestCase.assertThrows(function() {
TestCase.assertThrowsContaining(() => {
realm.create('IntPrimaryObject', {
primaryCol: 0,
valueCol: 'val0',
});
}, 'cannot create object with conflicting primary key');
}, "Attempting to create an object of type 'IntPrimaryObject' with an existing primary key value '0'.");
realm.create('IntPrimaryObject', {
primaryCol: 1,
valueCol: 'val1',
}, true);
var objects = realm.objects('IntPrimaryObject');
const objects = realm.objects('IntPrimaryObject');
TestCase.assertEqual(objects.length, 2);
realm.create('IntPrimaryObject', {
@ -324,13 +297,13 @@ module.exports = {
},
testRealmCreateOptionals: function() {
var realm = new Realm({schema: [schemas.NullableBasicTypes, schemas.LinkTypes, schemas.TestObject]});
var basic, links;
realm.write(function() {
const realm = new Realm({schema: [schemas.NullableBasicTypes, schemas.LinkTypes, schemas.TestObject]});
let basic, links;
realm.write(() => {
basic = realm.create('NullableBasicTypesObject', {});
links = realm.create('LinkTypesObject', {});
});
for (var name in schemas.NullableBasicTypes.properties) {
for (const name in schemas.NullableBasicTypes.properties) {
TestCase.assertEqual(basic[name], null);
}
TestCase.assertEqual(links.objectCol, null);
@ -338,9 +311,9 @@ module.exports = {
},
testRealmCreateUpsert: function() {
var realm = new Realm({schema: [schemas.IntPrimary, schemas.StringPrimary, schemas.AllTypes, schemas.TestObject, schemas.LinkToAllTypes]});
realm.write(function() {
var values = {
const realm = new Realm({schema: [schemas.IntPrimary, schemas.StringPrimary, schemas.AllTypes, schemas.TestObject, schemas.LinkToAllTypes]});
realm.write(() => {
const values = {
primaryCol: '0',
boolCol: true,
intCol: 1,
@ -353,13 +326,12 @@ module.exports = {
arrayCol: [],
};
var obj0 = realm.create('AllTypesObject', values);
const obj0 = realm.create('AllTypesObject', values);
TestCase.assertThrows(function() {
realm.create('AllTypesObject', values);
}, 'cannot create object with conflicting primary key');
TestCase.assertThrowsContaining(() => realm.create('AllTypesObject', values),
"Attempting to create an object of type 'AllTypesObject' with an existing primary key value '0'.");
var obj1 = realm.create('AllTypesObject', {
const obj1 = realm.create('AllTypesObject', {
primaryCol: '1',
boolCol: false,
intCol: 2,
@ -372,7 +344,7 @@ module.exports = {
arrayCol: [{doubleCol: 2}],
}, true);
var objects = realm.objects('AllTypesObject');
const objects = realm.objects('AllTypesObject');
TestCase.assertEqual(objects.length, 2);
realm.create('AllTypesObject', {
@ -427,7 +399,7 @@ module.exports = {
TestCase.assertEqual(obj1.objectCol, null);
// test with string primaries
var obj =realm.create('StringPrimaryObject', {
const obj =realm.create('StringPrimaryObject', {
primaryCol: '0',
valueCol: 0
});
@ -442,12 +414,12 @@ module.exports = {
},
testRealmWithIndexedProperties: function() {
var realm = new Realm({schema: [schemas.IndexedTypes]});
realm.write(function() {
const realm = new Realm({schema: [schemas.IndexedTypes]});
realm.write(() => {
realm.create('IndexedTypesObject', {boolCol: true, intCol: 1, stringCol: '1', dateCol: new Date(1)});
});
var NotIndexed = {
const NotIndexed = {
name: 'NotIndexedObject',
properties: {
floatCol: {type: 'float', indexed: false}
@ -456,38 +428,38 @@ module.exports = {
new Realm({schema: [NotIndexed], path: '1.realm'});
var IndexedSchema = {
const IndexedSchema = {
name: 'IndexedSchema',
};
TestCase.assertThrows(function() {
TestCase.assertThrowsContaining(() => {
IndexedSchema.properties = { floatCol: {type: 'float', indexed: true} };
new Realm({schema: [IndexedSchema], path: '2.realm'});
});
}, "Property 'IndexedSchema.floatCol' of type 'float' cannot be indexed.");
TestCase.assertThrows(function() {
TestCase.assertThrowsContaining(() => {
IndexedSchema.properties = { doubleCol: {type: 'double', indexed: true} }
new Realm({schema: [IndexedSchema], path: '3.realm'});
});
}, "Property 'IndexedSchema.doubleCol' of type 'double' cannot be indexed.");
TestCase.assertThrows(function() {
TestCase.assertThrowsContaining(() => {
IndexedSchema.properties = { dataCol: {type: 'data', indexed: true} }
new Realm({schema: [IndexedSchema], path: '4.realm'});
});
}, "Property 'IndexedSchema.dataCol' of type 'data' cannot be indexed.");
// primary key
IndexedSchema.properties = { boolCol: {type: 'bool', indexed: true} };
IndexedSchema.primaryKey = 'boolCol';
IndexedSchema.properties = { intCol: {type: 'int', indexed: true} };
IndexedSchema.primaryKey = 'intCol';
// Test this doesn't throw
new Realm({schema: [IndexedSchema], path: '5.realm'});
},
testRealmCreateWithDefaults: function() {
var realm = new Realm({schema: [schemas.DefaultValues, schemas.TestObject]});
let realm = new Realm({schema: [schemas.DefaultValues, schemas.TestObject]});
var createAndTestObject = function() {
var obj = realm.create('DefaultValuesObject', {});
var properties = schemas.DefaultValues.properties;
const createAndTestObject = () => {
const obj = realm.create('DefaultValuesObject', {});
const properties = schemas.DefaultValues.properties;
TestCase.assertEqual(obj.boolCol, properties.boolCol.default);
TestCase.assertEqual(obj.intCol, properties.intCol.default);
@ -510,17 +482,17 @@ module.exports = {
},
testRealmCreateWithChangingDefaults: function() {
var objectSchema = {
const objectSchema = {
name: 'IntObject',
properties: {
intCol: {type: 'int', default: 1},
}
};
var realm = new Realm({schema: [objectSchema]});
let realm = new Realm({schema: [objectSchema]});
var createAndTestObject = function() {
var object = realm.create('IntObject', {});
const createAndTestObject = () => {
const object = realm.create('IntObject', {});
TestCase.assertEqual(object.intCol, objectSchema.properties.intCol.default);
};
@ -533,7 +505,7 @@ module.exports = {
},
testRealmCreateWithConstructor: function() {
var customCreated = 0;
let customCreated = 0;
function CustomObject() {
customCreated++;
@ -548,9 +520,8 @@ module.exports = {
function InvalidObject() {
return {};
}
TestCase.assertThrows(function() {
new Realm({schema: [InvalidObject]});
});
TestCase.assertThrowsContaining(() => new Realm({schema: [InvalidObject]}),
"Realm object constructor must have a 'schema' property.");
InvalidObject.schema = {
name: 'InvalidObject',
@ -559,10 +530,10 @@ module.exports = {
}
};
var realm = new Realm({schema: [CustomObject, InvalidObject]});
let realm = new Realm({schema: [CustomObject, InvalidObject]});
realm.write(function() {
var object = realm.create('CustomObject', {intCol: 1});
realm.write(() => {
let object = realm.create('CustomObject', {intCol: 1});
TestCase.assertTrue(object instanceof CustomObject);
TestCase.assertTrue(Object.getPrototypeOf(object) == CustomObject.prototype);
TestCase.assertEqual(customCreated, 1);
@ -574,21 +545,21 @@ module.exports = {
TestCase.assertEqual(customCreated, 2);
});
TestCase.assertThrows(function() {
realm.write(function() {
TestCase.assertThrowsContaining(() => {
realm.write(() => {
realm.create('InvalidObject', {intCol: 1});
});
});
}, 'Realm object constructor must not return another value');
// Only the original constructor should be valid.
function InvalidCustomObject() {}
InvalidCustomObject.schema = CustomObject.schema;
TestCase.assertThrows(function() {
realm.write(function() {
TestCase.assertThrowsContaining(() => {
realm.write(() => {
realm.create(InvalidCustomObject, {intCol: 1});
});
});
}, 'Constructor was not registered in the schema for this Realm');
// The constructor should still work when creating another Realm instance.
realm = new Realm();
@ -605,9 +576,9 @@ module.exports = {
}
};
var realm = new Realm({schema: [CustomObject]});
realm.write(function() {
var object = realm.create('CustomObject', {intCol: 1});
let realm = new Realm({schema: [CustomObject]});
realm.write(() => {
const object = realm.create('CustomObject', {intCol: 1});
TestCase.assertTrue(object instanceof CustomObject);
});
@ -615,30 +586,27 @@ module.exports = {
NewCustomObject.schema = CustomObject.schema;
realm = new Realm({schema: [NewCustomObject]});
realm.write(function() {
var object = realm.create('CustomObject', {intCol: 1});
realm.write(() => {
const object = realm.create('CustomObject', {intCol: 1});
TestCase.assertTrue(object instanceof NewCustomObject);
});
},
testRealmDelete: function() {
var realm = new Realm({schema: [schemas.TestObject]});
const realm = new Realm({schema: [schemas.TestObject]});
realm.write(function() {
for (var i = 0; i < 10; i++) {
realm.write(() => {
for (let i = 0; i < 10; i++) {
realm.create('TestObject', {doubleCol: i});
}
});
var objects = realm.objects('TestObject');
TestCase.assertThrows(function() {
realm.delete(objects[0]);
}, 'can only delete in a write transaction');
const objects = realm.objects('TestObject');
TestCase.assertThrowsContaining(() => realm.delete(objects[0]),
"Can only delete objects within a transaction.");
realm.write(function() {
TestCase.assertThrows(function() {
realm.delete();
});
realm.write(() => {
TestCase.assertThrowsContaining(() => realm.delete(), 'Invalid arguments');
realm.delete(objects[0]);
TestCase.assertEqual(objects.length, 9, 'wrong object count');
@ -650,24 +618,23 @@ module.exports = {
TestCase.assertEqual(objects[0].doubleCol, 7, "wrong property value");
TestCase.assertEqual(objects[1].doubleCol, 8, "wrong property value");
var threeObjects = realm.objects('TestObject').filtered("doubleCol < 5");
const threeObjects = realm.objects('TestObject').filtered("doubleCol < 5");
TestCase.assertEqual(threeObjects.length, 3, "wrong results count");
realm.delete(threeObjects);
TestCase.assertEqual(objects.length, 4, 'wrong object count');
TestCase.assertEqual(threeObjects.length, 0, 'threeObject should have been deleted');
var o = objects[0];
const o = objects[0];
realm.delete(o);
TestCase.assertThrows(function() {
realm.delete(o);
});
TestCase.assertThrowsContaining(() => realm.delete(o),
'Object is invalid. Either it has been previously deleted or the Realm it belongs to has been closed.');
});
},
testDeleteAll: function() {
var realm = new Realm({schema: [schemas.TestObject, schemas.IntPrimary]});
const realm = new Realm({schema: [schemas.TestObject, schemas.IntPrimary]});
realm.write(function() {
realm.write(() => {
realm.create('TestObject', {doubleCol: 1});
realm.create('TestObject', {doubleCol: 2});
realm.create('IntPrimaryObject', {primaryCol: 2, valueCol: 'value'});
@ -676,11 +643,10 @@ module.exports = {
TestCase.assertEqual(realm.objects('TestObject').length, 2);
TestCase.assertEqual(realm.objects('IntPrimaryObject').length, 1);
TestCase.assertThrows(function() {
realm.deleteAll();
}, 'can only deleteAll in a write transaction');
TestCase.assertThrowsContaining(() => realm.deleteAll(),
"Can only delete objects within a transaction.");
realm.write(function() {
realm.write(() => {
realm.deleteAll();
});
@ -689,9 +655,9 @@ module.exports = {
},
testRealmObjects: function() {
var realm = new Realm({schema: [schemas.PersonObject, schemas.DefaultValues, schemas.TestObject]});
const realm = new Realm({schema: [schemas.PersonObject, schemas.DefaultValues, schemas.TestObject]});
realm.write(function() {
realm.write(() => {
realm.create('PersonObject', {name: 'Ari', age: 10});
realm.create('PersonObject', {name: 'Tim', age: 11});
realm.create('PersonObject', {name: 'Bjarne', age: 12});
@ -699,77 +665,44 @@ module.exports = {
});
// Should be able to pass constructor for getting objects.
var objects = realm.objects(schemas.PersonObject);
const objects = realm.objects(schemas.PersonObject);
TestCase.assertTrue(objects[0] instanceof schemas.PersonObject);
function InvalidPerson() {}
InvalidPerson.schema = schemas.PersonObject.schema;
TestCase.assertThrows(function() {
realm.objects();
});
TestCase.assertThrows(function() {
realm.objects([]);
});
TestCase.assertThrows(function() {
realm.objects('InvalidClass');
});
TestCase.assertThrows(function() {
realm.objects('PersonObject', 'truepredicate');
});
TestCase.assertThrows(function() {
realm.objects(InvalidPerson);
});
TestCase.assertThrowsContaining(() => realm.objects(), 'Invalid arguments');
TestCase.assertThrowsContaining(() => realm.objects([]), "objectType must be of type 'string', got ()");
TestCase.assertThrowsContaining(() => realm.objects('InvalidClass'), "Object type 'InvalidClass' not found in schema.");
TestCase.assertThrowsContaining(() => realm.objects('PersonObject', 'truepredicate'), 'Invalid arguments');
TestCase.assertThrowsContaining(() => realm.objects(InvalidPerson),
'Constructor was not registered in the schema for this Realm');
var person = realm.objects('PersonObject')[0];
var listenerCallback = () => {};
const person = realm.objects('PersonObject')[0];
const listenerCallback = () => {};
realm.addListener('change', listenerCallback);
// The tests below assert that everthing throws when
// operating on a closed realm
realm.close();
TestCase.assertThrows(function() {
console.log("Name: ", person.name);
});
TestCase.assertThrowsContaining(() => console.log("Name: ", person.name),
'Accessing object of type PersonObject which has been invalidated or deleted');
TestCase.assertThrows(function() {
realm.objects('PersonObject');
});
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(() => {});
});
TestCase.assertThrows(function() {
realm.removeListener('change', listenerCallback);
});
TestCase.assertThrows(function() {
realm.removeAllListeners();
});
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');
},
testRealmObjectForPrimaryKey: function() {
var realm = new Realm({schema: [schemas.IntPrimary, schemas.StringPrimary, schemas.TestObject]});
const realm = new Realm({schema: [schemas.IntPrimary, schemas.StringPrimary, schemas.TestObject]});
realm.write(function() {
realm.write(() => {
realm.create('IntPrimaryObject', {primaryCol: 0, valueCol: 'val0'});
realm.create('IntPrimaryObject', {primaryCol: 1, valueCol: 'val1'});
@ -789,36 +722,30 @@ module.exports = {
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);
});
TestCase.assertThrowsContaining(() => realm.objectForPrimaryKey('TestObject', 0),
"'TestObject' does not have a primary key defined");
TestCase.assertThrowsContaining(() => realm.objectForPrimaryKey(), 'Invalid arguments');
TestCase.assertThrowsContaining(() => realm.objectForPrimaryKey('IntPrimary'), "Invalid arguments");
TestCase.assertThrowsContaining(() => realm.objectForPrimaryKey('InvalidClass', 0),
"Object type 'InvalidClass' not found in schema.");
},
testNotifications: function() {
var realm = new Realm({schema: []});
var notificationCount = 0;
var notificationName;
const realm = new Realm({schema: []});
let notificationCount = 0;
let notificationName;
realm.addListener('change', function(realm, name) {
realm.addListener('change', (realm, name) => {
notificationCount++;
notificationName = name;
});
TestCase.assertEqual(notificationCount, 0);
realm.write(function() {});
realm.write(() => {});
TestCase.assertEqual(notificationCount, 1);
TestCase.assertEqual(notificationName, 'change');
var secondNotificationCount = 0;
let secondNotificationCount = 0;
function secondNotification() {
secondNotificationCount++;
}
@ -827,39 +754,37 @@ module.exports = {
realm.addListener('change', secondNotification);
realm.addListener('change', secondNotification);
realm.write(function() {});
realm.write(() => {});
TestCase.assertEqual(notificationCount, 2);
TestCase.assertEqual(secondNotificationCount, 1);
realm.removeListener('change', secondNotification);
realm.write(function() {});
realm.write(() => {});
TestCase.assertEqual(notificationCount, 3);
TestCase.assertEqual(secondNotificationCount, 1);
realm.removeAllListeners();
realm.write(function() {});
realm.write(() => {});
TestCase.assertEqual(notificationCount, 3);
TestCase.assertEqual(secondNotificationCount, 1);
TestCase.assertThrows(function() {
realm.addListener('invalid', function() {});
TestCase.assertThrowsContaining(() => realm.addListener('invalid', () => {}),
"Only the 'change' notification name is supported.");
realm.addListener('change', () => {
throw new Error('expected error message');
});
realm.addListener('change', function() {
throw new Error('error');
});
TestCase.assertThrows(function() {
realm.write(function() {});
});
TestCase.assertThrowsContaining(() => realm.write(() => {}),
'expected error message');
},
testSchema: function() {
var originalSchema = [schemas.TestObject, schemas.BasicTypes, schemas.NullableBasicTypes, schemas.IndexedTypes, schemas.IntPrimary,
const originalSchema = [schemas.TestObject, schemas.BasicTypes, schemas.NullableBasicTypes, schemas.IndexedTypes, schemas.IntPrimary,
schemas.PersonObject, schemas.LinkTypes, schemas.LinkingObjectsObject];
var schemaMap = {};
originalSchema.forEach(function(objectSchema) {
const schemaMap = {};
originalSchema.forEach(objectSchema => {
if (objectSchema.schema) { // for PersonObject
schemaMap[objectSchema.schema.name] = objectSchema;
} else {
@ -867,9 +792,9 @@ module.exports = {
}
});
var realm = new Realm({schema: originalSchema});
const realm = new Realm({schema: originalSchema});
var schema = realm.schema;
const schema = realm.schema;
TestCase.assertEqual(schema.length, originalSchema.length);
function isString(val) {
@ -877,15 +802,15 @@ module.exports = {
}
function verifyObjectSchema(returned) {
var original = schemaMap[returned.name];
let original = schemaMap[returned.name];
if (original.schema) {
original = original.schema;
}
TestCase.assertEqual(returned.primaryKey, original.primaryKey);
for (var propName in returned.properties) {
var prop1 = returned.properties[propName];
var prop2 = original.properties[propName];
for (const propName in returned.properties) {
const prop1 = returned.properties[propName];
const prop2 = original.properties[propName];
if (prop1.type == 'object') {
TestCase.assertEqual(prop1.objectType, isString(prop2) ? prop2 : prop2.objectType);
TestCase.assertEqual(prop1.optional, true);
@ -908,7 +833,7 @@ module.exports = {
}
}
for (var i = 0; i < originalSchema.length; i++) {
for (let i = 0; i < originalSchema.length; i++) {
verifyObjectSchema(schema[i]);
}
},
@ -916,12 +841,12 @@ module.exports = {
testCopyBundledRealmFiles: function() {
Realm.copyBundledRealmFiles();
var realm = new Realm({path: 'dates-v5.realm', schema: [schemas.DateObject]});
let realm = new Realm({path: 'dates-v5.realm', schema: [schemas.DateObject]});
TestCase.assertEqual(realm.objects('Date').length, 2);
TestCase.assertEqual(realm.objects('Date')[0].currentDate.getTime(), 1462500087955);
var newDate = new Date(1);
realm.write(function() {
const newDate = new Date(1);
realm.write(() => {
realm.objects('Date')[0].currentDate = newDate;
});
realm.close();
@ -933,33 +858,33 @@ module.exports = {
},
testErrorMessageFromInvalidWrite: function() {
var realm = new Realm({schema: [schemas.PersonObject]});
const realm = new Realm({schema: [schemas.PersonObject]});
TestCase.assertThrowsException(function() {
realm.write(function () {
var p1 = realm.create('PersonObject', { name: 'Ari', age: 10 });
TestCase.assertThrowsException(() => {
realm.write(() => {
const p1 = realm.create('PersonObject', { name: 'Ari', age: 10 });
p1.age = "Ten";
});
}, new Error("PersonObject.age must be of type: number"));
}, new Error("PersonObject.age must be of type 'number', got (Ten)"));
},
testErrorMessageFromInvalidCreate: function() {
var realm = new Realm({schema: [schemas.PersonObject]});
const realm = new Realm({schema: [schemas.PersonObject]});
TestCase.assertThrowsException(function() {
realm.write(function () {
var p1 = realm.create('PersonObject', { name: 'Ari', age: 'Ten' });
TestCase.assertThrowsException(() => {
realm.write(() => {
const p1 = realm.create('PersonObject', { name: 'Ari', age: 'Ten' });
});
}, new Error("PersonObject.age must be of type: number"));
}, new Error("PersonObject.age must be of type 'number', got (Ten)"));
},
testValidTypesForListProperties: function() {
var realm = new Realm({schema: [schemas.PersonObject]});
realm.write(function () {
var p1 = realm.create('PersonObject', { name: 'Ari', age: 10 });
var p2 = realm.create('PersonObject', { name: 'Harold', age: 55, children: realm.objects('PersonObject').filtered('age < 15') });
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') });
TestCase.assertEqual(p2.children.length, 1);
var p3 = realm.create('PersonObject', { name: 'Wendy', age: 52, children: p2.children });
const p3 = realm.create('PersonObject', { name: 'Wendy', age: 52, children: p2.children });
TestCase.assertEqual(p3.children.length, 1);
});
},
@ -1007,13 +932,13 @@ module.exports = {
},
testCompact: function() {
var wasCalled = false;
let wasCalled = false;
const count = 1000;
// create compactable Realm
const realm1 = new Realm({schema: [schemas.StringOnly]});
realm1.write(() => {
realm1.create('StringOnlyObject', { stringCol: 'A' });
for (var i = 0; i < count; i++) {
for (let i = 0; i < count; i++) {
realm1.create('StringOnlyObject', { stringCol: 'ABCDEFG' });
}
realm1.create('StringOnlyObject', { stringCol: 'B' });
@ -1021,7 +946,7 @@ module.exports = {
realm1.close();
// open Realm and see if it is compacted
var shouldCompact = function(totalBytes, usedBytes) {
const shouldCompact = (totalBytes, usedBytes) => {
wasCalled = true;
const fiveHundredKB = 500*1024;
return (totalBytes > fiveHundredKB) && (usedBytes / totalBytes) < 0.2;
@ -1049,9 +974,9 @@ module.exports = {
testManualCompactInWrite: function() {
const realm = new Realm({schema: [schemas.StringOnly]});
realm.write(() => {
TestCase.assertThrows(() => {
TestCase.assertThrowsContaining(() => {
realm.compact();
});
}, 'Cannot compact a Realm within a transaction.');
});
TestCase.assertTrue(realm.empty);
},
@ -1059,14 +984,14 @@ module.exports = {
testManualCompactMultipleInstances: function() {
const realm1 = new Realm({schema: [schemas.StringOnly]});
const realm2 = new Realm({schema: [schemas.StringOnly]});
TestCase.assertThrows(realm1.compact());
TestCase.assertTrue(realm1.compact());
},
testRealmDeleteFileDefaultConfigPath: function() {
const config = {schema: [schemas.TestObject]};
const realm = new Realm(config);
realm.write(function() {
realm.write(() => {
realm.create('TestObject', {doubleCol: 1});
});
@ -1084,7 +1009,7 @@ module.exports = {
const config = {schema: [schemas.TestObject], path: 'test-realm-delete-file.realm'};
const realm = new Realm(config);
realm.write(function() {
realm.write(() => {
realm.create('TestObject', {doubleCol: 1});
});