count.test.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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('count', () => {
  9. beforeEach(async function () {
  10. this.Child = this.sequelize.define('Child', {
  11. priority: DataTypes.INTEGER,
  12. });
  13. this.ScopeMe = this.sequelize.define(
  14. 'ScopeMe',
  15. {
  16. username: DataTypes.STRING,
  17. email: DataTypes.STRING,
  18. aliasValue: {
  19. field: 'alias_value',
  20. type: DataTypes.INTEGER,
  21. },
  22. access_level: DataTypes.INTEGER,
  23. other_value: DataTypes.INTEGER,
  24. },
  25. {
  26. defaultScope: {
  27. where: {
  28. access_level: {
  29. [Op.gte]: 5,
  30. },
  31. },
  32. attributes: ['id', 'username', 'email', 'access_level'],
  33. },
  34. scopes: {
  35. lowAccess: {
  36. where: {
  37. access_level: {
  38. [Op.lte]: 5,
  39. },
  40. },
  41. },
  42. withOrder: {
  43. order: ['username'],
  44. },
  45. withInclude: {
  46. include: [
  47. {
  48. model: this.Child,
  49. where: {
  50. priority: 1,
  51. },
  52. },
  53. ],
  54. },
  55. withIncludeFunction: () => {
  56. return {
  57. include: [
  58. {
  59. model: this.Child,
  60. where: {
  61. priority: 1,
  62. },
  63. },
  64. ],
  65. };
  66. },
  67. withIncludeFunctionAndStringAssociation: () => {
  68. return {
  69. include: [
  70. {
  71. association: 'children',
  72. where: {
  73. priority: 1,
  74. },
  75. },
  76. ],
  77. };
  78. },
  79. withAliasedField: {
  80. where: {
  81. aliasValue: { [Op.ne]: 1 },
  82. },
  83. },
  84. },
  85. },
  86. );
  87. this.Child.belongsTo(this.ScopeMe);
  88. this.ScopeMe.hasMany(this.Child);
  89. await this.sequelize.sync({ force: true });
  90. const records0 = [
  91. {
  92. username: 'tony',
  93. email: 'tony@sequelizejs.com',
  94. access_level: 3,
  95. other_value: 7,
  96. aliasValue: 12,
  97. },
  98. {
  99. username: 'tobi',
  100. email: 'tobi@fakeemail.com',
  101. access_level: 10,
  102. other_value: 11,
  103. aliasValue: 5,
  104. },
  105. {
  106. username: 'dan',
  107. email: 'dan@sequelizejs.com',
  108. access_level: 5,
  109. other_value: 10,
  110. aliasValue: 1,
  111. },
  112. {
  113. username: 'fred',
  114. email: 'fred@foobar.com',
  115. access_level: 3,
  116. other_value: 7,
  117. aliasValue: 10,
  118. },
  119. ];
  120. await this.ScopeMe.bulkCreate(records0);
  121. const records = await this.ScopeMe.findAll();
  122. await Promise.all([
  123. records[0].createChild({
  124. priority: 1,
  125. }),
  126. records[1].createChild({
  127. priority: 2,
  128. }),
  129. ]);
  130. });
  131. it('should apply defaultScope', async function () {
  132. await expect(this.ScopeMe.count()).to.eventually.equal(2);
  133. });
  134. it('should be able to override default scope', async function () {
  135. await expect(
  136. this.ScopeMe.count({ where: { access_level: { [Op.gt]: 5 } } }),
  137. ).to.eventually.equal(1);
  138. });
  139. it('should be able to unscope', async function () {
  140. await expect(this.ScopeMe.withoutScope().count()).to.eventually.equal(4);
  141. });
  142. it('should be able to apply other scopes', async function () {
  143. await expect(this.ScopeMe.withScope('lowAccess').count()).to.eventually.equal(3);
  144. });
  145. it('should be able to merge scopes with where', async function () {
  146. await expect(
  147. this.ScopeMe.withScope('lowAccess').count({ where: { username: 'dan' } }),
  148. ).to.eventually.equal(1);
  149. });
  150. it('should be able to merge scopes with where on aliased fields', async function () {
  151. await expect(
  152. this.ScopeMe.withScope('withAliasedField').count({ where: { aliasValue: 5 } }),
  153. ).to.eventually.equal(1);
  154. });
  155. it('should ignore the order option if it is found within the scope', async function () {
  156. await expect(this.ScopeMe.withScope('withOrder').count()).to.eventually.equal(4);
  157. });
  158. it('should be able to use where on include', async function () {
  159. await expect(this.ScopeMe.withScope('withInclude').count()).to.eventually.equal(1);
  160. });
  161. it('should be able to use include with function scope', async function () {
  162. await expect(this.ScopeMe.withScope('withIncludeFunction').count()).to.eventually.equal(1);
  163. });
  164. it('should be able to use include with function scope and string association', async function () {
  165. await expect(
  166. this.ScopeMe.withScope('withIncludeFunctionAndStringAssociation').count(),
  167. ).to.eventually.equal(1);
  168. });
  169. });
  170. });
  171. });