paranoid.test.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. 'use strict';
  2. const chai = require('chai');
  3. const expect = chai.expect;
  4. const sinon = require('sinon');
  5. const Support = require('../support');
  6. const { DataTypes } = require('@sequelize/core');
  7. describe(Support.getTestDialectTeaser('Paranoid'), () => {
  8. beforeEach(async function () {
  9. const S = this.sequelize;
  10. const DT = DataTypes;
  11. const A = (this.A = S.define('A', { name: DT.STRING }, { paranoid: true }));
  12. const B = (this.B = S.define('B', { name: DT.STRING }, { paranoid: true }));
  13. const C = (this.C = S.define('C', { name: DT.STRING }, { paranoid: true }));
  14. const D = (this.D = S.define('D', { name: DT.STRING }, { paranoid: true }));
  15. A.belongsTo(B);
  16. A.belongsToMany(D, { through: 'a_d' });
  17. A.hasMany(C);
  18. B.hasMany(A);
  19. B.hasMany(C);
  20. C.belongsTo(A);
  21. C.belongsTo(B);
  22. D.belongsToMany(A, { through: 'a_d' });
  23. await S.sync({ force: true });
  24. });
  25. before(function () {
  26. this.clock = sinon.useFakeTimers();
  27. });
  28. after(function () {
  29. this.clock.restore();
  30. });
  31. it('paranoid with timestamps: false should be ignored / not crash', async function () {
  32. const S = this.sequelize;
  33. const Test = S.define(
  34. 'Test',
  35. {
  36. name: DataTypes.STRING,
  37. },
  38. {
  39. timestamps: false,
  40. paranoid: true,
  41. },
  42. );
  43. await S.sync({ force: true });
  44. await Test.findByPk(1);
  45. });
  46. it('test if non required is marked as false', async function () {
  47. const A = this.A;
  48. const B = this.B;
  49. const options = {
  50. include: [
  51. {
  52. model: B,
  53. required: false,
  54. },
  55. ],
  56. };
  57. await A.findOne(options);
  58. expect(options.include[0].required).to.equal(false);
  59. });
  60. it('test if required is marked as true', async function () {
  61. const A = this.A;
  62. const B = this.B;
  63. const options = {
  64. include: [
  65. {
  66. model: B,
  67. required: true,
  68. },
  69. ],
  70. };
  71. await A.findOne(options);
  72. expect(options.include[0].required).to.equal(true);
  73. });
  74. it('should not load paranoid, destroyed instances, with a non-paranoid parent', async function () {
  75. const X = this.sequelize.define(
  76. 'x',
  77. {
  78. name: DataTypes.STRING,
  79. },
  80. {
  81. paranoid: false,
  82. },
  83. );
  84. const Y = this.sequelize.define(
  85. 'y',
  86. {
  87. name: DataTypes.STRING,
  88. },
  89. {
  90. timestamps: true,
  91. paranoid: true,
  92. },
  93. );
  94. X.hasMany(Y);
  95. await this.sequelize.sync({ force: true });
  96. const [x0, y] = await Promise.all([X.create(), Y.create()]);
  97. this.x = x0;
  98. this.y = y;
  99. await x0.addY(y);
  100. await this.y.destroy();
  101. // prevent CURRENT_TIMESTAMP to be same
  102. this.clock.tick(1000);
  103. const obj = await X.findAll({
  104. include: [Y],
  105. });
  106. const x = await obj[0];
  107. expect(x.ys).to.have.length(0);
  108. });
  109. });