table_model.test.ts 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. import TableModel, { mergeTablesIntoModel } from 'app/core/table_model';
  2. describe('when sorting table desc', () => {
  3. let table;
  4. const panel = {
  5. sort: { col: 0, desc: true },
  6. };
  7. beforeEach(() => {
  8. table = new TableModel();
  9. table.columns = [{}, {}];
  10. table.rows = [[100, 12], [105, 10], [103, 11]];
  11. table.sort(panel.sort);
  12. });
  13. it('should sort by time', () => {
  14. expect(table.rows[0][0]).toBe(105);
  15. expect(table.rows[1][0]).toBe(103);
  16. expect(table.rows[2][0]).toBe(100);
  17. });
  18. it('should mark column being sorted', () => {
  19. expect(table.columns[0].sort).toBe(true);
  20. expect(table.columns[0].desc).toBe(true);
  21. });
  22. });
  23. describe('when sorting table asc', () => {
  24. let table;
  25. const panel = {
  26. sort: { col: 1, desc: false },
  27. };
  28. beforeEach(() => {
  29. table = new TableModel();
  30. table.columns = [{}, {}];
  31. table.rows = [[100, 11], [105, 15], [103, 10]];
  32. table.sort(panel.sort);
  33. });
  34. it('should sort by time', () => {
  35. expect(table.rows[0][1]).toBe(10);
  36. expect(table.rows[1][1]).toBe(11);
  37. expect(table.rows[2][1]).toBe(15);
  38. });
  39. });
  40. describe('when sorting with nulls', () => {
  41. let table;
  42. let values;
  43. beforeEach(() => {
  44. table = new TableModel();
  45. table.columns = [{}, {}];
  46. table.rows = [[42, ''], [19, 'a'], [null, 'b'], [0, 'd'], [null, null], [2, 'c'], [0, null], [-8, '']];
  47. });
  48. it('numbers with nulls at end with asc sort', () => {
  49. table.sort({ col: 0, desc: false });
  50. values = table.rows.map(row => row[0]);
  51. expect(values).toEqual([-8, 0, 0, 2, 19, 42, null, null]);
  52. });
  53. it('numbers with nulls at start with desc sort', () => {
  54. table.sort({ col: 0, desc: true });
  55. values = table.rows.map(row => row[0]);
  56. expect(values).toEqual([null, null, 42, 19, 2, 0, 0, -8]);
  57. });
  58. it('strings with nulls at end with asc sort', () => {
  59. table.sort({ col: 1, desc: false });
  60. values = table.rows.map(row => row[1]);
  61. expect(values).toEqual(['', '', 'a', 'b', 'c', 'd', null, null]);
  62. });
  63. it('strings with nulls at start with desc sort', () => {
  64. table.sort({ col: 1, desc: true });
  65. values = table.rows.map(row => row[1]);
  66. expect(values).toEqual([null, null, 'd', 'c', 'b', 'a', '', '']);
  67. });
  68. });
  69. describe('mergeTables', () => {
  70. const time = new Date().getTime();
  71. const singleTable = new TableModel({
  72. type: 'table',
  73. columns: [{ text: 'Time' }, { text: 'Label Key 1' }, { text: 'Value' }],
  74. rows: [[time, 'Label Value 1', 42]],
  75. });
  76. const multipleTablesSameColumns = [
  77. new TableModel({
  78. type: 'table',
  79. columns: [{ text: 'Time' }, { text: 'Label Key 1' }, { text: 'Label Key 2' }, { text: 'Value #A' }],
  80. rows: [[time, 'Label Value 1', 'Label Value 2', 42]],
  81. }),
  82. new TableModel({
  83. type: 'table',
  84. columns: [{ text: 'Time' }, { text: 'Label Key 1' }, { text: 'Label Key 2' }, { text: 'Value #B' }],
  85. rows: [[time, 'Label Value 1', 'Label Value 2', 13]],
  86. }),
  87. new TableModel({
  88. type: 'table',
  89. columns: [{ text: 'Time' }, { text: 'Label Key 1' }, { text: 'Label Key 2' }, { text: 'Value #C' }],
  90. rows: [[time, 'Label Value 1', 'Label Value 2', 4]],
  91. }),
  92. new TableModel({
  93. type: 'table',
  94. columns: [{ text: 'Time' }, { text: 'Label Key 1' }, { text: 'Label Key 2' }, { text: 'Value #C' }],
  95. rows: [[time, 'Label Value 1', 'Label Value 2', 7]],
  96. }),
  97. ];
  98. const multipleTablesDifferentColumns = [
  99. new TableModel({
  100. type: 'table',
  101. columns: [{ text: 'Time' }, { text: 'Label Key 1' }, { text: 'Value #A' }],
  102. rows: [[time, 'Label Value 1', 42]],
  103. }),
  104. new TableModel({
  105. type: 'table',
  106. columns: [{ text: 'Time' }, { text: 'Label Key 2' }, { text: 'Value #B' }],
  107. rows: [[time, 'Label Value 2', 13]],
  108. }),
  109. new TableModel({
  110. type: 'table',
  111. columns: [{ text: 'Time' }, { text: 'Label Key 1' }, { text: 'Value #C' }],
  112. rows: [[time, 'Label Value 3', 7]],
  113. }),
  114. ];
  115. it('should return the single table as is', () => {
  116. const table = mergeTablesIntoModel(new TableModel(), singleTable);
  117. expect(table.columns.length).toBe(3);
  118. expect(table.columns[0].text).toBe('Time');
  119. expect(table.columns[1].text).toBe('Label Key 1');
  120. expect(table.columns[2].text).toBe('Value');
  121. });
  122. it('should return the union of columns for multiple tables', () => {
  123. const table = mergeTablesIntoModel(new TableModel(), ...multipleTablesSameColumns);
  124. expect(table.columns.length).toBe(6);
  125. expect(table.columns[0].text).toBe('Time');
  126. expect(table.columns[1].text).toBe('Label Key 1');
  127. expect(table.columns[2].text).toBe('Label Key 2');
  128. expect(table.columns[3].text).toBe('Value #A');
  129. expect(table.columns[4].text).toBe('Value #B');
  130. expect(table.columns[5].text).toBe('Value #C');
  131. });
  132. it('should return 1 row for a single table', () => {
  133. const table = mergeTablesIntoModel(new TableModel(), singleTable);
  134. expect(table.rows.length).toBe(1);
  135. expect(table.rows[0][0]).toBe(time);
  136. expect(table.rows[0][1]).toBe('Label Value 1');
  137. expect(table.rows[0][2]).toBe(42);
  138. });
  139. it('should return 2 rows for a multiple tables with same column values plus one extra row', () => {
  140. const table = mergeTablesIntoModel(new TableModel(), ...multipleTablesSameColumns);
  141. expect(table.rows.length).toBe(2);
  142. expect(table.rows[0][0]).toBe(time);
  143. expect(table.rows[0][1]).toBe('Label Value 1');
  144. expect(table.rows[0][2]).toBe('Label Value 2');
  145. expect(table.rows[0][3]).toBe(42);
  146. expect(table.rows[0][4]).toBe(13);
  147. expect(table.rows[0][5]).toBe(4);
  148. expect(table.rows[1][0]).toBe(time);
  149. expect(table.rows[1][1]).toBe('Label Value 1');
  150. expect(table.rows[1][2]).toBe('Label Value 2');
  151. expect(table.rows[1][3]).toBeUndefined();
  152. expect(table.rows[1][4]).toBeUndefined();
  153. expect(table.rows[1][5]).toBe(7);
  154. });
  155. it('should return 2 rows for multiple tables with different column values', () => {
  156. const table = mergeTablesIntoModel(new TableModel(), ...multipleTablesDifferentColumns);
  157. expect(table.rows.length).toBe(2);
  158. expect(table.columns.length).toBe(6);
  159. expect(table.rows[0][0]).toBe(time);
  160. expect(table.rows[0][1]).toBe('Label Value 1');
  161. expect(table.rows[0][2]).toBe(42);
  162. expect(table.rows[0][3]).toBe('Label Value 2');
  163. expect(table.rows[0][4]).toBe(13);
  164. expect(table.rows[0][5]).toBeUndefined();
  165. expect(table.rows[1][0]).toBe(time);
  166. expect(table.rows[1][1]).toBe('Label Value 3');
  167. expect(table.rows[1][2]).toBeUndefined();
  168. expect(table.rows[1][3]).toBeUndefined();
  169. expect(table.rows[1][4]).toBeUndefined();
  170. expect(table.rows[1][5]).toBe(7);
  171. });
  172. });