Skip to content

Commit

Permalink
fix: expect result is null but got zero (#13637)
Browse files Browse the repository at this point in the history
* fix: expect result is null but got zero

* revert: query-interface.js

* fix: model.js to return null

* fix: test title

* fix: if sum without rows, expect null

Co-authored-by: Sascha Depold <sdepold@users.noreply.github.com>
  • Loading branch information
r253hmdryou and sdepold committed Nov 10, 2021
1 parent f581543 commit da3ac09
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 11 deletions.
3 changes: 0 additions & 3 deletions lib/model.js
Expand Up @@ -1984,9 +1984,6 @@ class Model {
options = this._paranoidClause(this, options);

const value = await this.queryInterface.rawSelect(this.getTableName(options), options, aggregateFunction, this);
if (value === null) {
return 0;
}
return value;
}

Expand Down
19 changes: 12 additions & 7 deletions test/integration/model/notExist.test.js
Expand Up @@ -22,12 +22,13 @@ describe(Support.getTestDialectTeaser('Model'), () => {
{ sequence: 3, amount: 5, type: 'A' },
{ sequence: 4, amount: 1, type: 'A' },
{ sequence: 1, amount: 2, type: 'B' },
{ sequence: 2, amount: 6, type: 'B' }
{ sequence: 2, amount: 6, type: 'B' },
{ sequence: 0, amount: 0, type: 'C' }
]);
});

describe('max', () => {
it('should type exist', async function() {
it('type A to C should 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);
Expand All @@ -41,18 +42,22 @@ describe(Support.getTestDialectTeaser('Model'), () => {
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);
});

it('type D should not exist', async function() {
await expect(this.Order.sum('sequence', { where: { type: 'D' } })).to.eventually.be.null;
await expect(this.Order.max('sequence', { where: { type: 'D' } })).to.eventually.be.null;
await expect(this.Order.min('sequence', { where: { type: 'D' } })).to.eventually.be.null;
await expect(this.Order.sum('amount', { where: { type: 'D' } })).to.eventually.be.null;
await expect(this.Order.max('amount', { where: { type: 'D' } })).to.eventually.be.null;
await expect(this.Order.min('amount', { where: { type: 'D' } })).to.eventually.be.null;
});
});
});
2 changes: 1 addition & 1 deletion test/integration/model/sum.test.js
Expand Up @@ -28,7 +28,7 @@ describe(Support.getTestDialectTeaser('Model'), () => {
describe('sum', () => {

it('should sum without rows', async function() {
await expect(this.Payment.sum('amount', { where: { mood: 'sad' } })).to.eventually.be.equal(0);
await expect(this.Payment.sum('amount', { where: { mood: 'sad' } })).to.eventually.be.null;
});

it('should sum when is 0', async function() {
Expand Down

0 comments on commit da3ac09

Please sign in to comment.