|
| 1 | +import { ResponseError } from 'lib/common' |
| 2 | +import { HomeDocument } from 'test/module' |
| 3 | +import { TEST_ELASTICSEARCH_NODE } from 'test/constants' |
| 4 | +import { setupNestApplication } from 'test/toolkit' |
| 5 | +import { ElasticsearchModule } from 'module/elasticsearch.module' |
| 6 | +import { ElasticsearchService } from 'module/elasticsearch.service' |
| 7 | +import { getTermsAggregation } from '../get-terms' |
| 8 | +import { getSumAggregation } from '../get-sum' |
| 9 | +import { getBucketSelectorAggregation } from '../get-bucket-selector' |
| 10 | + |
| 11 | +describe('getBucketSelectorAggregation', () => { |
| 12 | + const { app } = setupNestApplication({ |
| 13 | + imports: [ |
| 14 | + ElasticsearchModule.register({ |
| 15 | + node: TEST_ELASTICSEARCH_NODE |
| 16 | + }) |
| 17 | + ] |
| 18 | + }) |
| 19 | + |
| 20 | + it('accepts object with string values for each field', () => { |
| 21 | + const query = getBucketSelectorAggregation('params.myVar1 > 5', { |
| 22 | + myVar1: 'theSum' |
| 23 | + }) |
| 24 | + |
| 25 | + expect(query).toEqual({ |
| 26 | + bucket_selector: { |
| 27 | + buckets_path: { |
| 28 | + myVar1: 'theSum' |
| 29 | + }, |
| 30 | + script: 'params.myVar1 > 5' |
| 31 | + } |
| 32 | + }) |
| 33 | + }) |
| 34 | + |
| 35 | + it('should query elasticsearch for bucket selector aggregation', async () => { |
| 36 | + const service = app.get(ElasticsearchService) |
| 37 | + |
| 38 | + const selectorFilterValue = 5 |
| 39 | + |
| 40 | + const result = await service.search(HomeDocument, { |
| 41 | + size: 0, |
| 42 | + aggregations: { |
| 43 | + date: { |
| 44 | + ...getTermsAggregation('contractDate'), |
| 45 | + aggregations: { |
| 46 | + sum: getSumAggregation('builtInYear'), |
| 47 | + selector: getBucketSelectorAggregation(`params.myVar1 > ${selectorFilterValue}`, { |
| 48 | + myVar1: 'sum' |
| 49 | + }) |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + }) |
| 54 | + |
| 55 | + expect(result.aggregations.date.buckets.length).toBeGreaterThan(0) |
| 56 | + |
| 57 | + result.aggregations.date.buckets.forEach(bucket => { |
| 58 | + expect(bucket.sum).toEqual( |
| 59 | + expect.objectContaining({ |
| 60 | + value: expect.any(Number) |
| 61 | + }) |
| 62 | + ) |
| 63 | + |
| 64 | + expect(bucket.sum.value).toBeGreaterThan(selectorFilterValue) |
| 65 | + }) |
| 66 | + }) |
| 67 | + |
| 68 | + it('should return an error if bucket selector aggregation is not inside another aggregation', async () => { |
| 69 | + const service = app.get(ElasticsearchService) |
| 70 | + |
| 71 | + await service |
| 72 | + .search(HomeDocument, { |
| 73 | + size: 0, |
| 74 | + aggregations: { |
| 75 | + sum: getSumAggregation('builtInYear'), |
| 76 | + selector: getBucketSelectorAggregation('params.myVar1 > 5', { |
| 77 | + myVar1: 'sum' |
| 78 | + }) |
| 79 | + } |
| 80 | + }) |
| 81 | + .catch(error => { |
| 82 | + expect(error).toBeInstanceOf(ResponseError) |
| 83 | + expect(error.message).toContain('action_request_validation_exception') |
| 84 | + expect(error.message).toContain('bucket_selector aggregation [selector] must be declared inside of another aggregation') |
| 85 | + }) |
| 86 | + }) |
| 87 | + |
| 88 | + it(`should return an error if bucket selector tries to use non existing aggregation path`, async () => { |
| 89 | + const service = app.get(ElasticsearchService) |
| 90 | + |
| 91 | + await service |
| 92 | + .search(HomeDocument, { |
| 93 | + size: 0, |
| 94 | + aggregations: { |
| 95 | + sum: getSumAggregation('builtInYear'), |
| 96 | + selector: getBucketSelectorAggregation('params.myVar1 > 5', { |
| 97 | + myVar1: 'sumAggregation' |
| 98 | + }) |
| 99 | + } |
| 100 | + }) |
| 101 | + .catch(error => { |
| 102 | + expect(error).toBeInstanceOf(ResponseError) |
| 103 | + expect(error.message).toContain('action_request_validation_exception') |
| 104 | + expect(error.message).toContain('No aggregation found for path [sumAggregation]') |
| 105 | + }) |
| 106 | + }) |
| 107 | +}) |
0 commit comments