From cff099a4afb7e900a630b7e96f99a7952d8f81b6 Mon Sep 17 00:00:00 2001 From: Ari Lazier Date: Thu, 28 Apr 2016 12:16:16 -0700 Subject: [PATCH] tests and bugfixes --- src/js_schema.hpp | 6 +++- tests/js/realm-tests.js | 72 +++++++++++++++++++++++++++++------------ tests/js/schemas.js | 11 +++++++ 3 files changed, 68 insertions(+), 21 deletions(-) diff --git a/src/js_schema.hpp b/src/js_schema.hpp index c2da0ef1..d2a67047 100644 --- a/src/js_schema.hpp +++ b/src/js_schema.hpp @@ -224,7 +224,7 @@ realm::Schema Schema::parse_schema(ContextType ctx, ObjectType schema_object, template typename T::Object Schema::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::object_for_schema(ContextType ctx, const realm::Sc template typename T::Object Schema::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)); diff --git a/tests/js/realm-tests.js b/tests/js/realm-tests.js index 3e6aec72..1b2db0ba 100644 --- a/tests/js/realm-tests.js +++ b/tests/js/realm-tests.js @@ -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]); + } + }, }); diff --git a/tests/js/schemas.js b/tests/js/schemas.js index 707d21c5..ef9b8077 100644 --- a/tests/js/schemas.js +++ b/tests/js/schemas.js @@ -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: {