Browse Source

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
pull/1634/head
Arthur Campbell 3 years ago
parent
commit
dd4006043b
  1. 11
      app/services/bulk_upload/sales/year2022/row_parser.rb
  2. 23
      spec/services/bulk_upload/sales/year2022/row_parser_spec.rb

11
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_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 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_nulls, on: :after_log
validate :validate_valid_radio_option, on: :before_log validate :validate_valid_radio_option, on: :before_log
@ -946,7 +947,7 @@ private
end end
else else
fields.each do |field| 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)) errors.add(field, I18n.t("validations.not_answered", question: question.check_answer_label&.downcase))
end end
end end
@ -971,7 +972,7 @@ private
end end
else else
fields.each do |field| 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])) errors.add(field, I18n.t("validations.invalid_option", question: QUESTIONS[field]))
end end
end end
@ -1021,4 +1022,10 @@ private
errors.add(:field_1, error_message) # Purchaser code errors.add(:field_1, error_message) # Purchaser code
end end
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 end

23
spec/services/bulk_upload/sales/year2022/row_parser_spec.rb

@ -107,13 +107,13 @@ RSpec.describe BulkUpload::Sales::Year2022::RowParser do
} }
end end
around do |example| # around do |example|
FormHandler.instance.use_real_forms! # FormHandler.instance.use_real_forms!
example.run # example.run
FormHandler.instance.use_fake_forms! # FormHandler.instance.use_fake_forms!
end # end
describe "#blank_row?" do describe "#blank_row?" do
context "when a new object" do context "when a new object" do
@ -469,6 +469,17 @@ RSpec.describe BulkUpload::Sales::Year2022::RowParser do
end end
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 describe "fields 2, 3, 4 => saledate" do
context "when all of these fields are blank" 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 }) } 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
end end
end end
describe "inferences" do describe "inferences" do

Loading…
Cancel
Save