find-one.test.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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, Op, Sequelize } = require('@sequelize/core');
  8. describe(Support.getTestDialectTeaser('Model'), () => {
  9. describe('method findOne', () => {
  10. before(function () {
  11. this.oldFindAll = Sequelize.Model.findAll;
  12. });
  13. after(function () {
  14. Sequelize.Model.findAll = this.oldFindAll;
  15. });
  16. beforeEach(function () {
  17. this.stub = Sequelize.Model.findAll = sinon.stub().resolves();
  18. });
  19. it('should add limit when using { $ gt on the primary key', async function () {
  20. const Model = current.define('model');
  21. await Model.findOne({ where: { id: { [Op.gt]: 42 } } });
  22. expect(this.stub.getCall(0).args[0]).to.be.an('object').to.have.property('limit');
  23. });
  24. it('should add limit when using multi-column unique key', async function () {
  25. const Model = current.define('model', {
  26. unique1: {
  27. type: DataTypes.INTEGER,
  28. unique: 'unique',
  29. },
  30. unique2: {
  31. type: DataTypes.INTEGER,
  32. unique: 'unique',
  33. },
  34. });
  35. await Model.findOne({ where: { unique1: 42 } });
  36. expect(this.stub.getCall(0).args[0]).to.be.an('object').to.have.property('limit');
  37. });
  38. it('should call internal findAll() method if findOne() is overridden', async () => {
  39. const Model = current.define('model', {
  40. unique1: {
  41. type: DataTypes.INTEGER,
  42. unique: 'unique',
  43. },
  44. unique2: {
  45. type: DataTypes.INTEGER,
  46. unique: 'unique',
  47. },
  48. });
  49. Model.findAll = sinon.stub();
  50. await Model.findOne();
  51. Model.findAll.should.not.have.been.called;
  52. Sequelize.Model.findAll.should.have.been.called;
  53. });
  54. });
  55. });