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

Commit 9e3a91ae by Mick Hansen

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

1 parent d206cde1
......@@ -15,8 +15,8 @@ module.exports = (function() {
HasManyDoubleLinked.prototype.injectGetter = function(options, queryOptions) {
var self = this
, through = self.association.through
, scopeWhere = null
, throughScopeWhere = null;
, scopeWhere
, throughWhere;
if (this.association.scope) {
scopeWhere = {};
......@@ -31,10 +31,12 @@ module.exports = (function() {
]);
if (Object(through.model) === through.model) {
throughWhere = {};
throughWhere[self.association.identifier] = self.instance.get(self.association.source.primaryKeyAttribute);
if (through && through.scope) {
throughScopeWhere = {};
Object.keys(through.scope).forEach(function (attribute) {
throughScopeWhere[attribute] = through.scope[attribute];
throughWhere[attribute] = through.scope[attribute];
}.bind(this));
}
......@@ -49,13 +51,7 @@ module.exports = (function() {
identifier: self.association.foreignIdentifier
},
required: true,
where: new Utils.and([
new Utils.where(
through.model.rawAttributes[self.association.identifier],
self.instance.get(self.association.source.primaryKeyAttribute)
),
throughScopeWhere
]),
where: throughWhere,
_pseudo: true
});
}
......
......@@ -1021,10 +1021,12 @@ module.exports = (function() {
// Add WHERE to sub or main query
if (options.hasOwnProperty('where')) {
options.where = this.getWhereConditions(options.where, mainTableAs || tableName, model, options);
if (subQuery) {
subQueryItems.push(' WHERE ' + options.where);
} else {
mainQueryItems.push(' WHERE ' + options.where);
if (options.where) {
if (subQuery) {
subQueryItems.push(' WHERE ' + options.where);
} else {
mainQueryItems.push(' WHERE ' + options.where);
}
}
}
......@@ -1220,7 +1222,7 @@ module.exports = (function() {
return self.getWhereConditions(arg, tableName, factory, options, prepend);
}).join(connector);
result = '(' + result + ')';
result = result.length && '(' + result + ')' || undefined;
} else if (smth instanceof Utils.where) {
var value = smth.logic
, key = this.quoteTable(smth.attribute.Model.name) + '.' + this.quoteIdentifier(smth.attribute.fieldName)
......@@ -1281,6 +1283,8 @@ module.exports = (function() {
} else {
result = Utils.format(smth, this.dialect);
}
} else if (smth === null) {
result = '1=1';
}
return result ? result : '1=1';
......
......@@ -341,7 +341,7 @@ module.exports = (function() {
var replacements = {
table: this.quoteIdentifiers(tableName),
where: this.getWhereConditions(where),
where: this.getWhereConditions(where) || '1=1',
limit: !!options.limit ? ' LIMIT ' + this.escape(options.limit) : '',
primaryKeys: primaryKeys[tableName].length > 1 ? '(' + pks + ')' : pks,
primaryKeysSelection: pks
......
......@@ -217,7 +217,6 @@ describe(Support.getTestDialectTeaser("Multiple Level Filters"), function() {
User.find(1).success(function(user){
Project.find(1).success(function(project){
user.setProjects([project]).success(function(){
User.find(2).success(function(user){
Project.find(2).success(function(project){
user.setProjects([project]).success(function(){
......@@ -246,6 +245,6 @@ describe(Support.getTestDialectTeaser("Multiple Level Filters"), function() {
});
});
});
})
})
})
});
});
});
......@@ -133,7 +133,7 @@ describe(Support.getTestDialectTeaser("Self"), function() {
});
}).then(function () {
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_parent');
});
......
......@@ -976,6 +976,8 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
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!