save.test.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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#save', () => {
  7. it('is not allowed if the instance does not have a primary key defined', async () => {
  8. const User = sequelize.define('User', {});
  9. const instance = User.build({}, { isNewRecord: false });
  10. await expect(instance.save()).to.be.rejectedWith(
  11. 'You attempted to save an instance with no primary key, this is not allowed since it would result in a global update',
  12. );
  13. });
  14. describe('options tests', () => {
  15. let stub;
  16. before(() => {
  17. stub = sinon.stub(sequelize, 'queryRaw').resolves([
  18. {
  19. _previousDataValues: {},
  20. dataValues: { id: 1 },
  21. },
  22. 1,
  23. ]);
  24. });
  25. after(() => {
  26. stub.restore();
  27. });
  28. it('should allow saves even if options are not given', () => {
  29. const User = sequelize.define('User', {
  30. id: {
  31. type: DataTypes.INTEGER,
  32. primaryKey: true,
  33. autoIncrement: true,
  34. },
  35. });
  36. const instance = User.build({});
  37. expect(() => {
  38. instance.save();
  39. }).to.not.throw();
  40. });
  41. });
  42. });