restore.test.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. 'use strict';
  2. const { expect } = require('chai');
  3. const { sequelize } = require('../../support');
  4. const { DataTypes } = require('@sequelize/core');
  5. const sinon = require('sinon');
  6. describe('Model#restore', () => {
  7. it('is not allowed if the instance does not have a primary key defined', async () => {
  8. const User = sequelize.define('User', {}, { paranoid: true });
  9. const instance = User.build({}, { isNewRecord: false });
  10. await expect(instance.restore()).to.be.rejectedWith(
  11. 'save an instance with no primary key, this is not allowed since it would',
  12. );
  13. });
  14. describe('options tests', () => {
  15. let stub;
  16. before(() => {
  17. stub = sinon.stub(sequelize, 'queryRaw').resolves([
  18. {
  19. _previousDataValues: { id: 1 },
  20. dataValues: { id: 2 },
  21. },
  22. 1,
  23. ]);
  24. });
  25. after(() => {
  26. stub.restore();
  27. });
  28. it('should allow restores even if options are not given', () => {
  29. const User = sequelize.define(
  30. 'User',
  31. {
  32. id: {
  33. type: DataTypes.INTEGER,
  34. primaryKey: true,
  35. autoIncrement: true,
  36. },
  37. deletedAt: {},
  38. },
  39. {
  40. paranoid: true,
  41. },
  42. );
  43. const instance = User.build({ id: 1 }, { isNewRecord: false });
  44. expect(() => {
  45. instance.restore();
  46. }).to.not.throw();
  47. });
  48. });
  49. });