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

Commit b2d8f5f2 by Verdier

Fix for count with scope containing an include

1 parent 2f7c717f
...@@ -2608,6 +2608,10 @@ Model.prototype.$expandAttributes = function (options) { ...@@ -2608,6 +2608,10 @@ Model.prototype.$expandAttributes = function (options) {
// Inject current scope into options. Includes should have been conformed (conformOptions) before calling this // Inject current scope into options. Includes should have been conformed (conformOptions) before calling this
Model.$injectScope = function (scope, options) { Model.$injectScope = function (scope, options) {
// Do not inject scope more than once.
if (options._scopeInjected) return;
options._scopeInjected = true;
scope = optClone(scope); scope = optClone(scope);
var filteredScope = _.omit(scope, 'include'); // Includes need special treatment var filteredScope = _.omit(scope, 'include'); // Includes need special treatment
......
'use strict';
/* jshint -W030 */
/* jshint -W110 */
var chai = require('chai')
, expect = chai.expect
, Support = require(__dirname + '/../support')
, DataTypes = require(__dirname + '/../../../lib/data-types');
describe(Support.getTestDialectTeaser('Model'), function() {
beforeEach(function() {
this.User = this.sequelize.define('User', {
username: DataTypes.STRING
});
this.Project = this.sequelize.define('Project', {
name: DataTypes.STRING
});
this.User.hasMany(this.Project);
this.Project.belongsTo(this.User);
return this.sequelize.sync({force: true});
});
describe('count', function() {
beforeEach(function () {
var self = this;
return this.User.bulkCreate([
{username: 'boo'},
{username: 'boo2'}
]).then(function () {
return self.User.findOne();
}).then(function (user) {
return user.createProject({
name: 'project1'
});
});
});
it('should counts rows', function () {
return expect(this.User.count()).to.eventually.equal(2);
});
it('should supports include', function () {
return expect(this.User.count({
include: [{
model: this.Project,
where: {
name: 'project1'
}
}]
})).to.eventually.equal(1);
});
});
});
...@@ -5,12 +5,16 @@ ...@@ -5,12 +5,16 @@
var chai = require('chai') var chai = require('chai')
, Sequelize = require('../../../../index') , Sequelize = require('../../../../index')
, expect = chai.expect , expect = chai.expect
, Promise = require(__dirname + '/../../../../lib/promise')
, Support = require(__dirname + '/../../support'); , Support = require(__dirname + '/../../support');
describe(Support.getTestDialectTeaser('Model'), function() { describe(Support.getTestDialectTeaser('Model'), function() {
describe('scope', function () { describe('scope', function () {
describe('count', function () { describe('count', function () {
beforeEach(function () { beforeEach(function () {
this.Child = this.sequelize.define('Child', {
priority: Sequelize.INTEGER
});
this.ScopeMe = this.sequelize.define('ScopeMe', { this.ScopeMe = this.sequelize.define('ScopeMe', {
username: Sequelize.STRING, username: Sequelize.STRING,
email: Sequelize.STRING, email: Sequelize.STRING,
...@@ -23,7 +27,7 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -23,7 +27,7 @@ describe(Support.getTestDialectTeaser('Model'), function() {
gte: 5 gte: 5
} }
}, },
attributes: ['username', 'email', 'access_level'] attributes: ['id', 'username', 'email', 'access_level']
}, },
scopes: { scopes: {
lowAccess: { lowAccess: {
...@@ -35,11 +39,21 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -35,11 +39,21 @@ describe(Support.getTestDialectTeaser('Model'), function() {
}, },
withOrder: { withOrder: {
order: 'username' order: 'username'
},
withInclude: {
include: [{
model: this.Child,
where: {
priority: 1
}
}]
} }
} }
}); });
this.Child.belongsTo(this.ScopeMe);
this.ScopeMe.hasMany(this.Child);
return this.sequelize.sync({force: true}).then(function() { return this.sequelize.sync({force: true}).then(function () {
var records = [ var records = [
{username: 'tony', email: 'tony@sequelizejs.com', access_level: 3, other_value: 7}, {username: 'tony', email: 'tony@sequelizejs.com', access_level: 3, other_value: 7},
{username: 'tobi', email: 'tobi@fakeemail.com', access_level: 10, other_value: 11}, {username: 'tobi', email: 'tobi@fakeemail.com', access_level: 10, other_value: 11},
...@@ -47,7 +61,18 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -47,7 +61,18 @@ describe(Support.getTestDialectTeaser('Model'), function() {
{username: 'fred', email: 'fred@foobar.com', access_level: 3, other_value: 7} {username: 'fred', email: 'fred@foobar.com', access_level: 3, other_value: 7}
]; ];
return this.ScopeMe.bulkCreate(records); return this.ScopeMe.bulkCreate(records);
}.bind(this)); }.bind(this)).then(function () {
return this.ScopeMe.findAll();
}.bind(this)).then(function (records) {
return Promise.all([
records[0].createChild({
priority: 1
}),
records[1].createChild({
priority: 2
})
]);
});
}); });
it('should apply defaultScope', function () { it('should apply defaultScope', function () {
...@@ -73,6 +98,10 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -73,6 +98,10 @@ describe(Support.getTestDialectTeaser('Model'), function() {
it('should ignore the order option if it is found within the scope', function () { it('should ignore the order option if it is found within the scope', function () {
return expect(this.ScopeMe.scope('withOrder').count()).to.eventually.equal(4); return expect(this.ScopeMe.scope('withOrder').count()).to.eventually.equal(4);
}); });
it('should be able to use where on include', function () {
return expect(this.ScopeMe.scope('withInclude').count()).to.eventually.equal(1);
});
}); });
}); });
}); });
...@@ -254,6 +254,7 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -254,6 +254,7 @@ describe(Support.getTestDialectTeaser('Model'), function() {
}; };
current.Model.$injectScope(scope, options); current.Model.$injectScope(scope, options);
delete options._scopeInjected;
expect(options).to.deep.equal({ expect(options).to.deep.equal({
where: { where: {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!