aggregates.test.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. 'use strict';
  2. const chai = require('chai');
  3. const expect = chai.expect;
  4. const Support = require('../support');
  5. const { DataTypes } = require('@sequelize/core');
  6. const dialect = Support.sequelize.dialect;
  7. describe('Model', () => {
  8. beforeEach(async function () {
  9. this.Payment = this.sequelize.define('Payment', {
  10. amount: DataTypes.FLOAT,
  11. mood: {
  12. type: DataTypes.ENUM(['happy', 'sad', 'neutral']),
  13. },
  14. });
  15. await this.sequelize.sync({ force: true });
  16. await this.Payment.bulkCreate([
  17. { amount: 5, mood: 'neutral' },
  18. { amount: -5, mood: 'neutral' },
  19. { amount: 10, mood: 'happy' },
  20. { amount: 90, mood: 'happy' },
  21. ]);
  22. });
  23. describe('sum', () => {
  24. it('should sum without rows', async function () {
  25. await expect(this.Payment.sum('amount', { where: { mood: 'sad' } })).to.eventually.be.null;
  26. });
  27. it('should sum when is 0', async function () {
  28. await expect(
  29. this.Payment.sum('amount', { where: { mood: 'neutral' } }),
  30. ).to.eventually.be.equal(0);
  31. });
  32. it('should sum', async function () {
  33. await expect(this.Payment.sum('amount', { where: { mood: 'happy' } })).to.eventually.be.equal(
  34. 100,
  35. );
  36. });
  37. });
  38. for (const methodName of ['min', 'max']) {
  39. describe(methodName, () => {
  40. beforeEach(async function () {
  41. this.UserWithAge = this.sequelize.define('UserWithAge', {
  42. age: DataTypes.INTEGER,
  43. order: DataTypes.INTEGER,
  44. });
  45. await this.UserWithAge.sync({ force: true });
  46. });
  47. if (Support.sequelize.dialect.supports.transactions) {
  48. it('supports transactions', async function () {
  49. const sequelize = await Support.createSingleTransactionalTestSequelizeInstance(
  50. this.sequelize,
  51. );
  52. const User = sequelize.define('User', { age: DataTypes.INTEGER });
  53. await User.sync({ force: true });
  54. const t = await sequelize.startUnmanagedTransaction();
  55. await User.bulkCreate([{ age: 2 }, { age: 5 }, { age: 3 }], { transaction: t });
  56. const val1 = await User[methodName]('age');
  57. const val2 = await User[methodName]('age', { transaction: t });
  58. expect(val1).to.be.not.ok;
  59. expect(val2).to.equal(methodName === 'min' ? 2 : 5);
  60. await t.rollback();
  61. });
  62. }
  63. it('returns the correct value', async function () {
  64. await this.UserWithAge.bulkCreate([{ age: 3 }, { age: 2 }]);
  65. expect(await this.UserWithAge[methodName]('age')).to.equal(methodName === 'min' ? 2 : 3);
  66. });
  67. it('allows sql logging', async function () {
  68. let test = false;
  69. await this.UserWithAge[methodName]('age', {
  70. logging(sql) {
  71. test = true;
  72. expect(sql).to.exist;
  73. expect(sql.toUpperCase()).to.include('SELECT');
  74. },
  75. });
  76. expect(test).to.be.true;
  77. });
  78. if (dialect.supports.dataTypes.DECIMAL) {
  79. it('should allow decimals', async function () {
  80. const UserWithDec = this.sequelize.define('UserWithDec', {
  81. value: DataTypes.DECIMAL(10, 3),
  82. });
  83. await UserWithDec.sync({ force: true });
  84. await UserWithDec.bulkCreate([{ value: 5.5 }, { value: 3.5 }]);
  85. expect(await UserWithDec[methodName]('value')).to.equal(methodName === 'min' ? 3.5 : 5.5);
  86. });
  87. }
  88. it('should work with fields named as an SQL reserved keyword', async function () {
  89. await this.UserWithAge.bulkCreate([
  90. { age: 2, order: 3 },
  91. { age: 3, order: 5 },
  92. ]);
  93. expect(await this.UserWithAge[methodName]('order')).to.equal(methodName === 'min' ? 3 : 5);
  94. });
  95. });
  96. }
  97. });