table_model.test.ts 6.7 KB

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