Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: don't call overloaded versions of find functions internally #13951

Merged
merged 5 commits into from Jan 15, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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.
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;
});
});
});