rowsToFields.test.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. import { toDataFrame, FieldType } from '@grafana/data';
  2. import { rowsToFields } from './rowsToFields';
  3. describe('Rows to fields', () => {
  4. it('Will extract min & max from field', () => {
  5. const input = toDataFrame({
  6. fields: [
  7. { name: 'Name', type: FieldType.string, values: ['Temperature', 'Pressure'] },
  8. { name: 'Value', type: FieldType.number, values: [10, 200] },
  9. { name: 'Unit', type: FieldType.string, values: ['degree', 'pressurebar'] },
  10. { name: 'Miiin', type: FieldType.number, values: [3, 100] },
  11. { name: 'max', type: FieldType.string, values: [15, 200] },
  12. ],
  13. });
  14. const result = rowsToFields(
  15. {
  16. mappings: [
  17. {
  18. fieldName: 'Miiin',
  19. handlerKey: 'min',
  20. },
  21. ],
  22. },
  23. input
  24. );
  25. expect(result).toMatchInlineSnapshot(`
  26. Object {
  27. "fields": Array [
  28. Object {
  29. "config": Object {
  30. "max": 15,
  31. "min": 3,
  32. "unit": "degree",
  33. },
  34. "labels": Object {},
  35. "name": "Temperature",
  36. "type": "number",
  37. "values": Array [
  38. 10,
  39. ],
  40. },
  41. Object {
  42. "config": Object {
  43. "max": 200,
  44. "min": 100,
  45. "unit": "pressurebar",
  46. },
  47. "labels": Object {},
  48. "name": "Pressure",
  49. "type": "number",
  50. "values": Array [
  51. 200,
  52. ],
  53. },
  54. ],
  55. "length": 1,
  56. }
  57. `);
  58. });
  59. it('Can handle custom name and value field mapping', () => {
  60. const input = toDataFrame({
  61. fields: [
  62. { name: 'Name', type: FieldType.string, values: ['Ignore'] },
  63. { name: 'SensorName', type: FieldType.string, values: ['Temperature'] },
  64. { name: 'Value', type: FieldType.number, values: [10] },
  65. { name: 'SensorReading', type: FieldType.number, values: [100] },
  66. ],
  67. });
  68. const result = rowsToFields(
  69. {
  70. mappings: [
  71. { fieldName: 'SensorName', handlerKey: 'field.name' },
  72. { fieldName: 'SensorReading', handlerKey: 'field.value' },
  73. ],
  74. },
  75. input
  76. );
  77. expect(result.fields[0].name).toBe('Temperature');
  78. expect(result.fields[0].config).toEqual({});
  79. expect(result.fields[0].values.get(0)).toBe(100);
  80. });
  81. it('Can handle colors', () => {
  82. const input = toDataFrame({
  83. fields: [
  84. { name: 'Name', type: FieldType.string, values: ['Temperature'] },
  85. { name: 'Value', type: FieldType.number, values: [10] },
  86. { name: 'Color', type: FieldType.string, values: ['blue'] },
  87. ],
  88. });
  89. const result = rowsToFields({}, input);
  90. expect(result.fields[0].config.color?.fixedColor).toBe('blue');
  91. });
  92. it('Can handle thresholds', () => {
  93. const input = toDataFrame({
  94. fields: [
  95. { name: 'Name', type: FieldType.string, values: ['Temperature'] },
  96. { name: 'Value', type: FieldType.number, values: [10] },
  97. { name: 'threshold1', type: FieldType.string, values: [30] },
  98. { name: 'threshold2', type: FieldType.string, values: [500] },
  99. ],
  100. });
  101. const result = rowsToFields({}, input);
  102. expect(result.fields[0].config.thresholds?.steps[1].value).toBe(30);
  103. });
  104. it('Will extract other string fields to labels', () => {
  105. const input = toDataFrame({
  106. fields: [
  107. { name: 'Name', type: FieldType.string, values: ['Temperature', 'Pressure'] },
  108. { name: 'Value', type: FieldType.number, values: [10, 200] },
  109. { name: 'City', type: FieldType.string, values: ['Stockholm', 'New York'] },
  110. ],
  111. });
  112. const result = rowsToFields({}, input);
  113. expect(result.fields[0].labels).toEqual({ City: 'Stockholm' });
  114. expect(result.fields[1].labels).toEqual({ City: 'New York' });
  115. });
  116. it('Can ignore field as auto picked for value or name', () => {
  117. const input = toDataFrame({
  118. fields: [
  119. { name: 'Name', type: FieldType.string, values: ['Temperature'] },
  120. { name: 'Value', type: FieldType.number, values: [10] },
  121. { name: 'City', type: FieldType.string, values: ['Stockholm'] },
  122. { name: 'Value2', type: FieldType.number, values: [20] },
  123. ],
  124. });
  125. const result = rowsToFields(
  126. {
  127. mappings: [
  128. { fieldName: 'Name', handlerKey: '__ignore' },
  129. { fieldName: 'Value', handlerKey: '__ignore' },
  130. ],
  131. },
  132. input
  133. );
  134. expect(result.fields[0].name).toEqual('Stockholm');
  135. expect(result.fields[0].values.get(0)).toEqual(20);
  136. });
  137. it('Can handle number fields as name field', () => {
  138. const input = toDataFrame({
  139. fields: [
  140. { name: 'SensorID', type: FieldType.number, values: [10, 20, 30] },
  141. { name: 'Value', type: FieldType.number, values: [1, 2, 3] },
  142. ],
  143. });
  144. const result = rowsToFields(
  145. {
  146. mappings: [
  147. { fieldName: 'SensorID', handlerKey: 'field.name' },
  148. { fieldName: 'Value', handlerKey: 'field.value' },
  149. ],
  150. },
  151. input
  152. );
  153. expect(result.fields[0].name).toEqual('10');
  154. expect(result.fields[0].values.get(0)).toEqual(1);
  155. });
  156. });