The open and composable observability and data visualization platform. Visualize metrics, logs, and traces from multiple sources like Prometheus, Loki, Elasticsearch, InfluxDB, Postgres and many more.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 
grafana/public/app/plugins/datasource/prometheus/querybuilder/shared/parsingUtils.test.ts

41 lines
1.6 KiB

import { getLeftMostChild, getString, replaceVariables } from './parsingUtils';
import { parser } from 'lezer-promql';
describe('getLeftMostChild', () => {
it('return left most child', () => {
const tree = parser.parse('sum_over_time(foo{bar="baz"}[5m])');
const child = getLeftMostChild(tree.topNode);
expect(child).toBeDefined();
expect(child!.name).toBe('SumOverTime');
});
});
describe('replaceVariables', () => {
it('should replace variables', () => {
expect(replaceVariables('sum_over_time([[metric_var]]{bar="${app}"}[$__interval])')).toBe(
'sum_over_time(__V_1__metric_var__V__{bar="__V_2__app__V__"}[__V_0____interval__V__])'
);
});
});
describe('getString', () => {
it('should return correct string representation of the node', () => {
const expr = 'sum_over_time(foo{bar="baz"}[5m])';
const tree = parser.parse(expr);
const child = getLeftMostChild(tree.topNode);
expect(getString(expr, child)).toBe('sum_over_time');
});
it('should return string with correct variables', () => {
const expr = 'sum_over_time(__V_1__metric_var__V__{bar="__V_2__app__V__"}[__V_0____interval__V__])';
const tree = parser.parse(expr);
expect(getString(expr, tree.topNode)).toBe('sum_over_time([[metric_var]]{bar="${app}"}[$__interval])');
});
it('is symmetrical with replaceVariables', () => {
const expr = 'sum_over_time([[metric_var]]{bar="${app}"}[$__interval])';
const replaced = replaceVariables(expr);
const tree = parser.parse(replaced);
expect(getString(replaced, tree.topNode)).toBe(expr);
});
});