Browse Source

CLDC-3217 Validate 24/25 sales field numbers (#2233)

* Validate 24/25 sales field numbers

* Add fields check to 2023 sales
pull/2251/head^2
kosiakkatrina 10 months ago committed by GitHub
parent
commit
83f43b21ee
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 9
      app/services/bulk_upload/sales/validator.rb
  2. 7
      app/services/bulk_upload/sales/year2023/csv_parser.rb
  3. 7
      app/services/bulk_upload/sales/year2024/csv_parser.rb
  4. 4
      spec/services/bulk_upload/sales/year2023/csv_parser_spec.rb
  5. 4
      spec/services/bulk_upload/sales/year2024/csv_parser_spec.rb

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

@ -5,6 +5,7 @@ class BulkUpload::Sales::Validator
attr_reader :bulk_upload, :path attr_reader :bulk_upload, :path
validate :validate_file_not_empty validate :validate_file_not_empty
validate :validate_field_numbers_count
validate :validate_max_columns validate :validate_max_columns
validate :validate_missing_required_headers validate :validate_missing_required_headers
validate :validate_correct_template validate :validate_correct_template
@ -166,6 +167,14 @@ private
if csv_parser.missing_required_headers? if csv_parser.missing_required_headers?
errors.add :base, I18n.t("activemodel.errors.models.bulk_upload/sales/validator.attributes.base.no_headers", guidance_link: bulk_upload_sales_log_url(id: "guidance", form: { year: bulk_upload.year }, host: ENV["APP_HOST"], anchor: "using-the-bulk-upload-template")) errors.add :base, I18n.t("activemodel.errors.models.bulk_upload/sales/validator.attributes.base.no_headers", guidance_link: bulk_upload_sales_log_url(id: "guidance", form: { year: bulk_upload.year }, host: ENV["APP_HOST"], anchor: "using-the-bulk-upload-template"))
end
end
def validate_field_numbers_count
return if halt_validations?
unless csv_parser.correct_field_count?
errors.add(:base, :wrong_field_numbers_count)
halt_validations! halt_validations!
end end
end end

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

@ -4,6 +4,7 @@ class BulkUpload::Sales::Year2023::CsvParser
include CollectionTimeHelper include CollectionTimeHelper
MAX_COLUMNS = 142 MAX_COLUMNS = 142
FIELDS = 135
FORM_YEAR = 2023 FORM_YEAR = 2023
attr_reader :path attr_reader :path
@ -59,6 +60,12 @@ class BulkUpload::Sales::Year2023::CsvParser
false false
end end
def correct_field_count?
valid_field_numbers_count = field_numbers.count { |f| f != "field_blank" }
valid_field_numbers_count == FIELDS
end
private private
def default_field_numbers def default_field_numbers

7
app/services/bulk_upload/sales/year2024/csv_parser.rb

@ -3,6 +3,7 @@ require "csv"
class BulkUpload::Sales::Year2024::CsvParser class BulkUpload::Sales::Year2024::CsvParser
include CollectionTimeHelper include CollectionTimeHelper
FIELDS = 131
MAX_COLUMNS = 142 MAX_COLUMNS = 142
FORM_YEAR = 2024 FORM_YEAR = 2024
@ -59,6 +60,12 @@ class BulkUpload::Sales::Year2024::CsvParser
!with_headers? !with_headers?
end end
def correct_field_count?
valid_field_numbers_count = field_numbers.count { |f| f != "field_blank" }
valid_field_numbers_count == FIELDS
end
private private
def default_field_numbers def default_field_numbers

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

@ -28,6 +28,10 @@ RSpec.describe BulkUpload::Sales::Year2023::CsvParser do
it "parses csv correctly" do it "parses csv correctly" do
expect(service.row_parsers[0].field_19).to eql(log.uprn) expect(service.row_parsers[0].field_19).to eql(log.uprn)
end end
it "counts the number of valid field numbers correctly" do
expect(service).to be_correct_field_count
end
end end
context "when parsing csv with headers in arbitrary order" do context "when parsing csv with headers in arbitrary order" do

4
spec/services/bulk_upload/sales/year2024/csv_parser_spec.rb

@ -28,6 +28,10 @@ RSpec.describe BulkUpload::Sales::Year2024::CsvParser do
it "parses csv correctly" do it "parses csv correctly" do
expect(service.row_parsers[0].field_22).to eql(log.uprn) expect(service.row_parsers[0].field_22).to eql(log.uprn)
end end
it "counts the number of valid field numbers correctly" do
expect(service).to be_correct_field_count
end
end end
context "when parsing csv with headers in arbitrary order" do context "when parsing csv with headers in arbitrary order" do

Loading…
Cancel
Save