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

Commit c84637c5 by Mick Hansen

Merge pull request #2286 from craigbeck/fix/scopes-are-broken-2228

add if to check for naked where clause to set scope
2 parents 481d5071 fcd4714a
Showing with 29 additions and 5 deletions
......@@ -567,6 +567,11 @@ module.exports = (function() {
scopeName = o;
scope = self.options.scopes[scopeName];
}
if (o.where) {
scope = o;
merge = true;
}
} else {
scopeName = o;
scope = self.options.scopes[scopeName];
......
......@@ -35,7 +35,8 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
username: Sequelize.STRING,
email: Sequelize.STRING,
access_level: Sequelize.INTEGER,
other_value: Sequelize.INTEGER
other_value: Sequelize.INTEGER,
parent_id: Sequelize.INTEGER
}, {
defaultScope: {
where: {
......@@ -110,14 +111,32 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
return this.sequelize.sync({force: true}).then(function() {
var records = [
{username: 'dan', email: 'dan@sequelizejs.com', access_level: 5, other_value: 10},
{username: 'tobi', email: 'tobi@fakeemail.com', access_level: 10, other_value: 11},
{username: 'tony', email: 'tony@sequelizejs.com', access_level: 3, other_value: 7}
{username: 'dan', email: 'dan@sequelizejs.com', access_level: 5, other_value: 10, parent_id: 1},
{username: 'tobi', email: 'tobi@fakeemail.com', access_level: 10, other_value: 11, parent_id: 2},
{username: 'tony', email: 'tony@sequelizejs.com', access_level: 3, other_value: 7, parent_id: 1},
{username: 'fred', email: 'fred@foobar.com', access_level: 3, other_value: 7, parent_id: 1}
];
return this.ScopeMe.bulkCreate(records);
}.bind(this));
});
it("should be able use where in scope", function() {
return this.ScopeMe.scope({where: { parent_id: 2 }}).findAll().then(function(users) {
expect(users).to.be.an.instanceof(Array);
expect(users.length).to.equal(1);
expect(users[0].username).to.equal('tobi');
});
});
it("should be able to combine scope and findAll where clauses", function() {
return this.ScopeMe.scope({where: { parent_id: 1 }}).findAll({ where: {access_level: 3}}).then(function(users) {
expect(users).to.be.an.instanceof(Array);
expect(users.length).to.equal(2);
expect(['tony', 'fred'].indexOf(users[0].username) !== -1).to.be.true;
expect(['tony', 'fred'].indexOf(users[1].username) !== -1).to.be.true;
});
});
it("should have no problems with escaping SQL", function() {
var self = this;
return this.ScopeMe.create({username: 'escape\'d', email: 'fake@fakemail.com'}).then(function(){
......@@ -265,7 +284,7 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
it("should be able to remove all scopes", function() {
return this.ScopeMe.scope(null).findAll().then(function(users) {
expect(users).to.be.an.instanceof(Array);
expect(users.length).to.equal(3);
expect(users.length).to.equal(4);
});
});
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!