transformers.jest.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. import {transformers, transformDataToTable} from '../transformers';
  2. describe('when transforming time series table', () => {
  3. var table;
  4. describe('given 2 time series', () => {
  5. var time = new Date().getTime();
  6. var timeSeries = [
  7. {
  8. target: 'series1',
  9. datapoints: [[12.12, time], [14.44, time+1]],
  10. },
  11. {
  12. target: 'series2',
  13. datapoints: [[16.12, time]],
  14. }
  15. ];
  16. describe('timeseries_to_rows', () => {
  17. var panel = {
  18. transform: 'timeseries_to_rows',
  19. sort: {col: 0, desc: true},
  20. };
  21. beforeEach(() => {
  22. table = transformDataToTable(timeSeries, panel);
  23. });
  24. it('should return 3 rows', () => {
  25. expect(table.rows.length).toBe(3);
  26. expect(table.rows[0][1]).toBe('series1');
  27. expect(table.rows[1][1]).toBe('series1');
  28. expect(table.rows[2][1]).toBe('series2');
  29. expect(table.rows[0][2]).toBe(12.12);
  30. });
  31. it('should return 3 rows', () => {
  32. expect(table.columns.length).toBe(3);
  33. expect(table.columns[0].text).toBe('Time');
  34. expect(table.columns[1].text).toBe('Metric');
  35. expect(table.columns[2].text).toBe('Value');
  36. });
  37. });
  38. describe('timeseries_to_columns', () => {
  39. var panel = {
  40. transform: 'timeseries_to_columns'
  41. };
  42. beforeEach(() => {
  43. table = transformDataToTable(timeSeries, panel);
  44. });
  45. it ('should return 3 columns', () => {
  46. expect(table.columns.length).toBe(3);
  47. expect(table.columns[0].text).toBe('Time');
  48. expect(table.columns[1].text).toBe('series1');
  49. expect(table.columns[2].text).toBe('series2');
  50. });
  51. it ('should return 2 rows', () => {
  52. expect(table.rows.length).toBe(2);
  53. expect(table.rows[0][1]).toBe(12.12);
  54. expect(table.rows[0][2]).toBe(16.12);
  55. });
  56. it ('should be undefined when no value for timestamp', () => {
  57. expect(table.rows[1][2]).toBe(undefined);
  58. });
  59. });
  60. describe('timeseries_aggregations', () => {
  61. var panel = {
  62. transform: 'timeseries_aggregations',
  63. sort: {col: 0, desc: true},
  64. columns: [{text: 'Max', value: 'max'}, {text: 'Min', value: 'min'}]
  65. };
  66. beforeEach(() => {
  67. table = transformDataToTable(timeSeries, panel);
  68. });
  69. it('should return 2 rows', () => {
  70. expect(table.rows.length).toBe(2);
  71. expect(table.rows[0][0]).toBe('series1');
  72. expect(table.rows[0][1]).toBe(14.44);
  73. expect(table.rows[0][2]).toBe(12.12);
  74. });
  75. it('should return 2 columns', () => {
  76. expect(table.columns.length).toBe(3);
  77. expect(table.columns[0].text).toBe('Metric');
  78. expect(table.columns[1].text).toBe('Max');
  79. expect(table.columns[2].text).toBe('Min');
  80. });
  81. });
  82. describe('JSON Data', () => {
  83. var panel = {
  84. transform: 'json',
  85. columns: [
  86. {text: 'Timestamp', value: 'timestamp'},
  87. {text: 'Message', value: 'message'},
  88. {text: 'nested.level2', value: 'nested.level2'},
  89. ]
  90. };
  91. var rawData = [
  92. {
  93. type: 'docs',
  94. datapoints: [
  95. {
  96. timestamp: 'time',
  97. message: 'message',
  98. nested: {
  99. level2: 'level2-value'
  100. }
  101. }
  102. ]
  103. }
  104. ];
  105. describe('getColumns', function() {
  106. it('should return nested properties', function() {
  107. var columns = transformers['json'].getColumns(rawData);
  108. expect(columns[0].text).toBe('timestamp');
  109. expect(columns[1].text).toBe('message');
  110. expect(columns[2].text).toBe('nested.level2');
  111. });
  112. });
  113. describe('transform', function() {
  114. beforeEach(() => {
  115. table = transformDataToTable(rawData, panel);
  116. });
  117. it ('should return 2 columns', () => {
  118. expect(table.columns.length).toBe(3);
  119. expect(table.columns[0].text).toBe('Timestamp');
  120. expect(table.columns[1].text).toBe('Message');
  121. expect(table.columns[2].text).toBe('nested.level2');
  122. });
  123. it ('should return 2 rows', () => {
  124. expect(table.rows.length).toBe(1);
  125. expect(table.rows[0][0]).toBe('time');
  126. expect(table.rows[0][1]).toBe('message');
  127. expect(table.rows[0][2]).toBe('level2-value');
  128. });
  129. });
  130. });
  131. describe('Annnotations', () => {
  132. var panel = {transform: 'annotations'};
  133. var rawData = {
  134. annotations: [
  135. {
  136. time: 1000,
  137. text: 'hej',
  138. tags: ['tags', 'asd'],
  139. title: 'title',
  140. }
  141. ]
  142. };
  143. beforeEach(() => {
  144. table = transformDataToTable(rawData, panel);
  145. });
  146. it ('should return 4 columns', () => {
  147. expect(table.columns.length).toBe(4);
  148. expect(table.columns[0].text).toBe('Time');
  149. expect(table.columns[1].text).toBe('Title');
  150. expect(table.columns[2].text).toBe('Text');
  151. expect(table.columns[3].text).toBe('Tags');
  152. });
  153. it ('should return 1 rows', () => {
  154. expect(table.rows.length).toBe(1);
  155. expect(table.rows[0][0]).toBe(1000);
  156. });
  157. });
  158. });
  159. });