-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(mongoose): Hardening reference support
- Loading branch information
1 parent
5cdfa39
commit 107bba0
Showing
18 changed files
with
468 additions
and
232 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
packages/query-mongoose/__tests__/query/aggregate.builder.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* eslint-disable @typescript-eslint/naming-convention */ | ||
import { AggregateQuery } from '@nestjs-query/core'; | ||
import { TestEntity } from '../__fixtures__/test.entity'; | ||
import { AggregateBuilder, MongooseAggregate } from '../../src/query'; | ||
|
||
describe('AggregateBuilder', (): void => { | ||
const createAggregateBuilder = () => new AggregateBuilder<TestEntity>(); | ||
|
||
const assertQuery = (agg: AggregateQuery<TestEntity>, expected: MongooseAggregate): void => { | ||
const actual = createAggregateBuilder().build(agg); | ||
expect(actual).toEqual(expected); | ||
}; | ||
|
||
it('should throw an error if no selects are generated', (): void => { | ||
expect(() => createAggregateBuilder().build({})).toThrow('No aggregate fields found.'); | ||
}); | ||
|
||
it('or multiple operators for a single field together', (): void => { | ||
assertQuery( | ||
{ | ||
count: ['id', 'stringType'], | ||
avg: ['numberType'], | ||
sum: ['numberType'], | ||
max: ['stringType', 'dateType', 'numberType'], | ||
min: ['stringType', 'dateType', 'numberType'], | ||
}, | ||
{ | ||
avg_numberType: { $avg: '$numberType' }, | ||
count_id: { $sum: { $cond: { if: { $ne: ['$_id', null] }, then: 1, else: 0 } } }, | ||
count_stringType: { $sum: { $cond: { if: { $ne: ['$stringType', null] }, then: 1, else: 0 } } }, | ||
max_dateType: { $max: '$dateType' }, | ||
max_numberType: { $max: '$numberType' }, | ||
max_stringType: { $max: '$stringType' }, | ||
min_dateType: { $min: '$dateType' }, | ||
min_numberType: { $min: '$numberType' }, | ||
min_stringType: { $min: '$stringType' }, | ||
sum_numberType: { $sum: '$numberType' }, | ||
}, | ||
); | ||
}); | ||
|
||
describe('.convertToAggregateResponse', () => { | ||
it('should convert a flat response into an Aggregtate response', () => { | ||
const dbResult = { | ||
count_id: 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: { id: 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, | ||
}; | ||
expect(() => AggregateBuilder.convertToAggregateResponse<TestEntity>(dbResult)).toThrow( | ||
'Unknown aggregate column encountered.', | ||
); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.