Skip to content

Commit

Permalink
CSV upload now ignores empty rows
Browse files Browse the repository at this point in the history
  • Loading branch information
asmega committed Oct 14, 2024
1 parent 6661f1e commit 68413ae
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 6 deletions.
10 changes: 10 additions & 0 deletions lib/csv_importer/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def run
Rails.logger.info "Processing #{target_data_model.to_s.titleize} batch #{i}"

record_hashes = batch_rows.map do |row|
next if empty_row?(row)
next if valid_skip_row_conditions?(row)

convert_row_to_hash(row)
Expand All @@ -32,6 +33,15 @@ def run

private

def empty_row?(row)
case row
when Array
row.all? { |cell| cell.blank? }
else
row.all? { |_, v| v.blank? }
end
end

def delete_all_scope
target_data_model
end
Expand Down
34 changes: 30 additions & 4 deletions spec/factories/file_uploads.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,37 @@
factory :file_upload do
association :uploaded_by, factory: :dfe_signin_user

transient do
row_count { 1 }
row do
[]
end
end

trait :school_workforce_census_upload do
transient do
row do
[
"1234567",
"123456",
"",
1,
"",
"",
""
]
end
end
end

body do
<<~CSV
1234567,1234567,Full time,19,Design and Technlogy - Textiles,DTT,34,
,,,,,,,,,,,,,,,
CSV
string = ""

row_count.times do
string << (row.join(",") + "\n")
end

string
end
end
end
4 changes: 2 additions & 2 deletions spec/jobs/import_census_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
describe "#perform" do
context "csv data processes successfully" do
let(:mail) { AdminMailer.census_csv_processing_success(file_upload.uploaded_by.email) }
let(:file_upload) { create(:file_upload) }
let(:file_upload) { create(:file_upload, :school_workforce_census_upload) }

it "imports census data, sends success email and deletes the file upload" do
subject.perform(file_upload.id)
Expand All @@ -23,7 +23,7 @@

context "csv data encounters an error" do
let(:mail) { AdminMailer.census_csv_processing_error(file_upload.uploaded_by.email) }
let(:file_upload) { create(:file_upload) }
let(:file_upload) { create(:file_upload, :school_workforce_census_upload) }

before do
allow(SchoolWorkforceCensus).to receive(:insert_all).and_raise(ActiveRecord::RecordInvalid)
Expand Down
24 changes: 24 additions & 0 deletions spec/models/eligible_fe_providers_importer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,30 @@ def to_row(hash)
end
end

context "with extra empty rows" do
before do
file.write correct_headers

file.write(described_class.mandatory_headers.map { nil }.join(",") + "\n")

3.times do
file.write to_row(attributes_for(:eligible_fe_provider))
end

file.write(described_class.mandatory_headers.map { nil }.join(",") + "\n")

file.close
end

it "ignores empty rows" do
expect { subject.run }.to change { EligibleFeProvider.count }.by(3)
end

it "does not raise errors" do
expect { subject.run }.not_to raise_error
end
end

context "with valid data" do
before do
file.write correct_headers
Expand Down

0 comments on commit 68413ae

Please sign in to comment.