response_parser.jest.ts 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import {
  2. getNameFromRecord,
  3. getTableModelFromResult,
  4. getTimeSeriesFromResult,
  5. parseResults,
  6. parseValue,
  7. } from '../response_parser';
  8. import response from './sample_response_csv';
  9. describe('influxdb ifql response parser', () => {
  10. describe('parseResults()', () => {
  11. it('expects three results', () => {
  12. const results = parseResults(response);
  13. expect(results.length).toBe(2);
  14. });
  15. });
  16. describe('getTableModelFromResult()', () => {
  17. it('expects a table model', () => {
  18. const results = parseResults(response);
  19. const table = getTableModelFromResult(results[0]);
  20. expect(table.columns.length).toBe(6);
  21. expect(table.rows.length).toBe(300);
  22. });
  23. });
  24. describe('getTimeSeriesFromResult()', () => {
  25. it('expects time series', () => {
  26. const results = parseResults(response);
  27. const series = getTimeSeriesFromResult(results[0]);
  28. expect(series.length).toBe(50);
  29. expect(series[0].datapoints.length).toBe(6);
  30. });
  31. });
  32. describe('getNameFromRecord()', () => {
  33. it('expects name based on measurements and tags', () => {
  34. const record = {
  35. '': '',
  36. result: '',
  37. table: '0',
  38. _start: '2018-06-02T06:35:25.651942602Z',
  39. _stop: '2018-06-02T07:35:25.651942602Z',
  40. _time: '2018-06-02T06:35:31Z',
  41. _value: '0',
  42. _field: 'usage_guest',
  43. _measurement: 'cpu',
  44. cpu: 'cpu-total',
  45. host: 'kenobi-3.local',
  46. };
  47. expect(getNameFromRecord(record)).toBe('cpu usage_guest cpu=cpu-total host=kenobi-3.local');
  48. });
  49. });
  50. describe('parseValue()', () => {
  51. it('parses a number', () => {
  52. expect(parseValue('42.3')).toBe(42.3);
  53. });
  54. it('parses a non-number to null', () => {
  55. expect(parseValue('foo')).toBe(null);
  56. });
  57. });
  58. });