From fad105302a18e372918d70a0bd832e7ac1fffa6d Mon Sep 17 00:00:00 2001 From: kosiakkatrina <54268893+kosiakkatrina@users.noreply.github.com> Date: Thu, 25 May 2023 17:08:11 +0100 Subject: [PATCH] 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 --- .../bulk_upload/lettings/validator.rb | 7 +++ .../lettings/year2022/csv_parser.rb | 4 ++ .../lettings/year2023/csv_parser.rb | 17 +++++++ app/services/bulk_upload/sales/validator.rb | 7 +++ .../bulk_upload/sales/year2022/csv_parser.rb | 4 ++ .../bulk_upload/sales/year2023/csv_parser.rb | 17 +++++++ config/locales/en.yml | 2 + .../bulk_upload/lettings/validator_spec.rb | 33 ++++++++++++++ .../bulk_upload/sales/validator_spec.rb | 44 ++++++++++++++++++- 9 files changed, 134 insertions(+), 1 deletion(-) diff --git a/app/services/bulk_upload/lettings/validator.rb b/app/services/bulk_upload/lettings/validator.rb index 68cb5be08..a7c20aa1b 100644 --- a/app/services/bulk_upload/lettings/validator.rb +++ b/app/services/bulk_upload/lettings/validator.rb @@ -8,6 +8,7 @@ class BulkUpload::Lettings::Validator validate :validate_file_not_empty validate :validate_field_numbers_count validate :validate_max_columns_count_if_no_headers + validate :validate_correct_template def initialize(bulk_upload:, path:) @bulk_upload = bulk_upload @@ -158,6 +159,12 @@ private errors.add(:base, :over_max_column_count) if csv_parser.too_many_columns? 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! @halt_validations = true end diff --git a/app/services/bulk_upload/lettings/year2022/csv_parser.rb b/app/services/bulk_upload/lettings/year2022/csv_parser.rb index 08cf4709d..663964195 100644 --- a/app/services/bulk_upload/lettings/year2022/csv_parser.rb +++ b/app/services/bulk_upload/lettings/year2022/csv_parser.rb @@ -61,6 +61,10 @@ class BulkUpload::Lettings::Year2022::CsvParser max_columns_count > MAX_COLUMNS end + def wrong_template_for_year? + false + end + private def default_field_numbers diff --git a/app/services/bulk_upload/lettings/year2023/csv_parser.rb b/app/services/bulk_upload/lettings/year2023/csv_parser.rb index b3b959be6..632e18329 100644 --- a/app/services/bulk_upload/lettings/year2023/csv_parser.rb +++ b/app/services/bulk_upload/lettings/year2023/csv_parser.rb @@ -1,8 +1,11 @@ require "csv" class BulkUpload::Lettings::Year2023::CsvParser + include CollectionTimeHelper + FIELDS = 134 MAX_COLUMNS = 142 + FORM_YEAR = 2023 attr_reader :path @@ -61,6 +64,12 @@ class BulkUpload::Lettings::Year2023::CsvParser 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 @@ -92,4 +101,12 @@ private @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 diff --git a/app/services/bulk_upload/sales/validator.rb b/app/services/bulk_upload/sales/validator.rb index e6ea92bf1..4508a5e96 100644 --- a/app/services/bulk_upload/sales/validator.rb +++ b/app/services/bulk_upload/sales/validator.rb @@ -5,6 +5,7 @@ class BulkUpload::Sales::Validator validate :validate_file_not_empty validate :validate_max_columns + validate :validate_correct_template def initialize(bulk_upload:, path:) @bulk_upload = bulk_upload @@ -133,6 +134,12 @@ private errors.add(:base, :over_max_column_count) if column_count > csv_parser.class::MAX_COLUMNS 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! @halt_validations = true end diff --git a/app/services/bulk_upload/sales/year2022/csv_parser.rb b/app/services/bulk_upload/sales/year2022/csv_parser.rb index 42fb6bcc4..76604d5fe 100644 --- a/app/services/bulk_upload/sales/year2022/csv_parser.rb +++ b/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] end + def wrong_template_for_year? + false + end + private def headers diff --git a/app/services/bulk_upload/sales/year2023/csv_parser.rb b/app/services/bulk_upload/sales/year2023/csv_parser.rb index 1409d5510..6a1929b5c 100644 --- a/app/services/bulk_upload/sales/year2023/csv_parser.rb +++ b/app/services/bulk_upload/sales/year2023/csv_parser.rb @@ -1,7 +1,10 @@ require "csv" class BulkUpload::Sales::Year2023::CsvParser + include CollectionTimeHelper + MAX_COLUMNS = 142 + FORM_YEAR = 2023 attr_reader :path @@ -46,6 +49,12 @@ class BulkUpload::Sales::Year2023::CsvParser cols[field_numbers.find_index(field) + col_offset] 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 @@ -87,4 +96,12 @@ private @normalised_string 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 diff --git a/config/locales/en.yml b/config/locales/en.yml index 64bc5687f..8bf58586f 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -50,11 +50,13 @@ en: base: 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" + wrong_template: "Incorrect start dates, please ensure you have used the correct template" bulk_upload/sales/validator: attributes: base: 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" + wrong_template: "Incorrect sale dates, please ensure you have used the correct template" forms/bulk_upload_lettings/year: attributes: year: diff --git a/spec/services/bulk_upload/lettings/validator_spec.rb b/spec/services/bulk_upload/lettings/validator_spec.rb index f4fd98a12..192f2e0ed 100644 --- a/spec/services/bulk_upload/lettings/validator_spec.rb +++ b/spec/services/bulk_upload/lettings/validator_spec.rb @@ -191,6 +191,39 @@ RSpec.describe BulkUpload::Lettings::Validator do 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 diff --git a/spec/services/bulk_upload/sales/validator_spec.rb b/spec/services/bulk_upload/sales/validator_spec.rb index 1df7d067f..cb2860352 100644 --- a/spec/services/bulk_upload/sales/validator_spec.rb +++ b/spec/services/bulk_upload/sales/validator_spec.rb @@ -27,7 +27,49 @@ RSpec.describe BulkUpload::Sales::Validator do 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 describe "#call" do