Skip to content

Commit

Permalink
fix(model): Convert number values only if they aren't null to avoid NaN
Browse files Browse the repository at this point in the history
Co-authored-by: Sascha Depold <sdepold@users.noreply.github.com>
  • Loading branch information
caolvchong and sdepold committed Oct 10, 2021
1 parent cf53734 commit 199b632
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/dialects/abstract/query-interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -1011,7 +1011,9 @@ class QueryInterface {
}
}
if (dataType instanceof DataTypes.INTEGER || dataType instanceof DataTypes.BIGINT) {
return parseInt(result, 10);
if (result !== null) {
return parseInt(result, 10);
}
}
if (dataType instanceof DataTypes.DATE) {
if (result !== null && !(result instanceof Date)) {
Expand Down
58 changes: 58 additions & 0 deletions test/integration/model/notExist.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
'use strict';

const chai = require('chai'),
expect = chai.expect,
Support = require('../support'),
DataTypes = require('../../../lib/data-types');

describe(Support.getTestDialectTeaser('Model'), () => {
beforeEach(async function() {
this.Order = this.sequelize.define('Order', {
id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true },
sequence: DataTypes.INTEGER,
amount: DataTypes.DECIMAL,
type: DataTypes.STRING
});

await this.sequelize.sync({ force: true });

await this.Order.bulkCreate([
{ sequence: 1, amount: 3, type: 'A' },
{ sequence: 2, amount: 4, type: 'A' },
{ sequence: 3, amount: 5, type: 'A' },
{ sequence: 4, amount: 1, type: 'A' },
{ sequence: 1, amount: 2, type: 'B' },
{ sequence: 2, amount: 6, type: 'B' }
]);
});

describe('max', () => {
it('should type exist', async function() {
await expect(this.Order.sum('sequence', { where: { type: 'A' } })).to.eventually.be.equal(10);
await expect(this.Order.max('sequence', { where: { type: 'A' } })).to.eventually.be.equal(4);
await expect(this.Order.min('sequence', { where: { type: 'A' } })).to.eventually.be.equal(1);
await expect(this.Order.sum('amount', { where: { type: 'A' } })).to.eventually.be.equal(13);
await expect(this.Order.max('amount', { where: { type: 'A' } })).to.eventually.be.equal(5);
await expect(this.Order.min('amount', { where: { type: 'A' } })).to.eventually.be.equal(1);

await expect(this.Order.sum('sequence', { where: { type: 'B' } })).to.eventually.be.equal(3);
await expect(this.Order.max('sequence', { where: { type: 'B' } })).to.eventually.be.equal(2);
await expect(this.Order.min('sequence', { where: { type: 'B' } })).to.eventually.be.equal(1);
await expect(this.Order.sum('amount', { where: { type: 'B' } })).to.eventually.be.equal(8);
await expect(this.Order.max('amount', { where: { type: 'B' } })).to.eventually.be.equal(6);
await expect(this.Order.min('amount', { where: { type: 'B' } })).to.eventually.be.equal(2);
});

it('should type not exist', async function() {
// DataTypes.INTEGER or DataTypes.BIGINT: previous version should use `.to.eventually.be.NaN`
await expect(this.Order.sum('sequence', { where: { type: 'C' } })).to.eventually.be.equal(0);
await expect(this.Order.max('sequence', { where: { type: 'C' } })).to.eventually.be.equal(0);
await expect(this.Order.min('sequence', { where: { type: 'C' } })).to.eventually.be.equal(0);

// DataTypes.DECIMAL or DataTypes.FLOAT: previous and PR#13422 both use `to.eventually.be.equal(0)`
await expect(this.Order.sum('amount', { where: { type: 'C' } })).to.eventually.be.equal(0);
await expect(this.Order.max('amount', { where: { type: 'C' } })).to.eventually.be.equal(0);
await expect(this.Order.min('amount', { where: { type: 'C' } })).to.eventually.be.equal(0);
});
});
});

0 comments on commit 199b632

Please sign in to comment.