file_export.test.ts 5.1 KB

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