group.test.js 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. 'use strict';
  2. const chai = require('chai');
  3. const expect = chai.expect;
  4. const Support = require('../../support');
  5. const { DataTypes, Sequelize } = require('@sequelize/core');
  6. const current = Support.sequelize;
  7. describe(Support.getTestDialectTeaser('Model'), () => {
  8. describe('findAll', () => {
  9. describe('group', () => {
  10. it('should correctly group with attributes, #3009', async () => {
  11. const Post = current.define('Post', {
  12. id: { type: DataTypes.INTEGER, autoIncrement: true, primaryKey: true },
  13. name: { type: DataTypes.STRING, allowNull: false },
  14. });
  15. const Comment = current.define('Comment', {
  16. id: { type: DataTypes.INTEGER, autoIncrement: true, primaryKey: true },
  17. text: { type: DataTypes.STRING, allowNull: false },
  18. });
  19. Post.hasMany(Comment);
  20. await current.sync({ force: true });
  21. // Create an enviroment
  22. await Post.bulkCreate([{ name: 'post-1' }, { name: 'post-2' }]);
  23. await Comment.bulkCreate([
  24. { text: 'Market', postId: 1 },
  25. { text: 'Text', postId: 2 },
  26. { text: 'Abc', postId: 2 },
  27. { text: 'Semaphor', postId: 1 },
  28. { text: 'Text', postId: 1 },
  29. ]);
  30. const posts = await Post.findAll({
  31. attributes: [[Sequelize.fn('COUNT', Sequelize.col('comments.id')), 'comment_count']],
  32. include: [{ model: Comment, attributes: [] }],
  33. group: ['Post.id'],
  34. order: [['id']],
  35. });
  36. expect(Number.parseInt(posts[0].get('comment_count'), 10)).to.equal(3);
  37. expect(Number.parseInt(posts[1].get('comment_count'), 10)).to.equal(2);
  38. });
  39. it('should not add primary key when grouping using a belongsTo association', async () => {
  40. const Post = current.define('Post', {
  41. id: { type: DataTypes.INTEGER, autoIncrement: true, primaryKey: true },
  42. name: { type: DataTypes.STRING, allowNull: false },
  43. });
  44. const Comment = current.define('Comment', {
  45. id: { type: DataTypes.INTEGER, autoIncrement: true, primaryKey: true },
  46. text: { type: DataTypes.STRING, allowNull: false },
  47. });
  48. Post.hasMany(Comment);
  49. Comment.belongsTo(Post);
  50. await current.sync({ force: true });
  51. await Post.bulkCreate([{ name: 'post-1' }, { name: 'post-2' }]);
  52. await Comment.bulkCreate([
  53. { text: 'Market', postId: 1 },
  54. { text: 'Text', postId: 2 },
  55. { text: 'Abc', postId: 2 },
  56. { text: 'Semaphor', postId: 1 },
  57. { text: 'Text', postId: 1 },
  58. ]);
  59. const posts = await Comment.findAll({
  60. attributes: [
  61. 'postId',
  62. [Sequelize.fn('COUNT', Sequelize.col('Comment.id')), 'comment_count'],
  63. ],
  64. include: [{ model: Post, attributes: [] }],
  65. group: ['postId'],
  66. order: [['postId']],
  67. });
  68. expect(posts[0].get().hasOwnProperty('id')).to.equal(false);
  69. expect(posts[1].get().hasOwnProperty('id')).to.equal(false);
  70. expect(Number.parseInt(posts[0].get('comment_count'), 10)).to.equal(3);
  71. expect(Number.parseInt(posts[1].get('comment_count'), 10)).to.equal(2);
  72. });
  73. });
  74. });
  75. });