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

Commit 9e3a91ae by Mick Hansen

chore(associations/scope): fix a few failing tests

1 parent d206cde1
...@@ -15,8 +15,8 @@ module.exports = (function() { ...@@ -15,8 +15,8 @@ module.exports = (function() {
HasManyDoubleLinked.prototype.injectGetter = function(options, queryOptions) { HasManyDoubleLinked.prototype.injectGetter = function(options, queryOptions) {
var self = this var self = this
, through = self.association.through , through = self.association.through
, scopeWhere = null , scopeWhere
, throughScopeWhere = null; , throughWhere;
if (this.association.scope) { if (this.association.scope) {
scopeWhere = {}; scopeWhere = {};
...@@ -31,10 +31,12 @@ module.exports = (function() { ...@@ -31,10 +31,12 @@ module.exports = (function() {
]); ]);
if (Object(through.model) === through.model) { if (Object(through.model) === through.model) {
throughWhere = {};
throughWhere[self.association.identifier] = self.instance.get(self.association.source.primaryKeyAttribute);
if (through && through.scope) { if (through && through.scope) {
throughScopeWhere = {};
Object.keys(through.scope).forEach(function (attribute) { Object.keys(through.scope).forEach(function (attribute) {
throughScopeWhere[attribute] = through.scope[attribute]; throughWhere[attribute] = through.scope[attribute];
}.bind(this)); }.bind(this));
} }
...@@ -49,13 +51,7 @@ module.exports = (function() { ...@@ -49,13 +51,7 @@ module.exports = (function() {
identifier: self.association.foreignIdentifier identifier: self.association.foreignIdentifier
}, },
required: true, required: true,
where: new Utils.and([ where: throughWhere,
new Utils.where(
through.model.rawAttributes[self.association.identifier],
self.instance.get(self.association.source.primaryKeyAttribute)
),
throughScopeWhere
]),
_pseudo: true _pseudo: true
}); });
} }
......
...@@ -1021,10 +1021,12 @@ module.exports = (function() { ...@@ -1021,10 +1021,12 @@ module.exports = (function() {
// Add WHERE to sub or main query // Add WHERE to sub or main query
if (options.hasOwnProperty('where')) { if (options.hasOwnProperty('where')) {
options.where = this.getWhereConditions(options.where, mainTableAs || tableName, model, options); options.where = this.getWhereConditions(options.where, mainTableAs || tableName, model, options);
if (subQuery) { if (options.where) {
subQueryItems.push(' WHERE ' + options.where); if (subQuery) {
} else { subQueryItems.push(' WHERE ' + options.where);
mainQueryItems.push(' WHERE ' + options.where); } else {
mainQueryItems.push(' WHERE ' + options.where);
}
} }
} }
...@@ -1220,7 +1222,7 @@ module.exports = (function() { ...@@ -1220,7 +1222,7 @@ module.exports = (function() {
return self.getWhereConditions(arg, tableName, factory, options, prepend); return self.getWhereConditions(arg, tableName, factory, options, prepend);
}).join(connector); }).join(connector);
result = '(' + result + ')'; result = result.length && '(' + result + ')' || undefined;
} else if (smth instanceof Utils.where) { } else if (smth instanceof Utils.where) {
var value = smth.logic var value = smth.logic
, key = this.quoteTable(smth.attribute.Model.name) + '.' + this.quoteIdentifier(smth.attribute.fieldName) , key = this.quoteTable(smth.attribute.Model.name) + '.' + this.quoteIdentifier(smth.attribute.fieldName)
...@@ -1281,6 +1283,8 @@ module.exports = (function() { ...@@ -1281,6 +1283,8 @@ module.exports = (function() {
} else { } else {
result = Utils.format(smth, this.dialect); result = Utils.format(smth, this.dialect);
} }
} else if (smth === null) {
result = '1=1';
} }
return result ? result : '1=1'; return result ? result : '1=1';
......
...@@ -341,7 +341,7 @@ module.exports = (function() { ...@@ -341,7 +341,7 @@ module.exports = (function() {
var replacements = { var replacements = {
table: this.quoteIdentifiers(tableName), table: this.quoteIdentifiers(tableName),
where: this.getWhereConditions(where), where: this.getWhereConditions(where) || '1=1',
limit: !!options.limit ? ' LIMIT ' + this.escape(options.limit) : '', limit: !!options.limit ? ' LIMIT ' + this.escape(options.limit) : '',
primaryKeys: primaryKeys[tableName].length > 1 ? '(' + pks + ')' : pks, primaryKeys: primaryKeys[tableName].length > 1 ? '(' + pks + ')' : pks,
primaryKeysSelection: pks primaryKeysSelection: pks
......
...@@ -217,7 +217,6 @@ describe(Support.getTestDialectTeaser("Multiple Level Filters"), function() { ...@@ -217,7 +217,6 @@ describe(Support.getTestDialectTeaser("Multiple Level Filters"), function() {
User.find(1).success(function(user){ User.find(1).success(function(user){
Project.find(1).success(function(project){ Project.find(1).success(function(project){
user.setProjects([project]).success(function(){ user.setProjects([project]).success(function(){
User.find(2).success(function(user){ User.find(2).success(function(user){
Project.find(2).success(function(project){ Project.find(2).success(function(project){
user.setProjects([project]).success(function(){ user.setProjects([project]).success(function(){
...@@ -246,6 +245,6 @@ describe(Support.getTestDialectTeaser("Multiple Level Filters"), function() { ...@@ -246,6 +245,6 @@ describe(Support.getTestDialectTeaser("Multiple Level Filters"), function() {
}); });
}); });
}); });
}) });
}) });
}) });
...@@ -133,7 +133,7 @@ describe(Support.getTestDialectTeaser("Self"), function() { ...@@ -133,7 +133,7 @@ describe(Support.getTestDialectTeaser("Self"), function() {
}); });
}).then(function () { }).then(function () {
return this.john.getChildren().on('sql', function(sql) { return this.john.getChildren().on('sql', function(sql) {
var whereClause = sql.split('WHERE')[1]; // look only in the whereClause var whereClause = sql.split('FROM')[1]; // look only in the whereClause
expect(whereClause).to.have.string('preexisting_child'); expect(whereClause).to.have.string('preexisting_child');
expect(whereClause).to.have.string('preexisting_parent'); expect(whereClause).to.have.string('preexisting_parent');
}); });
......
...@@ -976,6 +976,8 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () { ...@@ -976,6 +976,8 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
t.rollback().success(function(){ done() }) t.rollback().success(function(){ done() })
}) })
}) })
}).on('sql', function (sql) {
console.log(sql);
}) })
}) })
}) })
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!