Skip to content

Commit

Permalink
Do check in fix rows
Browse files Browse the repository at this point in the history
  • Loading branch information
Gerrit Vermeulen committed Dec 5, 2024
1 parent 748cc87 commit 79c929c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 13 deletions.
21 changes: 10 additions & 11 deletions home/import_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,23 +169,22 @@ def fix_rows(rows: Iterator[dict[str | Any, Any]]) -> Iterator[dict[str, str | N
"""
Fix keys for all rows by lowercasing keys and removing whitespace from keys and values
"""
for index, row in enumerate(rows):
yield fix_row(row, index)
first_row = next(rows)
if len(first_row) != len(fix_row(first_row)):
raise ImportException(
"Invalid format. Please check that there are no duplicate headers."
)
yield fix_row(first_row)

for row in rows:
yield fix_row(row)


def fix_row(row: dict[str, str | None], index: int) -> dict[str, str | None]:
def fix_row(row: dict[str, str | None]) -> dict[str, str | None]:
"""
Fix a single row by lowercasing the key and removing whitespace from the key and value
"""
try:
if index == 0:
keys = [_normalise_key(k) for k, v in row.items()]
if len(keys) != len(set(keys)):
raise ImportException(
"Invalid format. Please check that there are no duplicate headers.",
row_num=index,
)

return {_normalise_key(k): _normalise_value(v) for k, v in row.items()}
except AttributeError:
raise ImportException(
Expand Down
2 changes: 0 additions & 2 deletions home/tests/test_content_import_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -1452,8 +1452,6 @@ def test_import_ordered_sets_duplicate_header_csv(
with pytest.raises(ImportException) as e:
csv_impexp.import_ordered_sets(content)

assert e.value.row_num == 0

assert e.value.message == [
"Invalid format. Please check that there are no duplicate headers."
]
Expand Down

0 comments on commit 79c929c

Please sign in to comment.