decrement.test.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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#decrement', () => {
  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({});
  10. await expect(instance.decrement()).to.be.rejectedWith(
  11. 'but this model instance is missing the value of its primary key',
  12. );
  13. });
  14. describe('options tests', () => {
  15. let stub;
  16. before(() => {
  17. stub = sinon.stub(sequelize, 'queryRaw').resolves({
  18. _previousDataValues: { id: 3 },
  19. dataValues: { id: 1 },
  20. });
  21. });
  22. after(() => {
  23. stub.restore();
  24. });
  25. it('should allow decrements even if options are not given', async () => {
  26. const User = sequelize.define('User', {
  27. id: {
  28. type: DataTypes.INTEGER,
  29. primaryKey: true,
  30. autoIncrement: true,
  31. },
  32. });
  33. const instance = User.build({ id: 3 }, { isNewRecord: false });
  34. await expect(instance.decrement(['id'])).to.be.fulfilled;
  35. });
  36. });
  37. });