Browse Source

CLDC-2386 Validate the correct template for the year (#1665)

* Validate that correct template for the year is used for sales (with headers)

* Check the correct template is being used without headers

* Check correct template for lettings

* Update csv parser on sales

* Remove redundant methods

* Extract form years

* Reverse year check mathod
pull/1677/head
kosiakkatrina 2 years ago committed by GitHub
parent
commit
fad105302a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 7
      app/services/bulk_upload/lettings/validator.rb
  2. 4
      app/services/bulk_upload/lettings/year2022/csv_parser.rb
  3. 17
      app/services/bulk_upload/lettings/year2023/csv_parser.rb
  4. 7
      app/services/bulk_upload/sales/validator.rb
  5. 4
      app/services/bulk_upload/sales/year2022/csv_parser.rb
  6. 17
      app/services/bulk_upload/sales/year2023/csv_parser.rb
  7. 2
      config/locales/en.yml
  8. 33
      spec/services/bulk_upload/lettings/validator_spec.rb
  9. 44
      spec/services/bulk_upload/sales/validator_spec.rb

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

@ -8,6 +8,7 @@ class BulkUpload::Lettings::Validator
validate :validate_file_not_empty validate :validate_file_not_empty
validate :validate_field_numbers_count validate :validate_field_numbers_count
validate :validate_max_columns_count_if_no_headers validate :validate_max_columns_count_if_no_headers
validate :validate_correct_template
def initialize(bulk_upload:, path:) def initialize(bulk_upload:, path:)
@bulk_upload = bulk_upload @bulk_upload = bulk_upload
@ -158,6 +159,12 @@ private
errors.add(:base, :over_max_column_count) if csv_parser.too_many_columns? errors.add(:base, :over_max_column_count) if csv_parser.too_many_columns?
end end
def validate_correct_template
return if halt_validations?
errors.add(:base, :wrong_template) if csv_parser.wrong_template_for_year?
end
def halt_validations! def halt_validations!
@halt_validations = true @halt_validations = true
end end

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

@ -61,6 +61,10 @@ class BulkUpload::Lettings::Year2022::CsvParser
max_columns_count > MAX_COLUMNS max_columns_count > MAX_COLUMNS
end end
def wrong_template_for_year?
false
end
private private
def default_field_numbers def default_field_numbers

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

@ -1,8 +1,11 @@
require "csv" require "csv"
class BulkUpload::Lettings::Year2023::CsvParser class BulkUpload::Lettings::Year2023::CsvParser
include CollectionTimeHelper
FIELDS = 134 FIELDS = 134
MAX_COLUMNS = 142 MAX_COLUMNS = 142
FORM_YEAR = 2023
attr_reader :path attr_reader :path
@ -61,6 +64,12 @@ class BulkUpload::Lettings::Year2023::CsvParser
max_columns_count > MAX_COLUMNS max_columns_count > MAX_COLUMNS
end end
def wrong_template_for_year?
collection_start_year_for_date(first_record_start_date) != FORM_YEAR
rescue Date::Error
false
end
private private
def default_field_numbers def default_field_numbers
@ -92,4 +101,12 @@ private
@normalised_string @normalised_string
end 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 end

7
app/services/bulk_upload/sales/validator.rb

@ -5,6 +5,7 @@ class BulkUpload::Sales::Validator
validate :validate_file_not_empty validate :validate_file_not_empty
validate :validate_max_columns validate :validate_max_columns
validate :validate_correct_template
def initialize(bulk_upload:, path:) def initialize(bulk_upload:, path:)
@bulk_upload = bulk_upload @bulk_upload = bulk_upload
@ -133,6 +134,12 @@ private
errors.add(:base, :over_max_column_count) if column_count > csv_parser.class::MAX_COLUMNS errors.add(:base, :over_max_column_count) if column_count > csv_parser.class::MAX_COLUMNS
end end
def validate_correct_template
return if halt_validations?
errors.add(:base, :wrong_template) if csv_parser.wrong_template_for_year?
end
def halt_validations! def halt_validations!
@halt_validations = true @halt_validations = true
end end

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

@ -43,6 +43,10 @@ class BulkUpload::Sales::Year2022::CsvParser
cols[headers.find_index(field) + col_offset] cols[headers.find_index(field) + col_offset]
end end
def wrong_template_for_year?
false
end
private private
def headers def headers

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

@ -1,7 +1,10 @@
require "csv" require "csv"
class BulkUpload::Sales::Year2023::CsvParser class BulkUpload::Sales::Year2023::CsvParser
include CollectionTimeHelper
MAX_COLUMNS = 142 MAX_COLUMNS = 142
FORM_YEAR = 2023
attr_reader :path attr_reader :path
@ -46,6 +49,12 @@ class BulkUpload::Sales::Year2023::CsvParser
cols[field_numbers.find_index(field) + col_offset] cols[field_numbers.find_index(field) + col_offset]
end end
def wrong_template_for_year?
collection_start_year_for_date(first_record_start_date) != FORM_YEAR
rescue Date::Error
false
end
private private
def default_field_numbers def default_field_numbers
@ -87,4 +96,12 @@ private
@normalised_string @normalised_string
end end
def first_record_start_date
if with_headers?
Date.new(row_parsers.first.field_4.to_i + 2000, row_parsers.first.field_3.to_i, row_parsers.first.field_2.to_i)
else
Date.new(rows.first[3].to_i + 2000, rows.first[2].to_i, rows.first[1].to_i)
end
end
end end

2
config/locales/en.yml

@ -50,11 +50,13 @@ en:
base: base:
wrong_field_numbers_count: "Incorrect number of fields, please ensure you have used the correct template" wrong_field_numbers_count: "Incorrect number of fields, please ensure you have used the correct template"
over_max_column_count: "Too many columns, please ensure you have used the correct template" over_max_column_count: "Too many columns, please ensure you have used the correct template"
wrong_template: "Incorrect start dates, please ensure you have used the correct template"
bulk_upload/sales/validator: bulk_upload/sales/validator:
attributes: attributes:
base: base:
wrong_field_numbers_count: "Incorrect number of fields, please ensure you have used the correct template" wrong_field_numbers_count: "Incorrect number of fields, please ensure you have used the correct template"
over_max_column_count: "Too many columns, please ensure you have used the correct template" over_max_column_count: "Too many columns, please ensure you have used the correct template"
wrong_template: "Incorrect sale dates, please ensure you have used the correct template"
forms/bulk_upload_lettings/year: forms/bulk_upload_lettings/year:
attributes: attributes:
year: year:

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

@ -191,6 +191,39 @@ RSpec.describe BulkUpload::Lettings::Validator do
end end
end end
end end
context "when uploading a 2022 template for 2023 bulk upload" do
let(:bulk_upload) { create(:bulk_upload, user:, year: 2023) }
let(:log) { build(:lettings_log, :completed, startdate: Time.zone.local(2022, 5, 6)) }
context "with no headers" do
before do
file.write(BulkUpload::LettingsLogToCsv.new(log:, line_ending: "\r\n", col_offset: 0).to_2022_csv_row)
file.close
end
it "is not valid" do
expect(validator).not_to be_valid
end
end
context "with headers" do
let(:seed) { rand }
let(:log_to_csv) { BulkUpload::LettingsLogToCsv.new(log:) }
let(:field_numbers) { log_to_csv.default_2022_field_numbers + %w[invalid_field_number] }
let(:field_values) { log_to_csv.to_2022_row + %w[value_for_invalid_field_number] }
before do
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 "is not valid" do
expect(validator).not_to be_valid
end
end
end
end end
end end

44
spec/services/bulk_upload/sales/validator_spec.rb

@ -27,7 +27,49 @@ RSpec.describe BulkUpload::Sales::Validator do
end end
end end
context "when incorrect headers" context "when trying to upload 2022 data for 2023 bulk upload" do
let(:bulk_upload) { create(:bulk_upload, user:, year: 2023) }
context "with a valid csv" do
let(:path) { file_fixture("2022_23_sales_bulk_upload.csv") }
it "is not valid" do
expect(validator).not_to be_valid
end
end
context "with unix line endings" do
let(:fixture_path) { file_fixture("2022_23_sales_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 "is not valid" do
expect(validator).not_to be_valid
end
end
context "without headers" do
let(:log) { build(:sales_log, :completed) }
let(:file) { Tempfile.new }
let(:path) { file.path }
before do
file.write(BulkUpload::SalesLogToCsv.new(log:, line_ending: "\r\n", col_offset: 0).to_2022_csv_row)
file.close
end
it "is not valid" do
expect(validator).not_to be_valid
end
end
end
end end
describe "#call" do describe "#call" do

Loading…
Cancel
Save