Browse Source

Merge pull request #25 from CorpGlory/tests-recovery

Fix postgres and elastic tests
dependabot/npm_and_yarn/node-notifier-8.0.1
Coin de Gamma 4 years ago committed by GitHub
parent
commit
a5feb435e2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 42
      spec/elasticsearch.jest.ts
  2. 35
      spec/postgres.jest.ts

42
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,35 @@ 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'
},
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 +257,12 @@ describe('simple query', function(){
it('check results parsing', function() {
let expectedResult = {
columns: ['timestamp', 'target'],
values: [[1545934140000, null],
values: [
[1545934140000, null],
[1545934200000, 991287.4583339691],
[1545934260000, 898992.5]
]
}
};
expect(elasticMetric.getResults(result)).toEqual(expectedResult);
});

35
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 = {
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);

Loading…
Cancel
Save