4 changed files with 4576 additions and 0 deletions
@ -0,0 +1,113 @@ |
|||||||
|
require "csv" |
||||||
|
|
||||||
|
class BulkUpload::Lettings::Year2024::CsvParser |
||||||
|
include CollectionTimeHelper |
||||||
|
|
||||||
|
FIELDS = 134 |
||||||
|
MAX_COLUMNS = 142 |
||||||
|
FORM_YEAR = 2024 |
||||||
|
|
||||||
|
attr_reader :path |
||||||
|
|
||||||
|
def initialize(path:) |
||||||
|
@path = path |
||||||
|
end |
||||||
|
|
||||||
|
def row_offset |
||||||
|
if with_headers? |
||||||
|
rows.find_index { |row| row[0].match(/field number/i) } + 1 |
||||||
|
else |
||||||
|
0 |
||||||
|
end |
||||||
|
end |
||||||
|
|
||||||
|
def col_offset |
||||||
|
with_headers? ? 1 : 0 |
||||||
|
end |
||||||
|
|
||||||
|
def cols |
||||||
|
@cols ||= ("A".."EL").to_a |
||||||
|
end |
||||||
|
|
||||||
|
def row_parsers |
||||||
|
@row_parsers ||= body_rows.map do |row| |
||||||
|
stripped_row = row[col_offset..] |
||||||
|
hash = Hash[field_numbers.zip(stripped_row)] |
||||||
|
|
||||||
|
BulkUpload::Lettings::Year2024::RowParser.new(hash) |
||||||
|
end |
||||||
|
end |
||||||
|
|
||||||
|
def body_rows |
||||||
|
rows[row_offset..] |
||||||
|
end |
||||||
|
|
||||||
|
def rows |
||||||
|
@rows ||= CSV.parse(normalised_string, row_sep:) |
||||||
|
end |
||||||
|
|
||||||
|
def column_for_field(field) |
||||||
|
cols[field_numbers.find_index(field) + col_offset] |
||||||
|
end |
||||||
|
|
||||||
|
def correct_field_count? |
||||||
|
valid_field_numbers_count = field_numbers.count { |f| f != "field_blank" } |
||||||
|
|
||||||
|
valid_field_numbers_count == FIELDS |
||||||
|
end |
||||||
|
|
||||||
|
def too_many_columns? |
||||||
|
return if with_headers? |
||||||
|
|
||||||
|
max_columns_count = body_rows.map(&:size).max - col_offset |
||||||
|
|
||||||
|
max_columns_count > MAX_COLUMNS |
||||||
|
end |
||||||
|
|
||||||
|
def wrong_template_for_year? |
||||||
|
collection_start_year_for_date(first_record_start_date) != FORM_YEAR |
||||||
|
rescue Date::Error |
||||||
|
false |
||||||
|
end |
||||||
|
|
||||||
|
private |
||||||
|
|
||||||
|
def default_field_numbers |
||||||
|
[5, nil, nil, 15, 16, nil, 13, 40, 41, 42, 43, 46, 52, 56, 60, 64, 68, 72, 76, 47, 53, 57, 61, 65, 69, 73, 77, 51, 55, 59, 63, 67, 71, 75, 50, 54, 58, 62, 66, 70, 74, 78, 48, 49, 79, 81, 82, 123, 124, 122, 120, 102, 103, nil, 83, 84, 85, 86, 87, 88, 104, 109, 107, 108, 106, 100, 101, 105, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 126, 128, 129, 130, 131, 132, 127, 125, 133, 134, 33, 34, 35, 36, 37, 38, nil, 7, 8, 9, 28, 14, 32, 29, 30, 31, 26, 27, 25, 23, 24, nil, 1, 3, 2, 80, nil, 121, 44, 89, 98, 92, 95, 90, 91, 93, 94, 97, 96, 99, 10, 11, 12, 45, 39, 6, 4, 17, 18, 19, 20, 21, 22].map { |h| h.present? && h.to_s.match?(/^[0-9]+$/) ? "field_#{h}" : "field_blank" } |
||||||
|
end |
||||||
|
|
||||||
|
def field_numbers |
||||||
|
@field_numbers ||= if with_headers? |
||||||
|
rows[row_offset - 1][col_offset..].map { |h| h.present? && h.match?(/^[0-9]+$/) ? "field_#{h}" : "field_blank" } |
||||||
|
else |
||||||
|
default_field_numbers |
||||||
|
end |
||||||
|
end |
||||||
|
|
||||||
|
def with_headers? |
||||||
|
rows.map { |r| r[0] }.any? { |cell| cell&.match?(/field number/i) } |
||||||
|
end |
||||||
|
|
||||||
|
def row_sep |
||||||
|
"\n" |
||||||
|
end |
||||||
|
|
||||||
|
def normalised_string |
||||||
|
return @normalised_string if @normalised_string |
||||||
|
|
||||||
|
@normalised_string = File.read(path, encoding: "bom|utf-8") |
||||||
|
@normalised_string.gsub!("\r\n", "\n") |
||||||
|
@normalised_string.scrub!("") |
||||||
|
@normalised_string.tr!("\r", "\n") |
||||||
|
|
||||||
|
@normalised_string |
||||||
|
end |
||||||
|
|
||||||
|
def first_record_start_date |
||||||
|
if with_headers? |
||||||
|
Date.new(row_parsers.first.field_98.to_i + 2000, row_parsers.first.field_97.to_i, row_parsers.first.field_96.to_i) |
||||||
|
else |
||||||
|
Date.new(rows.first[97].to_i + 2000, rows.first[96].to_i, rows.first[95].to_i) |
||||||
|
end |
||||||
|
end |
||||||
|
end |
||||||
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,226 @@ |
|||||||
|
require "rails_helper" |
||||||
|
|
||||||
|
RSpec.describe BulkUpload::Lettings::Year2024::CsvParser do |
||||||
|
subject(:service) { described_class.new(path:) } |
||||||
|
|
||||||
|
let(:file) { Tempfile.new } |
||||||
|
let(:path) { file.path } |
||||||
|
let(:log) { build(:lettings_log, :completed) } |
||||||
|
|
||||||
|
context "when parsing csv with headers" do |
||||||
|
before do |
||||||
|
file.write("Question\n") |
||||||
|
file.write("Additional info\n") |
||||||
|
file.write("Values\n") |
||||||
|
file.write("Can be empty?\n") |
||||||
|
file.write("Type of letting the question applies to\n") |
||||||
|
file.write("Duplicate check field?\n") |
||||||
|
file.write(BulkUpload::LettingsLogToCsv.new(log:).default_2024_field_numbers_row) |
||||||
|
file.write(BulkUpload::LettingsLogToCsv.new(log:).to_2024_csv_row) |
||||||
|
file.rewind |
||||||
|
end |
||||||
|
|
||||||
|
it "returns correct offsets" do |
||||||
|
expect(service.row_offset).to eq(7) |
||||||
|
expect(service.col_offset).to eq(1) |
||||||
|
end |
||||||
|
|
||||||
|
it "parses csv correctly" do |
||||||
|
expect(service.row_parsers[0].field_13).to eql(log.tenancycode) |
||||||
|
end |
||||||
|
end |
||||||
|
|
||||||
|
context "when parsing csv with headers with extra rows" do |
||||||
|
before do |
||||||
|
file.write("Section\n") |
||||||
|
file.write("Question\n") |
||||||
|
file.write("Additional info\n") |
||||||
|
file.write("Values\n") |
||||||
|
file.write("Can be empty?\n") |
||||||
|
file.write("Type of letting the question applies to\n") |
||||||
|
file.write("Duplicate check field?\n") |
||||||
|
file.write(BulkUpload::LettingsLogToCsv.new(log:).default_2024_field_numbers_row) |
||||||
|
file.write(BulkUpload::LettingsLogToCsv.new(log:).to_2024_csv_row) |
||||||
|
file.rewind |
||||||
|
end |
||||||
|
|
||||||
|
it "returns correct offsets" do |
||||||
|
expect(service.row_offset).to eq(8) |
||||||
|
expect(service.col_offset).to eq(1) |
||||||
|
end |
||||||
|
|
||||||
|
it "parses csv correctly" do |
||||||
|
expect(service.row_parsers[0].field_13).to eql(log.tenancycode) |
||||||
|
end |
||||||
|
end |
||||||
|
|
||||||
|
context "when parsing csv with headers in arbitrary order" do |
||||||
|
let(:seed) { rand } |
||||||
|
|
||||||
|
before do |
||||||
|
file.write("Question\n") |
||||||
|
file.write("Additional info\n") |
||||||
|
file.write("Values\n") |
||||||
|
file.write("Can be empty?\n") |
||||||
|
file.write("Type of letting the question applies to\n") |
||||||
|
file.write("Duplicate check field?\n") |
||||||
|
file.write(BulkUpload::LettingsLogToCsv.new(log:).default_2024_field_numbers_row(seed:)) |
||||||
|
file.write(BulkUpload::LettingsLogToCsv.new(log:).to_2024_csv_row(seed:)) |
||||||
|
file.rewind |
||||||
|
end |
||||||
|
|
||||||
|
it "returns correct offsets" do |
||||||
|
expect(service.row_offset).to eq(7) |
||||||
|
expect(service.col_offset).to eq(1) |
||||||
|
end |
||||||
|
|
||||||
|
it "parses csv correctly" do |
||||||
|
expect(service.row_parsers[0].field_13).to eql(log.tenancycode) |
||||||
|
end |
||||||
|
end |
||||||
|
|
||||||
|
context "when parsing csv with extra invalid headers" do |
||||||
|
let(:seed) { rand } |
||||||
|
let(:log_to_csv) { BulkUpload::LettingsLogToCsv.new(log:) } |
||||||
|
let(:field_numbers) { log_to_csv.default_2024_field_numbers + %w[invalid_field_number] } |
||||||
|
let(:field_values) { log_to_csv.to_2024_row + %w[value_for_invalid_field_number] } |
||||||
|
|
||||||
|
before do |
||||||
|
file.write("Question\n") |
||||||
|
file.write("Additional info\n") |
||||||
|
file.write("Values\n") |
||||||
|
file.write("Can be empty?\n") |
||||||
|
file.write("Type of letting the question applies to\n") |
||||||
|
file.write("Duplicate check field?\n") |
||||||
|
file.write(log_to_csv.custom_field_numbers_row(seed:, field_numbers:)) |
||||||
|
file.write(log_to_csv.to_custom_csv_row(seed:, field_values:)) |
||||||
|
file.rewind |
||||||
|
end |
||||||
|
|
||||||
|
it "parses csv correctly" do |
||||||
|
expect(service.row_parsers[0].field_13).to eql(log.tenancycode) |
||||||
|
end |
||||||
|
|
||||||
|
it "counts the number of valid field numbers correctly" do |
||||||
|
expect(service).to be_correct_field_count |
||||||
|
end |
||||||
|
end |
||||||
|
|
||||||
|
context "when parsing csv without headers" do |
||||||
|
before do |
||||||
|
file.write(BulkUpload::LettingsLogToCsv.new(log:, col_offset: 0).to_2024_csv_row) |
||||||
|
file.rewind |
||||||
|
end |
||||||
|
|
||||||
|
it "returns correct offsets" do |
||||||
|
expect(service.row_offset).to eq(0) |
||||||
|
expect(service.col_offset).to eq(0) |
||||||
|
end |
||||||
|
|
||||||
|
it "parses csv correctly" do |
||||||
|
expect(service.row_parsers[0].field_13).to eql(log.tenancycode) |
||||||
|
end |
||||||
|
end |
||||||
|
|
||||||
|
context "when parsing with BOM aka byte order mark" do |
||||||
|
let(:bom) { "\uFEFF" } |
||||||
|
|
||||||
|
before do |
||||||
|
file.write(bom) |
||||||
|
file.write(BulkUpload::LettingsLogToCsv.new(log:, col_offset: 0).to_2024_csv_row) |
||||||
|
file.rewind |
||||||
|
end |
||||||
|
|
||||||
|
it "parses csv correctly" do |
||||||
|
expect(service.row_parsers[0].field_13).to eql(log.tenancycode) |
||||||
|
end |
||||||
|
end |
||||||
|
|
||||||
|
context "when an invalid byte sequence" do |
||||||
|
let(:invalid_sequence) { "\x81" } |
||||||
|
|
||||||
|
before do |
||||||
|
file.write(invalid_sequence) |
||||||
|
file.write(BulkUpload::LettingsLogToCsv.new(log:, col_offset: 0).to_2024_csv_row) |
||||||
|
file.rewind |
||||||
|
end |
||||||
|
|
||||||
|
it "parses csv correctly" do |
||||||
|
expect(service.row_parsers[0].field_13).to eql(log.tenancycode) |
||||||
|
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_2024_field_numbers_row) |
||||||
|
file.write(BulkUpload::LettingsLogToCsv.new(log:).to_2024_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 |
||||||
|
context "when with headers using default ordering" do |
||||||
|
before do |
||||||
|
file.write("Question\n") |
||||||
|
file.write("Additional info\n") |
||||||
|
file.write("Values\n") |
||||||
|
file.write("Can be empty?\n") |
||||||
|
file.write("Type of letting the question applies to\n") |
||||||
|
file.write("Duplicate check field?\n") |
||||||
|
file.write(BulkUpload::LettingsLogToCsv.new(log:).default_2024_field_numbers_row) |
||||||
|
file.write(BulkUpload::LettingsLogToCsv.new(log:).to_2024_csv_row) |
||||||
|
file.rewind |
||||||
|
end |
||||||
|
|
||||||
|
it "returns correct column" do |
||||||
|
expect(service.column_for_field("field_5")).to eql("B") |
||||||
|
expect(service.column_for_field("field_22")).to eql("EL") |
||||||
|
end |
||||||
|
end |
||||||
|
|
||||||
|
context "when without headers using default ordering" do |
||||||
|
before do |
||||||
|
file.write(BulkUpload::LettingsLogToCsv.new(log:, col_offset: 0).to_2024_csv_row) |
||||||
|
file.rewind |
||||||
|
end |
||||||
|
|
||||||
|
it "returns correct column" do |
||||||
|
expect(service.column_for_field("field_5")).to eql("A") |
||||||
|
expect(service.column_for_field("field_22")).to eql("EK") |
||||||
|
end |
||||||
|
end |
||||||
|
|
||||||
|
context "when with headers using custom ordering" do |
||||||
|
let(:seed) { 123 } |
||||||
|
|
||||||
|
before do |
||||||
|
file.write("Question\n") |
||||||
|
file.write("Additional info\n") |
||||||
|
file.write("Values\n") |
||||||
|
file.write("Can be empty?\n") |
||||||
|
file.write("Type of letting the question applies to\n") |
||||||
|
file.write("Duplicate check field?\n") |
||||||
|
file.write(BulkUpload::LettingsLogToCsv.new(log:).default_2024_field_numbers_row(seed:)) |
||||||
|
file.write(BulkUpload::LettingsLogToCsv.new(log:).to_2024_csv_row(seed:)) |
||||||
|
file.rewind |
||||||
|
end |
||||||
|
|
||||||
|
it "returns correct column" do |
||||||
|
expect(service.column_for_field("field_5")).to eql("N") |
||||||
|
expect(service.column_for_field("field_22")).to eql("O") |
||||||
|
expect(service.column_for_field("field_26")).to eql("B") |
||||||
|
expect(service.column_for_field("field_25")).to eql("EF") |
||||||
|
end |
||||||
|
end |
||||||
|
end |
||||||
|
end |
||||||
Loading…
Reference in new issue