UNPKG

database-jones

Version:

Lightweight database mapping

123 lines (105 loc) 4.67 kB
/* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; version 2 of the License. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ "use strict"; var udebug = unified_debug.getLogger("numerictypes/QueryLiteralIndexScanTest.js"); var t0 = new harness.ConcurrentTest('test'); t0.run = function() { this.pass(); }; var q1 = {p1: 4, expected: [4, 5, 6], queryType: 2, ordered: false, predicate: function(qdt) { return qdt.tint.ge(qdt.param('p1')).and(qdt.tint.le(6)); }}; var q2 = {p1: 3, expected: [4, 5, 6], queryType: 2, ordered: false, predicate: function(qdt) { return qdt.tint.gt(qdt.param('p1')).and(qdt.tint.lt('7')); }}; var q3 = {p1: 3, expected: [7], queryType: 2, ordered: false, predicate: function(qdt) { return qdt.tint.ne(qdt.param('p1')).and(qdt.tint.eq(7.0)); }}; var q4 = {p1: 8, expected: [0, 1, 2, 8, 9], queryType: 2, ordered: false, predicate: function(qdt) { return qdt.tint.ge(qdt.param('p1')).or(qdt.tint.le('2.0')); }}; function MyInt(value) { this.value = value.toString(); } MyInt.prototype.toString = function() { return this.value; }; var q5 = {p1: 3, expected: [7, 8, 9], queryType: 2, ordered: false, predicate: function(qdt) { return qdt.tint.gt(qdt.param('p1')).and(qdt.not((qdt.tint.lt(new MyInt(7))))); }}; var q6 = {expected: [7, 8, 9], queryType: 2, ordered: false, predicate: function(qdt) { return qdt.tint.between(7, 9); }}; var queryTests = [q1, q2, q3, q4, q5, q6]; /***** Build and run queries ***/ var testQueries = new harness.ConcurrentTest("testQueries"); testQueries.run = function() { var testCase = this; var from = global.integraltypes; testCase.mappings = from; fail_openSession(testCase, function(session) { var i = 0, j = 0, completedTestCount = 0, testCount = queryTests.length; queryTests.forEach(function(queryTest) { // set up to check results queryTest.resultIds = []; ++i; queryTest.testName = 'q' + i; // each query test gets its own query domain type session.createQuery(from, function(err, q) { if (err) { testCase.appendErrorMessage('QueryLiteralIndexScanTest ' + queryTest.testName + ' returned error: ' + err); --testCount; return; } q.where(queryTest.predicate(q)); testCase.errorIfNotEqual('Wrong query type for ' + queryTest.testName, queryTest.queryType, q.jones_query_domain_type.queryType); q.execute(queryTest, function(err, results, queryTest) { testCase.errorIfNull("NoResults: "+queryTest.testName+" ", results); if(results) { // check results // get the result ids in an array for (j = 0; j < results.length; ++j) { queryTest.resultIds[j] = results[j].id; } if (queryTest.expected.length !== results.length) { testCase.errorIfNotEqual(queryTest.testName + ' wrong results: expected ' + queryTest.expected + '; actual: ' + queryTest.resultIds, queryTest.expected.length, results.length); } else { if (!queryTest.ordered) { // compare results without considering order queryTest.resultIds.sort(function(a,b){return a-b;}); } // compare the results one by one, in order for (j = 0; j < queryTest.expected.length; ++j) { udebug.log_detail('QueryLiteralIndexScanTest ' + queryTest.testName + ' expected: ' + queryTest.expected[j] + ' actual: ' + queryTest.resultIds[j]); testCase.errorIfNotEqual(queryTest.testName + ' wrong result at position ' + j, queryTest.expected[j], queryTest.resultIds[j]); } } } if (++completedTestCount === testCount) { testCase.failOnError(); } }, queryTest); }); }); }); }; module.exports.tests = [testQueries];