group.test.js
3.44 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
'use strict';
const chai = require('chai'),
expect = chai.expect,
Support = require(__dirname + '/../../support'),
Sequelize = Support.Sequelize,
DataTypes = require(__dirname + '/../../../../lib/data-types'),
current = Support.sequelize;
describe(Support.getTestDialectTeaser('Model'), () => {
describe('findAll', () => {
describe('group', () => {
it('should correctly group with attributes, #3009', () => {
const Post = current.define('Post', {
id: { type: DataTypes.INTEGER, autoIncrement: true, primaryKey: true },
name: { type: DataTypes.STRING, allowNull: false }
});
const Comment = current.define('Comment', {
id: { type: DataTypes.INTEGER, autoIncrement: true, primaryKey: true },
text: { type: DataTypes.STRING, allowNull: false }
});
Post.hasMany(Comment);
return current.sync({ force: true }).then(() => {
// Create an enviroment
return Post.bulkCreate([
{ name: 'post-1' },
{ name: 'post-2' }
]);
}).then(() => {
return Comment.bulkCreate([
{ text: 'Market', PostId: 1},
{ text: 'Text', PostId: 2},
{ text: 'Abc', PostId: 2},
{ text: 'Semaphor', PostId: 1},
{ text: 'Text', PostId: 1}
]);
}).then(() => {
return Post.findAll({
attributes: [ [ Sequelize.fn('COUNT', Sequelize.col('Comments.id')), 'comment_count' ] ],
include: [
{ model: Comment, attributes: [] }
],
group: [ 'Post.id' ]
});
}).then(posts => {
expect(parseInt(posts[0].get('comment_count'))).to.be.equal(3);
expect(parseInt(posts[1].get('comment_count'))).to.be.equal(2);
});
});
it('should not add primary key when grouping using a belongsTo association', () => {
const Post = current.define('Post', {
id: { type: DataTypes.INTEGER, autoIncrement: true, primaryKey: true },
name: { type: DataTypes.STRING, allowNull: false }
});
const Comment = current.define('Comment', {
id: { type: DataTypes.INTEGER, autoIncrement: true, primaryKey: true },
text: { type: DataTypes.STRING, allowNull: false }
});
Post.hasMany(Comment);
Comment.belongsTo(Post);
return current.sync({ force: true }).then(() => {
return Post.bulkCreate([
{ name: 'post-1' },
{ name: 'post-2' }
]);
}).then(() => {
return Comment.bulkCreate([
{ text: 'Market', PostId: 1 },
{ text: 'Text', PostId: 2 },
{ text: 'Abc', PostId: 2 },
{ text: 'Semaphor', PostId: 1 },
{ text: 'Text', PostId: 1 }
]);
}).then(() => {
return Comment.findAll({
attributes: ['PostId', [Sequelize.fn('COUNT', Sequelize.col('Comment.id')), 'comment_count']],
include: [
{ model: Post, attributes: [] }
],
group: ['PostId']
});
}).then(posts => {
expect(posts[0].get().hasOwnProperty('id')).to.equal(false);
expect(posts[1].get().hasOwnProperty('id')).to.equal(false);
expect(parseInt(posts[0].get('comment_count'))).to.be.equal(3);
expect(parseInt(posts[1].get('comment_count'))).to.be.equal(2);
});
});
});
});
});