tests for keypath queries
This commit is contained in:
parent
c5cad3c3a4
commit
cdb254ca44
|
@ -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"]
|
||||
]
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
});
|
||||
|
||||
|
|
Loading…
Reference in New Issue