Skip to content

Commit

Permalink
fix: don't call overloaded versions of find functions internally (#13951
Browse files Browse the repository at this point in the history
)
  • Loading branch information
cincodenada committed Jan 15, 2022
1 parent 1a6884a commit e5d02ef
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/model.js
Expand Up @@ -1999,7 +1999,7 @@ class Model {
}

// Bypass a possible overloaded findOne
return await this.findOne(options);
return await Model.findOne.call(this, options);
}

/**
Expand Down Expand Up @@ -2034,9 +2034,9 @@ class Model {
}

// Bypass a possible overloaded findAll.
return await this.findAll(_.defaults(options, {
return await Model.findAll.call(this, (_.defaults(options, {
plain: true,
}));
})));
}

/**
Expand Down
File renamed without changes.
File renamed without changes.
42 changes: 42 additions & 0 deletions test/unit/model/find-by-pk.test.js
@@ -0,0 +1,42 @@
'use strict';

const chai = require('chai');

const expect = chai.expect;
const Support = require('../support');

const Sequelize = Support.Sequelize;
const Op = Sequelize.Op;
const current = Support.sequelize;
const sinon = require('sinon');
const DataTypes = require('../../../lib/data-types');

describe(Support.getTestDialectTeaser('Model'), () => {
describe('method findByPk', () => {
beforeEach(function () {
this.stub = sinon.stub(Sequelize.Model, 'findAll').resolves();
});
afterEach(() => {
sinon.restore();
});

it('should call internal findOne() method if findOne() is overridden', async () => {
const Model = current.define('model', {
unique1: {
type: DataTypes.INTEGER,
unique: 'unique',
},
unique2: {
type: DataTypes.INTEGER,
unique: 'unique',
},
});
Model.findOne = sinon.stub();
sinon.spy(Sequelize.Model, 'findOne');

await Model.findByPk(1);
Model.findOne.should.not.have.been.called;
Sequelize.Model.findOne.should.have.been.called;
});
});
});
Expand Up @@ -107,5 +107,22 @@ describe(Support.getTestDialectTeaser('Model'), () => {
await Model.findOne({ where: { unique1: 42 } });
expect(this.stub.getCall(0).args[0]).to.be.an('object').to.have.property('limit');
});
it('should call internal findAll() method if findOne() is overridden', async () => {
const Model = current.define('model', {
unique1: {
type: DataTypes.INTEGER,
unique: 'unique',
},
unique2: {
type: DataTypes.INTEGER,
unique: 'unique',
},
});
Model.findAll = sinon.stub();

await Model.findOne();
Model.findAll.should.not.have.been.called;
Sequelize.Model.findAll.should.have.been.called;
});
});
});
File renamed without changes.
File renamed without changes.

0 comments on commit e5d02ef

Please sign in to comment.