destroy.test.js 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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('destroy', () => {
  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. },
  26. scopes: {
  27. lowAccess: {
  28. where: {
  29. access_level: {
  30. [Op.lte]: 5,
  31. },
  32. },
  33. },
  34. },
  35. },
  36. );
  37. await this.sequelize.sync({ force: true });
  38. const records = [
  39. { username: 'tony', email: 'tony@sequelizejs.com', access_level: 3, other_value: 7 },
  40. { username: 'tobi', email: 'tobi@fakeemail.com', access_level: 10, other_value: 11 },
  41. { username: 'dan', email: 'dan@sequelizejs.com', access_level: 5, other_value: 10 },
  42. { username: 'fred', email: 'fred@foobar.com', access_level: 3, other_value: 7 },
  43. ];
  44. await this.ScopeMe.bulkCreate(records);
  45. });
  46. it('should apply defaultScope', async function () {
  47. await this.ScopeMe.destroy({ where: {} });
  48. const users = await this.ScopeMe.withoutScope().findAll();
  49. expect(users).to.have.length(2);
  50. expect(users[0].get('username')).to.equal('tony');
  51. expect(users[1].get('username')).to.equal('fred');
  52. });
  53. it('should be able to unscope destroy', async function () {
  54. await this.ScopeMe.withoutScope().destroy({ where: {} });
  55. await expect(this.ScopeMe.withoutScope().findAll()).to.eventually.have.length(0);
  56. });
  57. it('should be able to apply other scopes', async function () {
  58. await this.ScopeMe.withScope('lowAccess').destroy({ where: {} });
  59. const users = await this.ScopeMe.withoutScope().findAll();
  60. expect(users).to.have.length(1);
  61. expect(users[0].get('username')).to.equal('tobi');
  62. });
  63. it('should be able to merge scopes with where', async function () {
  64. await this.ScopeMe.withScope('lowAccess').destroy({ where: { username: 'dan' } });
  65. const users = await this.ScopeMe.withoutScope().findAll();
  66. expect(users).to.have.length(3);
  67. expect(users[0].get('username')).to.equal('tony');
  68. expect(users[1].get('username')).to.equal('tobi');
  69. expect(users[2].get('username')).to.equal('fred');
  70. });
  71. it('should be able to merge scopes with similar where', async function () {
  72. await this.ScopeMe.withScope('defaultScope', 'lowAccess').destroy();
  73. const users = await this.ScopeMe.withoutScope().findAll();
  74. expect(users).to.have.length(3);
  75. expect(users[0].get('username')).to.equal('tony');
  76. expect(users[1].get('username')).to.equal('tobi');
  77. expect(users[2].get('username')).to.equal('fred');
  78. });
  79. it('should work with empty where', async function () {
  80. await this.ScopeMe.withScope('lowAccess').destroy();
  81. const users = await this.ScopeMe.withoutScope().findAll();
  82. expect(users).to.have.length(1);
  83. expect(users[0].get('username')).to.equal('tobi');
  84. });
  85. });
  86. });
  87. });