From 812930285e957cff81480968080a0e306007db84 Mon Sep 17 00:00:00 2001 From: Ari Lazier Date: Tue, 24 Nov 2015 10:39:42 -0800 Subject: [PATCH] compount parser tests --- src/object-store/parser/queryTests.json | 24 +++++++++++++ tests/QueryTests.js | 48 +++++++++++++++++-------- 2 files changed, 58 insertions(+), 14 deletions(-) diff --git a/src/object-store/parser/queryTests.json b/src/object-store/parser/queryTests.json index f9a5f56f..75daa28f 100644 --- a/src/object-store/parser/queryTests.json +++ b/src/object-store/parser/queryTests.json @@ -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"] + ] } } diff --git a/tests/QueryTests.js b/tests/QueryTests.js index e49cb388..a26c6c02 100644 --- a/tests/QueryTests.js +++ b/tests/QueryTests.js @@ -37,27 +37,44 @@ function runQuerySuite(suite) { } }); + 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] + args.push(objects[arg[0]][arg[1]]); + } + else { + args.push(arg); + } + } + return args; + } + 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 arg = test[i]; - if (Array.isArray(arg)) { - // aray arguments correspond to [objectAtIndex, propertyName] - args.push(objects[arg[0]][arg[1]]); - } - else { - args.push(arg); - } + 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"; } - 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); + 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); } });