Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Get or/not working correctly, added tests

  • Loading branch information...
commit 08c44f6b49f92ba0ba4e3c544b21a61ade5889d6 1 parent 5bc48f4
mde authored
View
26 lib/adapters/sql/base.js
@@ -1,7 +1,8 @@
var Adapter
, BaseAdapter = require('../base_adapter').BaseAdapter
, datatypes = require('../../datatypes')
- , utils = require('utilities');
+ , utils = require('utilities')
+ , operation = require('../../query/operation');
Adapter = function () {
};
@@ -37,22 +38,31 @@ utils.mixin(Adapter.prototype, new (function () {
}
this._serializeConditions = function (conditions) {
+ var sql = this._serializeOperation(conditions);
+ return sql;
+ };
+
+ this._serializeOperation = function (op) {
var self = this
- , operation = conditions
, ops = []
, str = '';
- if (operation.isEmpty()) {
+ if (op.isEmpty()) {
return '';
}
else {
- operation.forEach(function (op) {
- ops.push(self._serializeComparison(op));
+ op.forEach(function (o) {
+ if (o instanceof operation.OperationBase) {
+ ops.push(self._serializeOperation(o));
+ }
+ else {
+ ops.push(self._serializeComparison(o));
+ }
});
- if (operation.type == 'Not') {
- return 'NOT(' + operation.operand() + ')';
+ if (op.type == 'not') {
+ return 'NOT (' + self._serializeOperation(op.operand()) + ')';
}
else {
- return '(' + ops.join(' ' + operation.type.toUpperCase() + ' ') + ')';
+ return '(' + ops.join(' ' + op.type.toUpperCase() + ' ') + ')';
}
}
};
View
1  lib/query/operation.js
@@ -243,6 +243,7 @@ operationTypes = {
// Export the specific constructors as well as the `create` function
utils.mixin(operation, operationTypes);
+operation.OperationBase = OperationBase;
module.exports = operation;
View
52 test/adapters/sql/postgres.js
@@ -157,7 +157,7 @@ tests = {
});
}
-, 'test all by string equality': function (next) {
+, 'test all, by string equality': function (next) {
Zooby.all({foo: 'FOO'}, {}, function (err, data) {
if (err) {
throw err;
@@ -168,7 +168,7 @@ tests = {
});
}
-, 'test all by string LIKE case-sensitive': function (next) {
+, 'test all, by string LIKE case-sensitive': function (next) {
Zooby.all({foo: {'like': 'B'}}, {}, function (err, data) {
if (err) {
throw err;
@@ -178,7 +178,7 @@ tests = {
});
}
-, 'test all by string LIKE case-insensitive bool': function (next) {
+, 'test all, by string LIKE case-insensitive bool': function (next) {
Zooby.all({foo: {'like': 'b'}}, {lowercase: true}, function (err, data) {
if (err) {
throw err;
@@ -188,7 +188,7 @@ tests = {
});
}
-, 'test all by LIKE case-insensitive array': function (next) {
+, 'test all, by LIKE case-insensitive array': function (next) {
Zooby.all({foo: {'like': 'b'}}, {lowercase: ['foo']}, function (err, data) {
if (err) {
throw err;
@@ -198,7 +198,7 @@ tests = {
});
}
-, 'test all sort string column name': function (next) {
+, 'test all, sort string column name': function (next) {
Zooby.all({}, {sort: 'zong'}, function (err, data) {
if (err) {
throw err;
@@ -207,14 +207,14 @@ tests = {
});
}
-, 'test all sort incorrect string column name': function () {
+, 'test all, sort incorrect string column name': function () {
assert.throws(function () {
Zooby.all({}, {sort: 'zongX'}, function (err, data) {
});
}, Error);
}
-, 'test all sort array column names': function (next) {
+, 'test all, sort array column names': function (next) {
Zooby.all({}, {sort: ['foo', 'zong']}, function (err, data) {
// Should be sorted BAR, BAZ, FOO
assert.equal(data[0].id, testItems[1].id);
@@ -225,7 +225,7 @@ tests = {
});
}
-, 'test all sort object literal desc': function (next) {
+, 'test all, sort object literal desc': function (next) {
Zooby.all({}, {sort: {zong: 'desc'}}, function (err, data) {
// Sort by datetime
assert.equal(data[0].id, testItems[0].id);
@@ -236,7 +236,7 @@ tests = {
});
}
-, 'test all sort object literal asc': function (next) {
+, 'test all, sort object literal asc': function (next) {
Zooby.all({}, {sort: {zong: 'asc'}}, function (err, data) {
// Sort by datetime reversed
assert.equal(data[0].id, testItems[2].id);
@@ -247,13 +247,45 @@ tests = {
});
}
-, 'test all sort incorrect sort direction': function () {
+, 'test all, sort incorrect sort direction': function () {
assert.throws(function () {
Zooby.all({}, {sort: {foo: 'asc', bar: 'descX'}}, function (err, data) {
});
}, Error);
}
+, 'test all, using or, simple equality': function (next) {
+ Zooby.all({or: [{foo: 'BAR'}, {foo: 'BAZ'}]}, {}, function (err, data) {
+ assert.equal(data.length, 2);
+ if (err) {
+ throw err;
+ }
+ next();
+ });
+ }
+
+, 'test all, using or, like comparison': function (next) {
+ Zooby.all({or: [{foo: {'like': 'b'}}, {foo: 'foo'}]}, {lowercase: ['foo']},
+ function (err, data) {
+ assert.equal(data.length, 3);
+ if (err) {
+ throw err;
+ }
+ next();
+ });
+ }
+
+, 'test all, using or, like comparison with not': function (next) {
+ Zooby.all({or: [{foo: {'like': 'b'}}, {foo: 'foo'}], not: {foo: 'baz'}},
+ {lowercase: ['foo']}, function (err, data) {
+ assert.equal(data.length, 2);
+ if (err) {
+ throw err;
+ }
+ next();
+ });
+ }
+
};
module.exports = tests;
View
14 test/query/query.js
@@ -32,7 +32,7 @@ var tests = {
, operands;
}
-, 'test not condition': function () {
+, 'test not': function () {
var conditions = {foo: 'bar', not: {bar: 'baz', baz: 'zoobie'}}
, query = new Query(Zooby, conditions, {})
, operands;
@@ -44,6 +44,18 @@ var tests = {
assert.ok(operands[0] instanceof operation.AndOperation);
}
+, 'test or': function () {
+ var conditions = {or: [{foo: 'bar'}, {bar: 'baz'}]}
+ , query = new Query(Zooby, conditions, {})
+ , operands;
+
+ operands = query.conditions.operands;
+ assert.ok(operands[0] instanceof operation.OrOperation);
+ operands = query.conditions.operands[0].operands;
+ assert.ok(operands[0] instanceof operation.AndOperation);
+ assert.ok(operands[1] instanceof operation.AndOperation);
+ }
+
};
module.exports = tests;
Please sign in to comment.
Something went wrong with that request. Please try again.