response_parser.jest.ts 2.1 KB

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