compount parser tests

This commit is contained in:
Ari Lazier 2015-11-24 10:39:42 -08:00
parent 2a03d9be94
commit 812930285e
2 changed files with 58 additions and 14 deletions

View File

@ -245,6 +245,30 @@
["QueryThrows", "LinkObject", "linkCol > $0", [0, "linkCol"]],
["QueryThrows", "LinkObject", "intCol = $0", [0, "linkCol"]]
]
},
"compoundTests" : {
"schema" : [
{ "name": "IntObject",
"properties": [{ "name": "intCol", "type": "int" }],
"primaryKey" : "intCol" }
],
"objects": [
{ "type": "IntObject", "value": [0] },
{ "type": "IntObject", "value": [1] },
{ "type": "IntObject", "value": [2] },
{ "type": "IntObject", "value": [3] }
],
"tests": [
["ObjectSet", [], "IntObject", "intCol == 0 && intCol == 1"],
["ObjectSet", [0, 1], "IntObject", "intCol == 0 || intCol == 1"],
["ObjectSet", [0], "IntObject", "intCol == 0 && intCol != 1"],
["ObjectSet", [2, 3], "IntObject", "intCol >= 2 && intCol < 4"],
["ObjectSet", [0], "IntObject", "intCol == 0 && NOT intCol != 0"],
["ObjectSet", [1], "IntObject", "(intCol == 0 || intCol == 1) && intCol >= 1"],
["ObjectSet", [0, 1], "IntObject", "intCol == 0 || (intCol == 1 && intCol >= 1)"],
["ObjectSet", [0, 1], "IntObject", "intCol == 0 || intCol == 1 && intCol >= 1"]
]
}
}

View File

@ -37,10 +37,10 @@ function runQuerySuite(suite) {
}
});
for (var test of suite.tests) {
if (test[0] == "QueryCount") {
var args = test.slice(2, 4);
for (var i = 4; i < test.length; i++) {
var args;
function getArgs(startArg) {
args = test.slice(startArg, startArg + 2);
for (var i = startArg + 2; i < test.length; i++) {
var arg = test[i];
if (Array.isArray(arg)) {
// aray arguments correspond to [objectAtIndex, propertyName]
@ -50,14 +50,31 @@ function runQuerySuite(suite) {
args.push(arg);
}
}
return args;
}
var results = realm.objects.apply(realm, args);
TestCase.assertEqual(test[1], results.length, "Query '" + args[1] + "' on type '" + args[0] + "' expected " + test[1] + " results, got " + results.length);
for (var test of suite.tests) {
if (test[0] == "QueryCount") {
var length = realm.objects.apply(realm, getArgs(2)).length;
TestCase.assertEqual(test[1], length, "Query '" + args[1] + "' on type '" + args[0] + "' expected " + test[1] + " results, got " + length);
}
else if (test[0] == "ObjectSet") {
var results = realm.objects.apply(realm, getArgs(2));
TestCase.assertEqual(test[1].length, results.length, "Query '" + args[1] + "' on type '" + args[0] + "' expected " + test[1].length + " results, got " + results.length);
var objSchema = suite.schema.find(function(el) { return el.name == args[0] });
var primary = objSchema.primaryKey;
if (!primary) {
throw "Primary key required for object comparison";
}
TestCase.assertArraysEqual(test[1], Array.prototype.map.call(results, function(el) {
return el[primary]
}));
}
else if (test[0] == "QueryThrows") {
var args = test.slice(1);
TestCase.assertThrows(function() {
realm.objects.apply(realm, args);
realm.objects.apply(realm, getArgs(1));
}, "Expected exception not thrown for query: " + JSON.stringify(args));
}
else if (test[0] != "Disabled") {
@ -91,6 +108,9 @@ module.exports = BaseTest.extend({
},
testObjectQueries: function() {
runQuerySuite(testCases.objectTests);
},
testCompoundQueries: function() {
runQuerySuite(testCases.compoundTests);
}
});