fix: improve CSV parser error handling

This commit is contained in:
carla 2026-01-15 11:08:22 +01:00
parent 31cf07c071
commit 3bbe9895ee
2 changed files with 51 additions and 34 deletions

View file

@ -109,7 +109,7 @@ defmodule Mv.Membership.Import.CsvParserTest do
assert {:ok, headers, rows} = CsvParser.parse(csv_content)
assert headers == ["email"]
# Lines 2, 3, 4 are empty (skipped), line 4 has data
# Lines 2 & 3 are empty (skipped), line 4 has data
assert rows == [{4, ["john@example.com"]}]
end
end
@ -156,6 +156,19 @@ defmodule Mv.Membership.Import.CsvParserTest do
assert headers == ["email", "name"]
assert rows == [{2, ["john@example.com", "John \"Johnny\" Doe"]}]
end
test "handles multiline quoted fields with correct line numbering" do
# Header line 1
# Data record starts line 2, contains "foo\nbar" in a field
# Record ends physically at line 3
# Expected: row gets line number 2 (start line)
csv_content = "email;description\njohn@example.com;\"foo\nbar\""
assert {:ok, headers, rows} = CsvParser.parse(csv_content)
assert headers == ["email", "description"]
assert rows == [{2, ["john@example.com", "foo\nbar"]}]
end
end
describe "error handling" do
@ -170,12 +183,23 @@ defmodule Mv.Membership.Import.CsvParserTest do
assert reason =~ "CSV file is empty"
end
test "returns {:error, reason} for invalid CSV format" do
# Unbalanced quotes
csv_content = "email;name\n\"john@example.com;John"
test "returns {:error, reason} for invalid UTF-8 content" do
# Invalid UTF-8 sequence
invalid_utf8 = <<0xFF, 0xFE, 0xFD>>
assert {:error, reason} = CsvParser.parse(invalid_utf8)
assert reason =~ "UTF-8"
end
test "returns {:error, reason} for unparsable data row" do
# Malformed CSV row that cannot be parsed
# NimbleCSV will throw an exception for unclosed quotes
csv_content = "email;name\njohn@example.com;\"unclosed quote"
assert {:error, reason} = CsvParser.parse(csv_content)
assert is_binary(reason)
# Error message should indicate parsing failure
assert reason =~ "parse" or reason =~ "CSV"
end
end