不要怂,就是干,撸起袖子干!

Commit 1b80e0b5 by Sascha Depold Committed by GitHub

Add tests and fix for usage of count with grouping (#13531)

* fix(model): convert count to number when grouping

* test(model#count): fix ordering issue in count test
1 parent fecc67fe
......@@ -2034,7 +2034,18 @@ class Model {
options.offset = null;
options.order = null;
return await this.aggregate(col, 'count', options);
const result = await this.aggregate(col, 'count', options);
// When grouping is used, some dialects such as PG are returning the count as string
// --> Manually convert it to number
if (Array.isArray(result)) {
return result.map(item => ({
...item,
count: Number(item.count)
}));
}
return result;
}
/**
......
......@@ -1352,6 +1352,7 @@ describe(Support.getTestDialectTeaser('HasMany'), () => {
});
expect(count.length).to.equal(1);
expect(count).to.deep.equal([{ userId: 1, count: 1 }]);
expect(rows[0].tasks[0].jobs.length).to.equal(2);
});
});
......
......@@ -1720,6 +1720,10 @@ describe(Support.getTestDialectTeaser('Model'), () => {
group: ['data']
});
expect(count).to.have.lengthOf(2);
// The order of count varies across dialects; Hence find element by identified first.
expect(count.find(i => i.data === 'A')).to.deep.equal({ data: 'A', count: 2 });
expect(count.find(i => i.data === 'B')).to.deep.equal({ data: 'B', count: 1 });
});
if (dialect !== 'mssql') {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!