update.test.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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('update', () => {
  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.update({ username: 'ruben' }, { where: {} });
  48. const users = await this.ScopeMe.withoutScope().findAll({ where: { username: 'ruben' } });
  49. expect(users).to.have.length(2);
  50. expect(users[0].get('email')).to.equal('tobi@fakeemail.com');
  51. expect(users[1].get('email')).to.equal('dan@sequelizejs.com');
  52. });
  53. it('should be able to unscope destroy', async function () {
  54. await this.ScopeMe.withoutScope().update({ username: 'ruben' }, { where: {} });
  55. const rubens = await this.ScopeMe.withoutScope().findAll();
  56. expect(rubens.every(r => r.get('username') === 'ruben')).to.be.true;
  57. });
  58. it('should be able to apply other scopes', async function () {
  59. await this.ScopeMe.withScope('lowAccess').update({ username: 'ruben' }, { where: {} });
  60. const users = await this.ScopeMe.withoutScope().findAll({
  61. where: { username: { [Op.ne]: 'ruben' } },
  62. });
  63. expect(users).to.have.length(1);
  64. expect(users[0].get('email')).to.equal('tobi@fakeemail.com');
  65. });
  66. it('should be able to merge scopes with where', async function () {
  67. await this.ScopeMe.withScope('lowAccess').update(
  68. { username: 'ruben' },
  69. { where: { username: 'dan' } },
  70. );
  71. const users = await this.ScopeMe.withoutScope().findAll({ where: { username: 'ruben' } });
  72. expect(users).to.have.length(1);
  73. expect(users[0].get('email')).to.equal('dan@sequelizejs.com');
  74. });
  75. it('should be able to merge scopes with similar where', async function () {
  76. await this.ScopeMe.withScope('defaultScope', 'lowAccess').update({ username: 'fakeName' });
  77. const users = await this.ScopeMe.withoutScope().findAll({
  78. where: { username: 'fakeName' },
  79. });
  80. expect(users).to.have.length(1);
  81. expect(users[0].get('email')).to.equal('dan@sequelizejs.com');
  82. });
  83. it('should work with empty where', async function () {
  84. await this.ScopeMe.withScope('lowAccess').update({
  85. username: 'ruby',
  86. });
  87. const users = await this.ScopeMe.withoutScope().findAll({ where: { username: 'ruby' } });
  88. expect(users).to.have.length(3);
  89. for (const user of users) {
  90. expect(user.get('username')).to.equal('ruby');
  91. }
  92. });
  93. });
  94. });
  95. });