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-11 21:57:31 +00:00
|
|
|
const Realm = require('realm');
|
|
|
|
const TestCase = require('./asserts');
|
|
|
|
const schemas = require('./schemas');
|
2015-10-06 07:57:35 +00:00
|
|
|
|
2017-09-11 21:57:31 +00:00
|
|
|
const RANDOM_DATA = new Uint8Array([
|
2015-11-16 21:48:37 +00:00
|
|
|
0xd8, 0x21, 0xd6, 0xe8, 0x00, 0x57, 0xbc, 0xb2, 0x6a, 0x15, 0x77, 0x30, 0xac, 0x77, 0x96, 0xd9,
|
|
|
|
0x67, 0x1e, 0x40, 0xa7, 0x6d, 0x52, 0x83, 0xda, 0x07, 0x29, 0x9c, 0x70, 0x38, 0x48, 0x4e, 0xff,
|
2015-11-16 11:26:33 +00:00
|
|
|
]);
|
|
|
|
|
2017-09-11 21:57:31 +00:00
|
|
|
const allTypesValues = {
|
|
|
|
boolCol: true,
|
|
|
|
intCol: 1,
|
|
|
|
floatCol: 1.1,
|
|
|
|
doubleCol: 1.11,
|
|
|
|
stringCol: 'string',
|
|
|
|
dateCol: new Date(1),
|
|
|
|
dataCol: RANDOM_DATA,
|
|
|
|
objectCol: {doubleCol: 2.2},
|
|
|
|
|
|
|
|
optBoolCol: true,
|
|
|
|
optIntCol: 1,
|
|
|
|
optFloatCol: 1.1,
|
|
|
|
optDoubleCol: 1.11,
|
|
|
|
optStringCol: 'string',
|
|
|
|
optDateCol: new Date(1),
|
|
|
|
optDataCol: RANDOM_DATA,
|
|
|
|
|
|
|
|
boolArrayCol: [true],
|
|
|
|
intArrayCol: [1],
|
|
|
|
floatArrayCol: [1.1],
|
|
|
|
doubleArrayCol: [1.11],
|
|
|
|
stringArrayCol: ['string'],
|
|
|
|
dateArrayCol: [new Date(1)],
|
|
|
|
dataArrayCol: [RANDOM_DATA],
|
|
|
|
objectArrayCol: [{doubleCol: 2.2}],
|
|
|
|
|
|
|
|
optBoolArrayCol: [true],
|
|
|
|
optIntArrayCol: [1],
|
|
|
|
optFloatArrayCol: [1.1],
|
|
|
|
optDoubleArrayCol: [1.11],
|
|
|
|
optStringArrayCol: ['string'],
|
|
|
|
optDateArrayCol: [new Date(1)],
|
|
|
|
optDataArrayCol: [RANDOM_DATA],
|
|
|
|
};
|
|
|
|
const nullPropertyValues = (() => {
|
|
|
|
let values = {}
|
|
|
|
for (let name in allTypesValues) {
|
|
|
|
if (name.includes('opt')) {
|
|
|
|
values[name] = name.includes('Array') ? [null] : null;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
values[name] = allTypesValues[name];
|
2015-10-12 08:59:34 +00:00
|
|
|
}
|
2017-09-11 21:57:31 +00:00
|
|
|
}
|
|
|
|
return values;
|
|
|
|
})();
|
2015-10-27 10:13:21 +00:00
|
|
|
|
2017-09-11 21:57:31 +00:00
|
|
|
module.exports = {
|
|
|
|
testAllPropertyGetters: function() {
|
|
|
|
const realm = new Realm({schema: [schemas.AllTypes, schemas.TestObject, schemas.LinkToAllTypes]});
|
|
|
|
let object, nullObject;
|
2015-11-02 20:02:53 +00:00
|
|
|
|
2015-10-22 03:36:21 +00:00
|
|
|
realm.write(function() {
|
2017-09-11 21:57:31 +00:00
|
|
|
object = realm.create('AllTypesObject', allTypesValues);
|
|
|
|
nullObject = realm.create('AllTypesObject', nullPropertyValues);
|
2015-10-22 03:36:21 +00:00
|
|
|
});
|
|
|
|
|
2017-09-11 21:57:31 +00:00
|
|
|
const objectSchema = realm.schema[0];
|
|
|
|
for (const name of Object.keys(objectSchema.properties)) {
|
|
|
|
const type = objectSchema.properties[name].type;
|
|
|
|
if (type === 'linkingObjects') {
|
|
|
|
TestCase.assertEqual(object[name].length, 0);
|
|
|
|
TestCase.assertEqual(nullObject[name].length, 0);
|
|
|
|
continue;
|
2015-10-27 17:04:56 +00:00
|
|
|
}
|
2017-09-11 21:57:31 +00:00
|
|
|
|
|
|
|
TestCase.assertSimilar(type, object[name], allTypesValues[name]);
|
|
|
|
TestCase.assertSimilar(type, nullObject[name], nullPropertyValues[name]);
|
2015-10-22 03:36:21 +00:00
|
|
|
}
|
2015-10-27 17:04:56 +00:00
|
|
|
|
2017-09-11 21:57:31 +00:00
|
|
|
TestCase.assertEqual(object.nonexistent, undefined);
|
2015-10-22 03:36:21 +00:00
|
|
|
},
|
2015-11-02 20:02:53 +00:00
|
|
|
|
2017-09-11 21:57:31 +00:00
|
|
|
testAllTypesPropertySetters: function() {
|
|
|
|
const realm = new Realm({schema: [schemas.AllTypes, schemas.TestObject, schemas.LinkToAllTypes]});
|
|
|
|
let obj;
|
2015-10-12 09:02:23 +00:00
|
|
|
|
2015-10-12 08:59:34 +00:00
|
|
|
realm.write(function() {
|
2017-09-11 21:57:31 +00:00
|
|
|
obj = realm.create('AllTypesObject', allTypesValues);
|
2015-10-12 23:50:45 +00:00
|
|
|
});
|
|
|
|
|
2015-10-12 19:59:40 +00:00
|
|
|
TestCase.assertThrows(function() {
|
2017-09-11 21:57:31 +00:00
|
|
|
obj.boolCol = false;
|
2015-10-12 19:59:40 +00:00
|
|
|
}, 'can only set property values in a write transaction');
|
|
|
|
|
2017-09-11 21:57:31 +00:00
|
|
|
TestCase.assertEqual(obj.boolCol, true, 'bool value changed outside transaction');
|
2015-11-02 20:02:53 +00:00
|
|
|
|
2015-10-27 17:28:34 +00:00
|
|
|
realm.write(function() {
|
2017-09-11 21:57:31 +00:00
|
|
|
TestCase.assertThrows(() => obj.boolCol = 'cat');
|
|
|
|
TestCase.assertThrows(() => obj.intCol = 'dog');
|
2015-11-02 20:02:53 +00:00
|
|
|
|
2017-09-11 21:57:31 +00:00
|
|
|
// Non-optional properties should complain about null
|
|
|
|
for (const name of ['boolCol', 'intCol', 'floatCol', 'doubleCol', 'stringCol', 'dataCol', 'dateCol']) {
|
|
|
|
TestCase.assertThrows(() => obj[name] = null, `Setting ${name} to null should throw`);
|
|
|
|
TestCase.assertThrows(() => obj[name] = undefined, `Setting ${name} to undefined should throw`);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Optional properties should allow it
|
|
|
|
for (const name of ['optBoolCol', 'optIntCol', 'optFloatCol', 'optDoubleCol',
|
|
|
|
'optStringCol', 'optDataCol', 'optDateCol', 'objectCol']) {
|
2015-10-30 19:08:31 +00:00
|
|
|
obj[name] = null;
|
2017-09-11 21:57:31 +00:00
|
|
|
TestCase.assertEqual(obj[name], null);
|
|
|
|
obj[name] = undefined;
|
|
|
|
TestCase.assertEqual(obj[name], null);
|
2015-10-27 17:28:34 +00:00
|
|
|
}
|
|
|
|
|
2017-09-11 21:57:31 +00:00
|
|
|
function tryAssign(name, value) {
|
|
|
|
var prop = schemas.AllTypes.properties[name];
|
|
|
|
var type = typeof prop == 'object' ? prop.type : prop;
|
|
|
|
obj[name] = value;
|
|
|
|
TestCase.assertSimilar(type, obj[name], value, undefined, 1);
|
|
|
|
}
|
2015-10-27 17:28:34 +00:00
|
|
|
|
2017-09-11 21:57:31 +00:00
|
|
|
tryAssign('boolCol', false);
|
|
|
|
tryAssign('intCol', 10);
|
|
|
|
tryAssign('floatCol', 2.2);
|
|
|
|
tryAssign('doubleCol', 3.3);
|
|
|
|
tryAssign('stringCol', 'new str');
|
|
|
|
tryAssign('dateCol', new Date(2));
|
|
|
|
tryAssign('dataCol', RANDOM_DATA);
|
|
|
|
|
|
|
|
tryAssign('optBoolCol', null);
|
|
|
|
tryAssign('optIntCol', null);
|
|
|
|
tryAssign('optFloatCol', null);
|
|
|
|
tryAssign('optDoubleCol', null);
|
|
|
|
tryAssign('optStringCol', null);
|
|
|
|
tryAssign('optDateCol', null);
|
|
|
|
tryAssign('optDataCol', null);
|
|
|
|
|
|
|
|
tryAssign('optBoolCol', false);
|
|
|
|
tryAssign('optIntCol', 10);
|
|
|
|
tryAssign('optFloatCol', 2.2);
|
|
|
|
tryAssign('optDoubleCol', 3.3);
|
|
|
|
tryAssign('optStringCol', 'new str');
|
|
|
|
tryAssign('optDateCol', new Date(2));
|
|
|
|
tryAssign('optDataCol', RANDOM_DATA);
|
2015-10-27 17:28:34 +00:00
|
|
|
|
2017-09-11 21:57:31 +00:00
|
|
|
});
|
2015-10-27 17:28:34 +00:00
|
|
|
},
|
2017-09-11 21:57:31 +00:00
|
|
|
|
2015-08-13 16:12:48 +00:00
|
|
|
testLinkTypesPropertyGetters: function() {
|
2017-09-11 21:57:31 +00:00
|
|
|
const realm = new Realm({schema: [schemas.LinkTypes, schemas.TestObject]});
|
2015-10-12 08:59:34 +00:00
|
|
|
var obj = null;
|
2015-11-02 20:02:53 +00:00
|
|
|
|
2015-10-12 08:59:34 +00:00
|
|
|
realm.write(function() {
|
2015-11-02 20:02:53 +00:00
|
|
|
obj = realm.create('LinkTypesObject', {
|
|
|
|
objectCol: {doubleCol: 1},
|
|
|
|
objectCol1: null,
|
|
|
|
arrayCol: [{doubleCol: 3}],
|
|
|
|
});
|
2015-10-12 08:59:34 +00:00
|
|
|
});
|
2015-08-13 16:12:48 +00:00
|
|
|
|
2015-10-12 08:59:34 +00:00
|
|
|
var objVal = obj.objectCol;
|
|
|
|
TestCase.assertEqual(typeof objVal, 'object');
|
|
|
|
TestCase.assertNotEqual(objVal, null);
|
|
|
|
TestCase.assertEqual(objVal.doubleCol, 1);
|
2015-08-13 16:12:48 +00:00
|
|
|
|
|
|
|
TestCase.assertEqual(obj.objectCol1, null);
|
|
|
|
|
2015-10-12 08:59:34 +00:00
|
|
|
var arrayVal = obj.arrayCol;
|
|
|
|
TestCase.assertEqual(typeof arrayVal, 'object');
|
|
|
|
TestCase.assertNotEqual(arrayVal, null);
|
|
|
|
TestCase.assertEqual(arrayVal.length, 1);
|
|
|
|
TestCase.assertEqual(arrayVal[0].doubleCol, 3);
|
2015-08-13 16:12:48 +00:00
|
|
|
},
|
2017-09-11 21:57:31 +00:00
|
|
|
|
2015-08-13 16:12:48 +00:00
|
|
|
testLinkTypesPropertySetters: function() {
|
2017-09-11 21:57:31 +00:00
|
|
|
const realm = new Realm({schema: [schemas.LinkTypes, schemas.TestObject]});
|
2015-10-28 17:21:32 +00:00
|
|
|
var objects = realm.objects('TestObject');
|
2015-11-02 20:02:53 +00:00
|
|
|
var obj;
|
2015-10-28 17:21:32 +00:00
|
|
|
|
2015-08-13 16:12:48 +00:00
|
|
|
realm.write(function() {
|
2015-11-02 20:02:53 +00:00
|
|
|
obj = realm.create('LinkTypesObject', {
|
|
|
|
objectCol: {doubleCol: 1},
|
|
|
|
objectCol1: null,
|
|
|
|
arrayCol: [{doubleCol: 3}],
|
|
|
|
});
|
2015-08-13 16:12:48 +00:00
|
|
|
});
|
2015-11-02 20:02:53 +00:00
|
|
|
|
2015-10-28 17:21:32 +00:00
|
|
|
TestCase.assertEqual(objects.length, 2);
|
2015-08-13 16:12:48 +00:00
|
|
|
|
2015-10-12 09:02:23 +00:00
|
|
|
TestCase.assertThrows(function() {
|
|
|
|
obj.objectCol1 = obj.objectCol;
|
|
|
|
}, 'can only set property values in a write transaction');
|
|
|
|
|
2015-08-13 16:12:48 +00:00
|
|
|
// set/reuse object property
|
|
|
|
realm.write(function() {
|
|
|
|
obj.objectCol1 = obj.objectCol;
|
|
|
|
});
|
|
|
|
TestCase.assertEqual(obj.objectCol1.doubleCol, 1);
|
|
|
|
//TestCase.assertEqual(obj.objectCol, obj.objectCol1);
|
2015-10-28 17:21:32 +00:00
|
|
|
TestCase.assertEqual(objects.length, 2);
|
2015-08-13 16:12:48 +00:00
|
|
|
|
|
|
|
realm.write(function() {
|
2015-09-04 22:43:26 +00:00
|
|
|
obj.objectCol = null;
|
2015-08-13 16:12:48 +00:00
|
|
|
obj.objectCol1 = null;
|
|
|
|
});
|
|
|
|
TestCase.assertEqual(obj.objectCol, null);
|
|
|
|
TestCase.assertEqual(obj.objectCol1, null);
|
|
|
|
|
|
|
|
// set object as JSON
|
|
|
|
realm.write(function() {
|
2015-11-02 20:02:53 +00:00
|
|
|
obj.objectCol = {doubleCol: 1};
|
2015-08-13 16:12:48 +00:00
|
|
|
});
|
2015-08-14 17:47:56 +00:00
|
|
|
TestCase.assertEqual(obj.objectCol.doubleCol, 1);
|
2015-10-28 17:21:32 +00:00
|
|
|
TestCase.assertEqual(objects.length, 3);
|
2015-08-14 17:47:56 +00:00
|
|
|
|
|
|
|
// set array property
|
|
|
|
realm.write(function() {
|
2015-11-02 20:02:53 +00:00
|
|
|
obj.arrayCol = [
|
|
|
|
obj.arrayCol[0],
|
|
|
|
obj.objectCol,
|
|
|
|
realm.create('TestObject', {doubleCol: 2}),
|
|
|
|
];
|
2015-08-14 17:47:56 +00:00
|
|
|
});
|
2015-11-02 20:02:53 +00:00
|
|
|
|
2015-10-28 17:21:32 +00:00
|
|
|
TestCase.assertEqual(objects.length, 4);
|
2015-08-14 17:47:56 +00:00
|
|
|
TestCase.assertEqual(obj.arrayCol.length, 3);
|
|
|
|
TestCase.assertEqual(obj.arrayCol[0].doubleCol, 3);
|
|
|
|
TestCase.assertEqual(obj.arrayCol[1].doubleCol, 1);
|
|
|
|
TestCase.assertEqual(obj.arrayCol[2].doubleCol, 2);
|
2016-06-08 18:22:12 +00:00
|
|
|
|
|
|
|
// set object from another realm
|
|
|
|
var another = new Realm({path: 'another.realm', schema: realm.schema});
|
|
|
|
var anotherObj;
|
|
|
|
another.write(function() {
|
|
|
|
anotherObj = another.create('TestObject', {doubleCol: 3});
|
|
|
|
});
|
|
|
|
realm.write(function() {
|
|
|
|
obj.objectCol = anotherObj;
|
|
|
|
});
|
|
|
|
TestCase.assertEqual(obj.objectCol.doubleCol, 3);
|
2015-08-13 16:12:48 +00:00
|
|
|
},
|
2017-09-11 21:57:31 +00:00
|
|
|
|
2015-11-25 18:48:51 +00:00
|
|
|
testEnumerablePropertyNames: function() {
|
2017-09-11 21:57:31 +00:00
|
|
|
const realm = new Realm({schema: [schemas.AllTypes, schemas.TestObject, schemas.LinkToAllTypes]});
|
|
|
|
let object;
|
2015-11-25 18:48:51 +00:00
|
|
|
|
2017-09-11 21:57:31 +00:00
|
|
|
realm.write(() => object = realm.create('AllTypesObject', allTypesValues));
|
2015-11-25 18:48:51 +00:00
|
|
|
|
2017-09-11 21:57:31 +00:00
|
|
|
const propNames = Object.keys(schemas.AllTypes.properties);
|
2015-11-25 18:48:51 +00:00
|
|
|
TestCase.assertArraysEqual(Object.keys(object), propNames, 'Object.keys');
|
|
|
|
|
2017-09-11 21:57:31 +00:00
|
|
|
for (let key in object) {
|
2015-11-25 18:48:51 +00:00
|
|
|
TestCase.assertEqual(key, propNames.shift());
|
|
|
|
}
|
|
|
|
|
|
|
|
TestCase.assertEqual(propNames.length, 0);
|
|
|
|
},
|
2017-09-11 21:57:31 +00:00
|
|
|
|
2015-11-16 11:26:33 +00:00
|
|
|
testDataProperties: function() {
|
2017-09-11 21:57:31 +00:00
|
|
|
const realm = new Realm({schema: [schemas.DefaultValues, schemas.TestObject]});
|
2015-11-16 11:26:33 +00:00
|
|
|
var object;
|
|
|
|
|
|
|
|
// Should be be able to set a data property with a typed array.
|
|
|
|
realm.write(function() {
|
|
|
|
object = realm.create('DefaultValuesObject', {dataCol: RANDOM_DATA});
|
|
|
|
});
|
|
|
|
|
|
|
|
// Data properties should return an instance of an ArrayBuffer.
|
|
|
|
TestCase.assertTrue(object.dataCol instanceof ArrayBuffer);
|
|
|
|
TestCase.assertArraysEqual(new Uint8Array(object.dataCol), RANDOM_DATA);
|
|
|
|
|
|
|
|
// Should be able to also set a data property to an ArrayBuffer.
|
|
|
|
realm.write(function() {
|
|
|
|
object.dataCol = RANDOM_DATA.buffer;
|
|
|
|
});
|
|
|
|
TestCase.assertArraysEqual(new Uint8Array(object.dataCol), RANDOM_DATA);
|
|
|
|
|
2017-12-07 12:47:20 +00:00
|
|
|
if (Realm.Sync) {
|
|
|
|
// The base64 decoder comes from realm-sync
|
|
|
|
// Should be able to also set a data property to base64-encoded string.
|
|
|
|
realm.write(function() {
|
|
|
|
object.dataCol = require('buffer/').Buffer.from(RANDOM_DATA).toString('base64');
|
|
|
|
});
|
|
|
|
TestCase.assertArraysEqual(new Uint8Array(object.dataCol), RANDOM_DATA);
|
|
|
|
}
|
|
|
|
|
2015-11-16 21:48:37 +00:00
|
|
|
// Should be to set a data property to a DataView.
|
|
|
|
realm.write(function() {
|
|
|
|
object.dataCol = new DataView(RANDOM_DATA.buffer);
|
|
|
|
});
|
|
|
|
TestCase.assertArraysEqual(new Uint8Array(object.dataCol), RANDOM_DATA);
|
|
|
|
|
|
|
|
// Test that a variety of size and slices of data still work.
|
|
|
|
[
|
|
|
|
[0, -1],
|
|
|
|
[0, -2],
|
|
|
|
[1, 0],
|
|
|
|
[1, -1],
|
|
|
|
[1, -2],
|
|
|
|
[2, 0],
|
|
|
|
[2, -1],
|
|
|
|
[2, -2],
|
|
|
|
].forEach(function(range) {
|
|
|
|
var array = RANDOM_DATA.subarray(range[0], range[1]);
|
|
|
|
realm.write(function() {
|
|
|
|
// Use a partial "view" of the underlying ArrayBuffer.
|
|
|
|
object.dataCol = new Uint8Array(RANDOM_DATA.buffer, range[0], array.length);
|
|
|
|
});
|
|
|
|
TestCase.assertArraysEqual(new Uint8Array(object.dataCol), array, range.join('...'));
|
|
|
|
});
|
|
|
|
|
|
|
|
// Test other TypedArrays to make sure they all work for setting data properties.
|
|
|
|
[
|
|
|
|
Int8Array,
|
|
|
|
Uint8ClampedArray,
|
|
|
|
Int16Array,
|
|
|
|
Uint16Array,
|
|
|
|
Int32Array,
|
|
|
|
Uint32Array,
|
|
|
|
Float32Array,
|
|
|
|
Float64Array,
|
|
|
|
].forEach(function(TypedArray) {
|
|
|
|
var array = new TypedArray(RANDOM_DATA.buffer);
|
|
|
|
realm.write(function() {
|
|
|
|
object.dataCol = array;
|
|
|
|
});
|
|
|
|
TestCase.assertArraysEqual(new TypedArray(object.dataCol), array, TypedArray.name);
|
|
|
|
});
|
|
|
|
|
2015-11-16 11:26:33 +00:00
|
|
|
realm.write(function() {
|
|
|
|
TestCase.assertThrows(function() {
|
|
|
|
object.dataCol = true;
|
|
|
|
});
|
|
|
|
TestCase.assertThrows(function() {
|
|
|
|
object.dataCol = 1;
|
|
|
|
});
|
|
|
|
TestCase.assertThrows(function() {
|
2017-12-07 12:47:20 +00:00
|
|
|
object.dataCol = 'some binary data';
|
2015-11-16 11:26:33 +00:00
|
|
|
});
|
|
|
|
TestCase.assertThrows(function() {
|
|
|
|
object.dataCol = [1];
|
|
|
|
});
|
|
|
|
});
|
2016-04-26 23:30:04 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
testObjectConstructor: function() {
|
2017-09-11 21:57:31 +00:00
|
|
|
const realm = new Realm({schema: [schemas.TestObject]});
|
2016-05-09 23:15:10 +00:00
|
|
|
|
2016-04-26 23:30:04 +00:00
|
|
|
realm.write(function() {
|
|
|
|
var obj = realm.create('TestObject', {doubleCol: 1});
|
2016-04-26 23:59:03 +00:00
|
|
|
TestCase.assertTrue(obj instanceof Realm.Object);
|
2016-04-26 23:30:04 +00:00
|
|
|
});
|
2016-05-09 23:15:10 +00:00
|
|
|
|
|
|
|
TestCase.assertEqual(typeof Realm.Object, 'function');
|
|
|
|
TestCase.assertTrue(Realm.Object instanceof Function);
|
2016-04-26 23:30:04 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
testIsValid: function() {
|
2017-09-11 21:57:31 +00:00
|
|
|
const realm = new Realm({schema: [schemas.TestObject]});
|
2016-04-26 23:30:04 +00:00
|
|
|
var obj;
|
|
|
|
realm.write(function() {
|
|
|
|
obj = realm.create('TestObject', {doubleCol: 1});
|
|
|
|
TestCase.assertEqual(obj.isValid(), true);
|
|
|
|
realm.delete(obj);
|
|
|
|
TestCase.assertEqual(obj.isValid(), false);
|
|
|
|
});
|
|
|
|
|
|
|
|
TestCase.assertEqual(obj.isValid(), false);
|
|
|
|
TestCase.assertThrows(function() {
|
|
|
|
obj.doubleCol;
|
|
|
|
});
|
|
|
|
},
|
2017-09-11 21:57:31 +00:00
|
|
|
|
2017-06-20 09:42:43 +00:00
|
|
|
testObjectSchema: function() {
|
2017-09-11 21:57:31 +00:00
|
|
|
const realm = new Realm({schema: [schemas.TestObject]});
|
2017-06-20 09:42:43 +00:00
|
|
|
var obj;
|
|
|
|
realm.write(function() {
|
|
|
|
obj = realm.create('TestObject', {doubleCol: 1});
|
|
|
|
});
|
|
|
|
|
|
|
|
const schema = obj.objectSchema();
|
|
|
|
TestCase.assertEqual(schema.name, schemas.TestObject.name);
|
|
|
|
TestCase.assertArraysEqual(Object.keys(schema.properties), Object.keys(schemas.TestObject.properties));
|
|
|
|
TestCase.assertEqual(schema.properties.doubleCol.type, 'double');
|
|
|
|
},
|
2016-05-03 20:05:48 +00:00
|
|
|
|
|
|
|
testIgnoredProperties: function() {
|
|
|
|
var realm = new Realm({schema: [schemas.TestObject]});
|
|
|
|
var obj;
|
|
|
|
realm.write(function() {
|
|
|
|
obj = realm.create('TestObject', {doubleCol: 1, ignored: true});
|
|
|
|
});
|
|
|
|
|
|
|
|
TestCase.assertEqual(obj.doubleCol, 1);
|
|
|
|
TestCase.assertEqual(obj.ignored, undefined);
|
|
|
|
obj.ignored = true;
|
|
|
|
TestCase.assertEqual(obj.ignored, true);
|
2016-05-06 00:36:48 +00:00
|
|
|
},
|
|
|
|
|
2016-05-24 15:23:21 +00:00
|
|
|
testDates: function() {
|
2016-05-09 20:12:48 +00:00
|
|
|
Realm.copyBundledRealmFiles();
|
|
|
|
|
2016-05-24 15:23:21 +00:00
|
|
|
// test file format upgrade
|
2016-05-13 22:29:59 +00:00
|
|
|
var realm_v3 = new Realm({path: 'dates-v3.realm', schema: [schemas.DateObject]});
|
2016-05-26 20:17:19 +00:00
|
|
|
TestCase.assertEqual(realm_v3.objects('Date').length, 2);
|
2016-05-13 22:29:59 +00:00
|
|
|
TestCase.assertEqual(realm_v3.objects('Date')[0].currentDate.getTime(), 1462500087955);
|
2016-05-26 20:17:19 +00:00
|
|
|
TestCase.assertEqual(realm_v3.objects('Date')[0].nullDate.getTime(), 1462500087955);
|
|
|
|
TestCase.assertEqual(realm_v3.objects('Date')[1].currentDate.getTime(), -10000);
|
|
|
|
TestCase.assertEqual(realm_v3.objects('Date')[1].nullDate, null);
|
2016-05-11 02:30:37 +00:00
|
|
|
|
2016-05-24 15:23:21 +00:00
|
|
|
// get new file format is not upgraded
|
2016-05-13 22:29:59 +00:00
|
|
|
var realm_v5 = new Realm({path: 'dates-v5.realm', schema: [schemas.DateObject]});
|
2016-05-26 20:17:19 +00:00
|
|
|
TestCase.assertEqual(realm_v5.objects('Date').length, 2);
|
|
|
|
TestCase.assertEqual(realm_v3.objects('Date')[0].currentDate.getTime(), 1462500087955);
|
|
|
|
TestCase.assertEqual(realm_v3.objects('Date')[0].nullDate.getTime(), 1462500087955);
|
|
|
|
TestCase.assertEqual(realm_v3.objects('Date')[1].currentDate.getTime(), -10000);
|
|
|
|
TestCase.assertEqual(realm_v3.objects('Date')[1].nullDate, null);
|
2017-09-11 21:57:31 +00:00
|
|
|
|
2016-05-24 15:23:21 +00:00
|
|
|
// test different dates
|
|
|
|
var realm = new Realm({schema: [schemas.DateObject]});
|
2017-12-07 12:47:20 +00:00
|
|
|
const stringifiedDate = new Date();
|
2016-05-24 15:23:21 +00:00
|
|
|
realm.write(function() {
|
|
|
|
realm.create('Date', { currentDate: new Date(10000) });
|
|
|
|
realm.create('Date', { currentDate: new Date(-10000) });
|
|
|
|
realm.create('Date', { currentDate: new Date(1000000000000) });
|
|
|
|
realm.create('Date', { currentDate: new Date(-1000000000000) });
|
2017-12-07 12:47:20 +00:00
|
|
|
realm.create('Date', { currentDate: stringifiedDate.toString() });
|
2016-05-24 15:23:21 +00:00
|
|
|
});
|
|
|
|
TestCase.assertEqual(realm.objects('Date')[0].currentDate.getTime(), 10000);
|
|
|
|
TestCase.assertEqual(realm.objects('Date')[1].currentDate.getTime(), -10000);
|
|
|
|
TestCase.assertEqual(realm.objects('Date')[2].currentDate.getTime(), 1000000000000);
|
|
|
|
TestCase.assertEqual(realm.objects('Date')[3].currentDate.getTime(), -1000000000000);
|
2017-12-07 12:47:20 +00:00
|
|
|
TestCase.assertEqual(realm.objects('Date')[4].currentDate.toString(), stringifiedDate.toString());
|
2017-12-20 13:19:42 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
testDateResolution: function() {
|
|
|
|
const dateObjectSchema = {
|
|
|
|
name: 'DateObject',
|
|
|
|
properties: {
|
|
|
|
dateCol: 'date'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var realm = new Realm({schema: [dateObjectSchema]})
|
|
|
|
realm.write(function() {
|
|
|
|
realm.create('DateObject', { dateCol: new Date('2017-12-07T20:16:03.837Z') })
|
|
|
|
})
|
|
|
|
|
|
|
|
var objects = realm.objects('DateObject')
|
|
|
|
TestCase.assertEqual(new Date('2017-12-07T20:16:03.837Z').getTime(), objects[0].dateCol.getTime())
|
|
|
|
TestCase.assertTrue(new Date('2017-12-07T20:16:03.837Z').toISOString() === objects[0].dateCol.toISOString())
|
|
|
|
|
|
|
|
realm.close()
|
2018-06-28 22:27:18 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
testSetLink: function() {
|
|
|
|
const schema = [
|
|
|
|
{
|
|
|
|
name: 'PrimaryInt',
|
|
|
|
primaryKey: 'pk',
|
|
|
|
properties: {
|
|
|
|
pk: 'int',
|
|
|
|
value: 'int'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'PrimaryOptionalInt',
|
|
|
|
primaryKey: 'pk',
|
|
|
|
properties: {
|
|
|
|
pk: 'int?',
|
|
|
|
value: 'int'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'PrimaryString',
|
|
|
|
primaryKey: 'pk',
|
|
|
|
properties: {
|
|
|
|
pk: 'string?',
|
|
|
|
value: 'int'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Links',
|
|
|
|
properties: {
|
|
|
|
intLink: 'PrimaryInt',
|
|
|
|
optIntLink: 'PrimaryOptionalInt',
|
|
|
|
stringLink: 'PrimaryString'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
|
|
|
const realm = new Realm({schema: schema});
|
|
|
|
realm.write(function() {
|
|
|
|
realm.create('PrimaryInt', {pk: 1, value: 2})
|
|
|
|
realm.create('PrimaryInt', {pk: 2, value: 4})
|
|
|
|
realm.create('PrimaryOptionalInt', {pk: 1, value: 2})
|
|
|
|
realm.create('PrimaryOptionalInt', {pk: 2, value: 4})
|
|
|
|
realm.create('PrimaryOptionalInt', {pk: null, value: 6})
|
|
|
|
realm.create('PrimaryString', {pk: 'a', value: 2})
|
|
|
|
realm.create('PrimaryString', {pk: 'b', value: 4})
|
|
|
|
realm.create('PrimaryString', {pk: null, value: 6})
|
|
|
|
|
|
|
|
const obj = realm.create('Links', {});
|
|
|
|
|
|
|
|
obj._setLink('intLink', 3);
|
|
|
|
TestCase.assertEqual(obj.intLink, null);
|
|
|
|
obj._setLink('intLink', 1);
|
|
|
|
TestCase.assertEqual(obj.intLink.value, 2);
|
|
|
|
obj._setLink('intLink', 2);
|
|
|
|
TestCase.assertEqual(obj.intLink.value, 4);
|
|
|
|
obj._setLink('intLink', 3);
|
|
|
|
TestCase.assertEqual(obj.intLink.value, 4);
|
|
|
|
|
|
|
|
obj._setLink('optIntLink', 3);
|
|
|
|
TestCase.assertEqual(obj.optIntLink, null);
|
|
|
|
obj._setLink('optIntLink', 1);
|
|
|
|
TestCase.assertEqual(obj.optIntLink.value, 2);
|
|
|
|
obj._setLink('optIntLink', 2);
|
|
|
|
TestCase.assertEqual(obj.optIntLink.value, 4);
|
|
|
|
obj._setLink('optIntLink', null);
|
|
|
|
TestCase.assertEqual(obj.optIntLink.value, 6);
|
|
|
|
obj._setLink('optIntLink', 3);
|
|
|
|
TestCase.assertEqual(obj.optIntLink.value, 6);
|
|
|
|
|
|
|
|
obj._setLink('stringLink', 'c');
|
|
|
|
TestCase.assertEqual(obj.stringLink, null);
|
|
|
|
obj._setLink('stringLink', 'a');
|
|
|
|
TestCase.assertEqual(obj.stringLink.value, 2);
|
|
|
|
obj._setLink('stringLink', 'b');
|
|
|
|
TestCase.assertEqual(obj.stringLink.value, 4);
|
|
|
|
obj._setLink('stringLink', null);
|
|
|
|
TestCase.assertEqual(obj.stringLink.value, 6);
|
|
|
|
obj._setLink('stringLink', 'c');
|
|
|
|
TestCase.assertEqual(obj.stringLink.value, 6);
|
|
|
|
});
|
2016-05-03 20:05:48 +00:00
|
|
|
}
|
2016-05-20 22:54:20 +00:00
|
|
|
};
|