realm-js/tests/ObjectTests.js

261 lines
9.5 KiB
JavaScript
Raw Normal View History

/* Copyright 2015 Realm Inc - All Rights Reserved
* Proprietary and Confidential
*/
2015-08-14 15:18:49 +00:00
2015-08-13 16:12:48 +00:00
'use strict';
var Realm = require('realm');
var BaseTest = require('./base-test');
var TestCase = require('./asserts');
var schemas = require('./schemas');
module.exports = BaseTest.extend({
2015-08-13 16:12:48 +00:00
testBasicTypesPropertyGetters: function() {
2015-10-12 08:59:34 +00:00
var basicTypesValues = [true, 1, 1.1, 1.11, 'string', new Date(1), 'DATA'];
var realm = new Realm({schema: [schemas.BasicTypes]});
2015-10-12 08:59:34 +00:00
var object = null;
realm.write(function() {
object = realm.create('BasicTypesObject', basicTypesValues);
});
2015-08-13 16:12:48 +00:00
for (var i = 0; i < schemas.BasicTypes.properties.length; i++) {
var prop = schemas.BasicTypes.properties[i];
2015-10-12 08:59:34 +00:00
if (prop.type == Realm.Types.FLOAT) {
TestCase.assertEqualWithTolerance(object[prop.name], basicTypesValues[i], 0.000001);
}
else if (prop.type == Realm.Types.DATE) {
TestCase.assertEqual(object[prop.name].getTime(), basicTypesValues[i].getTime());
}
else {
TestCase.assertEqual(object[prop.name], basicTypesValues[i]);
}
}
TestCase.assertEqual(object.nonexistent, undefined);
2015-08-13 16:12:48 +00:00
},
testNullableBasicTypesPropertyGetters: function() {
2015-10-27 17:04:56 +00:00
var nullValues = [null, null, null, null, null, null, null];
var basicTypesValues = [true, 1, 1.1, 1.11, 'string', new Date(1), 'DATA'];
var realm = new Realm({schema: [schemas.NullableBasicTypes]});
2015-10-27 17:04:56 +00:00
var nullObject = null;
var object = null;
realm.write(function() {
2015-10-27 17:04:56 +00:00
nullObject = realm.create('NullableBasicTypesObject', nullValues);
object = realm.create('NullableBasicTypesObject', basicTypesValues);
});
for (var i = 0; i < schemas.BasicTypes.properties.length; i++) {
var prop = schemas.BasicTypes.properties[i];
2015-10-27 17:04:56 +00:00
TestCase.assertEqual(nullObject[prop.name], null);
if (prop.type == Realm.Types.FLOAT) {
TestCase.assertEqualWithTolerance(object[prop.name], basicTypesValues[i], 0.000001);
}
else if (prop.type == Realm.Types.DATE) {
TestCase.assertEqual(object[prop.name].getTime(), basicTypesValues[i].getTime());
}
else {
TestCase.assertEqual(object[prop.name], basicTypesValues[i]);
}
}
2015-10-27 17:04:56 +00:00
},
2015-08-13 16:12:48 +00:00
testBasicTypesPropertySetters: function() {
2015-10-12 08:59:34 +00:00
var basicTypesValues = [true, 1, 1.1, 1.11, 'string', new Date(1), 'DATA'];
var realm = new Realm({schema: [schemas.BasicTypes]});
2015-10-12 08:59:34 +00:00
var obj = null;
2015-10-12 08:59:34 +00:00
realm.write(function() {
obj = realm.create('BasicTypesObject', basicTypesValues);
obj.boolCol = false;
obj.intCol = 2;
obj.floatCol = 2.2;
obj.doubleCol = 2.22;
obj.stringCol = 'STRING';
obj.dateCol = new Date(2);
obj.dataCol = 'b';
});
2015-10-12 08:59:34 +00:00
TestCase.assertEqual(obj.boolCol, false, 'wrong bool value');
TestCase.assertEqual(obj.intCol, 2, 'wrong int value');
TestCase.assertEqualWithTolerance(obj.floatCol, 2.2, 0.000001, 'wrong float value');
TestCase.assertEqual(obj.doubleCol, 2.22, 'wrong double value');
TestCase.assertEqual(obj.stringCol, 'STRING', 'wrong string value');
TestCase.assertEqual(obj.dateCol.getTime(), 2, 'wrong date value');
TestCase.assertEqual(obj.dataCol, 'b', 'wrong data value');
realm.write(function() {
TestCase.assertThrows(function() {
obj.boolCol = 'cat';
});
TestCase.assertThrows(function() {
obj.intCol = 'dog';
});
TestCase.assertThrows(function() {
obj.boolCol = null;
});
TestCase.assertThrows(function() {
obj.boolCol = undefined;
});
TestCase.assertThrows(function() {
obj.intCol = null;
});
TestCase.assertThrows(function() {
obj.intCol = undefined;
});
TestCase.assertThrows(function() {
obj.floatCol = null;
});
TestCase.assertThrows(function() {
obj.floatCol = undefined;
});
TestCase.assertThrows(function() {
obj.doubleCol = null;
});
TestCase.assertThrows(function() {
obj.doubleCol = undefined;
});
TestCase.assertThrows(function() {
obj.stringCol = null;
});
TestCase.assertThrows(function() {
obj.stringCol = undefined;
});
TestCase.assertThrows(function() {
obj.dateCol = null;
});
TestCase.assertThrows(function() {
obj.dateCol = undefined;
});
TestCase.assertThrows(function() {
obj.dataCol = null;
});
TestCase.assertThrows(function() {
obj.dataCol = undefined;
});
});
TestCase.assertThrows(function() {
obj.boolCol = true;
}, 'can only set property values in a write transaction');
TestCase.assertEqual(obj.boolCol, false, 'bool value changed outside transaction');
2015-08-13 16:12:48 +00:00
},
testNullableBasicTypesPropertySetters: function() {
var basicTypesValues = [true, 1, 1.1, 1.11, 'string', new Date(1), 'DATA'];
var realm = new Realm({schema: [schemas.NullableBasicTypes]});
var obj = null;
realm.write(function() {
obj = realm.create('NullableBasicTypesObject', basicTypesValues);
for (var prop of schemas.NullableBasicTypes.properties) {
obj[prop.name] = null;
}
});
for (var prop of schemas.NullableBasicTypes.properties) {
TestCase.assertEqual(obj[prop.name], null);
}
realm.write(function() {
TestCase.assertThrows(function() {
obj.boolCol = 'cat';
});
TestCase.assertThrows(function() {
obj.intCol = 'dog';
});
TestCase.assertThrows(function() {
obj.boolCol = undefined;
});
TestCase.assertThrows(function() {
obj.intCol = undefined;
});
TestCase.assertThrows(function() {
obj.floatCol = undefined;
});
TestCase.assertThrows(function() {
obj.doubleCol = undefined;
});
TestCase.assertThrows(function() {
obj.stringCol = undefined;
});
TestCase.assertThrows(function() {
obj.dateCol = undefined;
});
TestCase.assertThrows(function() {
obj.dataCol = undefined;
});
});
TestCase.assertThrows(function() {
obj.boolCol = null;
}, 'can only set property values in a write transaction');
},
2015-08-13 16:12:48 +00:00
testLinkTypesPropertyGetters: function() {
var realm = new Realm({schema: [schemas.LinkTypes, schemas.TestObject]});
2015-10-12 08:59:34 +00:00
var obj = null;
realm.write(function() {
obj = realm.create('LinkTypesObject', [[1], null, [[3]]]);
});
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
},
testLinkTypesPropertySetters: function() {
var realm = new Realm({schema: [schemas.LinkTypes, schemas.TestObject]});
2015-08-13 16:12:48 +00:00
var obj = null;
realm.write(function() {
2015-09-04 22:43:26 +00:00
obj = realm.create('LinkTypesObject', [[1], null, [[3]]]);
2015-08-13 16:12:48 +00:00
});
TestCase.assertEqual(realm.objects('TestObject').length, 2);
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);
TestCase.assertEqual(realm.objects('TestObject').length, 2);
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-08-14 17:47:56 +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-08-13 16:12:48 +00:00
TestCase.assertEqual(realm.objects('TestObject').length, 3);
2015-08-14 17:47:56 +00:00
// set array property
realm.write(function() {
obj.arrayCol = [obj.arrayCol[0], obj.objectCol, realm.create('TestObject', [2])];
});
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);
2015-08-13 16:12:48 +00:00
},
});