mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Fix/improved csv output (#11740)
* fix: initial cleanup and implementation * feat: finish special character escaping * feat: updates fileExport to generate RFC-4180 compliant CSV * chore: replace html decoder with the lodash version and final cleanup * fix: restore character html decoding
This commit is contained in:
committed by
Torkel Ödegaard
parent
871b85f199
commit
5d54bc00e1
@@ -30,17 +30,17 @@ describe('file_export', () => {
|
||||
it('should export points in proper order', () => {
|
||||
let text = fileExport.convertSeriesListToCsv(ctx.seriesList, ctx.timeFormat);
|
||||
const expectedText =
|
||||
'Series;Time;Value\n' +
|
||||
'series_1;1500026100;1\n' +
|
||||
'series_1;1500026200;2\n' +
|
||||
'series_1;1500026300;null\n' +
|
||||
'series_1;1500026400;null\n' +
|
||||
'series_1;1500026500;null\n' +
|
||||
'series_1;1500026600;6\n' +
|
||||
'series_2;1500026100;11\n' +
|
||||
'series_2;1500026200;12\n' +
|
||||
'series_2;1500026300;13\n' +
|
||||
'series_2;1500026500;15\n';
|
||||
'"Series";"Time";"Value"\r\n' +
|
||||
'"series_1";"1500026100";1\r\n' +
|
||||
'"series_1";"1500026200";2\r\n' +
|
||||
'"series_1";"1500026300";null\r\n' +
|
||||
'"series_1";"1500026400";null\r\n' +
|
||||
'"series_1";"1500026500";null\r\n' +
|
||||
'"series_1";"1500026600";6\r\n' +
|
||||
'"series_2";"1500026100";11\r\n' +
|
||||
'"series_2";"1500026200";12\r\n' +
|
||||
'"series_2";"1500026300";13\r\n' +
|
||||
'"series_2";"1500026500";15';
|
||||
|
||||
expect(text).toBe(expectedText);
|
||||
});
|
||||
@@ -50,15 +50,79 @@ describe('file_export', () => {
|
||||
it('should export points in proper order', () => {
|
||||
let text = fileExport.convertSeriesListToCsvColumns(ctx.seriesList, ctx.timeFormat);
|
||||
const expectedText =
|
||||
'Time;series_1;series_2\n' +
|
||||
'1500026100;1;11\n' +
|
||||
'1500026200;2;12\n' +
|
||||
'1500026300;null;13\n' +
|
||||
'1500026400;null;null\n' +
|
||||
'1500026500;null;15\n' +
|
||||
'1500026600;6;null\n';
|
||||
'"Time";"series_1";"series_2"\r\n' +
|
||||
'"1500026100";1;11\r\n' +
|
||||
'"1500026200";2;12\r\n' +
|
||||
'"1500026300";null;13\r\n' +
|
||||
'"1500026400";null;null\r\n' +
|
||||
'"1500026500";null;15\r\n' +
|
||||
'"1500026600";6;null';
|
||||
|
||||
expect(text).toBe(expectedText);
|
||||
});
|
||||
});
|
||||
|
||||
describe('when exporting table data to csv', () => {
|
||||
|
||||
it('should properly escape special characters and quote all string values', () => {
|
||||
const inputTable = {
|
||||
columns: [
|
||||
{ title: 'integer_value' },
|
||||
{ text: 'string_value' },
|
||||
{ title: 'float_value' },
|
||||
{ text: 'boolean_value' },
|
||||
],
|
||||
rows: [
|
||||
[123, 'some_string', 1.234, true],
|
||||
[0o765, 'some string with " in the middle', 1e-2, false],
|
||||
[0o765, 'some string with "" in the middle', 1e-2, false],
|
||||
[0o765, 'some string with """ in the middle', 1e-2, false],
|
||||
[0o765, '"some string with " at the beginning', 1e-2, false],
|
||||
[0o765, 'some string with " at the end"', 1e-2, false],
|
||||
[0x123, 'some string with \n in the middle', 10.01, false],
|
||||
[0b1011, 'some string with ; in the middle', -12.34, true],
|
||||
[123, 'some string with ;; in the middle', -12.34, true],
|
||||
],
|
||||
};
|
||||
|
||||
const returnedText = fileExport.convertTableDataToCsv(inputTable, false);
|
||||
|
||||
const expectedText =
|
||||
'"integer_value";"string_value";"float_value";"boolean_value"\r\n' +
|
||||
'123;"some_string";1.234;true\r\n' +
|
||||
'501;"some string with "" in the middle";0.01;false\r\n' +
|
||||
'501;"some string with """" in the middle";0.01;false\r\n' +
|
||||
'501;"some string with """""" in the middle";0.01;false\r\n' +
|
||||
'501;"""some string with "" at the beginning";0.01;false\r\n' +
|
||||
'501;"some string with "" at the end""";0.01;false\r\n' +
|
||||
'291;"some string with \n in the middle";10.01;false\r\n' +
|
||||
'11;"some string with ; in the middle";-12.34;true\r\n' +
|
||||
'123;"some string with ;; in the middle";-12.34;true';
|
||||
|
||||
expect(returnedText).toBe(expectedText);
|
||||
});
|
||||
|
||||
it('should decode HTML encoded characters', function() {
|
||||
const inputTable = {
|
||||
columns: [
|
||||
{ text: 'string_value' },
|
||||
],
|
||||
rows: [
|
||||
['"&ä'],
|
||||
['<strong>"some html"</strong>'],
|
||||
['<a href="http://something/index.html">some text</a>']
|
||||
],
|
||||
};
|
||||
|
||||
const returnedText = fileExport.convertTableDataToCsv(inputTable, false);
|
||||
|
||||
const expectedText =
|
||||
'"string_value"\r\n' +
|
||||
'"""&ä"\r\n' +
|
||||
'"<strong>""some html""</strong>"\r\n' +
|
||||
'"<a href=""http://something/index.html"">some text</a>"';
|
||||
|
||||
expect(returnedText).toBe(expectedText);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user