findAndCountAll.test.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. 'use strict';
  2. const chai = require('chai');
  3. const { DataTypes, Op } = require('@sequelize/core');
  4. const expect = chai.expect;
  5. const Support = require('../../support');
  6. describe(Support.getTestDialectTeaser('Model'), () => {
  7. describe('scope', () => {
  8. describe('findAndCountAll', () => {
  9. beforeEach(async function () {
  10. this.ScopeMe = this.sequelize.define(
  11. 'ScopeMe',
  12. {
  13. username: DataTypes.STRING,
  14. email: DataTypes.STRING,
  15. access_level: DataTypes.INTEGER,
  16. other_value: DataTypes.INTEGER,
  17. },
  18. {
  19. defaultScope: {
  20. where: {
  21. access_level: {
  22. [Op.gte]: 5,
  23. },
  24. },
  25. attributes: ['username', 'email', 'access_level'],
  26. },
  27. scopes: {
  28. lowAccess: {
  29. where: {
  30. access_level: {
  31. [Op.lte]: 5,
  32. },
  33. },
  34. },
  35. withOrder: {
  36. order: ['username'],
  37. },
  38. },
  39. },
  40. );
  41. await this.sequelize.sync({ force: true });
  42. const records = [
  43. { username: 'tony', email: 'tony@sequelizejs.com', access_level: 3, other_value: 7 },
  44. { username: 'tobi', email: 'tobi@fakeemail.com', access_level: 10, other_value: 11 },
  45. { username: 'dan', email: 'dan@sequelizejs.com', access_level: 5, other_value: 10 },
  46. { username: 'fred', email: 'fred@foobar.com', access_level: 3, other_value: 7 },
  47. ];
  48. await this.ScopeMe.bulkCreate(records);
  49. });
  50. it('should apply defaultScope', async function () {
  51. const result = await this.ScopeMe.findAndCountAll();
  52. expect(result.count).to.equal(2);
  53. expect(result.rows.length).to.equal(2);
  54. });
  55. it('should be able to override default scope', async function () {
  56. const result = await this.ScopeMe.findAndCountAll({
  57. where: { access_level: { [Op.gt]: 5 } },
  58. });
  59. expect(result.count).to.equal(1);
  60. expect(result.rows.length).to.equal(1);
  61. });
  62. it('should be able to unscope', async function () {
  63. const result = await this.ScopeMe.withoutScope().findAndCountAll({ limit: 1 });
  64. expect(result.count).to.equal(4);
  65. expect(result.rows.length).to.equal(1);
  66. });
  67. it('should be able to apply other scopes', async function () {
  68. const result = await this.ScopeMe.withScope('lowAccess').findAndCountAll();
  69. expect(result.count).to.equal(3);
  70. });
  71. it('should be able to merge scopes with where', async function () {
  72. const result = await this.ScopeMe.withScope('lowAccess').findAndCountAll({
  73. where: { username: 'dan' },
  74. });
  75. expect(result.count).to.equal(1);
  76. });
  77. it('should be able to merge multiple scopes', async function () {
  78. const result = await this.ScopeMe.withScope('defaultScope', 'lowAccess').findAndCountAll();
  79. expect(result.count).to.equal(1);
  80. });
  81. it('should ignore the order option if it is found within the scope', async function () {
  82. const result = await this.ScopeMe.withScope('withOrder').findAndCountAll();
  83. expect(result.count).to.equal(4);
  84. });
  85. });
  86. });
  87. });