2016-02-18 19:59:34 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// Copyright 2016 Realm Inc.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
//
|
|
|
|
////////////////////////////////////////////////////////////////////////////
|
2015-11-16 23:05:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var Realm = require('realm');
|
|
|
|
var TestCase = require('./asserts');
|
2015-12-09 23:49:43 +00:00
|
|
|
var testCases = require('./query-tests.json');
|
2015-11-16 23:05:50 +00:00
|
|
|
|
2015-11-18 00:13:52 +00:00
|
|
|
var typeConverters = {};
|
2015-11-28 02:26:58 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-07-13 16:53:47 +00:00
|
|
|
typeConverters['date'] = function(value) { return new Date(value); };
|
|
|
|
typeConverters['data'] = function(value) { return new Uint8Array(value); };
|
|
|
|
typeConverters['object'] = convertValue;
|
2015-11-18 00:13:52 +00:00
|
|
|
|
2015-11-18 01:17:54 +00:00
|
|
|
function runQuerySuite(suite) {
|
2015-11-18 00:13:52 +00:00
|
|
|
var realm = new Realm({schema: suite.schema});
|
2015-11-18 01:17:54 +00:00
|
|
|
var objects = suite.objects.map(function(obj) {
|
2015-11-28 02:26:58 +00:00
|
|
|
return { type: obj.type, value: convertValue(obj.value, suite.schema, obj.type) };
|
2015-11-20 22:14:35 +00:00
|
|
|
});
|
2015-11-18 01:17:54 +00:00
|
|
|
|
|
|
|
realm.write(function() {
|
2015-11-20 22:14:35 +00:00
|
|
|
for (var i = 0; i < objects.length; i++) {
|
|
|
|
objects[i] = realm.create(objects[i].type, objects[i].value);
|
2015-11-18 00:13:52 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-11-24 18:39:42 +00:00
|
|
|
function getArgs(startArg) {
|
2016-02-18 04:06:42 +00:00
|
|
|
var args = [test[startArg]];
|
|
|
|
for (var i = startArg + 1; i < test.length; i++) {
|
2015-11-24 18:39:42 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-01-12 03:32:46 +00:00
|
|
|
for (var index in suite.tests) {
|
|
|
|
var test = suite.tests[index];
|
2016-05-20 23:33:16 +00:00
|
|
|
var type;
|
|
|
|
var args;
|
|
|
|
var results;
|
|
|
|
|
2015-11-18 01:17:54 +00:00
|
|
|
if (test[0] == "QueryCount") {
|
2016-05-20 23:33:16 +00:00
|
|
|
type = test[2];
|
|
|
|
args = getArgs(3);
|
|
|
|
results = realm.objects(type);
|
|
|
|
|
|
|
|
var length = results.filtered.apply(results, args).length;
|
2016-02-18 04:06:42 +00:00
|
|
|
TestCase.assertEqual(test[1], length, "Query '" + args[0] + "' on type '" + type + "' expected " + test[1] + " results, got " + length);
|
2015-11-24 18:39:42 +00:00
|
|
|
}
|
|
|
|
else if (test[0] == "ObjectSet") {
|
2016-05-20 23:33:16 +00:00
|
|
|
type = test[2];
|
|
|
|
args = getArgs(3);
|
|
|
|
results = realm.objects(type);
|
|
|
|
results = results.filtered.apply(results, args);
|
|
|
|
|
2016-02-18 04:06:42 +00:00
|
|
|
TestCase.assertEqual(test[1].length, results.length, "Query '" + args[0] + "' on type '" + type+ "' expected " + test[1].length + " results, got " + results.length);
|
2015-11-24 18:39:42 +00:00
|
|
|
|
2016-02-18 04:06:42 +00:00
|
|
|
var objSchema = suite.schema.find(function(el) { return el.name == type });
|
2015-11-24 18:39:42 +00:00
|
|
|
var primary = objSchema.primaryKey;
|
|
|
|
if (!primary) {
|
|
|
|
throw "Primary key required for object comparison";
|
2015-11-18 00:13:52 +00:00
|
|
|
}
|
2015-11-18 01:17:54 +00:00
|
|
|
|
2016-02-29 05:40:27 +00:00
|
|
|
TestCase.assertArraysEqual(test[1], results.map(function(el) {
|
|
|
|
return el[primary];
|
2015-11-24 18:39:42 +00:00
|
|
|
}));
|
2015-11-18 01:17:54 +00:00
|
|
|
}
|
|
|
|
else if (test[0] == "QueryThrows") {
|
2016-05-20 23:33:16 +00:00
|
|
|
type = test[1];
|
|
|
|
args = getArgs(2);
|
|
|
|
results = realm.objects(type);
|
|
|
|
|
2015-11-18 01:17:54 +00:00
|
|
|
TestCase.assertThrows(function() {
|
2016-05-20 23:33:16 +00:00
|
|
|
results.filtered.apply(results, args);
|
2015-11-18 20:40:25 +00:00
|
|
|
}, "Expected exception not thrown for query: " + JSON.stringify(args));
|
2015-11-18 01:17:54 +00:00
|
|
|
}
|
2015-11-19 22:36:42 +00:00
|
|
|
else if (test[0] != "Disabled") {
|
2015-11-18 01:17:54 +00:00
|
|
|
throw "Invalid query test '" + test[0] + "'";
|
2015-11-18 00:13:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-18 19:32:52 +00:00
|
|
|
|
2016-05-20 22:54:20 +00:00
|
|
|
module.exports = {
|
2015-11-18 01:50:17 +00:00
|
|
|
testDateQueries: function() {
|
2015-11-18 19:54:45 +00:00
|
|
|
runQuerySuite(testCases.dateTests);
|
2015-11-18 01:50:17 +00:00
|
|
|
},
|
2015-11-18 01:17:54 +00:00
|
|
|
testBoolQueries: function() {
|
2015-11-18 19:54:45 +00:00
|
|
|
runQuerySuite(testCases.boolTests);
|
2015-11-16 23:05:50 +00:00
|
|
|
},
|
2015-11-18 01:50:17 +00:00
|
|
|
testIntQueries: function() {
|
2015-11-18 19:54:45 +00:00
|
|
|
runQuerySuite(testCases.intTests);
|
2015-11-18 01:50:17 +00:00
|
|
|
},
|
2015-11-18 19:32:52 +00:00
|
|
|
testFloatQueries: function() {
|
2015-11-18 20:17:39 +00:00
|
|
|
runQuerySuite(testCases.floatTests);
|
2015-11-18 19:32:52 +00:00
|
|
|
},
|
|
|
|
testDoubleQueries: function() {
|
2015-11-18 20:17:39 +00:00
|
|
|
runQuerySuite(testCases.doubleTests);
|
|
|
|
},
|
|
|
|
testStringQueries: function() {
|
|
|
|
runQuerySuite(testCases.stringTests);
|
2015-11-18 19:32:52 +00:00
|
|
|
},
|
2015-11-19 23:17:57 +00:00
|
|
|
testBinaryQueries: function() {
|
|
|
|
runQuerySuite(testCases.binaryTests);
|
|
|
|
},
|
2015-11-20 22:14:35 +00:00
|
|
|
testObjectQueries: function() {
|
|
|
|
runQuerySuite(testCases.objectTests);
|
2015-11-24 18:39:42 +00:00
|
|
|
},
|
|
|
|
testCompoundQueries: function() {
|
|
|
|
runQuerySuite(testCases.compoundTests);
|
2015-11-28 02:26:58 +00:00
|
|
|
},
|
|
|
|
testKeyPathQueries: function() {
|
|
|
|
runQuerySuite(testCases.keyPathTests);
|
2016-04-11 23:01:51 +00:00
|
|
|
},
|
|
|
|
testOptionalQueries: function() {
|
|
|
|
runQuerySuite(testCases.optionalTests);
|
2018-02-07 15:11:43 +00:00
|
|
|
},
|
|
|
|
testOrderingQueries: function() {
|
|
|
|
runQuerySuite(testCases.orderingTests);
|
2015-11-20 22:14:35 +00:00
|
|
|
}
|
2016-05-20 22:54:20 +00:00
|
|
|
};
|