Skip to content

Commit

Permalink
feat: Hl 1048 add bom (#2495)
Browse files Browse the repository at this point in the history
* feat: add BOM to batch zip csv

* feat: add BOM to reports csv
  • Loading branch information
rikuke authored Nov 22, 2023
1 parent 968ec9d commit a6d4323
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 9 deletions.
8 changes: 6 additions & 2 deletions backend/benefit/applications/services/csv_export_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand Down
13 changes: 8 additions & 5 deletions backend/benefit/applications/tests/test_applications_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"'
Expand Down Expand Up @@ -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
Expand All @@ -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:
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion frontend/shared/src/utils/file.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`);
}
Expand Down

0 comments on commit a6d4323

Please sign in to comment.