mirror of
https://github.com/grafana/grafana.git
synced 2025-02-12 00:25:46 -06:00
add tests for query generation
This commit is contained in:
parent
17591ca29f
commit
b745fab190
@ -204,17 +204,15 @@ export default class PostgresQuery {
|
|||||||
query = target.timeColumn + ' AS "time"';
|
query = target.timeColumn + ' AS "time"';
|
||||||
}
|
}
|
||||||
|
|
||||||
return '\n ' + query;
|
return query;
|
||||||
}
|
}
|
||||||
|
|
||||||
buildMetricColumn(target) {
|
buildMetricColumn(target) {
|
||||||
let query = '';
|
if (target.metricColumn !== 'None') {
|
||||||
|
return target.metricColumn + ' AS metric';
|
||||||
if (this.target.metricColumn !== 'None') {
|
|
||||||
query += ',\n ' + this.target.metricColumn + ' AS metric';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return query;
|
return '';
|
||||||
}
|
}
|
||||||
|
|
||||||
buildValueColumns(target) {
|
buildValueColumns(target) {
|
||||||
@ -289,21 +287,21 @@ export default class PostgresQuery {
|
|||||||
let query = '';
|
let query = '';
|
||||||
let groupBySection = '';
|
let groupBySection = '';
|
||||||
|
|
||||||
for (let i = 0; i < this.groupByParts.length; i++) {
|
for (let i = 0; i < target.groupBy.length; i++) {
|
||||||
let part = this.groupByParts[i];
|
let part = target.groupBy[i];
|
||||||
if (i > 0) {
|
if (i > 0) {
|
||||||
groupBySection += ', ';
|
groupBySection += ', ';
|
||||||
}
|
}
|
||||||
if (part.def.type === 'time') {
|
if (part.type === 'time') {
|
||||||
groupBySection += '1';
|
groupBySection += '1';
|
||||||
} else {
|
} else {
|
||||||
groupBySection += part.render('');
|
groupBySection += part.params[0];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (groupBySection.length) {
|
if (groupBySection.length) {
|
||||||
query = '\nGROUP BY ' + groupBySection;
|
query = '\nGROUP BY ' + groupBySection;
|
||||||
if (this.target.metricColumn !== 'None') {
|
if (target.metricColumn !== 'None') {
|
||||||
query += ',2';
|
query += ',2';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -311,10 +309,12 @@ export default class PostgresQuery {
|
|||||||
}
|
}
|
||||||
|
|
||||||
buildQuery(target) {
|
buildQuery(target) {
|
||||||
let query = 'SELECT ';
|
let query = 'SELECT';
|
||||||
|
|
||||||
query += this.buildTimeColumn(target);
|
query += '\n ' + this.buildTimeColumn(target);
|
||||||
query += this.buildMetricColumn(target);
|
if (target.metricColumn !== 'None') {
|
||||||
|
query += '\n ' + this.buildMetricColumn(target);
|
||||||
|
}
|
||||||
query += this.buildValueColumns(target);
|
query += this.buildValueColumns(target);
|
||||||
|
|
||||||
query += '\nFROM ' + target.schema + '.' + target.table;
|
query += '\nFROM ' + target.schema + '.' + target.table;
|
||||||
|
@ -299,7 +299,7 @@ export class PostgresQueryCtrl extends QueryCtrl {
|
|||||||
getWhereOptions() {
|
getWhereOptions() {
|
||||||
var options = [];
|
var options = [];
|
||||||
options.push(this.uiSegmentSrv.newSegment({ type: 'macro', value: '$__timeFilter' }));
|
options.push(this.uiSegmentSrv.newSegment({ type: 'macro', value: '$__timeFilter' }));
|
||||||
options.push(this.uiSegmentSrv.newSegment({ type: 'macro', value: '$__unixEpochFilter' }));
|
// options.push(this.uiSegmentSrv.newSegment({ type: 'macro', value: '$__unixEpochFilter' }));
|
||||||
options.push(this.uiSegmentSrv.newSegment({ type: 'expression', value: 'Expression' }));
|
options.push(this.uiSegmentSrv.newSegment({ type: 'expression', value: 'Expression' }));
|
||||||
return this.$q.when(options);
|
return this.$q.when(options);
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,100 @@
|
|||||||
|
import PostgresQuery from '../postgres_query';
|
||||||
|
|
||||||
|
describe('PostgresQuery', function() {
|
||||||
|
describe('When initializing', function() {
|
||||||
|
it('should not be in SQL mode', function() {
|
||||||
|
let query = new PostgresQuery({}, templateSrv);
|
||||||
|
expect(query.target.rawQuery).toBe(false);
|
||||||
|
});
|
||||||
|
it('should be in SQL mode for pre query builder queries', function() {
|
||||||
|
let query = new PostgresQuery({ rawSql: 'SELECT 1' }, templateSrv);
|
||||||
|
expect(query.target.rawQuery).toBe(true);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('When generating time column SQL', function() {
|
||||||
|
let query = new PostgresQuery({}, templateSrv);
|
||||||
|
|
||||||
|
expect(query.buildTimeColumn({ timeColumn: 'time' })).toBe('time AS "time"');
|
||||||
|
expect(query.buildTimeColumn({ timeColumn: '"time"' })).toBe('"time" AS "time"');
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('When generating time column SQL with group by time', function() {
|
||||||
|
let query = new PostgresQuery(
|
||||||
|
{ timeColumn: 'time', groupBy: [{ type: 'time', params: ['5m', 'none'] }] },
|
||||||
|
templateSrv
|
||||||
|
);
|
||||||
|
expect(query.buildTimeColumn(query.target)).toBe('$__timeGroup(time,5m)');
|
||||||
|
|
||||||
|
query = new PostgresQuery({ timeColumn: 'time', groupBy: [{ type: 'time', params: ['5m', 'NULL'] }] }, templateSrv);
|
||||||
|
expect(query.buildTimeColumn(query.target)).toBe('$__timeGroup(time,5m,NULL)');
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('When generating metric column SQL', function() {
|
||||||
|
let query = new PostgresQuery({}, templateSrv);
|
||||||
|
|
||||||
|
expect(query.buildMetricColumn({ metricColumn: 'host' })).toBe('host AS metric');
|
||||||
|
expect(query.buildMetricColumn({ metricColumn: '"host"' })).toBe('"host" AS metric');
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('When generating value column SQL', function() {
|
||||||
|
let query = new PostgresQuery({}, templateSrv);
|
||||||
|
|
||||||
|
let column = [{ type: 'column', params: ['value'] }];
|
||||||
|
expect(query.buildValueColumn(query.target, column)).toBe('value');
|
||||||
|
column = [{ type: 'column', params: ['value'] }, { type: 'alias', params: ['alias'] }];
|
||||||
|
expect(query.buildValueColumn(query.target, column)).toBe('value AS "alias"');
|
||||||
|
column = [
|
||||||
|
{ type: 'column', params: ['v'] },
|
||||||
|
{ type: 'alias', params: ['a'] },
|
||||||
|
{ type: 'aggregate', params: ['max'] },
|
||||||
|
];
|
||||||
|
expect(query.buildValueColumn(query.target, column)).toBe('max(v) AS "a"');
|
||||||
|
column = [
|
||||||
|
{ type: 'column', params: ['v'] },
|
||||||
|
{ type: 'alias', params: ['a'] },
|
||||||
|
{ type: 'special', params: ['increase'] },
|
||||||
|
];
|
||||||
|
expect(query.buildValueColumn(query.target, column)).toBe('v - lag(v) OVER () AS "a"');
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('When generating WHERE clause', function() {
|
||||||
|
let query = new PostgresQuery({ where: [] }, templateSrv);
|
||||||
|
let target;
|
||||||
|
|
||||||
|
expect(query.buildWhereClause(query.target)).toBe('');
|
||||||
|
target = { where: [{ type: 'macro', name: '$__timeFilter' }], timeColumn: 't' };
|
||||||
|
expect(query.buildWhereClause(target)).toBe('\nWHERE\n $__timeFilter(t)');
|
||||||
|
target = { where: [{ type: 'expression', params: ['v', '=', '1'] }], timeColumn: 't' };
|
||||||
|
expect(query.buildWhereClause(target)).toBe('\nWHERE\n v = 1');
|
||||||
|
target = {
|
||||||
|
where: [{ type: 'macro', name: '$__timeFilter' }, { type: 'expression', params: ['v', '=', '1'] }],
|
||||||
|
timeColumn: 't',
|
||||||
|
};
|
||||||
|
expect(query.buildWhereClause(target)).toBe('\nWHERE\n $__timeFilter(t) AND\n v = 1');
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('When generating GROUP BY clause', function() {
|
||||||
|
let query = new PostgresQuery({ groupBy: [] }, templateSrv);
|
||||||
|
let target;
|
||||||
|
|
||||||
|
expect(query.buildGroupByClause(query.target)).toBe('');
|
||||||
|
target = { groupBy: [{ type: 'time', params: ['5m'] }], metricColumn: 'None' };
|
||||||
|
expect(query.buildGroupByClause(target)).toBe('\nGROUP BY 1');
|
||||||
|
target = { groupBy: [{ type: 'time', params: ['5m'] }], metricColumn: 'm' };
|
||||||
|
expect(query.buildGroupByClause(target)).toBe('\nGROUP BY 1,2');
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('When generating complete statement', function() {
|
||||||
|
let target = {
|
||||||
|
timeColumn: 't',
|
||||||
|
schema: 'public',
|
||||||
|
table: 'table',
|
||||||
|
select: [[{ type: 'column', params: ['value'] }]],
|
||||||
|
};
|
||||||
|
let result = 'SELECT\n t AS "time",\n value\nFROM public.table\nORDER BY 1';
|
||||||
|
let query = new PostgresQuery(target, templateSrv);
|
||||||
|
|
||||||
|
expect(query.buildQuery(query.target)).toBe(result);
|
||||||
|
});
|
||||||
|
});
|
Loading…
Reference in New Issue
Block a user