不要怂,就是干,撸起袖子干!

Commit 9d5fcc2a by Mick Hansen

test(unit/sql): cover additional cases + postgres array cases

1 parent a7405663
...@@ -1559,13 +1559,15 @@ module.exports = (function() { ...@@ -1559,13 +1559,15 @@ module.exports = (function() {
}).join(binding) || ''; }).join(binding) || '';
}, },
whereItemQuery: function(key, value, options) { whereItemQuery: function(key, value, options) {
options = options || {};
var self = this var self = this
, binding , binding
, comparatorMap , comparatorMap
, aliasMap , aliasMap
, comparator = '='; , comparator = '='
, field = options.field || options.model && options.model.rawAttributes && options.model.rawAttributes[key] && options.model.rawAttributes[key]
options = options || {}; , fieldType = options.type || (field && field.type);
comparatorMap = { comparatorMap = {
$eq: '=', $eq: '=',
...@@ -1581,7 +1583,10 @@ module.exports = (function() { ...@@ -1581,7 +1583,10 @@ module.exports = (function() {
$iLike: 'ILIKE', $iLike: 'ILIKE',
$notILike: 'NOT ILIKE', $notILike: 'NOT ILIKE',
$between: 'BETWEEN', $between: 'BETWEEN',
$notBetween: 'NOT BETWEEN' $notBetween: 'NOT BETWEEN',
$overlap: "&&",
$contains: "@>",
$contained: "<@"
}; };
// Maintain BC // Maintain BC
...@@ -1604,7 +1609,11 @@ module.exports = (function() { ...@@ -1604,7 +1609,11 @@ module.exports = (function() {
"between": "$between", "between": "$between",
"!..": "$notBetween", "!..": "$notBetween",
"notbetween": "$notBetween", "notbetween": "$notBetween",
"nbetween": "$notBetween" "nbetween": "$notBetween",
"overlap": "$overlap",
"&&": "$overlap",
"@>": "$contains",
"<@": "$contained"
}; };
key = aliasMap[key] || key; key = aliasMap[key] || key;
...@@ -1664,9 +1673,10 @@ module.exports = (function() { ...@@ -1664,9 +1673,10 @@ module.exports = (function() {
} }
// Setup keys and comparators // Setup keys and comparators
if (options.model && options.model.rawAttributes && options.model.rawAttributes[key] && typeof options.model.rawAttributes[key].type === "string" && options.model.rawAttributes[key].type.substr(-2) === '[]') {
// Stupid horrible ARRAY checking, need a new way if (Array.isArray(value) && typeof fieldType === "string" && fieldType.substr(-2) === '[]') {
value = this.escape(value, options.model.rawAttributes[key]); // Stupid horrible PG ARRAY checking, need a new way
value = this.escape(value, field);
} else if (value && (value.$in || Array.isArray(value) || (value.$not && Array.isArray(value.$not)) || value.$notIn)) { } else if (value && (value.$in || Array.isArray(value) || (value.$not && Array.isArray(value.$not)) || value.$notIn)) {
comparator = 'IN'; comparator = 'IN';
if (value.$not || value.$notIn) comparator = 'NOT IN'; if (value.$not || value.$notIn) comparator = 'NOT IN';
...@@ -1696,7 +1706,7 @@ module.exports = (function() { ...@@ -1696,7 +1706,7 @@ module.exports = (function() {
comparator = 'IS NOT'; comparator = 'IS NOT';
} }
value = this.escape(value); value = this.escape(value, field);
} }
key = this.quoteIdentifier(key); key = this.quoteIdentifier(key);
......
...@@ -22,6 +22,7 @@ PostgresDialect.prototype.supports = _.merge(_.cloneDeep(Abstract.prototype.supp ...@@ -22,6 +22,7 @@ PostgresDialect.prototype.supports = _.merge(_.cloneDeep(Abstract.prototype.supp
'EXCEPTION': true, 'EXCEPTION': true,
'ON DUPLICATE KEY': false, 'ON DUPLICATE KEY': false,
'ORDER NULLS': true, 'ORDER NULLS': true,
'ARRAY': true,
returnValues: { returnValues: {
returning: true returning: true
}, },
......
...@@ -902,7 +902,7 @@ module.exports = (function() { ...@@ -902,7 +902,7 @@ module.exports = (function() {
} }
} else if (Utils._.isObject(value) && field && (field.type === DataTypes.JSON || field.type === DataTypes.JSONB)) { } else if (Utils._.isObject(value) && field && (field.type === DataTypes.JSON || field.type === DataTypes.JSONB)) {
value = JSON.stringify(value); value = JSON.stringify(value);
} else if (Array.isArray(value) && field.type === DataTypes.ARRAY(DataTypes.JSON)) { } else if (Array.isArray(value) && field && field.type === DataTypes.ARRAY(DataTypes.JSON)) {
return "ARRAY[" + value.map(function (v) { return "ARRAY[" + value.map(function (v) {
return SqlString.escape(JSON.stringify(v), false, this.options.timezone, this.dialect, field); return SqlString.escape(JSON.stringify(v), false, this.options.timezone, this.dialect, field);
}, this).join(",") + "]::JSON[]"; }, this).join(",") + "]::JSON[]";
......
...@@ -196,11 +196,12 @@ var Support = { ...@@ -196,11 +196,12 @@ var Support = {
expectsql: function(query, expectations) { expectsql: function(query, expectations) {
var expectation = expectations[Support.sequelize.dialect.name]; var expectation = expectations[Support.sequelize.dialect.name];
if (!expectation && Support.sequelize.dialect.name !== 'mssql') { if (!expectation) {
expectation = expectations['default'] expectation = expectations['default']
.replace(/\[/g, Support.sequelize.dialect.TICK_CHAR_LEFT) .replace(/\[/g, Support.sequelize.dialect.TICK_CHAR_LEFT)
.replace(/\]/g, Support.sequelize.dialect.TICK_CHAR_RIGHT); .replace(/\]/g, Support.sequelize.dialect.TICK_CHAR_RIGHT);
} }
expect(query).to.equal(expectation); expect(query).to.equal(expectation);
} }
}; };
......
'use strict'; 'use strict';
var Support = require(__dirname + '/../support') var Support = require(__dirname + '/../support')
, DataTypes = require(__dirname + '/../../../lib/data-types')
, util = require('util') , util = require('util')
, expectsql = Support.expectsql , expectsql = Support.expectsql
, current = Support.sequelize , current = Support.sequelize
...@@ -16,7 +17,7 @@ suite('SQL', function() { ...@@ -16,7 +17,7 @@ suite('SQL', function() {
options = undefined; options = undefined;
} }
test(util.inspect(params)+(options && ', '+util.inspect(options) || ''), function () { test(util.inspect(params, {depth: 10})+(options && ', '+util.inspect(options) || ''), function () {
return expectsql(sql.whereQuery(params, options), expectation); return expectsql(sql.whereQuery(params, options), expectation);
}); });
}; };
...@@ -36,9 +37,33 @@ suite('SQL', function() { ...@@ -36,9 +37,33 @@ suite('SQL', function() {
test("{ id: 1 }, { prefix: current.literal(sql.quoteTable.call(current.dialect.QueryGenerator, {schema: 'yolo', tableName: 'User'})) }", function () { test("{ id: 1 }, { prefix: current.literal(sql.quoteTable.call(current.dialect.QueryGenerator, {schema: 'yolo', tableName: 'User'})) }", function () {
expectsql(sql.whereQuery({id: 1}, {prefix: current.literal(sql.quoteTable.call(current.dialect.QueryGenerator, {schema: 'yolo', tableName: 'User'}))}), { expectsql(sql.whereQuery({id: 1}, {prefix: current.literal(sql.quoteTable.call(current.dialect.QueryGenerator, {schema: 'yolo', tableName: 'User'}))}), {
default: 'WHERE [yolo.User].[id] = 1' default: 'WHERE [yolo.User].[id] = 1',
postgres: 'WHERE "yolo"."User"."id" = 1',
mssql: 'WHERE [yolo].[User].[id] = 1',
}); });
}); });
testsql({
name: 'a project',
$or: [
{ id: [1,2,3] },
{ id: { $gt: 10 } }
]
}, {
default: "WHERE [name] = 'a project' AND ([id] IN (1, 2, 3) OR [id] > 10)"
});
testsql({
name: 'a project',
id: {
$or: [
[1,2,3],
{ $gt: 10 }
]
}
}, {
default: "WHERE [name] = 'a project' AND ([id] IN (1, 2, 3) OR [id] > 10)"
});
}); });
suite('whereItemQuery', function () { suite('whereItemQuery', function () {
...@@ -48,7 +73,7 @@ suite('SQL', function() { ...@@ -48,7 +73,7 @@ suite('SQL', function() {
options = undefined; options = undefined;
} }
test(key+": "+util.inspect(value), function () { test(key+": "+util.inspect(value, {depth: 10})+(options && ', '+util.inspect(options) || ''), function () {
return expectsql(sql.whereItemQuery(key, value, options), expectation); return expectsql(sql.whereItemQuery(key, value, options), expectation);
}); });
}; };
...@@ -101,6 +126,7 @@ suite('SQL', function() { ...@@ -101,6 +126,7 @@ suite('SQL', function() {
}); });
}); });
suite('$and/$or', function () {
suite('$or', function () { suite('$or', function () {
testsql('email', { testsql('email', {
$or: ['maker@mhansen.io', 'janzeh@gmail.com'] $or: ['maker@mhansen.io', 'janzeh@gmail.com']
...@@ -155,6 +181,7 @@ suite('SQL', function() { ...@@ -155,6 +181,7 @@ suite('SQL', function() {
}); });
}); });
}); });
});
suite('$gt', function () { suite('$gt', function () {
testsql('rank', { testsql('rank', {
...@@ -194,5 +221,43 @@ suite('SQL', function() { ...@@ -194,5 +221,43 @@ suite('SQL', function() {
default: "[date] NOT BETWEEN '2013-01-01' AND '2013-01-11'" default: "[date] NOT BETWEEN '2013-01-01' AND '2013-01-11'"
}); });
}); });
if (current.dialect.supports['ARRAY']) {
suite('ARRAY', function () {
testsql('muscles', {
$contains: [2, 3]
}, {
postgres: '"muscles" @> ARRAY[2,3]'
});
testsql('muscles', {
$contained: [6, 8]
}, {
postgres: '"muscles" <@ ARRAY[6,8]'
});
testsql('muscles', {
$overlap: [3, 11]
}, {
postgres: '"muscles" && ARRAY[3,11]'
});
testsql('muscles', {
$overlap: [3, 1]
}, {
postgres: '"muscles" && ARRAY[3,1]'
});
testsql('muscles', {
$contains: [2, 5]
}, {
field: {
type: DataTypes.ARRAY(DataTypes.INTEGER)
}
}, {
postgres: '"muscles" @> ARRAY[2,5]::INTEGER[]'
});
});
}
}); });
}); });
\ No newline at end of file
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!