refactor error handling of CSV::MalformedCSVError and fix some typos

This commit is contained in:
Gaetan Riou
2020-06-11 16:47:58 +10:00
parent 9b8a97aadd
commit d3943bc92a
2 changed files with 14 additions and 10 deletions

View File

@@ -238,16 +238,20 @@ module ProductImport
end
[]
rescue CSV::MalformedCSVError => e
# NOTE the init_product_importer method calls build_entries and
# buils_all_entries resulting in the error being raised twice
unless errors.added?(:importer, I18n.t('admin.product_import.model.malformed_csv',
error_message: e.message))
errors.add(:importer, I18n.t('admin.product_import.model.malformed_csv',
error_message: e.message))
end
add_malformed_csv_error e.message
[]
end
# This error is raised twice because init_product_importer calls both
# build_entries and buils_all_entries
def add_malformed_csv_error(error_message)
unless errors.added?(:importer, I18n.t('admin.product_import.model.malformed_csv',
error_message: error_message))
errors.add(:importer, I18n.t('admin.product_import.model.malformed_csv',
error_message: error_message))
end
end
def build_entries_in_range
# In the JS, start and end are calculated like this:
# start = (batchIndex * $scope.batchSize) + 1

View File

@@ -170,7 +170,7 @@ describe ProductImport::ProductImporter do
end
describe "when uploading a spreadsheet with some malformed data" do
# Use a simple string as CVS.generate will do some escaping
# Use a simple string as CSV.generate will do some escaping
let(:csv_data) {
csv = "name,producer,category,on_hand,price,units,unit_type,shipping_category\n"
csv += "Good Carrots,#{enterprise.name},Vegetables,5,3.20,500,g,#{shipping_category.name}\n"
@@ -178,8 +178,8 @@ describe ProductImport::ProductImporter do
}
let(:importer) { import_data csv_data }
# NOTE an unquoted \n will create a non valid line which will fail entry validation hence why we are only testing with \r
it "should raise an unquoted field error if data include unquoted filed with \r character" do
# an unquoted \n will create a non valid line which will fail entry validation hence why we are only testing with \r
it "should raise an unquoted field error if data include unquoted field with \r character" do
expect(importer.errors.messages.values).to include(
[I18n.t('admin.product_import.model.malformed_csv', error_message: "Unquoted fields do not allow \\r or \\n (line 3).")]
)