From 3b83614ab51fe20dd9095d75d1a4fc01688b8526 Mon Sep 17 00:00:00 2001 From: Arthur Campbell <51094020+arfacamble@users.noreply.github.com> Date: Thu, 18 May 2023 13:06:10 +0100 Subject: [PATCH] CLDC-2313 add bulk upload validation buyer 1 cannot be child (#1634) * add a bespoke validation to the row parser when buyer 1 is uploaded with working situation child, this should be validated with a custom message. a test for this case was also created * replicate the work from the last commit for the 2023 row parser and associated test file * lint correction remove blank lines --- .../bulk_upload/sales/year2022/row_parser.rb | 13 ++++++++++--- .../bulk_upload/sales/year2023/row_parser.rb | 9 ++++++++- .../sales/year2022/row_parser_spec.rb | 19 +++++++++++-------- .../sales/year2023/row_parser_spec.rb | 19 +++++++++++-------- 4 files changed, 40 insertions(+), 20 deletions(-) diff --git a/app/services/bulk_upload/sales/year2022/row_parser.rb b/app/services/bulk_upload/sales/year2022/row_parser.rb index 83523d3e8..6dfb6512d 100644 --- a/app/services/bulk_upload/sales/year2022/row_parser.rb +++ b/app/services/bulk_upload/sales/year2022/row_parser.rb @@ -274,6 +274,7 @@ class BulkUpload::Sales::Year2022::RowParser validates :field_114, presence: { message: I18n.t("validations.not_answered", question: "company buyer") }, if: :outright_sale?, on: :after_log validates :field_109, presence: { message: I18n.t("validations.not_answered", question: "more than 2 buyers") }, if: :joint_purchase?, on: :after_log + validate :validate_buyer1_economic_status, on: :before_log validate :validate_nulls, on: :after_log validate :validate_valid_radio_option, on: :before_log @@ -812,7 +813,7 @@ private end def owning_organisation - Organisation.find_by_id_on_multiple_fields(field_92) + @owning_organisation ||= Organisation.find_by_id_on_multiple_fields(field_92) end def created_by @@ -946,7 +947,7 @@ private end else fields.each do |field| - unless errors.any? { |e| fields.include?(e.attribute) } + if errors.none? { |e| fields.include?(e.attribute) } errors.add(field, I18n.t("validations.not_answered", question: question.check_answer_label&.downcase)) end end @@ -971,7 +972,7 @@ private end else fields.each do |field| - unless errors.any? { |e| fields.include?(e.attribute) } + if errors.none? { |e| fields.include?(e.attribute) } errors.add(field, I18n.t("validations.invalid_option", question: QUESTIONS[field])) end end @@ -1021,4 +1022,10 @@ private errors.add(:field_1, error_message) # Purchaser code end end + + def validate_buyer1_economic_status + if field_24 == 9 + errors.add(:field_24, "Buyer 1 cannot be a child under 16") + end + end end diff --git a/app/services/bulk_upload/sales/year2023/row_parser.rb b/app/services/bulk_upload/sales/year2023/row_parser.rb index 205db7726..de23cb41e 100644 --- a/app/services/bulk_upload/sales/year2023/row_parser.rb +++ b/app/services/bulk_upload/sales/year2023/row_parser.rb @@ -396,6 +396,7 @@ class BulkUpload::Sales::Year2023::RowParser }, on: :after_log + validate :validate_buyer1_economic_status, on: :before_log validate :validate_nulls, on: :after_log validate :validate_valid_radio_option, on: :before_log @@ -1019,7 +1020,7 @@ private end def owning_organisation - Organisation.find_by_id_on_multiple_fields(field_1) + @owning_organisation ||= Organisation.find_by_id_on_multiple_fields(field_1) end def created_by @@ -1219,4 +1220,10 @@ private end end end + + def validate_buyer1_economic_status + if field_35 == 9 + errors.add(:field_35, "Buyer 1 cannot be a child under 16") + end + end end diff --git a/spec/services/bulk_upload/sales/year2022/row_parser_spec.rb b/spec/services/bulk_upload/sales/year2022/row_parser_spec.rb index 7966d4921..53ecaa01a 100644 --- a/spec/services/bulk_upload/sales/year2022/row_parser_spec.rb +++ b/spec/services/bulk_upload/sales/year2022/row_parser_spec.rb @@ -107,14 +107,6 @@ RSpec.describe BulkUpload::Sales::Year2022::RowParser do } end - around do |example| - FormHandler.instance.use_real_forms! - - example.run - - FormHandler.instance.use_fake_forms! - end - describe "#blank_row?" do context "when a new object" do it "returns true" do @@ -469,6 +461,17 @@ RSpec.describe BulkUpload::Sales::Year2022::RowParser do end end + describe "field_24" do # ecstat1 + context "when buyer 1 is marked as a child" do + let(:attributes) { valid_attributes.merge({ field_24: 9 }) } + + it "a custom validation is applied" do + validation_message = "Buyer 1 cannot be a child under 16" + expect(parser.errors[:field_24]).to include validation_message + end + end + end + describe "fields 2, 3, 4 => saledate" do context "when all of these fields are blank" do let(:attributes) { setup_section_params.merge({ field_2: nil, field_3: nil, field_4: nil }) } diff --git a/spec/services/bulk_upload/sales/year2023/row_parser_spec.rb b/spec/services/bulk_upload/sales/year2023/row_parser_spec.rb index 3c480962a..4c248d7f2 100644 --- a/spec/services/bulk_upload/sales/year2023/row_parser_spec.rb +++ b/spec/services/bulk_upload/sales/year2023/row_parser_spec.rb @@ -110,14 +110,6 @@ RSpec.describe BulkUpload::Sales::Year2023::RowParser do } end - around do |example| - FormHandler.instance.use_real_forms! - - example.run - - FormHandler.instance.use_fake_forms! - end - describe "#blank_row?" do context "when a new object" do it "returns true" do @@ -705,6 +697,17 @@ RSpec.describe BulkUpload::Sales::Year2023::RowParser do end end + describe "field_35" do # ecstat1 + context "when buyer 1 is marked as a child" do + let(:attributes) { valid_attributes.merge({ field_35: "9" }) } + + it "a custom validation is applied" do + validation_message = "Buyer 1 cannot be a child under 16" + expect(parser.errors[:field_35]).to include validation_message + end + end + end + describe "#field_36" do # will buyer1 live in property? context "when not a possible value" do let(:attributes) { valid_attributes.merge({ field_36: "3" }) }