bulk-destroy.test.ts 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import type { InferAttributes, InferCreationAttributes } from '@sequelize/core';
  2. import { Model } from '@sequelize/core';
  3. import { expect } from 'chai';
  4. import sinon from 'sinon';
  5. import { beforeAll2, expectsql, sequelize } from '../../support';
  6. describe('ModelRepository#_UNSTABLE_bulkDestroy', () => {
  7. const vars = beforeAll2(() => {
  8. class User extends Model<InferAttributes<User>, InferCreationAttributes<User>> {
  9. declare id: number;
  10. }
  11. sequelize.addModels([User]);
  12. return { User };
  13. });
  14. afterEach(() => {
  15. vars.User.hooks.removeAllListeners();
  16. sinon.restore();
  17. });
  18. it('throw an error if the "where" option is not specified', async () => {
  19. const { User } = vars;
  20. const repository = User.modelRepository;
  21. // @ts-expect-error -- testing that not specifying "where" leads to an error
  22. await expect(repository._UNSTABLE_bulkDestroy({})).to.be.rejectedWith(
  23. 'requires explicitly specifying a "where"',
  24. );
  25. });
  26. it('creates a single DELETE query', async () => {
  27. const stub = sinon.stub(sequelize, 'queryRaw');
  28. const { User } = vars;
  29. const repository = User.modelRepository;
  30. await repository._UNSTABLE_bulkDestroy({ where: { id: 5 } });
  31. const firstCall = stub.getCall(0);
  32. expectsql(firstCall.args[0], {
  33. default: `DELETE FROM [Users] WHERE [id] = 5`,
  34. mssql: `DELETE FROM [Users] WHERE [id] = 5; SELECT @@ROWCOUNT AS AFFECTEDROWS;`,
  35. });
  36. });
  37. it('supports _UNSTABLE_beforeBulkDestroy/_UNSTABLE_afterBulkDestroy hooks', async () => {
  38. sinon.stub(sequelize, 'queryRaw');
  39. const { User } = vars;
  40. const repository = User.modelRepository;
  41. const beforeDestroySpy = sinon.spy();
  42. const afterDestroySpy = sinon.spy();
  43. User.hooks.addListener('_UNSTABLE_beforeBulkDestroy', beforeDestroySpy);
  44. User.hooks.addListener('_UNSTABLE_afterBulkDestroy', afterDestroySpy);
  45. await repository._UNSTABLE_bulkDestroy({ where: { id: 5 } });
  46. expect(beforeDestroySpy.callCount).to.eq(1);
  47. expect(beforeDestroySpy.getCall(0).args).to.deep.eq([
  48. { manualOnDelete: 'paranoid', where: { id: 5 } },
  49. ]);
  50. expect(afterDestroySpy.callCount).to.eq(1);
  51. expect(afterDestroySpy.getCall(0).args).to.deep.eq([
  52. { manualOnDelete: 'paranoid', where: { id: 5 } },
  53. undefined, // returned from queryRaw stub
  54. ]);
  55. });
  56. it('allows modifying the options in _UNSTABLE_beforeBulkDestroy', async () => {
  57. const stub = sinon.stub(sequelize, 'queryRaw');
  58. const { User } = vars;
  59. const repository = User.modelRepository;
  60. User.hooks.addListener('_UNSTABLE_beforeBulkDestroy', options => {
  61. options.where = { id: 6 };
  62. });
  63. await repository._UNSTABLE_bulkDestroy({ where: { id: 5 } });
  64. const firstCall = stub.getCall(0);
  65. expectsql(firstCall.args[0], {
  66. default: `DELETE FROM [Users] WHERE [id] = 6`,
  67. mssql: `DELETE FROM [Users] WHERE [id] = 6; SELECT @@ROWCOUNT AS AFFECTEDROWS;`,
  68. });
  69. });
  70. });