response_parser.jest.ts 2.5 KB

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