Skip to content

Commit

Permalink
feat(sequelize): Update to support new aggregate with groupBy
Browse files Browse the repository at this point in the history
  • Loading branch information
doug-martin committed Mar 31, 2021
1 parent ccd0438 commit 81fdef1
Show file tree
Hide file tree
Showing 7 changed files with 482 additions and 172 deletions.
64 changes: 44 additions & 20 deletions packages/query-sequelize/__tests__/query/aggregate.builder.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ describe('AggregateBuilder', (): void => {
expect(() => createAggregateBuilder().build({})).toThrow('No aggregate fields found.');
});

it('or multiple operators for a single field together', (): void => {
it('should create selects for all aggregate functions', (): void => {
assertSQL(
{
count: ['testEntityPk', 'stringType'],
Expand All @@ -61,30 +61,54 @@ describe('AggregateBuilder', (): void => {
);
});

it('should create selects for all aggregate functions and group bys', (): void => {
assertSQL(
{
groupBy: ['stringType', 'boolType'],
count: ['testEntityPk'],
},
{
attributes: [
[sequelize.col('string_type'), 'GROUP_BY_stringType'],
[sequelize.col('bool_type'), 'GROUP_BY_boolType'],
[sequelize.fn('COUNT', sequelize.col('test_entity_pk')), 'COUNT_testEntityPk'],
],
},
);
});

describe('.convertToAggregateResponse', () => {
it('should convert a flat response into an Aggregtate response', () => {
const dbResult = {
COUNT_testEntityPk: 10,
SUM_numberType: 55,
AVG_numberType: 5,
MAX_stringType: 'z',
MAX_numberType: 10,
MIN_stringType: 'a',
MIN_numberType: 1,
};
expect(AggregateBuilder.convertToAggregateResponse<TestEntity>(dbResult)).toEqual({
count: { testEntityPk: 10 },
sum: { numberType: 55 },
avg: { numberType: 5 },
max: { stringType: 'z', numberType: 10 },
min: { stringType: 'a', numberType: 1 },
});
const dbResult = [
{
GROUP_BY_stringType: 'z',
COUNT_testEntityPk: 10,
SUM_numberType: 55,
AVG_numberType: 5,
MAX_stringType: 'z',
MAX_numberType: 10,
MIN_stringType: 'a',
MIN_numberType: 1,
},
];
expect(AggregateBuilder.convertToAggregateResponse<TestEntity>(dbResult)).toEqual([
{
groupBy: { stringType: 'z' },
count: { testEntityPk: 10 },
sum: { numberType: 55 },
avg: { numberType: 5 },
max: { stringType: 'z', numberType: 10 },
min: { stringType: 'a', numberType: 1 },
},
]);
});

it('should throw an error if a column is not expected', () => {
const dbResult = {
COUNTtestEntityPk: 10,
};
const dbResult = [
{
COUNTtestEntityPk: 10,
},
];
expect(() => AggregateBuilder.convertToAggregateResponse<TestEntity>(dbResult)).toThrow(
'Unknown aggregate column encountered.',
);
Expand Down
Loading

0 comments on commit 81fdef1

Please sign in to comment.