From cdb254ca44a45e90b90d67dee567b6795f416589 Mon Sep 17 00:00:00 2001 From: Ari Lazier Date: Fri, 27 Nov 2015 18:26:58 -0800 Subject: [PATCH] tests for keypath queries --- src/object-store/parser/queryTests.json | 45 +++++++++++++++++++++++-- tests/QueryTests.js | 34 +++++++++++++------ 2 files changed, 66 insertions(+), 13 deletions(-) diff --git a/src/object-store/parser/queryTests.json b/src/object-store/parser/queryTests.json index 630694e8..bc1d4bc3 100644 --- a/src/object-store/parser/queryTests.json +++ b/src/object-store/parser/queryTests.json @@ -229,8 +229,12 @@ "objectTests" : { "schema" : [ - { "name": "IntObject", "properties": [{ "name": "intCol", "type": "int" }] }, - { "name": "LinkObject", "properties": [{ "name": "linkCol", "type": "IntObject" }] } + { "name": "IntObject", "properties": [ + { "name": "intCol", "type": "int" } + ]}, + { "name": "LinkObject", "properties": [ + { "name": "linkCol", "type": "object", "objectType": "IntObject" } + ]} ], "objects": [ { "type": "LinkObject", "value": [[1]] }, @@ -276,6 +280,43 @@ ["ObjectSet", [0, 1], "IntObject", "intCol == 1 || intCol == 0 && intCol <= 0 && intCol >= 0"], ["ObjectSet", [0, 1], "IntObject", "intCol == 0 || NOT (intCol == 3 && intCol >= 0) && intCol == 1"] ] +}, + +"keyPathTests" : { + "schema" : [ + { + "name": "BasicTypesObject", + "properties": [ + { "name": "intCol", "type": "int" }, + { "name": "floatCol", "type": "float" }, + { "name": "doubleCol", "type": "double" }, + { "name": "stringCol", "type": "string" }, + { "name": "dateCol", "type": "date" }, + { "name": "dataCol", "type": "data" } + ] + }, + { + "name": "LinkTypesObject", + "primaryKey": "primaryKey", + "properties": [ + { "name": "primaryKey", "type": "int" }, + { "name": "basicLink", "type": "object", "objectType": "BasicTypesObject" }, + { "name": "linkLink", "type": "object", "objectType": "LinkTypesObject" } + ] + }], + "objects": [ + { "type": "LinkTypesObject", "value": [0, [1, 0.1, 0.001, "1", 1, [1, 10, 100]], null] }, + { "type": "LinkTypesObject", "value": [1, null, [2, [1, 0.1, 0.001, "1", 1, [1, 10, 100]], null]] }, + { "type": "LinkTypesObject", "value": [3, null, [4, [2, 0.2, 0.002, "2", 2, [2, 20, 200]], null]] } + ], + "tests": [ + ["ObjectSet", [0, 2], "LinkTypesObject", "basicLink.intCol == 1"], + ["ObjectSet", [1], "LinkTypesObject", "linkLink.basicLink.intCol == 1"], + ["ObjectSet", [1, 3], "LinkTypesObject", "linkLink.basicLink.intCol > 0"], + ["ObjectSet", [0, 2], "LinkTypesObject", "basicLink.floatCol == 0.1"], + ["ObjectSet", [1], "LinkTypesObject", "linkLink.basicLink.floatCol == 0.1"], + ["ObjectSet", [1, 3], "LinkTypesObject", "linkLink.basicLink.floatCol > 0"] + ] } } diff --git a/tests/QueryTests.js b/tests/QueryTests.js index 093716d1..137cf9a4 100644 --- a/tests/QueryTests.js +++ b/tests/QueryTests.js @@ -12,23 +12,32 @@ var schemas = require('./schemas'); var testCases = require('./queryTests.json'); var typeConverters = {}; + +function convertValue(value, schema, type) { + var objSchema = schema.find(function(el) { return el.name == type }); + if (!objSchema) { + throw "Object schema '" + type + "' not found in test suite."; + } + + return value.map(function(propValue, index) { + if (propValue == null) { + return null; + } + var property = objSchema.properties[index]; + var converter = typeConverters[property.type]; + var propType = property.objectType ? property.objectType : property.type; + return converter ? converter(propValue, schema, propType) : propValue; + }); +} + typeConverters[Realm.Types.DATE] = function(value) { return new Date(value); }; typeConverters[Realm.Types.DATA] = function(value) { return new Uint8Array(value); }; +typeConverters[Realm.Types.OBJECT] = convertValue; function runQuerySuite(suite) { var realm = new Realm({schema: suite.schema}); var objects = suite.objects.map(function(obj) { - var objSchema = suite.schema.find(function(el) { return el.name == obj.type }); - if (!objSchema) { - throw "Object schema '" + obj.type + "' not found in test suite."; - } - - var converted = obj.value.map(function(propValue, index) { - var converter = typeConverters[objSchema.properties[index].type]; - return converter ? converter(propValue) : propValue; - }); - - return { type: obj.type, value: converted }; + return { type: obj.type, value: convertValue(obj.value, suite.schema, obj.type) }; }); realm.write(function() { @@ -111,6 +120,9 @@ module.exports = BaseTest.extend({ }, testCompoundQueries: function() { runQuerySuite(testCases.compoundTests); + }, + testKeyPathQueries: function() { + runQuerySuite(testCases.keyPathTests); } });