From dd4006043bf2b64c31630145be6a5bd8db1a29e3 Mon Sep 17 00:00:00 2001 From: Arthur Campbell Date: Tue, 9 May 2023 12:58:11 +0100 Subject: [PATCH] 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 --- .../bulk_upload/sales/year2022/row_parser.rb | 11 +++++++-- .../sales/year2022/row_parser_spec.rb | 23 +++++++++++++++---- 2 files changed, 27 insertions(+), 7 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..e0fcc051c 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 @@ -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/spec/services/bulk_upload/sales/year2022/row_parser_spec.rb b/spec/services/bulk_upload/sales/year2022/row_parser_spec.rb index 7966d4921..2d35fca5d 100644 --- a/spec/services/bulk_upload/sales/year2022/row_parser_spec.rb +++ b/spec/services/bulk_upload/sales/year2022/row_parser_spec.rb @@ -107,13 +107,13 @@ RSpec.describe BulkUpload::Sales::Year2022::RowParser do } end - around do |example| - FormHandler.instance.use_real_forms! + # around do |example| + # FormHandler.instance.use_real_forms! - example.run + # example.run - FormHandler.instance.use_fake_forms! - end + # FormHandler.instance.use_fake_forms! + # end describe "#blank_row?" do context "when a new object" do @@ -469,6 +469,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 }) } @@ -619,6 +630,8 @@ RSpec.describe BulkUpload::Sales::Year2022::RowParser do end end end + + end describe "inferences" do