datasource.jest.ts 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. import moment from 'moment';
  2. import { TemplateSrv } from 'app/features/templating/template_srv';
  3. import Datasource from '../datasource';
  4. describe('InfluxDB (IFQL)', () => {
  5. const templateSrv = new TemplateSrv();
  6. const ds = new Datasource({ url: '' }, {}, templateSrv);
  7. const DEFAULT_OPTIONS = {
  8. rangeRaw: { to: 'now', from: 'now - 3h' },
  9. scopedVars: {},
  10. targets: [],
  11. };
  12. describe('prepareQueryTarget()', () => {
  13. let target: any;
  14. it('replaces $range variable', () => {
  15. target = ds.prepareQueryTarget({ query: 'from(db: "test") |> range($range)' }, DEFAULT_OPTIONS);
  16. expect(target.query).toBe('from(db: "test") |> range(start: -3h)');
  17. });
  18. it('replaces $range variable with custom dates', () => {
  19. const to = moment();
  20. const from = moment().subtract(1, 'hours');
  21. target = ds.prepareQueryTarget(
  22. { query: 'from(db: "test") |> range($range)' },
  23. {
  24. ...DEFAULT_OPTIONS,
  25. rangeRaw: { to, from },
  26. }
  27. );
  28. const start = from.toISOString();
  29. const stop = to.toISOString();
  30. expect(target.query).toBe(`from(db: "test") |> range(start: ${start}, stop: ${stop})`);
  31. });
  32. });
  33. });