From bebaad87fb28340836e86b3d192e3ab7985bbc38 Mon Sep 17 00:00:00 2001 From: corpglory-dev Date: Mon, 14 Dec 2020 15:13:48 +0300 Subject: [PATCH 1/3] fix postgres tests --- spec/postgres.jest.ts | 41 ++++++++++++++++------------------------- 1 file changed, 16 insertions(+), 25 deletions(-) diff --git a/spec/postgres.jest.ts b/spec/postgres.jest.ts index 04b3b68..94dae85 100644 --- a/spec/postgres.jest.ts +++ b/spec/postgres.jest.ts @@ -11,7 +11,7 @@ describe('Test query creation', function() { let offset = 0; let from = 1542983750857; let to = 1542984313292; - let postgres = getDefaultMetric(); + let postgres = getMetricForSqlQuery(); let mQuery: MetricQuery = postgres.getQuery(from, to, limit, offset); it('test that payload placed to data field', function() { @@ -31,7 +31,7 @@ describe('Test query creation', function() { }); describe('Test result parsing', function() { - let postgres = getDefaultMetric(); + let postgres = getMetricForSqlQuery(); let timestamps = [1542983800000, 1542983800060, 1542983800120] let response = { data: { @@ -216,38 +216,29 @@ describe('Test sql processing', function() { }); function checkExpectation(original: string, expected: string, from: number, to: number, limit: number, offset: number) { - let metric = getMetricWithSql(original); + let metric = getMetricForSqlQuery(original); expect(metric.getQuery(from, to, limit, offset).schema.data.queries[0].rawSql).toBe(expected); } -function getMetricWithSql(sql: string): PostgresMetric { - let metric = getDefaultMetric(); - metric.datasource.data.queries[0].rawSql = sql; - return metric; -} - -function getDefaultMetric(): PostgresMetric { - let queryPayload = { +function getMetricForSqlQuery(query: string = ''): PostgresMetric { + const queryPayload = { from: 1542983750857, - to: 1542984313292, - queries:[{ - refId: 'A', - intervalMs:2000, - maxDataPoints:191, - datasourceId:1, - rawSql: 'SELECT\n \"time\" AS \"time\",\n val\nFROM local\nORDER BY 1', - format: 'time_series' - }] + to: 1542984313292 }; - let datasource = { - url: 'api/tsdb/query', - type: 'postgres', - data: queryPayload + const datasource = { + url: 'api/tsdb/query', + type: 'postgres', + data: queryPayload }; - let targets = [{ + const targets = [{ refId: 'A', + intervalMs: 2000, + maxDataPoints: 191, + datasourceId: 1, + rawSql: query, + format: 'time_series' }]; return new PostgresMetric(datasource, targets); From 50cbb804d3914c972997f770fb6ca924264ffaf8 Mon Sep 17 00:00:00 2001 From: corpglory-dev Date: Mon, 14 Dec 2020 15:13:54 +0300 Subject: [PATCH 2/3] 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); }); From 4a2ee9e6cdfe11f00f0521ca707f85e0bb8beb44 Mon Sep 17 00:00:00 2001 From: rozetko Date: Tue, 15 Dec 2020 13:19:05 +0300 Subject: [PATCH 3/3] Update spec/elasticsearch.jest.ts --- spec/elasticsearch.jest.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/spec/elasticsearch.jest.ts b/spec/elasticsearch.jest.ts index 950fa69..4c71ba6 100644 --- a/spec/elasticsearch.jest.ts +++ b/spec/elasticsearch.jest.ts @@ -175,7 +175,6 @@ describe('simple query', function(){ 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()