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

Commit 71e14613 by Michael Schonfeld

add tests

1 parent fe8a1591
...@@ -4,8 +4,8 @@ var Sequelize = require(__dirname + "/../../index") ...@@ -4,8 +4,8 @@ var Sequelize = require(__dirname + "/../../index")
var Person = sequelize.define('Person', var Person = sequelize.define('Person',
{ name: Sequelize.STRING, { name: Sequelize.STRING,
age : Sequelize.INTEGER age : Sequelize.INTEGER,
gender: Sequelize.ENUM('male', 'female')
}) })
, chainer = new Sequelize.Utils.QueryChainer , chainer = new Sequelize.Utils.QueryChainer
...@@ -14,14 +14,17 @@ sequelize.sync({force: true}).on('success', function() { ...@@ -14,14 +14,17 @@ sequelize.sync({force: true}).on('success', function() {
queries = [] queries = []
for(var i = 0; i < count; i++) for(var i = 0; i < count; i++)
chainer.add(Person.create({name: 'someone' + (i % 3), age : i+5})) chainer.add(Person.create({name: 'someone' + (i % 3), age : i+5, gender: (i % 2 == 0) ? 'male' : 'female'}))
console.log("Begin to save " + count + " items!") console.log("Begin to save " + count + " items!")
chainer.run().on('success', function() { chainer.run().on('success', function() {
console.log("finished") console.log("finished")
Person.sum('age').on('success', function(sum) { Person.sum('age').on('success', function(sum) {
console.log("Sum of all people's ages: " + sum) console.log("Sum of all peoples' ages: " + sum)
});
Person.sum('age', { where: { 'gender': 'male' } }).on('success', function(sum) {
console.log("Sum of all males' ages: " + sum)
}); });
}) })
}) })
...@@ -621,11 +621,7 @@ module.exports = (function() { ...@@ -621,11 +621,7 @@ module.exports = (function() {
} }
DAOFactory.prototype.sum = function(field, options) { DAOFactory.prototype.sum = function(field, options) {
options = Utils._.extend({ attributes: [] }, options || {}) return this.aggregate(field, 'min', options)
options.attributes.push(['sum(' + this.QueryInterface.QueryGenerator.quoteIdentifier(field) + ')', 'sum'])
options.parseFloat = true
return this.QueryInterface.rawSelect(this.getTableName(), options, 'sum')
} }
DAOFactory.prototype.build = function(values, options) { DAOFactory.prototype.build = function(values, options) {
......
...@@ -1177,6 +1177,77 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () { ...@@ -1177,6 +1177,77 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
}) })
}) })
describe('sum', function() {
beforeEach(function(done) {
var self = this
this.UserWithAge = this.sequelize.define('UserWithAge', {
age: Sequelize.INTEGER,
order: Sequelize.INTEGER,
gender: Sequelize.ENUM('male', 'female')
})
this.UserWithDec = this.sequelize.define('UserWithDec', {
value: Sequelize.DECIMAL(10, 3)
})
this.UserWithAge.sync({ force: true }).success(function() {
self.UserWithDec.sync({ force: true }).success(function() {
done()
})
})
})
it("should return the sum of the values for a field named the same as an SQL reserved keyword", function(done) {
var self = this
this.UserWithAge.bulkCreate([{age: 2, order: 3}, {age: 3, order: 5}]).success(function(){
self.UserWithAge.sum('order').success(function(sum) {
expect(sum).to.equal(8)
done()
})
})
})
it("should return the sum of a field in various records", function(done) {
var self = this
self.UserWithAge.bulkCreate([{age: 2}, {age: 3}]).success(function() {
self.UserWithAge.sum('age').success(function(sum) {
expect(sum).to.equal(5)
done()
})
})
})
it("should allow decimals in sum", function(done) {
var self = this
this.UserWithDec.bulkCreate([{value: 3.5}, {value: 5.25}]).success(function(){
self.UserWithDec.sum('value').success(function(sum){
expect(sum).to.equal(8.75)
done()
})
})
})
it('should accept a where clause', function (done) {
var options = { where: { 'gender': 'male' }}
var self = this
self.UserWithAge.bulkCreate([{age: 2, gender: 'male'}, {age: 3, gender: 'female'}]).success(function() {
self.UserWithAge.sum('age', options).success(function(sum) {
expect(sum).to.equal(2)
done()
})
})
})
it('allows sql logging', function(done) {
this.UserWithAge.sum('age').on('sql', function(sql) {
expect(sql).to.exist
expect(sql.toUpperCase().indexOf("SELECT")).to.be.above(-1)
done()
})
})
})
describe('schematic support', function() { describe('schematic support', function() {
beforeEach(function(done){ beforeEach(function(done){
var self = this; var self = this;
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!