From 50cbb804d3914c972997f770fb6ca924264ffaf8 Mon Sep 17 00:00:00 2001 From: corpglory-dev Date: Mon, 14 Dec 2020 15:13:54 +0300 Subject: [PATCH] fix elastic tests --- spec/elasticsearch.jest.ts | 51 +++++++++++++++++++++++--------------- 1 file changed, 31 insertions(+), 20 deletions(-) diff --git a/spec/elasticsearch.jest.ts b/spec/elasticsearch.jest.ts index e5900bf..950fa69 100644 --- a/spec/elasticsearch.jest.ts +++ b/spec/elasticsearch.jest.ts @@ -1,12 +1,11 @@ import { ElasticsearchMetric } from '../src/metrics/elasticsearch_metric'; -import { MetricQuery, Datasource } from '../src/metrics/metric'; +import { Datasource } from '../src/metrics/metric'; -import 'jest'; import * as _ from 'lodash'; describe('simple query', function(){ - let datasourse: Datasource = { + let datasource: Datasource = { url: "api/datasources/proxy/1/_msearch", data: [{ "search_type": "query_then_fetch", @@ -65,7 +64,7 @@ describe('simple query', function(){ }], type: "elasticsearch" }; - datasourse.data = datasourse.data.map(d => JSON.stringify(d)).join('\n'); + datasource.data = datasource.data.map(d => JSON.stringify(d)).join('\n'); let targets = [ { @@ -120,8 +119,8 @@ describe('simple query', function(){ { "range": { "@timestamp": { - "gte": "0", - "lte": "1", + "gte": "1545933121101", + "lte": "1545954721101", "format": "epoch_millis" } } @@ -163,25 +162,36 @@ describe('simple query', function(){ } }]; - let elasticMetric = new ElasticsearchMetric(datasourse, targets); + let elasticMetric = new ElasticsearchMetric(datasource, targets); it('check correct time processing', function() { + const expectedQueryTemplate = _.cloneDeep(queryTemplate); + + const from = 0; + const to = 1; + const limit = 222; + const offset = 333; + + expectedQueryTemplate[1].query.bool.filter[0].range['@timestamp'].gte = from.toString(); + expectedQueryTemplate[1].query.bool.filter[0].range['@timestamp'].lte = to.toString(); + + + expectedQueryTemplate[1].aggs["2"].date_histogram.extended_bounds = { + min: from.toString(), + max: to.toString() + }; + let expectedQuery = { headers: { - "Content-Type": 'application/json' + 'Content-Type': 'application/json' }, - url: datasourse.url, + url: datasource.url, method: 'POST', schema: { - data: queryTemplate.map(e => JSON.stringify(e)).join('\n') + data: expectedQueryTemplate.map(e => JSON.stringify(e)).join('\n') + '\n' } }; - let from = 0; - let to = 1; - let limit = 222; - let offset = 333; - let result = elasticMetric.getQuery(from, to, limit, offset); expect(result).toEqual(expectedQuery); @@ -248,11 +258,12 @@ describe('simple query', function(){ it('check results parsing', function() { let expectedResult = { columns: ['timestamp', 'target'], - values: [[1545934140000, null], - [1545934200000, 991287.4583339691], - [1545934260000, 898992.5] - ] - } + values: [ + [1545934140000, null], + [1545934200000, 991287.4583339691], + [1545934260000, 898992.5] + ] + }; expect(elasticMetric.getResults(result)).toEqual(expectedResult); });