count.test.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. 'use strict';
  2. const chai = require('chai');
  3. const expect = chai.expect;
  4. const Support = require('../../support');
  5. const current = Support.sequelize;
  6. const sinon = require('sinon');
  7. const { DataTypes, Model } = require('@sequelize/core');
  8. describe(Support.getTestDialectTeaser('Model'), () => {
  9. describe('method count', () => {
  10. before(function () {
  11. this.oldFindAll = Model.findAll;
  12. this.oldAggregate = Model.aggregate;
  13. Model.findAll = sinon.stub().resolves();
  14. this.User = current.define('User', {
  15. username: DataTypes.STRING,
  16. age: DataTypes.INTEGER,
  17. });
  18. this.Project = current.define('Project', {
  19. name: DataTypes.STRING,
  20. });
  21. this.User.hasMany(this.Project);
  22. this.Project.belongsTo(this.User);
  23. });
  24. after(function () {
  25. Model.findAll = this.oldFindAll;
  26. Model.aggregate = this.oldAggregate;
  27. });
  28. beforeEach(function () {
  29. this.stub = Model.aggregate = sinon.stub().resolves();
  30. });
  31. describe('should pass the same options to model.aggregate as findAndCountAll', () => {
  32. it('with includes', async function () {
  33. const queryObject = {
  34. include: [this.Project],
  35. };
  36. await this.User.count(queryObject);
  37. await this.User.findAndCountAll(queryObject);
  38. const count = this.stub.getCall(0).args;
  39. const findAndCountAll = this.stub.getCall(1).args;
  40. expect(count).to.eql(findAndCountAll);
  41. });
  42. it('attributes should be stripped in case of findAndCountAll', async function () {
  43. const queryObject = {
  44. attributes: ['username'],
  45. };
  46. await this.User.count(queryObject);
  47. await this.User.findAndCountAll(queryObject);
  48. const count = this.stub.getCall(0).args;
  49. const findAndCountAll = this.stub.getCall(1).args;
  50. expect(count).not.to.eql(findAndCountAll);
  51. count[2].attributes = undefined;
  52. expect(count).to.eql(findAndCountAll);
  53. });
  54. });
  55. });
  56. });