update.test.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. 'use strict';
  2. const chai = require('chai');
  3. const expect = chai.expect;
  4. const Support = require('../../support');
  5. const current = Support.sequelize;
  6. const sinon = require('sinon');
  7. const { DataTypes } = require('@sequelize/core');
  8. describe(Support.getTestDialectTeaser('Model'), () => {
  9. describe('method update', () => {
  10. before(function () {
  11. this.User = current.define('User', {
  12. name: DataTypes.STRING,
  13. secretValue: DataTypes.INTEGER,
  14. });
  15. });
  16. beforeEach(function () {
  17. this.stubUpdate = sinon.stub(current.queryInterface, 'bulkUpdate').resolves([]);
  18. this.updates = { name: 'Batman', secretValue: '7' };
  19. this.cloneUpdates = { ...this.updates };
  20. });
  21. afterEach(function () {
  22. this.stubUpdate.restore();
  23. });
  24. afterEach(function () {
  25. delete this.updates;
  26. delete this.cloneUpdates;
  27. });
  28. describe('properly clones input values', () => {
  29. it('with default options', async function () {
  30. await this.User.update(this.updates, { where: { secretValue: '1' } });
  31. expect(this.updates).to.be.deep.eql(this.cloneUpdates);
  32. });
  33. it('when using fields option', async function () {
  34. await this.User.update(this.updates, { where: { secretValue: '1' }, fields: ['name'] });
  35. expect(this.updates).to.be.deep.eql(this.cloneUpdates);
  36. });
  37. });
  38. it('can detect complexe objects', async function () {
  39. const Where = function () {
  40. this.secretValue = '1';
  41. };
  42. await expect(this.User.update(this.updates, { where: new Where() })).to.be.rejected;
  43. });
  44. });
  45. describe('Update with multiple models to the same table', () => {
  46. before(function () {
  47. this.Model1 = current.define(
  48. 'Model1',
  49. {
  50. value: DataTypes.INTEGER,
  51. name: DataTypes.STRING,
  52. isModel2: DataTypes.BOOLEAN,
  53. model1ExclusiveData: DataTypes.STRING,
  54. },
  55. {
  56. tableName: 'model_table',
  57. },
  58. );
  59. this.Model2 = current.define(
  60. 'Model2',
  61. {
  62. value: DataTypes.INTEGER,
  63. name: DataTypes.STRING,
  64. },
  65. {
  66. tableName: 'model_table',
  67. },
  68. );
  69. });
  70. beforeEach(function () {
  71. this.stubQuery = sinon.stub(current, 'queryRaw').resolves([]);
  72. });
  73. afterEach(function () {
  74. this.stubQuery.restore();
  75. });
  76. it('updates model1 using model1 model', async function () {
  77. await this.Model1.update(
  78. {
  79. name: 'other name',
  80. model1ExclusiveData: 'only I can update this field',
  81. },
  82. {
  83. where: { value: 1 },
  84. },
  85. );
  86. expect(this.stubQuery.lastCall.lastArg.model).to.eq(this.Model1);
  87. });
  88. it('updates model2 using model2 model', async function () {
  89. await this.Model2.update(
  90. {
  91. name: 'other name',
  92. },
  93. {
  94. where: { value: 2 },
  95. },
  96. );
  97. expect(this.stubQuery.lastCall.lastArg.model).to.eq(this.Model2);
  98. });
  99. });
  100. });