count.test.js
5.46 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
'use strict';
const chai = require('chai'),
expect = chai.expect,
Support = require('../support'),
DataTypes = require('../../../lib/data-types');
describe(Support.getTestDialectTeaser('Model'), () => {
describe('count', () => {
beforeEach(async 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);
await this.sequelize.sync({ force: true });
});
it('should count rows', async function() {
await this.User.bulkCreate([
{ username: 'foo' },
{ username: 'bar' }
]);
await expect(this.User.count()).to.eventually.equal(2);
});
it('should support include', async function() {
await this.User.bulkCreate([
{ username: 'foo' },
{ username: 'bar' }
]);
const user = await this.User.findOne();
await user.createProject({ name: 'project1' });
await expect(this.User.count({
include: [{
model: this.Project,
where: { name: 'project1' }
}]
})).to.eventually.equal(1);
});
it('should count groups correctly and return attributes', async function() {
await this.User.bulkCreate([
{ username: 'foo' },
{ username: 'bar' },
{
username: 'valak',
createdAt: new Date().setFullYear(2015)
}
]);
const users = await this.User.count({
attributes: ['createdAt'],
group: ['createdAt']
});
expect(users.length).to.be.eql(2);
expect(users[0].createdAt).to.exist;
expect(users[1].createdAt).to.exist;
});
it('should not return NaN', async function() {
await this.User.bulkCreate([
{ username: 'valak', age: 10 },
{ username: 'conjuring', age: 20 },
{ username: 'scary', age: 10 }
]);
const result = await this.User.count({
where: { age: 10 },
group: ['age'],
order: ['age']
});
// TODO: `parseInt` should not be needed, see #10533
expect(parseInt(result[0].count, 10)).to.be.eql(2);
const count0 = await this.User.count({
where: { username: 'fire' }
});
expect(count0).to.be.eql(0);
const count = await this.User.count({
where: { username: 'fire' },
group: 'age'
});
expect(count).to.be.eql([]);
});
it('should be able to specify column for COUNT()', async function() {
await this.User.bulkCreate([
{ username: 'ember', age: 10 },
{ username: 'angular', age: 20 },
{ username: 'mithril', age: 10 }
]);
const count0 = await this.User.count({ col: 'username' });
expect(count0).to.be.eql(3);
const count = await this.User.count({
col: 'age',
distinct: true
});
expect(count).to.be.eql(2);
});
it('should be able to specify NO column for COUNT() with DISTINCT', async function() {
await this.User.bulkCreate([
{ username: 'ember', age: 10 },
{ username: 'angular', age: 20 },
{ username: 'mithril', age: 10 }
]);
const count = await this.User.count({
distinct: true
});
expect(count).to.be.eql(3);
});
it('should be able to use where clause on included models', async function() {
const countOptions = {
col: 'username',
include: [this.Project],
where: {
'$Projects.name$': 'project1'
}
};
await this.User.bulkCreate([
{ username: 'foo' },
{ username: 'bar' }
]);
const user = await this.User.findOne();
await user.createProject({ name: 'project1' });
const count0 = await this.User.count(countOptions);
expect(count0).to.be.eql(1);
countOptions.where['$Projects.name$'] = 'project2';
const count = await this.User.count(countOptions);
expect(count).to.be.eql(0);
});
it('should be able to specify column for COUNT() with includes', async function() {
await this.User.bulkCreate([
{ username: 'ember', age: 10 },
{ username: 'angular', age: 20 },
{ username: 'mithril', age: 10 }
]);
const count0 = await this.User.count({
col: 'username',
distinct: true,
include: [this.Project]
});
expect(count0).to.be.eql(3);
const count = await this.User.count({
col: 'age',
distinct: true,
include: [this.Project]
});
expect(count).to.be.eql(2);
});
it('should work correctly with include and whichever raw option', async function() {
const Post = this.sequelize.define('Post', {});
this.User.hasMany(Post);
await Post.sync({ force: true });
const [user, post] = await Promise.all([this.User.create({}), Post.create({})]);
await user.addPost(post);
const counts = await Promise.all([
this.User.count(),
this.User.count({ raw: undefined }),
this.User.count({ raw: false }),
this.User.count({ raw: true }),
this.User.count({ include: Post }),
this.User.count({ include: Post, raw: undefined }),
this.User.count({ include: Post, raw: false }),
this.User.count({ include: Post, raw: true })
]);
expect(counts).to.deep.equal([1, 1, 1, 1, 1, 1, 1, 1]);
});
});
});