1
0
Fork 0

added test cases

This commit is contained in:
Jan Steemann 2012-04-24 14:49:59 +02:00
parent cf47e8e477
commit 19d7a70d65
10 changed files with 303 additions and 39 deletions

View File

@ -127,13 +127,15 @@ TRI_associative_pointer_t* TRI_InitialiseFunctionsAql (void) {
REGISTER_FUNCTION("ISDOCUMENT", "IS_DOCUMENT", true, 1, 1);
// string concat
REGISTER_FUNCTION("CONCAT", "STRING_CONCAT", true, 2, 2);
REGISTER_FUNCTION("CONCAT", "STRING_CONCAT", true, 2, 256);
// numeric functions
// string functions
// misc functions
REGISTER_FUNCTION("MERGE", "MERGE", true, 2, 256);
REGISTER_FUNCTION("UNION", "UNION", true, 2, 256);
REGISTER_FUNCTION("LENGTH", "LENGTH", true, 1, 1);
if (!result) {

View File

@ -1222,14 +1222,20 @@ char* TRI_GenerateCodeAql (const void* const data) {
if (!generator->_error) {
char* funcName = generator->_funcName;
TRI_AppendStringStringBuffer(&generator->_buffer, "return ");
TRI_AppendStringStringBuffer(&generator->_buffer, "try {\n");
TRI_AppendStringStringBuffer(&generator->_buffer, " return ");
TRI_AppendStringStringBuffer(&generator->_buffer, funcName);
TRI_AppendStringStringBuffer(&generator->_buffer, "( { } );\n");
TRI_AppendStringStringBuffer(&generator->_buffer, "}\n");
TRI_AppendStringStringBuffer(&generator->_buffer, "catch (e) {\n");
TRI_AppendStringStringBuffer(&generator->_buffer, "print(e);\n");
TRI_AppendStringStringBuffer(&generator->_buffer, "}\n");
TRI_AppendStringStringBuffer(&generator->_buffer, "})();\n");
code = TRI_DuplicateString(generator->_buffer._buffer);
LOG_TRACE("generated code:\n%s\n",code);
//printf("generated code:\n%s\n",code);
}
TRI_FreeCodegenAql(generator);

View File

@ -1010,6 +1010,7 @@ UNITTESTS_SERVER = $(addprefix --unit-tests ,$(SHELL_SERVER))
################################################################################
################################################################################
SHELL_SERVER_AHUACATL = @srcdir@/js/server/tests/ahuacatl-operators.js \
@srcdir@/js/server/tests/ahuacatl-functions.js \
@srcdir@/js/server/tests/ahuacatl-queries-collection.js \
@srcdir@/js/server/tests/ahuacatl-queries-noncollection.js

View File

@ -154,6 +154,7 @@ unittests-shell-server:
################################################################################
SHELL_SERVER_AHUACATL = @srcdir@/js/server/tests/ahuacatl-operators.js \
@srcdir@/js/server/tests/ahuacatl-functions.js \
@srcdir@/js/server/tests/ahuacatl-queries-collection.js \
@srcdir@/js/server/tests/ahuacatl-queries-noncollection.js

View File

@ -79,7 +79,7 @@ function AHUACATL_CLONE (obj) {
////////////////////////////////////////////////////////////////////////////////
function AHUACATL_FCALL(name, parameters) {
return name(parameters);
return name.apply(null, parameters);
}
////////////////////////////////////////////////////////////////////////////////
@ -941,13 +941,24 @@ function AHUACATL_ARITHMETIC_MODULUS (lhs, rhs) {
/// both operands must be strings or this function will fail
////////////////////////////////////////////////////////////////////////////////
function AHUACATL_STRING_CONCAT (lhs, rhs) {
if (AHUACATL_TYPEWEIGHT(lhs) !== AHUACATL_TYPEWEIGHT_STRING ||
AHUACATL_TYPEWEIGHT(rhs) !== AHUACATL_TYPEWEIGHT_STRING) {
throw "expecting strings for string concatenation";
function AHUACATL_STRING_CONCAT () {
var result = '';
for (var i in arguments) {
var element = arguments[i];
if (AHUACATL_TYPEWEIGHT(element) === AHUACATL_TYPEWEIGHT_NULL) {
continue;
}
if (AHUACATL_TYPEWEIGHT(element) !== AHUACATL_TYPEWEIGHT_STRING) {
throw "expecting strings for string concatenation";
}
result += element;
}
return (lhs + rhs);
return result;
}
////////////////////////////////////////////////////////////////////////////////
@ -1205,14 +1216,41 @@ function AHUACATL_LIMIT (value, offset, count) {
/// @brief get the length of a list
////////////////////////////////////////////////////////////////////////////////
function AHUACATL_LENGTH (args) {
var value = args[0];
function AHUACATL_LENGTH () {
var value = arguments[0];
AHUACATL_LIST(value);
return value.length;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief merge all arguments
////////////////////////////////////////////////////////////////////////////////
function AHUACATL_MERGE () {
var result = { };
for (var i in arguments) {
var element = arguments[i];
if (AHUACATL_TYPEWEIGHT(element) !== AHUACATL_TYPEWEIGHT_DOCUMENT) {
print(element);
throw "expecting documents for merge";
}
for (var k in element) {
if (!element.hasOwnProperty(k)) {
continue;
}
result[k] = element[k];
}
}
return result;
}
////////////////////////////////////////////////////////////////////////////////
/// @}
////////////////////////////////////////////////////////////////////////////////

View File

@ -80,7 +80,7 @@ static string JS_server_ahuacatl =
"////////////////////////////////////////////////////////////////////////////////\n"
"\n"
"function AHUACATL_FCALL(name, parameters) {\n"
" return name(parameters);\n"
" return name.apply(null, parameters);\n"
"}\n"
"\n"
"////////////////////////////////////////////////////////////////////////////////\n"
@ -942,13 +942,24 @@ static string JS_server_ahuacatl =
"/// both operands must be strings or this function will fail\n"
"////////////////////////////////////////////////////////////////////////////////\n"
"\n"
"function AHUACATL_STRING_CONCAT (lhs, rhs) {\n"
" if (AHUACATL_TYPEWEIGHT(lhs) !== AHUACATL_TYPEWEIGHT_STRING ||\n"
" AHUACATL_TYPEWEIGHT(rhs) !== AHUACATL_TYPEWEIGHT_STRING) {\n"
" throw \"expecting strings for string concatenation\";\n"
"function AHUACATL_STRING_CONCAT () {\n"
" var result = '';\n"
"\n"
" for (var i in arguments) {\n"
" var element = arguments[i];\n"
"\n"
" if (AHUACATL_TYPEWEIGHT(element) === AHUACATL_TYPEWEIGHT_NULL) {\n"
" continue;\n"
" }\n"
" \n"
" if (AHUACATL_TYPEWEIGHT(element) !== AHUACATL_TYPEWEIGHT_STRING) {\n"
" throw \"expecting strings for string concatenation\";\n"
" }\n"
"\n"
" result += element;\n"
" }\n"
"\n"
" return (lhs + rhs);\n"
" return result; \n"
"}\n"
"\n"
"////////////////////////////////////////////////////////////////////////////////\n"
@ -1206,8 +1217,8 @@ static string JS_server_ahuacatl =
"/// @brief get the length of a list\n"
"////////////////////////////////////////////////////////////////////////////////\n"
"\n"
"function AHUACATL_LENGTH (args) {\n"
" var value = args[0];\n"
"function AHUACATL_LENGTH () {\n"
" var value = arguments[0];\n"
"\n"
" AHUACATL_LIST(value);\n"
"\n"
@ -1215,6 +1226,33 @@ static string JS_server_ahuacatl =
"}\n"
"\n"
"////////////////////////////////////////////////////////////////////////////////\n"
"/// @brief merge all arguments\n"
"////////////////////////////////////////////////////////////////////////////////\n"
"\n"
"function AHUACATL_MERGE () {\n"
" var result = { };\n"
"\n"
" for (var i in arguments) {\n"
" var element = arguments[i];\n"
"\n"
" if (AHUACATL_TYPEWEIGHT(element) !== AHUACATL_TYPEWEIGHT_DOCUMENT) {\n"
" print(element);\n"
" throw \"expecting documents for merge\";\n"
" }\n"
"\n"
" for (var k in element) {\n"
" if (!element.hasOwnProperty(k)) {\n"
" continue;\n"
" }\n"
"\n"
" result[k] = element[k];\n"
" }\n"
" }\n"
"\n"
" return result; \n"
"}\n"
"\n"
"////////////////////////////////////////////////////////////////////////////////\n"
"/// @}\n"
"////////////////////////////////////////////////////////////////////////////////\n"
"\n"

View File

@ -0,0 +1,178 @@
////////////////////////////////////////////////////////////////////////////////
/// @brief tests for query language, functions
///
/// @file
///
/// DISCLAIMER
///
/// Copyright 2010-2012 triagens 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 triAGENS GmbH, Cologne, Germany
///
/// @author Jan Steemann
/// @author Copyright 2012, triAGENS GmbH, Cologne, Germany
////////////////////////////////////////////////////////////////////////////////
var jsunity = require("jsunity");
////////////////////////////////////////////////////////////////////////////////
/// @brief test suite
////////////////////////////////////////////////////////////////////////////////
function ahuacatlFunctionsTestSuite () {
////////////////////////////////////////////////////////////////////////////////
/// @brief execute a given query
////////////////////////////////////////////////////////////////////////////////
function executeQuery (query) {
var cursor = AHUACATL_RUN(query, undefined);
if (cursor instanceof AvocadoQueryError) {
print(query, cursor.message);
}
assertFalse(cursor instanceof AvocadoQueryError);
return cursor;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief execute a given query and return the results as an array
////////////////////////////////////////////////////////////////////////////////
function getQueryResults (query, isFlat) {
var result = executeQuery(query);
var results = [ ];
for (var i in result) {
if (!result.hasOwnProperty(i)) {
continue;
}
var row = result[i];
if (isFlat) {
results.push(row);
}
else {
var keys = [ ];
for (var k in row) {
if (row.hasOwnProperty(k) && k != '_id' && k != '_rev') {
keys.push(k);
}
}
keys.sort();
var resultRow = { };
for (var k in keys) {
if (keys.hasOwnProperty(k)) {
resultRow[keys[k]] = row[keys[k]];
}
}
results.push(resultRow);
}
}
return results;
}
return {
////////////////////////////////////////////////////////////////////////////////
/// @brief set up
////////////////////////////////////////////////////////////////////////////////
setUp : function () {
},
////////////////////////////////////////////////////////////////////////////////
/// @brief tear down
////////////////////////////////////////////////////////////////////////////////
tearDown : function () {
},
////////////////////////////////////////////////////////////////////////////////
/// @brief test length function
////////////////////////////////////////////////////////////////////////////////
testLength : function () {
var expected = [ 4, 4, 4 ];
var actual = getQueryResults("FOR year IN [ 2010, 2011, 2012 ] LET quarters = ((FOR q IN [ 1, 2, 3, 4 ] return q)) return LENGTH(quarters)", true);
assertEqual(expected, actual);
},
////////////////////////////////////////////////////////////////////////////////
/// @brief test concat function
////////////////////////////////////////////////////////////////////////////////
testConcat : function () {
var expected = [ "theQuickBrownFoxJumps" ];
var actual = getQueryResults("FOR r IN [ 1 ] return CONCAT('the', 'Quick', null, 'Brown', null, 'Fox', 'Jumps')", true);
assertEqual(expected, actual);
},
////////////////////////////////////////////////////////////////////////////////
/// @brief test merge function
////////////////////////////////////////////////////////////////////////////////
testMerge1 : function () {
var expected = [ { "quarter" : 1, "year" : 2010 }, { "quarter" : 2, "year" : 2010 }, { "quarter" : 3, "year" : 2010 }, { "quarter" : 4, "year" : 2010 }, { "quarter" : 1, "year" : 2011 }, { "quarter" : 2, "year" : 2011 }, { "quarter" : 3, "year" : 2011 }, { "quarter" : 4, "year" : 2011 }, { "quarter" : 1, "year" : 2012 }, { "quarter" : 2, "year" : 2012 }, { "quarter" : 3, "year" : 2012 }, { "quarter" : 4, "year" : 2012 } ];
var actual = getQueryResults("FOR year IN [ 2010, 2011, 2012 ] FOR quarter IN [ 1, 2, 3, 4 ] return MERGE({ \"year\" : year }, { \"quarter\" : quarter })", false);
assertEqual(expected, actual);
},
////////////////////////////////////////////////////////////////////////////////
/// @brief test merge function
////////////////////////////////////////////////////////////////////////////////
testMerge2 : function () {
var expected = [ { "age" : 15, "isAbove18" : false, "name" : "John" }, { "age" : 19, "isAbove18" : true, "name" : "Corey" } ];
var actual = getQueryResults("FOR u IN [ { \"name\" : \"John\", \"age\" : 15 }, { \"name\" : \"Corey\", \"age\" : 19 } ] return MERGE(u, { \"isAbove18\" : u.age >= 18 })", false);
assertEqual(expected, actual);
},
////////////////////////////////////////////////////////////////////////////////
/// @brief test merge function
////////////////////////////////////////////////////////////////////////////////
testMerge3 : function () {
var expected = [ { "age" : 15, "id" : 9, "name" : "John" }, { "age" : 19, "id" : 9, "name" : "Corey" } ];
var actual = getQueryResults("FOR u IN [ { \"id\" : 100, \"name\" : \"John\", \"age\" : 15 }, { \"id\" : 101, \"name\" : \"Corey\", \"age\" : 19 } ] return MERGE(u, { \"id\" : 9 })", false);
assertEqual(expected, actual);
},
////////////////////////////////////////////////////////////////////////////////
/// @brief test merge function
////////////////////////////////////////////////////////////////////////////////
testMerge4 : function () {
var expected = [ { "age" : 15, "id" : 33, "name" : "foo" }, { "age" : 19, "id" : 33, "name" : "foo" } ];
var actual = getQueryResults("FOR u IN [ { \"id\" : 100, \"name\" : \"John\", \"age\" : 15 }, { \"id\" : 101, \"name\" : \"Corey\", \"age\" : 19 } ] return MERGE(u, { \"id\" : 9 }, { \"name\" : \"foo\", \"id\" : 33 })", false);
assertEqual(expected, actual);
},
};
}
////////////////////////////////////////////////////////////////////////////////
/// @brief executes the test suite
////////////////////////////////////////////////////////////////////////////////
jsunity.run(ahuacatlFunctionsTestSuite);
return jsunity.done();
// Local Variables:
// mode: outline-minor
// outline-regexp: "^\\(/// @brief\\|/// @addtogroup\\|// --SECTION--\\|/// @page\\|/// @}\\)"
// End:

View File

@ -3913,39 +3913,26 @@ function ahuacatlOperatorsTestSuite () {
////////////////////////////////////////////////////////////////////////////////
testStringConcatUndefined : function () {
assertException(function() { AHUACATL_STRING_CONCAT(undefined, undefined); });
assertException(function() { AHUACATL_STRING_CONCAT(undefined, null); });
assertException(function() { AHUACATL_STRING_CONCAT(undefined, false); });
assertException(function() { AHUACATL_STRING_CONCAT(undefined, true); });
assertException(function() { AHUACATL_STRING_CONCAT(undefined, 0); });
assertException(function() { AHUACATL_STRING_CONCAT(undefined, 1); });
assertException(function() { AHUACATL_STRING_CONCAT(undefined, 2); });
assertException(function() { AHUACATL_STRING_CONCAT(undefined, -1); });
assertException(function() { AHUACATL_STRING_CONCAT(undefined, ''); });
assertException(function() { AHUACATL_STRING_CONCAT(undefined, '0'); });
assertException(function() { AHUACATL_STRING_CONCAT(undefined, ' '); });
assertException(function() { AHUACATL_STRING_CONCAT(undefined, 'a'); });
assertException(function() { AHUACATL_STRING_CONCAT(undefined, [ ]); });
assertException(function() { AHUACATL_STRING_CONCAT(undefined, [ 1 ]); });
assertException(function() { AHUACATL_STRING_CONCAT(undefined, { }); });
assertException(function() { AHUACATL_STRING_CONCAT(undefined, { 'a' : 0 }); });
assertException(function() { AHUACATL_STRING_CONCAT(undefined, NaN); });
assertException(function() { AHUACATL_STRING_CONCAT(null, undefined); });
assertException(function() { AHUACATL_STRING_CONCAT(false, undefined); });
assertException(function() { AHUACATL_STRING_CONCAT(true, undefined); });
assertException(function() { AHUACATL_STRING_CONCAT(0, undefined); });
assertException(function() { AHUACATL_STRING_CONCAT(1, undefined); });
assertException(function() { AHUACATL_STRING_CONCAT(2, undefined); });
assertException(function() { AHUACATL_STRING_CONCAT(-1, undefined); });
assertException(function() { AHUACATL_STRING_CONCAT('', undefined); });
assertException(function() { AHUACATL_STRING_CONCAT('0', undefined); });
assertException(function() { AHUACATL_STRING_CONCAT(' ', undefined); });
assertException(function() { AHUACATL_STRING_CONCAT('a', undefined); });
assertException(function() { AHUACATL_STRING_CONCAT([ ], undefined); });
assertException(function() { AHUACATL_STRING_CONCAT([ 1 ], undefined); });
assertException(function() { AHUACATL_STRING_CONCAT({ }, undefined); });
assertException(function() { AHUACATL_STRING_CONCAT({ 'a' : 0 }, undefined); });
assertException(function() { AHUACATL_STRING_CONCAT(NaN, undefined); });
assertException(function() { AHUACATL_STRING_CONCAT(1, NaN); });
assertException(function() { AHUACATL_STRING_CONCAT(1, null); });
assertException(function() { AHUACATL_STRING_CONCAT(1, false); });
@ -3977,12 +3964,10 @@ function ahuacatlOperatorsTestSuite () {
assertException(function() { AHUACATL_STRING_CONCAT(-1, 0); });
assertException(function() { AHUACATL_STRING_CONCAT(-100, 0); });
assertException(function() { AHUACATL_STRING_CONCAT(0, 0); });
assertException(function() { AHUACATL_STRING_CONCAT('', null); });
assertException(function() { AHUACATL_STRING_CONCAT('', false); });
assertException(function() { AHUACATL_STRING_CONCAT('', true); });
assertException(function() { AHUACATL_STRING_CONCAT('', [ ]); });
assertException(function() { AHUACATL_STRING_CONCAT('', { }); });
assertException(function() { AHUACATL_STRING_CONCAT('a', null); });
assertException(function() { AHUACATL_STRING_CONCAT('a', false); });
assertException(function() { AHUACATL_STRING_CONCAT('a', true); });
assertException(function() { AHUACATL_STRING_CONCAT('a', [ ]); });
@ -3994,6 +3979,23 @@ function ahuacatlOperatorsTestSuite () {
////////////////////////////////////////////////////////////////////////////////
testStringConcatValue : function () {
assertEqual('', AHUACATL_STRING_CONCAT());
assertEqual('', AHUACATL_STRING_CONCAT(''));
assertEqual('a', AHUACATL_STRING_CONCAT('a'));
assertEqual('a', AHUACATL_STRING_CONCAT('a', null));
assertEqual('', AHUACATL_STRING_CONCAT('', null));
assertEqual('', AHUACATL_STRING_CONCAT(undefined, ''));
assertEqual('0', AHUACATL_STRING_CONCAT(undefined, '0'));
assertEqual(' ', AHUACATL_STRING_CONCAT(undefined, ' '));
assertEqual('a', AHUACATL_STRING_CONCAT(undefined, 'a'));
assertEqual('', AHUACATL_STRING_CONCAT('', undefined));
assertEqual('0', AHUACATL_STRING_CONCAT('0', undefined));
assertEqual(' ', AHUACATL_STRING_CONCAT(' ', undefined));
assertEqual('a', AHUACATL_STRING_CONCAT('a', undefined));
assertEqual('', AHUACATL_STRING_CONCAT(undefined, NaN));
assertEqual('', AHUACATL_STRING_CONCAT(null, undefined));
assertEqual('', AHUACATL_STRING_CONCAT(NaN, undefined));
assertEqual('', AHUACATL_STRING_CONCAT('', ''));
assertEqual(' ', AHUACATL_STRING_CONCAT(' ', ''));
assertEqual(' ', AHUACATL_STRING_CONCAT('', ' '));
@ -4013,6 +4015,9 @@ function ahuacatlOperatorsTestSuite () {
assertEqual('0.00', AHUACATL_STRING_CONCAT('0.00', ''));
assertEqual('abc', AHUACATL_STRING_CONCAT('a', AHUACATL_STRING_CONCAT('b', 'c')));
assertEqual('', AHUACATL_STRING_CONCAT('', AHUACATL_STRING_CONCAT('', '')));
assertEqual('fux', AHUACATL_STRING_CONCAT('f', 'u', 'x'));
assertEqual('fux', AHUACATL_STRING_CONCAT('f', 'u', null, 'x'));
assertEqual('fux', AHUACATL_STRING_CONCAT(null, 'f', null, 'u', null, 'x', null));
}
};
}

View File

@ -507,18 +507,16 @@ function ahuacatlQueryCollectionTestSuite () {
testCollectSimple : function () {
var expected = [ { "gender" : "f", "numUsers" : 10 }, { "gender" : "m", "numUsers" : 10 } ];
var actual = getQueryResults("FOR u in " + users.name() + " COLLECT gender = u.gender INTO g SORT gender ASC RETURN { \"gender\" : gender, \"numUsers\" : LENGTH(g) }", false);
assertEqual(expected, actual);
},
////////////////////////////////////////////////////////////////////////////////
/// @brief test collect with filter
////////////////////////////////////////////////////////////////////////////////
testCollectFiltered : function () {
var expected = [ { "gender" : "m", "numUsers" : 8 }, { "gender" : "f", "numUsers" : 8 } ];
actual = getQueryResults("FOR u in " + users.name() + " FILTER u.active == true COLLECT gender = u.gender INTO g SORT gender DESC RETURN { \"gender\" : gender, \"numUsers\" : LENGTH(g) }", false);
assertEqual(expected, actual);
},
@ -529,7 +527,6 @@ function ahuacatlQueryCollectionTestSuite () {
testCollectMultipleCriteria : function () {
var expected = [ { "active" : false, "gender" : "m", "numUsers" : 2 }, { "active" : true, "gender" : "m", "numUsers" : 8 }, { "active" : false, "gender" : "f", "numUsers" : 2 }, { "active" : true, "gender" : "f", "numUsers" : 8 } ];
actual = getQueryResults("FOR u in " + users.name() + " COLLECT gender = u.gender, active = u.active INTO g SORT gender DESC, active ASC RETURN { \"gender\" : gender, \"active\" : active, \"numUsers\" : LENGTH(g) }", false);
assertEqual(expected, actual);
},

View File

@ -32,8 +32,6 @@ var jsunity = require("jsunity");
////////////////////////////////////////////////////////////////////////////////
function ahuacatlQueryNonCollectionTestSuite () {
var users = null;
var relations = null;
////////////////////////////////////////////////////////////////////////////////
/// @brief execute a given query