Browse Source

fix bulk upload line ending parsing

pull/1208/head
Phil Lee 3 years ago
parent
commit
e7e03cacd5
  1. 6
      app/services/bulk_upload/lettings/log_creator.rb
  2. 6
      app/services/bulk_upload/lettings/validator.rb
  3. 33
      spec/services/bulk_upload/lettings/validator_spec.rb
  4. 7
      spec/support/bulk_upload/log_to_csv.rb

6
app/services/bulk_upload/lettings/log_creator.rb

@ -56,14 +56,10 @@ private
contents = "" contents = ""
File.open(path, "r") do |f| File.open(path, "r") do |f|
f.seek(9900)
contents = f.read contents = f.read
end end
rn_count = contents.scan("\r\n").count if contents[-2..] == "\r\n"
n_count = contents.scan(/[^\r]\n/).count
if rn_count > n_count
"\r\n" "\r\n"
else else
"\n" "\n"

6
app/services/bulk_upload/lettings/validator.rb

@ -212,14 +212,10 @@ private
contents = "" contents = ""
File.open(path, "r") do |f| File.open(path, "r") do |f|
f.seek(9900)
contents = f.read contents = f.read
end end
rn_count = contents.scan("\r\n").count if contents[-2..] == "\r\n"
n_count = contents.scan(/[^\r]\n/).count
if rn_count > n_count
"\r\n" "\r\n"
else else
"\n" "\n"

33
spec/services/bulk_upload/lettings/validator_spec.rb

@ -46,6 +46,39 @@ RSpec.describe BulkUpload::Lettings::Validator do
expect(error.row).to eq("7") expect(error.row).to eq("7")
end end
end end
context "with unix line endings" do
let(:fixture_path) { file_fixture("2022_23_lettings_bulk_upload.csv") }
let(:file) { Tempfile.new }
let(:path) { file.path }
before do
string = File.read(fixture_path)
string.gsub!("\r\n", "\n")
file.write(string)
file.rewind
end
it "creates validation errors" do
expect { validator.call }.to change(BulkUploadError, :count)
end
end
context "without headers" do
let(:log) { build(:lettings_log, :completed) }
let(:file) { Tempfile.new }
let(:path) { file.path }
before do
file.write("\r\n" * 5)
file.write(BulkUpload::LogToCsv.new(log:, line_ending: "\r\n").to_csv_row)
file.rewind
end
it "creates validation errors" do
expect { validator.call }.to change(BulkUploadError, :count)
end
end
end end
describe "#should_create_logs?" do describe "#should_create_logs?" do

7
spec/support/bulk_upload/log_to_csv.rb

@ -1,8 +1,9 @@
class BulkUpload::LogToCsv class BulkUpload::LogToCsv
attr_reader :log attr_reader :log, :line_ending
def initialize(log:) def initialize(log:, line_ending: "\n")
@log = log @log = log
@line_ending = line_ending
end end
def to_csv_row def to_csv_row
@ -152,7 +153,7 @@ class BulkUpload::LogToCsv
log.declaration, log.declaration,
log.joint, log.joint,
renewal, renewal,
"\n", line_ending,
].join(",") ].join(",")
end end

Loading…
Cancel
Save