diff --git a/app/services/bulk_upload/lettings/year2023/row_parser.rb b/app/services/bulk_upload/lettings/year2023/row_parser.rb index 2ee81b2bc..708c1c9e9 100644 --- a/app/services/bulk_upload/lettings/year2023/row_parser.rb +++ b/app/services/bulk_upload/lettings/year2023/row_parser.rb @@ -362,13 +362,13 @@ class BulkUpload::Lettings::Year2023::RowParser on: :after_log validates :field_46, format: { with: /\A\d{1,3}\z|\AR\z/, message: "Age of person 1 must be a number or the letter R" }, on: :after_log - validates :field_52, format: { with: /\A\d{1,3}\z|\AR\z/, message: "Age of person 2 must be a number or the letter R" }, allow_blank: true, on: :after_log - validates :field_56, format: { with: /\A\d{1,3}\z|\AR\z/, message: "Age of person 3 must be a number or the letter R" }, allow_blank: true, on: :after_log - validates :field_60, format: { with: /\A\d{1,3}\z|\AR\z/, message: "Age of person 4 must be a number or the letter R" }, allow_blank: true, on: :after_log - validates :field_64, format: { with: /\A\d{1,3}\z|\AR\z/, message: "Age of person 5 must be a number or the letter R" }, allow_blank: true, on: :after_log - validates :field_68, format: { with: /\A\d{1,3}\z|\AR\z/, message: "Age of person 6 must be a number or the letter R" }, allow_blank: true, on: :after_log - validates :field_72, format: { with: /\A\d{1,3}\z|\AR\z/, message: "Age of person 7 must be a number or the letter R" }, allow_blank: true, on: :after_log - validates :field_76, format: { with: /\A\d{1,3}\z|\AR\z/, message: "Age of person 8 must be a number or the letter R" }, allow_blank: true, on: :after_log + validates :field_52, format: { with: /\A\d{1,3}\z|\AR\z/, message: "Age of person 2 must be a number or the letter R" }, on: :after_log, if: proc { details_known?(2).zero? } + validates :field_56, format: { with: /\A\d{1,3}\z|\AR\z/, message: "Age of person 3 must be a number or the letter R" }, on: :after_log, if: proc { details_known?(3).zero? } + validates :field_60, format: { with: /\A\d{1,3}\z|\AR\z/, message: "Age of person 4 must be a number or the letter R" }, on: :after_log, if: proc { details_known?(4).zero? } + validates :field_64, format: { with: /\A\d{1,3}\z|\AR\z/, message: "Age of person 5 must be a number or the letter R" }, on: :after_log, if: proc { details_known?(5).zero? } + validates :field_68, format: { with: /\A\d{1,3}\z|\AR\z/, message: "Age of person 6 must be a number or the letter R" }, on: :after_log, if: proc { details_known?(6).zero? } + validates :field_72, format: { with: /\A\d{1,3}\z|\AR\z/, message: "Age of person 7 must be a number or the letter R" }, on: :after_log, if: proc { details_known?(7).zero? } + validates :field_76, format: { with: /\A\d{1,3}\z|\AR\z/, message: "Age of person 8 must be a number or the letter R" }, on: :after_log, if: proc { details_known?(8).zero? } validate :validate_needs_type_present, on: :after_log validate :validate_data_types, on: :after_log @@ -1079,28 +1079,28 @@ private attributes["declaration"] = field_45 attributes["age1_known"] = age1_known? - attributes["age1"] = field_46 if attributes["age1_known"].zero? && field_46&.match(/\A\d{1,3}\z|\AR\z/) + attributes["age1"] = field_46 if attributes["age1_known"]&.zero? && field_46&.match(/\A\d{1,3}\z|\AR\z/) attributes["age2_known"] = age2_known? - attributes["age2"] = field_52 if attributes["age2_known"].zero? && field_52&.match(/\A\d{1,3}\z|\AR\z/) + attributes["age2"] = field_52 if attributes["age2_known"]&.zero? && field_52&.match(/\A\d{1,3}\z|\AR\z/) attributes["age3_known"] = age3_known? - attributes["age3"] = field_56 if attributes["age3_known"].zero? && field_56&.match(/\A\d{1,3}\z|\AR\z/) + attributes["age3"] = field_56 if attributes["age3_known"]&.zero? && field_56&.match(/\A\d{1,3}\z|\AR\z/) attributes["age4_known"] = age4_known? - attributes["age4"] = field_60 if attributes["age4_known"].zero? && field_60&.match(/\A\d{1,3}\z|\AR\z/) + attributes["age4"] = field_60 if attributes["age4_known"]&.zero? && field_60&.match(/\A\d{1,3}\z|\AR\z/) attributes["age5_known"] = age5_known? - attributes["age5"] = field_64 if attributes["age5_known"].zero? && field_64&.match(/\A\d{1,3}\z|\AR\z/) + attributes["age5"] = field_64 if attributes["age5_known"]&.zero? && field_64&.match(/\A\d{1,3}\z|\AR\z/) attributes["age6_known"] = age6_known? - attributes["age6"] = field_68 if attributes["age6_known"].zero? && field_68&.match(/\A\d{1,3}\z|\AR\z/) + attributes["age6"] = field_68 if attributes["age6_known"]&.zero? && field_68&.match(/\A\d{1,3}\z|\AR\z/) attributes["age7_known"] = age7_known? - attributes["age7"] = field_72 if attributes["age7_known"].zero? && field_72&.match(/\A\d{1,3}\z|\AR\z/) + attributes["age7"] = field_72 if attributes["age7_known"]&.zero? && field_72&.match(/\A\d{1,3}\z|\AR\z/) attributes["age8_known"] = age8_known? - attributes["age8"] = field_76 if attributes["age8_known"].zero? && field_76&.match(/\A\d{1,3}\z|\AR\z/) + attributes["age8"] = field_76 if attributes["age8_known"]&.zero? && field_76&.match(/\A\d{1,3}\z|\AR\z/) attributes["sex1"] = field_47 attributes["sex2"] = field_53 @@ -1383,7 +1383,6 @@ private def age1_known? return 1 if field_46 == "R" - return 1 if field_46.blank? 0 end @@ -1400,9 +1399,6 @@ private define_method("age#{hash[:person]}_known?") do return 1 if public_send(hash[:field]) == "R" return 0 if send("person_#{hash[:person]}_present?") - return 1 if public_send(hash[:field]).blank? - - 0 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 485a412ba..d5dca1c04 100644 --- a/app/services/bulk_upload/sales/year2023/row_parser.rb +++ b/app/services/bulk_upload/sales/year2023/row_parser.rb @@ -788,22 +788,22 @@ private attributes["noint"] = 2 if field_28 == 1 attributes["age1_known"] = age1_known? - attributes["age1"] = field_30 if attributes["age1_known"].zero? && field_30&.match(/\A\d{1,3}\z|\AR\z/) + attributes["age1"] = field_30 if attributes["age1_known"]&.zero? && field_30&.match(/\A\d{1,3}\z|\AR\z/) attributes["age2_known"] = age2_known? - attributes["age2"] = field_38 if attributes["age2_known"].zero? && field_38&.match(/\A\d{1,3}\z|\AR\z/) + attributes["age2"] = field_38 if attributes["age2_known"]&.zero? && field_38&.match(/\A\d{1,3}\z|\AR\z/) attributes["age3_known"] = age3_known? - attributes["age3"] = field_47 if attributes["age3_known"].zero? && field_47&.match(/\A\d{1,3}\z|\AR\z/) + attributes["age3"] = field_47 if attributes["age3_known"]&.zero? && field_47&.match(/\A\d{1,3}\z|\AR\z/) attributes["age4_known"] = age4_known? - attributes["age4"] = field_51 if attributes["age4_known"].zero? && field_51&.match(/\A\d{1,3}\z|\AR\z/) + attributes["age4"] = field_51 if attributes["age4_known"]&.zero? && field_51&.match(/\A\d{1,3}\z|\AR\z/) attributes["age5_known"] = age5_known? - attributes["age5"] = field_55 if attributes["age5_known"].zero? && field_55&.match(/\A\d{1,3}\z|\AR\z/) + attributes["age5"] = field_55 if attributes["age5_known"]&.zero? && field_55&.match(/\A\d{1,3}\z|\AR\z/) attributes["age6_known"] = age6_known? - attributes["age6"] = field_59 if attributes["age6_known"].zero? && field_59&.match(/\A\d{1,3}\z|\AR\z/) + attributes["age6"] = field_59 if attributes["age6_known"]&.zero? && field_59&.match(/\A\d{1,3}\z|\AR\z/) attributes["sex1"] = field_31 attributes["sex2"] = field_39 @@ -973,7 +973,6 @@ private def age1_known? return 1 if field_30 == "R" - return 1 if field_30.blank? 0 end @@ -988,9 +987,6 @@ private define_method("age#{hash[:person]}_known?") do return 1 if public_send(hash[:field]) == "R" return 0 if send("person_#{hash[:person]}_present?") - return 1 if public_send(hash[:field]).blank? - - 0 end end diff --git a/spec/services/bulk_upload/lettings/year2023/row_parser_spec.rb b/spec/services/bulk_upload/lettings/year2023/row_parser_spec.rb index b1dfb676d..522b3388a 100644 --- a/spec/services/bulk_upload/lettings/year2023/row_parser_spec.rb +++ b/spec/services/bulk_upload/lettings/year2023/row_parser_spec.rb @@ -1722,25 +1722,44 @@ RSpec.describe BulkUpload::Lettings::Year2023::RowParser do end [ - %w[age1_known age1 field_46], - %w[age2_known age2 field_52], - %w[age3_known age3 field_56], - %w[age4_known age4 field_60], - %w[age5_known age5 field_64], - %w[age6_known age6 field_68], - %w[age7_known age7 field_72], - %w[age8_known age8 field_76], - ].each do |known, age, field| + %w[age1_known details_known_1 age1 field_46 field_51 field_53], + %w[age2_known details_known_2 age2 field_52 field_51 field_53], + %w[age3_known details_known_3 age3 field_56 field_55 field_57], + %w[age4_known details_known_4 age4 field_60 field_59 field_61], + %w[age5_known details_known_5 age5 field_64 field_63 field_65], + %w[age6_known details_known_6 age6 field_68 field_67 field_69], + %w[age7_known details_known_7 age7 field_72 field_71 field_73], + %w[age8_known details_known_8 age8 field_76 field_75 field_77], + ].each do |known, details_known, age, field, relationship, gender| describe "##{known} and ##{age}" do context "when #{field} is blank" do - let(:attributes) { { bulk_upload:, field.to_s => nil, field_4: 1 } } + context "and person details are not given" do + let(:attributes) { { bulk_upload:, field.to_sym => nil, field_4: 1, relationship.to_sym => nil, gender.to_sym => nil } } - it "sets ##{known} 1" do - expect(parser.log.public_send(known)).to be(1) + it "does not set ##{known}" do + unless known == "age1_known" + expect(parser.log.public_send(known)).to be_nil + end + end + + it "sets ##{details_known} to no" do + unless details_known == "details_known_1" + expect(parser.log.public_send(details_known)).to eq(1) + end + end + + it "sets ##{age} to nil" do + expect(parser.log.public_send(age)).to be_nil + end end - it "sets ##{age} to nil" do - expect(parser.log.public_send(age)).to be_nil + context "and person details are given" do + let(:attributes) { { bulk_upload:, field.to_sym => nil, field_4: 1, relationship.to_sym => "C", gender.to_sym => "X" } } + + it "does not set ##{age}" do + parser.valid? + expect(parser.errors[field.to_sym]).to include(/must be a number or the letter R/) + end end end 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 9cbdc7cdc..58a077a1b 100644 --- a/spec/services/bulk_upload/sales/year2023/row_parser_spec.rb +++ b/spec/services/bulk_upload/sales/year2023/row_parser_spec.rb @@ -789,23 +789,42 @@ RSpec.describe BulkUpload::Sales::Year2023::RowParser do end [ - %w[age1_known age1 field_30], - %w[age2_known age2 field_38], - %w[age3_known age3 field_47], - %w[age4_known age4 field_51], - %w[age5_known age5 field_55], - %w[age6_known age6 field_59], - ].each do |known, age, field| + %w[age1_known details_known_1 age1 field_30 field_37 field_39], + %w[age2_known details_known_2 age2 field_38 field_37 field_39], + %w[age3_known details_known_3 age3 field_47 field_46 field_48], + %w[age4_known details_known_4 age4 field_51 field_50 field_52], + %w[age5_known details_known_5 age5 field_55 field_54 field_56], + %w[age6_known details_known_6 age6 field_59 field_58 field_60], + ].each do |known, details_known, age, field, relationship, gender| describe "##{known} and ##{age}" do context "when #{field} is blank" do - let(:attributes) { { bulk_upload:, field.to_s => nil } } + context "and person details are blank" do + let(:attributes) { setup_section_params.merge({ field.to_s => nil, relationship.to_sym => nil, gender.to_sym => nil, field_29: "1", field_45: "5" }) } - it "sets ##{known} 1" do - expect(parser.log.public_send(known)).to be(1) + it "does not set ##{known}" do + unless known == "age1_known" + expect(parser.log.public_send(known)).to be_nil + end + end + + it "sets ##{details_known} to no" do + unless details_known == "details_known_1" + expect(parser.log.public_send(details_known)).to eq(2) + end + end + + it "sets ##{age} to nil" do + expect(parser.log.public_send(age)).to be_nil + end end - it "sets ##{age} to nil" do - expect(parser.log.public_send(age)).to be_nil + context "and person details are given" do + let(:attributes) { setup_section_params.merge({ field.to_sym => nil, relationship.to_sym => "C", gender.to_sym => "X", field_29: "1", field_45: "5" }) } + + it "does not set ##{age}" do + parser.valid? + expect(parser.errors[field.to_sym]).to include(/You must answer/) + end end end