Skip to content

Commit

Permalink
Pass source_column_count from CSV row
Browse files Browse the repository at this point in the history
As we create a ChildrensBarredListEntry from each CSV row we want to
check that the row has the correct number of columns to avoid errors
where the data is a column out if the CSV has been incorrectly created.

Pass the row column count with the params.
  • Loading branch information
gpeng committed Jan 19, 2024
1 parent 6edaab6 commit dea6ced
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 7 deletions.
2 changes: 2 additions & 0 deletions app/services/create_childrens_barred_list_entries.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@ def call
CSV
.parse(@raw_data, encoding: "ISO8859-1:utf-8")
.each do |row|
source_column_count = row.count
entry = ChildrensBarredListEntry.create(
trn: pad_trn(row[0]),
last_name: format_names(row[1]),
first_names: format_names(row[2]),
date_of_birth: format_date_of_birth(row[3]),
national_insurance_number: row[4],
upload_file_hash:,
source_column_count:
)

@failed_entries << entry unless entry.persisted?
Expand Down
8 changes: 7 additions & 1 deletion config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,20 @@ en:
blank: Select a file
invalid_csv: The selected file must be a CSV
activerecord:
models:
childrens_barred_list_entry: Children’s Barred List entry
feedback: Feedback
attributes:
childrens_barred_list_entry:
source_column_count: The uploaded file
errors:
models:
childrens_barred_list_entry:
attributes:
last_name:
taken: with same first names and date of birth already exists
source_column_count:
invalid: The uploaded file must have %{required_count} columns
invalid: must have %{required_count} columns
feedback:
attributes:
satisfaction_rating:
Expand Down
4 changes: 4 additions & 0 deletions spec/fixtures/example-incorrect-columns.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Darby,James,10/11/1979,,N
Jones,Tim,01/12/2001,,N
Clongbong,Joe,07/06/1989,,N
Kramer,Richie,18/09/1982,,N
4 changes: 2 additions & 2 deletions spec/forms/support_interface/upload_form_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
context "when the form is valid" do
let(:csv_data) do
[
["12567", "Smith", "John James", "01/02/1990", "AB123456C"].join(","),
["1234568", "Smith", "Jane Jemima", "07/05/1980", "AB123456D"].join(
["12567", "Smith", "John James", "01/02/1990", "AB123456C", "N"].join(","),
["1234568", "Smith", "Jane Jemima", "07/05/1980", "AB123456D", "N"].join(
","
)
].join("\n")
Expand Down
6 changes: 3 additions & 3 deletions spec/services/create_childrens_barred_list_entries_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
RSpec.describe CreateChildrensBarredListEntries do
let(:csv_data) do
[
["12567", "SMITH ", "DR. JOHN JAMES ", "01/02/1990", "AB123456C"].join(","),
["1234568", " jones ", " mrs jane jemima", "07/05/1980", "AB123456D"].join(",")
["12567", "SMITH ", "DR. JOHN JAMES ", "01/02/1990", "AB123456C, N"].join(","),
["1234568", " jones ", " mrs jane jemima", "07/05/1980", "AB123456D, N"].join(",")
].join("\n")
end

Expand Down Expand Up @@ -78,7 +78,7 @@
context "when a row contains non-UTF-8 characters" do
let(:csv_data) do
[
["12567", "Sánchezera-blobbá", "Angélina", "01/11/1990", "AB123456C"].join(","),
["12567", "Sánchezera-blobbá", "Angélina", "01/11/1990", "AB123456C", "N"].join(","),
].join("\n").encode("ISO-8859-1")
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@
then_i_see_a_success_message
end

scenario "Support user uploads an CSV file that contains rows with an incorrect number of columnss", test: :with_stubbed_auth do
given_the_service_is_open
and_i_am_signed_in_as_an_internal_user_via_dsi
and_i_am_on_the_upload_page
when_i_upload_a_csv_file_with_an_incorrect_number_of_columns
then_i_see_failed_entries
end

def given_the_service_is_open
FeatureFlags::FeatureFlag.activate(:service_open)
end
Expand Down Expand Up @@ -74,7 +82,6 @@ def and_unconfirmed_entries_are_removed
def and_i_can_see_what_will_not_be_saved
expect(page).to have_content("1 entry will not be saved")

invalid_entries_table = page.all("table")[0]
within(invalid_entries_table) do
expect(page).to have_content("Simpson")
expect(page).to have_content("Homer Duff")
Expand All @@ -91,11 +98,29 @@ def then_i_see_a_success_message
expect(page).to have_content("Records uploaded")
end

def when_i_upload_a_csv_file_with_an_incorrect_number_of_columns
attach_file "support_interface_upload_form[file]",
Rails.root.join("spec/fixtures/example-incorrect-columns.csv")
click_on "Upload file"
end

def then_i_see_failed_entries
expect(page).to have_content("4 entries will not be saved")

within(invalid_entries_table) do
expect(page).to have_content("Darby")
expect(page).to have_content("James")
expect(page).to have_content("10/11/1979")
expect(page).to have_content("The uploaded file must have 6 columns")
end
end

def valid_entries_table
page.all("table")[1]
end

def invalid_entries_table
page.all("table")[0]
end

end

0 comments on commit dea6ced

Please sign in to comment.