tests and bugfixes

This commit is contained in:
Ari Lazier 2016-04-28 12:16:16 -07:00
parent ca87f5a3e0
commit cff099a4af
3 changed files with 68 additions and 21 deletions

View File

@ -224,7 +224,7 @@ realm::Schema Schema<T>::parse_schema(ContextType ctx, ObjectType schema_object,
template<typename T>
typename T::Object Schema<T>::object_for_schema(ContextType ctx, const realm::Schema &schema) {
ObjectType object = Object::create_empty(ctx);
ObjectType object = Object::create_array(ctx);
uint32_t count = 0;
for (auto object_schema : schema) {
Object::set_property(ctx, object, count++, object_for_object_schema(ctx, object_schema));
@ -235,6 +235,10 @@ typename T::Object Schema<T>::object_for_schema(ContextType ctx, const realm::Sc
template<typename T>
typename T::Object Schema<T>::object_for_object_schema(ContextType ctx, const ObjectSchema &object_schema) {
ObjectType object = Object::create_empty(ctx);
static const String name_string = "name";
Object::set_property(ctx, object, name_string, Value::from_string(ctx, object_schema.name));
ObjectType properties = Object::create_empty(ctx);
for (auto property : object_schema.properties) {
Object::set_property(ctx, properties, property.name, object_for_property(ctx, property));

View File

@ -323,17 +323,7 @@ module.exports = BaseTest.extend({
},
testRealmWithIndexedProperties: function() {
var IndexedTypes = {
name: 'IndexedTypesObject',
properties: {
boolCol: {type: 'bool', indexed: true},
intCol: {type: 'int', indexed: true},
stringCol: {type: 'string', indexed: true},
dateCol: {type: 'date', indexed: true},
}
};
var realm = new Realm({schema: [IndexedTypes]});
var realm = new Realm({schema: [schemas.IndexedTypes]});
realm.write(function() {
realm.create('IndexedTypesObject', {boolCol: true, intCol: 1, stringCol: '1', dateCol: new Date(1)});
});
@ -348,26 +338,26 @@ module.exports = BaseTest.extend({
new Realm({schema: [NotIndexed], path: '1'});
TestCase.assertThrows(function() {
IndexedTypes.properties = { floatCol: {type: 'float', indexed: true} }
new Realm({schema: [IndexedTypes], path: '2'});
schemas.IndexedTypes.properties = { floatCol: {type: 'float', indexed: true} }
new Realm({schema: [schemas.IndexedTypes], path: '2'});
});
TestCase.assertThrows(function() {
IndexedTypes.properties = { doubleCol: {type: 'double', indexed: true} }
new Realm({schema: [IndexedTypes], path: '3'});
schemas.IndexedTypes.properties = { doubleCol: {type: 'double', indexed: true} }
new Realm({schema: [schemas.IndexedTypes], path: '3'});
});
TestCase.assertThrows(function() {
IndexedTypes.properties = { dataCol: {type: 'data', indexed: true} }
new Realm({schema: [IndexedTypes], path: '4'});
schemas.IndexedTypes.properties = { dataCol: {type: 'data', indexed: true} }
new Realm({schema: [schemas.IndexedTypes], path: '4'});
});
// primary key
IndexedTypes.primaryKey = 'boolCol';
IndexedTypes.properties = { boolCol: {type: 'bool', indexed: true} }
schemas.IndexedTypes.primaryKey = 'boolCol';
schemas.IndexedTypes.properties = { boolCol: {type: 'bool', indexed: true} }
// Test this doesn't throw
new Realm({schema: [IndexedTypes], path: '5'});
new Realm({schema: [schemas.IndexedTypes], path: '5'});
},
testRealmCreateWithDefaults: function() {
@ -600,4 +590,46 @@ module.exports = BaseTest.extend({
realm.write(function() {});
});
},
testSchema: function() {
var originalSchema = [schemas.TestObject, schemas.BasicTypes, schemas.NullableBasicTypes, schemas.IntPrimary,
schemas.IndexedTypes, schemas.PersonObject, schemas.LinkTypes];
var schemaMap = {};
originalSchema.forEach(function(objectSchema) { schemaMap[objectSchema.name] = objectSchema; });
var realm = new Realm({schema: originalSchema});
var schema = realm.schema;
TestCase.assertEqual(schema.length, originalSchema.length);
function isString(val) {
return typeof val === 'string' || val instanceof String;
}
function verifyObjectSchema(returned) {
var original = schemaMap[returned.name];
TestCase.assertEqual(returned.primaryKey, original.primaryKey);
for (var propName in returned.properties) {
var prop1 = returned.properties[propName];
var prop2 = original.properties[propName];
if (prop1.type == 'object') {
TestCase.assertEqual(prop1.optional, true);
}
else if (prop1.type == 'list') {
TestCase.assertEqual(prop1.optional, false);
}
else {
TestCase.assertEqual(prop1.type, isString(prop2) ? prop2 : prop2.type);
TestCase.assertEqual(prop1.optional, prop2.optional || undefined);
}
TestCase.assertEqual(prop1.indexed, prop2.indexed || prop1.name == returned.primaryKey || undefined);
}
}
for (var i = 0; i < originalSchema.length; i++) {
verifyObjectSchema(schema[i]);
}
},
});

View File

@ -77,6 +77,17 @@ exports.NullableBasicTypes = {
}
};
exports.IndexedTypes = {
name: 'IndexedTypesObject',
properties: {
boolCol: {type: 'bool', indexed: true},
intCol: {type: 'int', indexed: true},
stringCol: {type: 'string', indexed: true},
dateCol: {type: 'date', indexed: true},
}
};
exports.LinkTypes = {
name: 'LinkTypesObject',
properties: {