Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support select ... where foo = true #592

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions modules/engine/lib/engine/filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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]) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change is necessary to prevent the boolean true matching 0, etc. This does, however, mean that queries that previously relied upon '' matching 0 will break, but arguably it was a bug that they matched in the first place...

The alternative is to check whether the expected value is a boolean, and only doing the stricter === check if so.

match = true;
break;
}
}
}
else {
if(result[v] == expected[j]) {
if(result[v] === expected[j]) {
match = true;
break;
}
Expand All @@ -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;
}
Expand Down
107 changes: 106 additions & 1 deletion modules/engine/test/select-obj-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",\
Expand Down Expand Up @@ -149,4 +254,4 @@ module.exports = {
})
})
}
};
};