file_export.test.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import * as fileExport from '../utils/file_export';
  2. import { beforeEach, expect } from 'test/lib/common';
  3. import { TableData } from '@grafana/data';
  4. describe('file_export', () => {
  5. const ctx: any = {};
  6. beforeEach(() => {
  7. ctx.seriesList = [
  8. {
  9. alias: 'series_1',
  10. datapoints: [
  11. [1, 1500026100000],
  12. [2, 1500026200000],
  13. [null, 1500026300000],
  14. [null, 1500026400000],
  15. [null, 1500026500000],
  16. [6, 1500026600000],
  17. ],
  18. },
  19. {
  20. alias: 'series_2',
  21. datapoints: [[11, 1500026100000], [12, 1500026200000], [13, 1500026300000], [15, 1500026500000]],
  22. },
  23. ];
  24. ctx.timeFormat = 'X'; // Unix timestamp (seconds)
  25. });
  26. describe('when exporting series as rows', () => {
  27. it('should export points in proper order', () => {
  28. const text = fileExport.convertSeriesListToCsv(ctx.seriesList, { dateTimeFormat: ctx.timeFormat });
  29. const expectedText =
  30. '"Series";"Time";"Value"\r\n' +
  31. '"series_1";"1500026100";1\r\n' +
  32. '"series_1";"1500026200";2\r\n' +
  33. '"series_1";"1500026300";null\r\n' +
  34. '"series_1";"1500026400";null\r\n' +
  35. '"series_1";"1500026500";null\r\n' +
  36. '"series_1";"1500026600";6\r\n' +
  37. '"series_2";"1500026100";11\r\n' +
  38. '"series_2";"1500026200";12\r\n' +
  39. '"series_2";"1500026300";13\r\n' +
  40. '"series_2";"1500026500";15';
  41. expect(text).toBe(expectedText);
  42. });
  43. });
  44. describe('when exporting series as columns', () => {
  45. it('should export points in proper order', () => {
  46. const text = fileExport.convertSeriesListToCsvColumns(ctx.seriesList, { dateTimeFormat: ctx.timeFormat });
  47. const expectedText =
  48. '"Time";"series_1";"series_2"\r\n' +
  49. '"1500026100";1;11\r\n' +
  50. '"1500026200";2;12\r\n' +
  51. '"1500026300";null;13\r\n' +
  52. '"1500026400";null;null\r\n' +
  53. '"1500026500";null;15\r\n' +
  54. '"1500026600";6;null';
  55. expect(text).toBe(expectedText);
  56. });
  57. it('should not modify series.datapoints', () => {
  58. const expectedSeries1DataPoints = ctx.seriesList[0].datapoints.slice();
  59. const expectedSeries2DataPoints = ctx.seriesList[1].datapoints.slice();
  60. fileExport.convertSeriesListToCsvColumns(ctx.seriesList, { dateTimeFormat: ctx.timeFormat });
  61. expect(expectedSeries1DataPoints).toEqual(ctx.seriesList[0].datapoints);
  62. expect(expectedSeries2DataPoints).toEqual(ctx.seriesList[1].datapoints);
  63. });
  64. });
  65. describe('when exporting table data to csv', () => {
  66. it('should properly escape special characters and quote all string values', () => {
  67. const inputTable: any = {
  68. columns: [
  69. { title: 'integer_value' },
  70. { text: 'string_value' },
  71. { title: 'float_value' },
  72. { text: 'boolean_value' },
  73. ],
  74. rows: [
  75. [123, 'some_string', 1.234, true],
  76. [1000, 'some_string', 1.234567891, true],
  77. [0o765, 'some string with " in the middle', 1e-2, false],
  78. [0o765, 'some string with "" in the middle', 1e-2, false],
  79. [0o765, 'some string with """ in the middle', 1e-2, false],
  80. [0o765, '"some string with " at the beginning', 1e-2, false],
  81. [0o765, 'some string with " at the end"', 1e-2, false],
  82. [0x123, 'some string with \n in the middle', 10.01, false],
  83. [0b1011, 'some string with ; in the middle', -12.34, true],
  84. [123, 'some string with ;; in the middle', -12.34, true],
  85. [1234, '=a bogus formula ', '-and another', '+another', '@ref'],
  86. ],
  87. };
  88. const returnedText = fileExport.convertTableDataToCsv(inputTable, false);
  89. const expectedText =
  90. '"integer_value";"string_value";"float_value";"boolean_value"\r\n' +
  91. '123;"some_string";1.234;true\r\n' +
  92. '1000;"some_string";1.234567891;true\r\n' +
  93. '501;"some string with "" in the middle";0.01;false\r\n' +
  94. '501;"some string with """" in the middle";0.01;false\r\n' +
  95. '501;"some string with """""" in the middle";0.01;false\r\n' +
  96. '501;"""some string with "" at the beginning";0.01;false\r\n' +
  97. '501;"some string with "" at the end""";0.01;false\r\n' +
  98. '291;"some string with \n in the middle";10.01;false\r\n' +
  99. '11;"some string with ; in the middle";-12.34;true\r\n' +
  100. '123;"some string with ;; in the middle";-12.34;true\r\n' +
  101. '1234;"\'=a bogus formula";"\'-and another";"\'+another";"\'@ref"';
  102. expect(returnedText).toBe(expectedText);
  103. });
  104. it('should decode HTML encoded characters', () => {
  105. const inputTable: TableData = {
  106. columns: [{ text: 'string_value' }],
  107. rows: [
  108. ['"&ä'],
  109. ['<strong>&quot;some html&quot;</strong>'],
  110. ['<a href="http://something/index.html">some text</a>'],
  111. ],
  112. };
  113. const returnedText = fileExport.convertTableDataToCsv(inputTable, false);
  114. const expectedText =
  115. '"string_value"\r\n' +
  116. '"""&ä"\r\n' +
  117. '"<strong>""some html""</strong>"\r\n' +
  118. '"<a href=""http://something/index.html"">some text</a>"';
  119. expect(returnedText).toBe(expectedText);
  120. });
  121. });
  122. });