Commit a935229f authored by Marco Mariani's avatar Marco Mariani

fixed grammar for !=, don't throw exception in case of malformed query string, added tests

parent 1d22cda3
...@@ -344,9 +344,9 @@ Below you can find schemas for constructing queries. ...@@ -344,9 +344,9 @@ Below you can find schemas for constructing queries.
OR -> /OR[ ]/ OR -> /OR[ ]/
AND -> /AND[ ]/ AND -> /AND[ ]/
NOT -> /NOT[ ]/ NOT -> /NOT[ ]/
COLUMN -> /[^><= :\(\)"][^ :\(\)"]*:/ COLUMN -> /[^><!= :\(\)"][^ :\(\)"]*:/
STRING -> /"(\\.|[^\\"])*"/ STRING -> /"(\\.|[^\\"])*"/
WORD -> /[^><= :\(\)"][^ :\(\)"]*/ WORD -> /[^><!= :\(\)"][^ :\(\)"]*/
OPERATOR -> /(>=?|<=?|!?=)/ OPERATOR -> /(>=?|<=?|!?=)/
LEFT_PARENTHESE -> /\(/ LEFT_PARENTHESE -> /\(/
RIGHT_PARENTHESE -> /\)/ RIGHT_PARENTHESE -> /\)/
......
...@@ -8,9 +8,9 @@ ...@@ -8,9 +8,9 @@
'AND' AND ; 'AND' AND ;
'OR' OR ; 'OR' OR ;
'NOT' NOT ; 'NOT' NOT ;
'[^><= :\(\)"][^ :\(\)"]*:' COLUMN ; '[^><!= :\(\)"][^ :\(\)"]*:' COLUMN ;
'"(\\.|[^\\"])*"' STRING ; '"(\\.|[^\\"])*"' STRING ;
'[^><= :\(\)"][^ :\(\)"]*' WORD ; '[^><!= :\(\)"][^ :\(\)"]*' WORD ;
'(>=?|<=?|!?=)' OPERATOR ; '(>=?|<=?|!?=)' OPERATOR ;
## ##
......
...@@ -280,6 +280,9 @@ SimpleQuery.prototype["<"] = function (object_value, comparison_value) { ...@@ -280,6 +280,9 @@ SimpleQuery.prototype["<"] = function (object_value, comparison_value) {
if (typeof value === 'object' && value.hasOwnProperty('content')) { if (typeof value === 'object' && value.hasOwnProperty('content')) {
value = value.content; value = value.content;
} }
if (value === undefined || comparison_value === undefined) {
return false;
}
if (value.cmp !== undefined) { if (value.cmp !== undefined) {
return value.cmp(comparison_value) < 0; return value.cmp(comparison_value) < 0;
} }
...@@ -304,6 +307,9 @@ SimpleQuery.prototype["<="] = function (object_value, comparison_value) { ...@@ -304,6 +307,9 @@ SimpleQuery.prototype["<="] = function (object_value, comparison_value) {
if (typeof value === 'object' && value.hasOwnProperty('content')) { if (typeof value === 'object' && value.hasOwnProperty('content')) {
value = value.content; value = value.content;
} }
if (value === undefined || comparison_value === undefined) {
return false;
}
if (value.cmp !== undefined) { if (value.cmp !== undefined) {
return value.cmp(comparison_value) <= 0; return value.cmp(comparison_value) <= 0;
} }
...@@ -328,6 +334,9 @@ SimpleQuery.prototype[">"] = function (object_value, comparison_value) { ...@@ -328,6 +334,9 @@ SimpleQuery.prototype[">"] = function (object_value, comparison_value) {
if (typeof value === 'object' && value.hasOwnProperty('content')) { if (typeof value === 'object' && value.hasOwnProperty('content')) {
value = value.content; value = value.content;
} }
if (value === undefined || comparison_value === undefined) {
return false;
}
if (value.cmp !== undefined) { if (value.cmp !== undefined) {
return value.cmp(comparison_value) > 0; return value.cmp(comparison_value) > 0;
} }
...@@ -352,6 +361,9 @@ SimpleQuery.prototype[">="] = function (object_value, comparison_value) { ...@@ -352,6 +361,9 @@ SimpleQuery.prototype[">="] = function (object_value, comparison_value) {
if (typeof value === 'object' && value.hasOwnProperty('content')) { if (typeof value === 'object' && value.hasOwnProperty('content')) {
value = value.content; value = value.content;
} }
if (value === undefined || comparison_value === undefined) {
return false;
}
if (value.cmp !== undefined) { if (value.cmp !== undefined) {
return value.cmp(comparison_value) >= 0; return value.cmp(comparison_value) >= 0;
} }
......
...@@ -31,7 +31,7 @@ ...@@ -31,7 +31,7 @@
cast_to: jiodate.JIODate cast_to: jiodate.JIODate
} }
} }
}; }, query_list = null;
doc_list = docList(); doc_list = docList();
complex_queries.QueryFactory.create({ complex_queries.QueryFactory.create({
...@@ -101,6 +101,42 @@ ...@@ -101,6 +101,42 @@
{'date': '2012-03-04T08:52:13.746Z', 'identifier': 'twenty twelve'} {'date': '2012-03-04T08:52:13.746Z', 'identifier': 'twenty twelve'}
], 'Match with "date >= 2011" (query tree form)'); ], 'Match with "date >= 2011" (query tree form)');
query_list = [
[
'date: < "2011" OR date: "2012-03"',
[
{'date': '2010-03-04T08:52:13.746Z', 'identifier': 'twenty ten'},
{'date': '2012-03-04T08:52:13.746Z', 'identifier': 'twenty twelve'}
]
],
[
'date: >= "2011-01" AND date: != "2012-03-04T08:52:13.746Z"',
[
{'date': '2011-03-04T08:52:13.746Z', 'identifier': 'twenty eleven'}
]
]
];
query_list.forEach(function (o) {
var qs = o[0], expected = o[1];
doc_list = docList();
complex_queries.QueryFactory.create(qs, key_schema).exec(doc_list);
deepEqual(doc_list, expected, "Match with '" + qs + "' (parsed query string)");
});
query_list = [
'date < "2011"',
'date <= "2011"',
'date > "2011"',
'date >= "2011"'
];
query_list.forEach(function (qs) {
doc_list = docList();
complex_queries.QueryFactory.create(qs, key_schema).exec(doc_list);
deepEqual(doc_list, [
], "Match with an invalid parsed string " + qs + " should return empty list but not raise errors");
});
}); });
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment