From 14113e06f908db792c3c07b3ac7aaab391bc4df2 Mon Sep 17 00:00:00 2001 From: Michael Stillwell Date: Fri, 7 Jun 2013 00:57:22 +0100 Subject: [PATCH] Support select ... where foo = true i.e. filtering by the boolean value true, rather than the string 'true' --- modules/engine/lib/engine/filter.js | 19 +++-- modules/engine/test/select-obj-test.js | 107 ++++++++++++++++++++++++- 2 files changed, 120 insertions(+), 6 deletions(-) diff --git a/modules/engine/lib/engine/filter.js b/modules/engine/lib/engine/filter.js index 1caec933..3cb232ec 100644 --- a/modules/engine/lib/engine/filter.js +++ b/modules/engine/lib/engine/filter.js @@ -49,7 +49,16 @@ function _iterate(resource, statement, context, source, keep) { }); } else if(cond.operator === '=') { - expected = expected.concat(jsonfill.fill(cond.rhs.value, context)); + switch (cond.rhs.value) { + case 'true': + expected = expected.concat(true); + break; + case 'false': + expected = expected.concat(false); + break; + default: + expected = expected.concat(jsonfill.fill(cond.rhs.value, context)); + } } else if(cond.operator !== 'udf') { assert.ok(cond.operator === '=', 'Local filtering supported for = only'); @@ -96,14 +105,14 @@ function _iterate(resource, statement, context, source, keep) { for(var v in result) { if(_.isArray(expected[j])) { for(var vv in expected[j]) { - if(result[v] == expected[j][vv]) { + if(result[v] === expected[j][vv]) { match = true; break; } } } else { - if(result[v] == expected[j]) { + if(result[v] === expected[j]) { match = true; break; } @@ -113,14 +122,14 @@ function _iterate(resource, statement, context, source, keep) { else { if(_.isArray(expected[j])) { for(var vv in expected[j]) { - if(result[v] == expected[j][vv]) { + if(result[v] === expected[j][vv]) { match = true; break; } } } else { - if(result == expected[j]) { + if(result === expected[j]) { match = true; break; } diff --git a/modules/engine/test/select-obj-test.js b/modules/engine/test/select-obj-test.js index a34fece3..8758617e 100644 --- a/modules/engine/test/select-obj-test.js +++ b/modules/engine/test/select-obj-test.js @@ -102,6 +102,111 @@ module.exports = { }}); }, + 'select-filter-integer' : function(test) { + var context, q; + context = { + foo: [ + { "id": 1, "foo": true }, + { "id": 2, "foo": false } + ] + }; + q = 'select * from foo where id = 1'; + engine.exec({script: q, context: context, cb: function(err, result) { + if(err) { + test.fail('got error: ' + err.stack); + test.done(); + } + else { + test.deepEqual(result.body, [ { "id": 1, "foo": true } ]); + test.done(); + } + }}); + }, + + 'select-filter-string' : function(test) { + var context, q; + context = { + foo: [ + { "name": "name-A", "foo": true }, + { "name": "name-B", "foo": false } + ] + }; + q = 'select * from foo where name = "name-A"'; + engine.exec({script: q, context: context, cb: function(err, result) { + if(err) { + test.fail('got error: ' + err.stack); + test.done(); + } + else { + test.deepEqual(result.body, [ { "name": "name-A", "foo": true } ]); + test.done(); + } + }}); + }, + + 'select-filter-true' : function(test) { + var context, q; + context = { + foo: [ + { "id": 1, "foo": true }, + { "id": 2, "foo": false } + ] + }; + q = 'select * from foo where foo = true'; + engine.exec({script: q, context: context, cb: function(err, result) { + if(err) { + test.fail('got error: ' + err.stack); + test.done(); + } + else { + test.deepEqual(result.body, [ { "id": 1, "foo": true } ]); + test.done(); + } + }}); + }, + + 'select-filter-true-only' : function(test) { + var context, q; + context = { + foo: [ + { "id": 1, "foo": true }, + { "id": 2, "foo": 1 } // 1 is not true + ] + }; + q = 'select * from foo where foo = true'; + engine.exec({script: q, context: context, cb: function(err, result) { + if(err) { + test.fail('got error: ' + err.stack); + test.done(); + } + else { + test.deepEqual(result.body, [ { "id": 1, "foo": true } ]); + test.done(); + } + }}); + }, + + 'select-filter-false-only' : function(test) { + var context, q; + context = { + foo: [ + { "id": 1, "foo": 0 }, // 0 is not false + { "id": 2, "foo": false } + ] + }; + q = 'select * from foo where foo = false'; + engine.exec({script: q, context: context, cb: function(err, result) { + if(err) { + test.fail('got error: ' + err.stack); + test.done(); + } + else { + test.deepEqual(result.body, [ { "id": 2, "foo": false } ]); + test.done(); + } + }}); + }, + 'select-join-one-field': function(test) { var q = 'a1 = [{ \ "name": "Name-A",\ @@ -149,4 +254,4 @@ module.exports = { }) }) } -}; \ No newline at end of file +};