1
0
Fork 0

Started adding tests for Array Indexes.

This commit is contained in:
Michael Hackstein 2015-10-20 14:33:44 +02:00
parent 1f8b5e54af
commit 2ecf67780b
1 changed files with 243 additions and 0 deletions

View File

@ -0,0 +1,243 @@
/*jshint globalstrict:false, strict:false, maxlen: 500 */
/*global assertEqual, assertNotEqual, assertTrue, AQL_EXPLAIN, AQL_EXECUTE */
////////////////////////////////////////////////////////////////////////////////
/// @brief tests for array indexes in AQL
///
/// @file
///
/// DISCLAIMER
///
/// Copyright 2015 ArangoDB GmbH, Cologne, Germany
///
/// 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.
///
/// Copyright holder is ArangoDB GmbH, Cologne, Germany
///
/// @author Michael Hackstein
/// @author Copyright 2015, ArangoDB GmbH, Cologne, Germany
////////////////////////////////////////////////////////////////////////////////
var jsunity = require("jsunity");
var db = require("org/arangodb").db;
////////////////////////////////////////////////////////////////////////////////
/// @brief test suite
////////////////////////////////////////////////////////////////////////////////
function arrayIndexSuite () {
const cName = "UnitTestsArray";
var col;
const buildTags = function (i) {
var tags = [];
if (i % 10 === 0) {
// Every tenth Array has the tag "tenth"
tags.push("tenth");
}
if (i % 50 === 0) {
// Every fifties Array has an explicit tag null
tags.push(null);
}
if (i % 3 === 0) {
// Every third Array has the tag duplicate multiple times
tags.push("duplicate");
tags.push("duplicate");
tags.push("duplicate");
}
// Shuffle the array randomly
return tags.sort(function() { return 0.5 - Math.random(); });
};
var checkIsOptimizedQuery = function (query, bindVars) {
var plan = AQL_EXPLAIN(query, bindVars).plan;
var nodeTypes = plan.nodes.map(function(node) {
return node.type;
});
assertEqual("SingletonNode", nodeTypes[0], query);
assertEqual(-1, nodeTypes.indexOf("EnumerateCollection"),
"found EnumerateCollection node for:" + query);
assertNotEqual(-1, nodeTypes.indexOf("IndexNode"),
"no index used for: " + query);
/*
assertEqual(-1, nodeTypes.indexOf("FilterNode"),
"found Filter node for:" + query);
*/
};
var validateResults = function (query) {
var bindVars = {};
bindVars.tag = "tenth";
checkIsOptimizedQuery(query, bindVars);
// Assert the result
var actual = AQL_EXECUTE(query, bindVars);
assertTrue(actual.stats.scannedIndex > 0);
assertEqual(actual.stats.scannedFull, 0);
assertEqual(actual.json.length, 10);
for (var i = 0; i < actual.json.length; ++i) {
assertEqual(parseInt(actual.json[i]) % 10, 0, "A tenth _key is not divisble by ten: " + actual);
}
bindVars.tag = "duplicate";
checkIsOptimizedQuery(query, bindVars);
actual = AQL_EXECUTE(query, bindVars);
assertTrue(actual.stats.scannedIndex > 0);
assertEqual(actual.stats.scannedFull, 0);
// No element is duplicated
assertEqual(actual.json.length, 34);
var last = -1;
for (i = 0; i < actual.length; ++i) {
assertEqual(parseInt(actual.json[i]) % 3, 0, "A duplicate _key is not divisble by 3: " + actual);
assertTrue(last < parseInt(actual.json[i]));
last = parseInt(actual.json[i]);
}
};
var validateCombinedResults = function (query) {
var bindVars = {};
bindVars.tag = "tenth";
bindVars.list = [5, 10, 15, 20];
checkIsOptimizedQuery(query, bindVars);
// Assert the result
var actual = AQL_EXECUTE(query, bindVars);
assertTrue(actual.stats.scannedIndex > 0);
assertEqual(actual.stats.scannedFull, 0);
assertEqual(actual.json.length, 2);
assertEqual(parseInt(actual.json[0]._key), 10);
assertEqual(parseInt(actual.json[1]._key), 20);
bindVars.tag = "duplicate";
bindVars.list = [3, 4, 5, 6, 7, 8, 9];
checkIsOptimizedQuery(query, bindVars);
actual = AQL_EXECUTE(query, bindVars);
assertTrue(actual.stats.scannedIndex > 0);
assertEqual(actual.stats.scannedFull, 0);
// No element is duplicated
assertEqual(actual.json.length, 3);
assertEqual(parseInt(actual.json[0]._key), 3);
assertEqual(parseInt(actual.json[1]._key), 6);
assertEqual(parseInt(actual.json[2]._key), 9);
};
return {
setUp : function () {
this.tearDown();
col = db._create(cName);
},
tearDown : function () {
db._drop(cName);
},
testHashPlainArray : function () {
col.ensureHashIndex("a[*]");
for (var i = 0; i < 100; ++i) {
col.save({ _key: i + "t", a: buildTags(i)});
}
const query = `FOR x IN ${cName} FILTER @tag IN x.a[*] SORT x._key RETURN x._key`;
validateResults(query);
},
testHashNestedArray : function () {
col.ensureHashIndex("a.b[*]");
for (var i = 0; i < 100; ++i) {
col.save({ _key: i + "t", a: {b: buildTags(i)}});
}
const query = `FOR x IN ${cName} FILTER @tag IN x.a.b[*] SORT x._key RETURN x._key`;
validateResults(query);
},
testHashArraySubattributes : function () {
col.ensureHashIndex("a[*].b");
for (var i = 0; i < 100; ++i) {
var tags = buildTags(i);
var obj = [];
for (var t = 0; t < tags.length; ++t) {
obj.push({b: tags[t]});
}
col.save({ _key: i + "t", a: obj});
}
const query = `FOR x IN ${cName} FILTER @tag IN x.a[*].b SORT x._key RETURN x._key`;
validateResults(query);
},
testHashArrayCombinedIndex : function () {
col.ensureHashIndex("a[*]", "b");
for (var i = 0; i < 100; ++i) {
col.save({ _key: i + "t", a: buildTags(i), b: i});
}
const query = `FOR x IN ${cName} FILTER @tag IN x.a[*] AND x.b IN @list SORT x._key RETURN x`;
validateCombinedResults(query);
},
testSkiplistPlainArray : function () {
col.ensureSkiplist("a[*]");
for (var i = 0; i < 100; ++i) {
col.save({ _key: i + "t", a: buildTags(i)});
}
const query = `FOR x IN ${cName} FILTER @tag IN x.a[*] SORT x._key RETURN x._key`;
validateResults(query);
},
testSkiplistNestedArray : function () {
col.ensureSkiplist("a.b[*]");
for (var i = 0; i < 100; ++i) {
col.save({ _key: i + "t", a: {b: buildTags(i)}});
}
const query = `FOR x IN ${cName} FILTER @tag IN x.a.b[*] SORT x._key RETURN x._key`;
validateResults(query);
},
testSkiplistArraySubattributes : function () {
col.ensureSkiplist("a[*].b");
for (var i = 0; i < 100; ++i) {
var tags = buildTags(i);
var obj = [];
for (var t = 0; t < tags.length; ++t) {
obj.push({b: tags[t]});
}
col.save({ _key: i + "t", a: obj});
}
const query = `FOR x IN ${cName} FILTER @tag IN x.a[*].b SORT x._key RETURN x._key`;
validateResults(query);
},
testSkiplistArrayCombinedIndex : function () {
col.ensureSkiplist("a[*]", "b");
for (var i = 0; i < 100; ++i) {
col.save({ _key: i + "t", a: buildTags(i), b: i});
}
const query = `FOR x IN ${cName} FILTER @tag IN x.a[*] AND x.b IN @list SORT x._key RETURN x`;
validateCombinedResults(query);
}
};
}
////////////////////////////////////////////////////////////////////////////////
/// @brief executes the test suite
////////////////////////////////////////////////////////////////////////////////
jsunity.run(arrayIndexSuite);
return jsunity.done();
// Local Variables:
// mode: outline-minor
// outline-regexp: "^\\(/// @brief\\|/// @addtogroup\\|// --SECTION--\\|/// @page\\|/// @}\\)"
// End: