count.test.js
3.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
'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,
age: DataTypes.INTEGER
});
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 count rows', function () {
return expect(this.User.count()).to.eventually.equal(2);
});
it('should support include', function () {
return expect(this.User.count({
include: [{
model: this.Project,
where: {
name: 'project1'
}
}]
})).to.eventually.equal(1);
});
it('should return attributes', function () {
return this.User.create({
username: 'valak',
createdAt: (new Date()).setFullYear(2015)
})
.then(() =>
this.User.count({
attributes: ['createdAt'],
group: ['createdAt']
})
)
.then((users) => {
expect(users.length).to.be.eql(2);
// have attributes
expect(users[0].createdAt).to.exist;
expect(users[1].createdAt).to.exist;
});
});
it('should not return NaN', function() {
return this.sequelize.sync({ force: true })
.then(() =>
this.User.bulkCreate([
{ username: 'valak' , age: 10},
{ username: 'conjuring' , age: 20},
{ username: 'scary' , age: 10}
])
)
.then(() =>
this.User.count({
where: { age: 10 },
group: ['age'],
order: 'age'
})
)
.then((result) => {
expect(parseInt(result[0].count)).to.be.eql(2);
return this.User.count({
where: { username: 'fire' }
});
})
.then((count) => {
expect(count).to.be.eql(0);
return this.User.count({
where: { username: 'fire' },
group: 'age'
});
})
.then((count) => {
expect(count).to.be.eql([]);
});
});
it('should be able to specify column for COUNT()', function() {
return this.sequelize.sync({ force: true })
.then(() =>
this.User.bulkCreate([
{ username: 'ember' , age: 10},
{ username: 'angular' , age: 20},
{ username: 'mithril' , age: 10}
])
)
.then(() =>
this.User.count({
col: 'username'
})
)
.then((count) => {
expect(parseInt(count)).to.be.eql(3);
return this.User.count({
col: 'age',
distinct: true
});
})
.then((count) => {
expect(parseInt(count)).to.be.eql(2);
});
});
});
});