From a6d43231045564e552f3d8f8f0f97f02957d7720 Mon Sep 17 00:00:00 2001 From: rikuke <33894149+rikuke@users.noreply.github.com> Date: Wed, 22 Nov 2023 11:08:27 +0200 Subject: [PATCH] feat: Hl 1048 add bom (#2495) * feat: add BOM to batch zip csv * feat: add BOM to reports csv --- .../applications/services/csv_export_base.py | 8 ++++++-- .../applications/tests/test_applications_report.py | 13 ++++++++----- .../applications/tests/test_talpa_integration.py | 2 +- frontend/shared/src/utils/file.utils.ts | 2 +- 4 files changed, 16 insertions(+), 9 deletions(-) diff --git a/backend/benefit/applications/services/csv_export_base.py b/backend/benefit/applications/services/csv_export_base.py index 6db485c6c3..79734faf3d 100644 --- a/backend/benefit/applications/services/csv_export_base.py +++ b/backend/benefit/applications/services/csv_export_base.py @@ -75,7 +75,9 @@ def write_csv_file(self, path) -> None: def get_csv_string(self, remove_quotes: bool = False) -> str: return "".join( # Lines end with '\r\n' already so no need to add newlines here - self.get_csv_string_lines_generator(remove_quotes=remove_quotes) + self.get_csv_string_lines_generator( + remove_quotes=remove_quotes, add_bom=True + ) ) def get_csv_cell_list_lines_generator( @@ -138,7 +140,9 @@ def get_csv_string_lines_generator( for line in self.get_csv_cell_list_lines_generator(): line_length_set.add(len(line)) - assert len(line_length_set) == 1, "Each CSV line must have same colum count" + assert ( + len(line_length_set) == 1 + ), "Each CSV line must have same column count" csv_writer.writerow(line) yield io.getvalue() # Reset StringIO object diff --git a/backend/benefit/applications/tests/test_applications_report.py b/backend/benefit/applications/tests/test_applications_report.py index fef2c6ceec..f580ead840 100644 --- a/backend/benefit/applications/tests/test_applications_report.py +++ b/backend/benefit/applications/tests/test_applications_report.py @@ -307,10 +307,10 @@ def test_pruned_applications_csv_output( application = ( pruned_applications_csv_service_with_one_application.get_applications()[0] ) - # Assert that there are 15 column headers in the pruned CSV + # Assert that there are 18 column headers in the pruned CSV assert len(csv_lines[0]) == 18 - assert csv_lines[0][0] == '"Hakemusnumero"' + assert csv_lines[0][0] == '\ufeff"Hakemusnumero"' assert csv_lines[0][1] == '"Työnantajan tyyppi"' assert csv_lines[0][2] == '"Työnantajan tilinumero"' assert csv_lines[0][3] == '"Työnantajan nimi"' @@ -357,7 +357,7 @@ def test_pruned_applications_csv_output( def test_applications_csv_output(applications_csv_service): # noqa: C901 csv_lines = split_lines_at_semicolon(applications_csv_service.get_csv_string()) - assert csv_lines[0][0] == '"Hakemusnumero"' + assert csv_lines[0][0] == '\ufeff"Hakemusnumero"' assert ( int(csv_lines[1][0]) == applications_csv_service.get_applications()[0].application_number @@ -370,7 +370,10 @@ def test_applications_csv_output(applications_csv_service): # noqa: C901 assert application.ahjo_rows[0].start_date == application.calculation.start_date assert application.ahjo_rows[0].end_date == application.calculation.end_date - for idx, col in enumerate(applications_csv_service.CSV_COLUMNS): + csv_columns = iter(applications_csv_service.CSV_COLUMNS) + next(csv_columns, None) # Skip the first element + + for idx, col in enumerate(csv_columns, start=1): assert csv_lines[0][idx] == f'"{col.heading}"' if "Työnantajan tyyppi" in col.heading: @@ -495,7 +498,7 @@ def test_applications_csv_two_ahjo_rows(applications_csv_service_with_one_applic applications_csv_service_with_one_application.get_csv_string() ) assert len(application.ahjo_rows) == 2 - assert csv_lines[0][0] == '"Hakemusnumero"' + assert csv_lines[0][0] == '\ufeff"Hakemusnumero"' assert int(csv_lines[1][0]) == application.application_number assert int(csv_lines[1][1]) == 1 assert int(csv_lines[2][0]) == application.application_number diff --git a/backend/benefit/applications/tests/test_talpa_integration.py b/backend/benefit/applications/tests/test_talpa_integration.py index 1e1a596baa..097a6b46b2 100644 --- a/backend/benefit/applications/tests/test_talpa_integration.py +++ b/backend/benefit/applications/tests/test_talpa_integration.py @@ -41,7 +41,7 @@ def test_talpa_csv_output(pruned_applications_csv_service_with_one_application): pruned_applications_csv_service_with_one_application.get_csv_string() ) # BOM at the beginning of the file - assert csv_lines[0][0] == '"Hakemusnumero"' + assert csv_lines[0][0] == '\ufeff"Hakemusnumero"' csv_columns = iter(pruned_applications_csv_service_with_one_application.CSV_COLUMNS) next(csv_columns, None) # Skip the first element diff --git a/frontend/shared/src/utils/file.utils.ts b/frontend/shared/src/utils/file.utils.ts index 48ad2fffd4..d6b2f0a355 100644 --- a/frontend/shared/src/utils/file.utils.ts +++ b/frontend/shared/src/utils/file.utils.ts @@ -11,7 +11,7 @@ export const downloadFile = (data: string, type: ExportFileType): void => { if (type === 'csv') { // eslint-disable-next-line security/detect-non-literal-fs-filename - fileDownload(data, `hl ${dateString}.csv`, 'text/csv;charset=utf-8'); + fileDownload(data, `hl ${dateString}.csv`, 'text/csv;charset=utf-8-sig', '\uFEFF'); } else { fileDownload(data, `hl ${dateString}.zip`); }