Browse Source

CLDC-2438 Fix carriage return csv bug (#1685)

* feat: add extra subs to row_parsers

* feat: add tests

* refactor: gsub not tr

* feat: remove redundant sub

* feat: simplify replacement

* feat: scrub before tr to fix invalid byte error

* feat: add !
pull/1691/head
natdeanlewissoftwire 2 years ago committed by GitHub
parent
commit
19335a2367
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      app/services/bulk_upload/lettings/year2022/csv_parser.rb
  2. 1
      app/services/bulk_upload/lettings/year2023/csv_parser.rb
  3. 1
      app/services/bulk_upload/sales/year2022/csv_parser.rb
  4. 1
      app/services/bulk_upload/sales/year2023/csv_parser.rb
  5. 18
      spec/services/bulk_upload/lettings/year2022/csv_parser_spec.rb
  6. 18
      spec/services/bulk_upload/lettings/year2023/csv_parser_spec.rb
  7. 22
      spec/services/bulk_upload/sales/year2022/csv_parser_spec.rb
  8. 18
      spec/services/bulk_upload/sales/year2023/csv_parser_spec.rb

1
app/services/bulk_upload/lettings/year2022/csv_parser.rb

@ -93,6 +93,7 @@ private
@normalised_string = File.read(path, encoding: "bom|utf-8") @normalised_string = File.read(path, encoding: "bom|utf-8")
@normalised_string.gsub!("\r\n", "\n") @normalised_string.gsub!("\r\n", "\n")
@normalised_string.scrub!("") @normalised_string.scrub!("")
@normalised_string.tr!("\r", "\n")
@normalised_string @normalised_string
end end

1
app/services/bulk_upload/lettings/year2023/csv_parser.rb

@ -98,6 +98,7 @@ private
@normalised_string = File.read(path, encoding: "bom|utf-8") @normalised_string = File.read(path, encoding: "bom|utf-8")
@normalised_string.gsub!("\r\n", "\n") @normalised_string.gsub!("\r\n", "\n")
@normalised_string.scrub!("") @normalised_string.scrub!("")
@normalised_string.tr!("\r", "\n")
@normalised_string @normalised_string
end end

1
app/services/bulk_upload/sales/year2022/csv_parser.rb

@ -67,6 +67,7 @@ private
@normalised_string = File.read(path, encoding: "bom|utf-8") @normalised_string = File.read(path, encoding: "bom|utf-8")
@normalised_string.gsub!("\r\n", "\n") @normalised_string.gsub!("\r\n", "\n")
@normalised_string.scrub!("") @normalised_string.scrub!("")
@normalised_string.tr!("\r", "\n")
@normalised_string @normalised_string
end end

1
app/services/bulk_upload/sales/year2023/csv_parser.rb

@ -93,6 +93,7 @@ private
@normalised_string = File.read(path, encoding: "bom|utf-8") @normalised_string = File.read(path, encoding: "bom|utf-8")
@normalised_string.gsub!("\r\n", "\n") @normalised_string.gsub!("\r\n", "\n")
@normalised_string.scrub!("") @normalised_string.scrub!("")
@normalised_string.tr!("\r", "\n")
@normalised_string @normalised_string
end end

18
spec/services/bulk_upload/lettings/year2022/csv_parser_spec.rb

@ -169,4 +169,22 @@ RSpec.describe BulkUpload::Lettings::Year2022::CsvParser do
end end
end end
end end
context "when parsing csv with carriage returns" do
before do
file.write("Question\r\n")
file.write("Additional info\r")
file.write("Values\r\n")
file.write("Can be empty?\r")
file.write("Type of letting the question applies to\r\n")
file.write("Duplicate check field?\r")
file.write(BulkUpload::LettingsLogToCsv.new(log:).default_2022_field_numbers_row)
file.write(BulkUpload::LettingsLogToCsv.new(log:).to_2022_csv_row)
file.rewind
end
it "parses csv correctly" do
expect(service.row_parsers[0].field_12.to_i).to eq(35)
end
end
end end

18
spec/services/bulk_upload/lettings/year2023/csv_parser_spec.rb

@ -150,6 +150,24 @@ RSpec.describe BulkUpload::Lettings::Year2023::CsvParser do
end end
end end
context "when parsing csv with carriage returns" do
before do
file.write("Question\r\n")
file.write("Additional info\r")
file.write("Values\r\n")
file.write("Can be empty?\r")
file.write("Type of letting the question applies to\r\n")
file.write("Duplicate check field?\r")
file.write(BulkUpload::LettingsLogToCsv.new(log:).default_2023_field_numbers_row)
file.write(BulkUpload::LettingsLogToCsv.new(log:).to_2023_csv_row)
file.rewind
end
it "parses csv correctly" do
expect(service.row_parsers[0].field_13).to eql(log.tenancycode)
end
end
describe "#column_for_field", aggregate_failures: true do describe "#column_for_field", aggregate_failures: true do
context "when with headers using default ordering" do context "when with headers using default ordering" do
before do before do

22
spec/services/bulk_upload/sales/year2022/csv_parser_spec.rb

@ -94,4 +94,26 @@ RSpec.describe BulkUpload::Sales::Year2022::CsvParser do
end end
end end
end end
context "when parsing csv with carriage returns" do
let(:file) { Tempfile.new }
let(:path) { file.path }
let(:log) { build(:sales_log, :completed) }
before do
file.write("Question\r\n")
file.write("Additional info\r")
file.write("Values\r\n")
file.write("Can be empty?\r")
file.write("Type of letting the question applies to\r\n")
file.write("Duplicate check field?\r")
file.write(BulkUpload::SalesLogToCsv.new(log:).to_2022_csv_row)
file.rewind
end
it "parses csv correctly" do
expect(service.column_for_field("field_1")).to eql("A")
expect(service.column_for_field("field_125")).to eql("DU")
end
end
end end

18
spec/services/bulk_upload/sales/year2023/csv_parser_spec.rb

@ -145,4 +145,22 @@ RSpec.describe BulkUpload::Sales::Year2023::CsvParser do
end end
end end
end end
context "when parsing csv with carriage returns" do
before do
file.write("Question\r\n")
file.write("Additional info\r")
file.write("Values\r\n")
file.write("Can be empty?\r")
file.write("Type of letting the question applies to\r\n")
file.write("Duplicate check field?\r")
file.write(BulkUpload::SalesLogToCsv.new(log:).default_2023_field_numbers_row)
file.write(BulkUpload::SalesLogToCsv.new(log:).to_2023_csv_row)
file.rewind
end
it "parses csv correctly" do
expect(service.row_parsers[0].field_19).to eql(log.uprn)
end
end
end end

Loading…
Cancel
Save