From 095a24f6553a31e15955941de982249dd9b815a7 Mon Sep 17 00:00:00 2001 From: Phil Lee Date: Mon, 13 Mar 2023 13:24:23 +0000 Subject: [PATCH] port 23/24 bulk upload validations --- .../lettings/year2022/row_parser.rb | 7 +- .../lettings/year2023/row_parser.rb | 480 +++- .../lettings/year2023/row_parser_spec.rb | 2073 ++++++++--------- 3 files changed, 1496 insertions(+), 1064 deletions(-) diff --git a/app/services/bulk_upload/lettings/year2022/row_parser.rb b/app/services/bulk_upload/lettings/year2022/row_parser.rb index edb966ee5..69b7d9b4e 100644 --- a/app/services/bulk_upload/lettings/year2022/row_parser.rb +++ b/app/services/bulk_upload/lettings/year2022/row_parser.rb @@ -352,7 +352,12 @@ class BulkUpload::Lettings::Year2022::RowParser end def blank_row? - attribute_set.to_hash.reject { |k, _| %w[bulk_upload block_log_creation].include?(k) }.values.compact.empty? + attribute_set + .to_hash + .reject { |k, _| %w[bulk_upload block_log_creation].include?(k) } + .values + .compact + .empty? end def log diff --git a/app/services/bulk_upload/lettings/year2023/row_parser.rb b/app/services/bulk_upload/lettings/year2023/row_parser.rb index 06332a5a7..375e0ae2e 100644 --- a/app/services/bulk_upload/lettings/year2023/row_parser.rb +++ b/app/services/bulk_upload/lettings/year2023/row_parser.rb @@ -203,23 +203,23 @@ class BulkUpload::Lettings::Year2023::RowParser attribute :field_57, :string attribute :field_58, :integer attribute :field_59, :string - attribute :field_60, :integer + attribute :field_60, :string attribute :field_61, :string attribute :field_62, :integer attribute :field_63, :string - attribute :field_64, :integer + attribute :field_64, :string attribute :field_65, :string attribute :field_66, :integer attribute :field_67, :string - attribute :field_68, :integer + attribute :field_68, :string attribute :field_69, :string attribute :field_70, :integer attribute :field_71, :string - attribute :field_72, :integer + attribute :field_72, :string attribute :field_73, :string attribute :field_74, :integer attribute :field_75, :string - attribute :field_76, :integer + attribute :field_76, :string attribute :field_77, :string attribute :field_78, :integer attribute :field_79, :integer @@ -265,7 +265,7 @@ class BulkUpload::Lettings::Year2023::RowParser attribute :field_119, :integer attribute :field_120, :integer attribute :field_121, :integer - attribute :field_122, :integer + attribute :field_122, :decimal attribute :field_123, :integer attribute :field_124, :integer attribute :field_125, :integer @@ -279,10 +279,80 @@ class BulkUpload::Lettings::Year2023::RowParser attribute :field_133, :integer attribute :field_134, :decimal + validates :field_5, presence: { message: I18n.t("validations.not_answered", question: "letting type") }, + inclusion: { in: (1..12).to_a, message: I18n.t("validations.invalid_option", question: "letting type") } + validates :field_15, presence: { if: proc { [2, 4, 6, 8, 10, 12].include?(field_5) } } + + 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" } + 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" } + 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" } + 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" } + 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" } + 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" } + 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" } + 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" } + + validates :field_7, presence: { message: I18n.t("validations.not_answered", question: "tenancy start date (day)") } + validates :field_8, presence: { message: I18n.t("validations.not_answered", question: "tenancy start date (month)") } + validates :field_9, presence: { message: I18n.t("validations.not_answered", question: "tenancy start date (year)") } + + validates :field_9, format: { with: /\A\d{2}\z/, message: I18n.t("validations.setup.startdate.year_not_two_digits") } + + validate :validate_data_types + validate :validate_nulls + validate :validate_relevant_collection_window + validate :validate_la_with_local_housing_referral + validate :validate_cannot_be_la_referral_if_general_needs_and_la + validate :validate_leaving_reason_for_renewal + validate :validate_lettings_type_matches_bulk_upload + validate :validate_only_one_housing_needs_type + validate :validate_no_disabled_needs_conjunction + validate :validate_dont_know_disabled_needs_conjunction + validate :validate_no_and_dont_know_disabled_needs_conjunction + + validate :validate_owning_org_permitted + validate :validate_owning_org_owns_stock + validate :validate_owning_org_exists + validate :validate_owning_org_data_given + + validate :validate_managing_org_related + validate :validate_managing_org_exists + validate :validate_managing_org_data_given + + validate :validate_scheme_related + validate :validate_scheme_exists + validate :validate_scheme_data_given + + validate :validate_location_related + validate :validate_location_exists + validate :validate_location_data_given + def self.question_for_field(field) QUESTIONS[field] end + def valid? + errors.clear + + return true if blank_row? + + super + + log.valid? + + log.errors.each do |error| + fields = field_mapping_for_errors[error.attribute] || [] + + fields.each do |field| + unless errors.include?(field) + errors.add(field, error.type) + end + end + end + + errors.blank? + end + def blank_row? attribute_set .to_hash @@ -296,8 +366,384 @@ class BulkUpload::Lettings::Year2023::RowParser @log ||= LettingsLog.new(attributes_for_log) end + def block_log_creation! + self.block_log_creation = true + end + + def block_log_creation? + block_log_creation + end + private + def start_date + return if field_7.blank? || field_8.blank? || field_9.blank? + + Date.parse("20#{field_9.to_s.rjust(2, '0')}-#{field_8}-#{field_7}") + rescue StandardError + nil + end + + def validate_no_and_dont_know_disabled_needs_conjunction + if field_87 == 1 && field_88 == 1 + errors.add(:field_87, I18n.t("validations.household.housingneeds.no_and_dont_know_disabled_needs_conjunction")) + errors.add(:field_88, I18n.t("validations.household.housingneeds.no_and_dont_know_disabled_needs_conjunction")) + end + end + + def validate_dont_know_disabled_needs_conjunction + if field_88 == 1 && [field_83, field_84, field_85, field_86].compact.count.positive? + errors.add(:field_88, I18n.t("validations.household.housingneeds.dont_know_disabled_needs_conjunction")) + end + end + + def validate_no_disabled_needs_conjunction + if field_87 == 1 && [field_83, field_84, field_85, field_86].compact.count.positive? + errors.add(:field_87, I18n.t("validations.household.housingneeds.no_disabled_needs_conjunction")) + end + end + + def validate_only_one_housing_needs_type + if [field_83, field_84, field_85].compact.count.positive? + errors.add(:field_83, I18n.t("validations.household.housingneeds_type.only_one_option_permitted")) + errors.add(:field_84, I18n.t("validations.household.housingneeds_type.only_one_option_permitted")) + errors.add(:field_85, I18n.t("validations.household.housingneeds_type.only_one_option_permitted")) + end + end + + def validate_lettings_type_matches_bulk_upload + if [1, 3, 5, 7, 9, 11].include?(field_5) && !general_needs? + errors.add(:field_5, I18n.t("validations.setup.lettype.supported_housing_mismatch")) + end + + if [2, 4, 6, 8, 10, 12].include?(field_5) && !supported_housing? + errors.add(:field_5, I18n.t("validations.setup.lettype.general_needs_mismatch")) + end + end + + def validate_leaving_reason_for_renewal + if field_6 == 1 && ![40, 42].include?(field_102) + errors.add(:field_102, I18n.t("validations.household.reason.renewal_reason_needed")) + end + end + + def general_needs? + field_4 == 1 + end + + def supported_housing? + field_4 == 2 + end + + def validate_cannot_be_la_referral_if_general_needs_and_la + if field_119 == 4 && general_needs? && owning_organisation && owning_organisation.la? + errors.add :field_119, I18n.t("validations.household.referral.la_general_needs.prp_referred_by_la") + end + end + + def validate_la_with_local_housing_referral + if field_119 == 3 && owning_organisation && owning_organisation.la? + errors.add(:field_119, I18n.t("validations.household.referral.nominated_by_local_ha_but_la")) + end + end + + def validate_relevant_collection_window + return if start_date.blank? || bulk_upload.form.blank? + + unless bulk_upload.form.valid_start_date_for_form?(start_date) + errors.add(:field_7, I18n.t("validations.date.outside_collection_window")) + errors.add(:field_8, I18n.t("validations.date.outside_collection_window")) + errors.add(:field_9, I18n.t("validations.date.outside_collection_window")) + end + end + + def validate_data_types + unless attribute_set["field_5"].value_before_type_cast&.match?(/\A\d+\z/) + errors.add(:field_5, I18n.t("validations.invalid_number", question: "letting type")) + end + end + + def validate_nulls + field_mapping_for_errors.each do |error_key, fields| + question_id = error_key.to_s + question = questions.find { |q| q.id == question_id } + + next unless question + next if log.optional_fields.include?(question.id) + next if question.completed?(log) + + if setup_question?(question) + fields.each do |field| + if errors[field].present? + errors.add(field, I18n.t("validations.not_answered", question: question.check_answer_label&.downcase), category: :setup) + end + end + else + fields.each do |field| + unless errors.any? { |e| fields.include?(e.attribute) } + errors.add(field, I18n.t("validations.not_answered", question: question.check_answer_label&.downcase)) + end + end + end + end + end + + def validate_location_related + return if scheme.blank? || location.blank? + + unless location.scheme == scheme + block_log_creation! + errors.add(:field_16, "Scheme code must relate to a location that is owned by owning organisation or managing organisation") + end + end + + def location + return if scheme.nil? + + @location ||= scheme.locations.find_by_id_on_mulitple_fields(field_16) + end + + def validate_location_exists + if scheme && field_16.present? && location.nil? + errors.add(:field_16, "Location could be found with provided scheme code") + end + end + + def validate_location_data_given + if bulk_upload.supported_housing? && field_16.blank? + errors.add(:field_16, "The scheme code must be present", category: "setup") + end + end + + def validate_scheme_related + return unless field_15.present? && scheme.present? + + owned_by_owning_org = owning_organisation && scheme.owning_organisation == owning_organisation + owned_by_managing_org = managing_organisation && scheme.owning_organisation == managing_organisation + + unless owned_by_owning_org || owned_by_managing_org + block_log_creation! + errors.add(:field_15, "This management group code does not belong to your organisation, or any of your stock owners / managing agents") + end + end + + def validate_scheme_exists + if field_15.present? && scheme.nil? + errors.add(:field_15, "The management group code is not correct") + end + end + + def validate_scheme_data_given + if bulk_upload.supported_housing? && field_15.blank? + errors.add(:field_15, "The management group code is not correct", category: "setup") + end + end + + def validate_managing_org_related + if owning_organisation && managing_organisation && !owning_organisation.can_be_managed_by?(organisation: managing_organisation) + block_log_creation! + errors.add(:field_2, "This managing organisation does not have a relationship with the owning organisation") + end + end + + def validate_managing_org_exists + if managing_organisation.nil? + errors.delete(:field_2) + errors.add(:field_2, "The managing organisation code is incorrect") + end + end + + def validate_managing_org_data_given + if field_2.blank? + errors.add(:field_2, "The managing organisation code is incorrect", category: :setup) + end + end + + def validate_owning_org_owns_stock + if owning_organisation && !owning_organisation.holds_own_stock? + block_log_creation! + errors.delete(:field_1) + errors.add(:field_1, "The owning organisation code provided is for an organisation that does not own stock") + end + end + + def validate_owning_org_exists + if owning_organisation.nil? + errors.delete(:field_1) + errors.add(:field_1, "The owning organisation code is incorrect") + end + end + + def validate_owning_org_data_given + if field_1.blank? + errors.add(:field_1, "The owning organisation code is incorrect", category: :setup) + end + end + + def validate_owning_org_permitted + if owning_organisation && !bulk_upload.user.organisation.affiliated_stock_owners.include?(owning_organisation) + block_log_creation! + errors.delete(:field_1) + errors.add(:field_1, "You do not have permission to add logs for this owning organisation") + end + end + + def setup_question?(question) + log.form.setup_sections[0].subsections[0].questions.include?(question) + end + + def field_mapping_for_errors + { + lettype: [:field_5], + tenancycode: [:field_13], + postcode_known: %i[field_25 field_23 field_24], + postcode_full: %i[field_25 field_23 field_24], + la: %i[field_25], + owning_organisation: [:field_1], + managing_organisation: [:field_2], + owning_organisation_id: [:field_1], + managing_organisation_id: [:field_2], + renewal: [:field_6], + scheme: %i[field_15 field_16], + created_by: [], + needstype: [], + rent_type: %i[field_5 field_10 field_11], + startdate: %i[field_7 field_8 field_9], + unittype_gn: %i[field_29], + builtype: %i[field_30], + wchair: %i[field_31], + beds: %i[field_32], + joint: %i[field_39], + startertenancy: %i[field_40], + tenancy: %i[field_41], + tenancyother: %i[field_42], + tenancylength: %i[field_43], + declaration: %i[field_45], + + age1_known: %i[field_46], + age1: %i[field_46], + age2_known: %i[field_52], + age2: %i[field_52], + age3_known: %i[field_56], + age3: %i[field_56], + age4_known: %i[field_60], + age4: %i[field_60], + age5_known: %i[field_64], + age5: %i[field_64], + age6_known: %i[field_68], + age6: %i[field_68], + age7_known: %i[field_72], + age7: %i[field_72], + age8_known: %i[field_76], + age8: %i[field_76], + + sex1: %i[field_47], + sex2: %i[field_53], + sex3: %i[field_57], + sex4: %i[field_61], + sex5: %i[field_65], + sex6: %i[field_69], + sex7: %i[field_73], + sex8: %i[field_77], + + ethnic_group: %i[field_48], + ethnic: %i[field_48], + national: %i[field_49], + + relat2: %i[field_51], + relat3: %i[field_55], + relat4: %i[field_59], + relat5: %i[field_63], + relat6: %i[field_67], + relat7: %i[field_71], + relat8: %i[field_75], + + ecstat1: %i[field_50], + ecstat2: %i[field_54], + ecstat3: %i[field_58], + ecstat4: %i[field_62], + ecstat5: %i[field_66], + ecstat6: %i[field_70], + ecstat7: %i[field_74], + ecstat8: %i[field_78], + + armedforces: %i[field_79], + leftreg: %i[field_80], + reservist: %i[field_81], + preg_occ: %i[field_82], + housingneeds: %i[field_82], + + illness: %i[field_89], + + layear: %i[field_100], + waityear: %i[field_101], + reason: %i[field_102], + reasonother: %i[field_103], + prevten: %i[field_104], + homeless: %i[field_105], + + prevloc: %i[field_109], + previous_la_known: %i[field_109], + ppcodenk: %i[field_106], + ppostcode_full: %i[field_107 field_108], + + reasonpref: %i[field_110], + rp_homeless: %i[field_111], + rp_insan_unsat: %i[field_112], + rp_medwel: %i[field_113], + rp_hardship: %i[field_114], + rp_dontknow: %i[field_115], + + cbl: %i[field_116], + chr: %i[field_118], + cap: %i[field_117], + + referral: %i[field_119], + + net_income_known: %i[field_120], + earnings: %i[field_122], + incfreq: %i[field_121], + hb: %i[field_123], + benefits: %i[field_124], + + period: %i[field_126], + brent: %i[field_128], + scharge: %i[field_129], + pscharge: %i[field_130], + supcharg: %i[field_131], + tcharge: %i[field_132], + chcharge: %i[field_127], + household_charge: %i[field_125], + hbrentshortfall: %i[field_133], + tshortfall: %i[field_134], + + unitletas: %i[field_26], + rsnvac: %i[field_27], + sheltered: %i[field_44], + + illness_type_1: %i[field_98], + illness_type_2: %i[field_92], + illness_type_3: %i[field_95], + illness_type_4: %i[field_90], + illness_type_5: %i[field_91], + illness_type_6: %i[field_93], + illness_type_7: %i[field_94], + illness_type_8: %i[field_97], + illness_type_9: %i[field_96], + illness_type_10: %i[field_99], + + irproduct_other: %i[field_12], + + offered: %i[field_28], + propcode: %i[field_14], + + majorrepairs: %i[field_36 field_37 field_38], + mrcdate: %i[field_36 field_37 field_38], + + voiddate: %i[field_33 field_34 field_35], + } + end + def attribute_set @attribute_set ||= instance_variable_get(:@attributes) end @@ -336,28 +782,28 @@ private attributes["declaration"] = field_45 attributes["age1_known"] = field_46 == "R" ? 1 : 0 - attributes["age1"] = field_46 if attributes["age1_known"].zero? + attributes["age1"] = field_46 if attributes["age1_known"].zero? && field_46&.match(/\A\d{1,3}\z|\AR\z/) attributes["age2_known"] = field_52 == "R" ? 1 : 0 - attributes["age2"] = field_52 if attributes["age2_known"].zero? + attributes["age2"] = field_52 if attributes["age2_known"].zero? && field_52&.match(/\A\d{1,3}\z|\AR\z/) attributes["age3_known"] = field_56 == "R" ? 1 : 0 - attributes["age3"] = field_56 if attributes["age3_known"].zero? + attributes["age3"] = field_56 if attributes["age3_known"].zero? && field_56&.match(/\A\d{1,3}\z|\AR\z/) attributes["age4_known"] = field_60 == "R" ? 1 : 0 - attributes["age4"] = field_60 if attributes["age4_known"].zero? + attributes["age4"] = field_60 if attributes["age4_known"].zero? && field_60&.match(/\A\d{1,3}\z|\AR\z/) attributes["age5_known"] = field_64 == "R" ? 1 : 0 - attributes["age5"] = field_64 if attributes["age5_known"].zero? + attributes["age5"] = field_64 if attributes["age5_known"].zero? && field_64&.match(/\A\d{1,3}\z|\AR\z/) attributes["age6_known"] = field_68 == "R" ? 1 : 0 - attributes["age6"] = field_68 if attributes["age6_known"].zero? + attributes["age6"] = field_68 if attributes["age6_known"].zero? && field_68&.match(/\A\d{1,3}\z|\AR\z/) attributes["age7_known"] = field_72 == "R" ? 1 : 0 - attributes["age7"] = field_72 if attributes["age7_known"].zero? + attributes["age7"] = field_72 if attributes["age7_known"].zero? && field_72&.match(/\A\d{1,3}\z|\AR\z/) attributes["age8_known"] = field_76 == "R" ? 1 : 0 - attributes["age8"] = field_76 if attributes["age8_known"].zero? + 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 @@ -523,6 +969,8 @@ private 0 when nil rsnvac == 14 ? 1 : 0 + else + field_6 end end @@ -707,7 +1155,7 @@ private end def chr - case field_117 + case field_118 when 2 0 when 1 @@ -716,7 +1164,7 @@ private end def cap - case field_118 + case field_117 when 2 0 when 1 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 b7e0d3974..3a4e7f484 100644 --- a/spec/services/bulk_upload/lettings/year2023/row_parser_spec.rb +++ b/spec/services/bulk_upload/lettings/year2023/row_parser_spec.rb @@ -17,13 +17,14 @@ RSpec.describe BulkUpload::Lettings::Year2023::RowParser do let(:setup_section_params) do { bulk_upload:, - field_1: "2", - field_111: owning_org.old_visible_id, - field_113: managing_org.old_visible_id, - field_96: now.day.to_s, - field_97: now.month.to_s, - field_98: now.strftime("%g"), - field_134: "2", + field_1: owning_org.old_visible_id, + field_2: managing_org.old_visible_id, + field_4: "1", + field_5: "2", + field_6: "2", + field_7: now.day.to_s, + field_8: now.month.to_s, + field_9: now.strftime("%g"), } end @@ -184,7 +185,7 @@ RSpec.describe BulkUpload::Lettings::Year2023::RowParser do field_126: "4", field_128: "1234.56", - field_129: "321.32", + field_129: "43.32", field_130: "13.14", field_131: "101.11", field_132: "1500.19", @@ -216,1075 +217,1053 @@ RSpec.describe BulkUpload::Lettings::Year2023::RowParser do end end - # context "when setup section not complete" do - # let(:attributes) { { bulk_upload:, field_7: "123" } } + context "when setup section not complete" do + let(:attributes) { { bulk_upload:, field_13: "123" } } - # it "has errors on setup fields" do - # errors = parser.errors.select { |e| e.options[:category] == :setup }.map(&:attribute) + it "has errors on setup fields" do + errors = parser.errors.select { |e| e.options[:category] == :setup }.map(&:attribute) - # expect(errors).to eql(%i[field_1 field_129 field_130 field_98 field_97 field_96 field_111 field_113]) - # end - # end + expect(errors).to eql(%i[field_5 field_7 field_8 field_9 field_1 field_2]) + end + end - # describe "#field_1" do - # context "when null" do - # let(:attributes) { { bulk_upload:, field_1: nil, field_4: "1" } } + describe "#field_5" do + context "when null" do + let(:attributes) { { bulk_upload:, field_5: nil, field_15: "1" } } - # it "returns an error" do - # expect(parser.errors[:field_1]).to be_present - # end - # end + it "returns an error" do + expect(parser.errors[:field_5]).to be_present + end + end - # context "when incorrect data type" do - # let(:attributes) { { bulk_upload:, field_1: "foo" } } + context "when incorrect data type" do + let(:attributes) { { bulk_upload:, field_5: "foo" } } - # it "returns an error" do - # expect(parser.errors[:field_1]).to be_present - # end - # end + it "returns an error" do + expect(parser.errors[:field_5]).to be_present + end + end - # context "when unpermitted value" do - # let(:attributes) { { bulk_upload:, field_1: "101" } } + context "when unpermitted value" do + let(:attributes) { { bulk_upload:, field_5: "101" } } - # it "returns an error" do - # expect(parser.errors[:field_1]).to be_present - # end - # end + it "returns an error" do + expect(parser.errors[:field_5]).to be_present + end + end - # context "when valid" do - # let(:attributes) { { bulk_upload:, field_1: "1" } } + context "when valid" do + let(:attributes) { { bulk_upload:, field_5: "1", field_4: "1" } } - # it "does not return any errors" do - # expect(parser.errors[:field_1]).to be_blank - # end - # end + it "does not return any errors" do + expect(parser.errors[:field_5]).to be_blank + end + end - # context "when bulk upload is for general needs" do - # let(:bulk_upload) { create(:bulk_upload, :lettings, user:, needstype: "1") } + context "when bulk upload is for general needs" do + context "when general needs option selected" do + let(:attributes) { { bulk_upload:, field_5: "1", field_4: "1" } } - # context "when general needs option selected" do - # let(:attributes) { { bulk_upload:, field_1: "1" } } - - # it "is permitted" do - # expect(parser.errors[:field_1]).to be_blank - # end - # end - - # context "when supported housing option selected" do - # let(:attributes) { { bulk_upload:, field_1: "2" } } - - # it "is not permitted" do - # expect(parser.errors[:field_1]).to include("Lettings type must be a general needs type because you selected general needs when uploading the file") - # end - # end - # end - - # context "when bulk upload is for supported housing" do - # let(:bulk_upload) { create(:bulk_upload, :lettings, user:, needstype: "2") } - - # context "when general needs option selected" do - # let(:attributes) { { bulk_upload:, field_1: "1" } } - - # it "is not permitted" do - # expect(parser.errors[:field_1]).to include("Lettings type must be a supported housing type because you selected supported housing when uploading the file") - # end - # end - - # context "when supported housing option selected" do - # let(:attributes) { { bulk_upload:, field_1: "2" } } - - # it "is permitted" do - # expect(parser.errors[:field_1]).to be_blank - # end - # end - # end - # end - - # describe "#field_4" do - # context "when nullable not permitted" do - # let(:attributes) { { bulk_upload:, field_1: "2", field_4: nil } } - - # it "cannot be nulled" do - # expect(parser.errors[:field_4]).to be_present - # end - # end - - # context "when nullable permitted" do - # let(:attributes) { { bulk_upload:, field_1: "1", field_4: nil } } - - # it "can be nulled" do - # expect(parser.errors[:field_4]).to be_blank - # end - # end - - # context "when matching scheme cannot be found" do - # let(:attributes) { { bulk_upload:, field_1: "1", field_4: "123" } } - - # it "returns an error" do - # expect(parser.errors[:field_4]).to be_present - # end - # end - - # context "when scheme belongs to someone else" do - # let(:other_scheme) { create(:scheme, :with_old_visible_id) } - # let(:attributes) { { bulk_upload:, field_1: "1", field_4: other_scheme.old_visible_id, field_111: owning_org.old_visible_id } } - - # it "returns an error" do - # expect(parser.errors[:field_4]).to include("This management group code does not belong to your organisation, or any of your stock owners / managing agents") - # end - # end - - # context "when scheme belongs to owning org" do - # let(:scheme) { create(:scheme, :with_old_visible_id, owning_organisation: owning_org) } - # let(:attributes) { { bulk_upload:, field_1: "1", field_4: scheme.old_visible_id, field_111: owning_org.old_visible_id } } - - # it "does not return an error" do - # expect(parser.errors[:field_4]).to be_blank - # end - # end - - # context "when scheme belongs to managing org" do - # let(:scheme) { create(:scheme, :with_old_visible_id, owning_organisation: managing_org) } - # let(:attributes) { { bulk_upload:, field_1: "1", field_4: scheme.old_visible_id, field_113: managing_org.old_visible_id } } - - # it "does not return an error" do - # expect(parser.errors[:field_4]).to be_blank - # end - # end - # end - - # describe "#field_5" do - # context "when location does not exist" do - # let(:scheme) { create(:scheme, :with_old_visible_id, owning_organisation: owning_org) } - # let(:attributes) do - # { - # bulk_upload:, - # field_1: "1", - # field_4: scheme.old_visible_id, - # field_5: "dontexist", - # field_111: owning_org.old_visible_id, - # } - # end - - # it "returns an error" do - # expect(parser.errors[:field_5]).to be_present - # end - # end - - # context "when location exists" do - # let(:scheme) { create(:scheme, :with_old_visible_id, owning_organisation: owning_org) } - # let(:attributes) do - # { - # bulk_upload:, - # field_1: "1", - # field_4: scheme.old_visible_id, - # field_5: location.old_visible_id, - # field_111: owning_org.old_visible_id, - # } - # end - - # it "does not return an error" do - # expect(parser.errors[:field_5]).to be_blank - # end - # end - - # context "when location exists but not related" do - # let(:location) { create(:scheme, :with_old_visible_id) } - # let(:attributes) do - # { - # bulk_upload:, - # field_1: "1", - # field_4: scheme.old_visible_id, - # field_5: location.old_visible_id, - # field_111: owning_org.old_visible_id, - # } - # end - - # it "returns an error" do - # expect(parser.errors[:field_5]).to be_present - # end - # end - # end - - # describe "#field_7" do - # context "when null" do - # let(:attributes) { { bulk_upload:, field_7: nil } } - - # xit "returns an error" do - # expect(parser.errors[:field_7]).to be_present - # end - # end - # end - - # describe "#field_10" do - # context "when field_9 is 3 aka other" do - # let(:attributes) { { bulk_upload:, field_9: "3" } } - - # xit "returns an error" do - # expect(parser.errors[:field_10]).to be_present - # end - # end - # end - - # describe "#field_52" do # leaving reason - # context "when field_134 is 1 meaning it is a renewal" do - # context "when field_52 is 40" do - # let(:attributes) { { bulk_upload:, field_52: "40", field_134: "1" } } - - # it "is permitted" do - # expect(parser.errors[:field_52]).to be_blank - # end - # end - - # context "when field_52 is 42" do - # let(:attributes) { { bulk_upload:, field_52: "42", field_134: "1" } } - - # it "is permitted" do - # expect(parser.errors[:field_52]).to be_blank - # end - # end - - # context "when field_52 is not 40 or 42" do - # let(:attributes) { { bulk_upload:, field_52: "1", field_134: "1" } } - - # it "is not permitted" do - # expect(parser.errors[:field_52]).to be_present - # end - # end - # end - # end - - # describe "#field_55, #field_56, #field_57" do - # context "when more than one item selected" do - # let(:attributes) { { bulk_upload:, field_55: "1", field_56: "1" } } - - # it "is not permitted" do - # expect(parser.errors[:field_55]).to be_present - # expect(parser.errors[:field_56]).to be_present - # expect(parser.errors[:field_57]).to be_present - # end - # end - # end - - # describe "#field_59" do - # context "when 1 and another disability field selected" do - # let(:attributes) { { bulk_upload:, field_59: "1", field_58: "1" } } - - # it "is not permitted" do - # expect(parser.errors[:field_59]).to be_present - # end - # end - # end - - # describe "#field_60" do - # context "when 1 and another disability field selected" do - # let(:attributes) { { bulk_upload:, field_60: "1", field_58: "1" } } - - # it "is not permitted" do - # expect(parser.errors[:field_60]).to be_present - # end - # end - # end - - # describe "#field_59, #field_60" do - # context "when both 1" do - # let(:attributes) { { bulk_upload:, field_59: "1", field_60: "1" } } - - # it "is not permitted" do - # expect(parser.errors[:field_59]).to be_present - # expect(parser.errors[:field_60]).to be_present - # end - # end - # end - - # describe "#field_78" do # referral - # context "when 3 ie PRP nominated by LA and owning org is LA" do - # let(:attributes) { { bulk_upload:, field_78: "3", field_111: owning_org.old_visible_id } } - - # it "is not permitted" do - # expect(parser.errors[:field_78]).to be_present - # end - # end - - # context "when 4 ie referred by LA and is general needs and owning org is LA" do - # let(:attributes) { { bulk_upload:, field_78: "4", field_111: owning_org.old_visible_id.to_s } } - - # it "is not permitted" do - # expect(parser.errors[:field_78]).to be_present - # end - # end - - # context "when 4 ie referred by LA and is general needs and owning org is PRP" do - # let(:owning_org) { create(:organisation, :prp, :with_old_visible_id) } - - # let(:attributes) { { bulk_upload:, field_78: "4", field_111: owning_org.old_visible_id.to_s } } - - # it "is permitted" do - # expect(parser.errors[:field_78]).to be_blank - # end - # end - - # context "when 4 ie referred by LA and is not general needs" do - # let(:bulk_upload) { create(:bulk_upload, :lettings, user:, needstype: 2) } - # let(:attributes) { { bulk_upload:, field_78: "4" } } - - # it "is permitted" do - # expect(parser.errors[:field_78]).to be_blank - # end - # end - # end - - # describe "fields 96, 97, 98 => startdate" do - # context "when any one of these fields is blank" do - # let(:attributes) { { bulk_upload:, field_1: "1", field_96: nil, field_97: nil, field_98: nil } } - - # it "returns an error" do - # parser.valid? - - # expect(parser.errors[:field_96]).to be_present - # expect(parser.errors[:field_97]).to be_present - # expect(parser.errors[:field_98]).to be_present - # end - # end - - # context "when field 98 is 4 digits instead of 2" do - # let(:attributes) { { bulk_upload:, field_98: "2022" } } - - # it "returns an error" do - # parser.valid? - - # expect(parser.errors[:field_98]).to include("Tenancy start year must be 2 digits") - # end - # end - - # context "when invalid date given" do - # let(:attributes) { { bulk_upload:, field_1: "1", field_96: "a", field_97: "12", field_98: "2022" } } - - # it "does not raise an error" do - # expect { parser.valid? }.not_to raise_error - # end - # end - - # context "when inside of collection year" do - # let(:attributes) { { bulk_upload:, field_96: "1", field_97: "10", field_98: "22" } } - - # let(:bulk_upload) { create(:bulk_upload, :lettings, user:, year: 2022) } - - # it "does not return errors" do - # parser.valid? - - # expect(parser.errors[:field_96]).not_to be_present - # expect(parser.errors[:field_97]).not_to be_present - # expect(parser.errors[:field_98]).not_to be_present - # end - # end - - # context "when outside of collection year" do - # around do |example| - # Timecop.freeze(Date.new(2022, 4, 2)) do - # example.run - # end - # end + it "is permitted" do + expect(parser.errors[:field_5]).to be_blank + end + end - # let(:attributes) { { bulk_upload:, field_96: "1", field_97: "1", field_98: "22" } } + context "when supported housing option selected" do + let(:attributes) { { bulk_upload:, field_5: "2", field_4: "1" } } - # let(:bulk_upload) { create(:bulk_upload, :lettings, user:, year: 2022) } + it "is not permitted" do + expect(parser.errors[:field_5]).to include("Lettings type must be a general needs type because you selected general needs when uploading the file") + end + end + end - # it "returns errors" do - # parser.valid? - - # expect(parser.errors[:field_96]).to be_present - # expect(parser.errors[:field_97]).to be_present - # expect(parser.errors[:field_98]).to be_present - # end - # end - # end + context "when bulk upload is for supported housing" do + let(:bulk_upload) { create(:bulk_upload, :lettings, user:) } + + context "when general needs option selected" do + let(:attributes) { { bulk_upload:, field_5: "1", field_4: "2" } } + + it "is not permitted" do + expect(parser.errors[:field_5]).to include("Lettings type must be a supported housing type because you selected supported housing when uploading the file") + end + end + + context "when supported housing option selected" do + let(:attributes) { { bulk_upload:, field_5: "2", field_4: "2" } } + + it "is permitted" do + expect(parser.errors[:field_5]).to be_blank + end + end + end + end + + describe "#field_15" do + context "when nullable not permitted" do + let(:attributes) { { bulk_upload:, field_5: "2", field_15: nil } } + + it "cannot be nulled" do + expect(parser.errors[:field_15]).to be_present + end + end - # describe "#field_111" do # owning org - # context "when cannot find owning org" do - # let(:attributes) { { bulk_upload:, field_111: "donotexist" } } + context "when nullable permitted" do + let(:attributes) { { bulk_upload:, field_5: "1", field_15: nil } } - # it "is not permitted" do - # expect(parser.errors[:field_111]).to eql(["The owning organisation code is incorrect"]) - # end - # end + it "can be nulled" do + expect(parser.errors[:field_15]).to be_blank + end + end - # context "when org is not stock owning" do - # let(:owning_org) { create(:organisation, :with_old_visible_id, :does_not_own_stock) } + context "when matching scheme cannot be found" do + let(:attributes) { { bulk_upload:, field_5: "1", field_15: "123" } } - # let(:attributes) { { bulk_upload:, field_111: owning_org.old_visible_id } } + it "returns an error" do + expect(parser.errors[:field_15]).to be_present + end + end - # it "is not permitted" do - # expect(parser.errors[:field_111]).to eql(["The owning organisation code provided is for an organisation that does not own stock"]) - # end - - # it "blocks log creation" do - # expect(parser).to be_block_log_creation - # end - # end + context "when scheme belongs to someone else" do + let(:other_scheme) { create(:scheme, :with_old_visible_id) } + let(:attributes) { { bulk_upload:, field_5: "1", field_15: other_scheme.old_visible_id, field_1: owning_org.old_visible_id } } - # context "when not affiliated with owning org" do - # let(:unaffiliated_org) { create(:organisation, :with_old_visible_id) } - - # let(:attributes) { { bulk_upload:, field_111: unaffiliated_org.old_visible_id } } + it "returns an error" do + expect(parser.errors[:field_15]).to include("This management group code does not belong to your organisation, or any of your stock owners / managing agents") + end + end - # it "is not permitted" do - # expect(parser.errors[:field_111]).to eql(["You do not have permission to add logs for this owning organisation"]) - # end + context "when scheme belongs to owning org" do + let(:scheme) { create(:scheme, :with_old_visible_id, owning_organisation: owning_org) } + let(:attributes) { { bulk_upload:, field_5: "1", field_15: scheme.old_visible_id, field_1: owning_org.old_visible_id } } - # it "blocks log creation" do - # expect(parser).to be_block_log_creation - # end - # end - # end + it "does not return an error" do + expect(parser.errors[:field_15]).to be_blank + end + end - # describe "#field_113" do # managing org - # context "when cannot find managing org" do - # let(:attributes) { { bulk_upload:, field_113: "donotexist" } } - - # it "is not permitted" do - # expect(parser.errors[:field_113]).to eql(["The managing organisation code is incorrect"]) - # end - # end + context "when scheme belongs to managing org" do + let(:scheme) { create(:scheme, :with_old_visible_id, owning_organisation: managing_org) } + let(:attributes) { { bulk_upload:, field_5: "1", field_15: scheme.old_visible_id, field_2: managing_org.old_visible_id } } - # context "when not affiliated with managing org" do - # let(:unaffiliated_org) { create(:organisation, :with_old_visible_id) } + it "does not return an error" do + expect(parser.errors[:field_15]).to be_blank + end + end + end - # let(:attributes) { { bulk_upload:, field_111: owning_org.old_visible_id, field_113: unaffiliated_org.old_visible_id } } + describe "#field_16" do + context "when location does not exist" do + let(:scheme) { create(:scheme, :with_old_visible_id, owning_organisation: owning_org) } + let(:attributes) do + { + bulk_upload:, + field_5: "1", + field_15: scheme.old_visible_id, + field_16: "dontexist", + field_1: owning_org.old_visible_id, + } + end - # it "is not permitted" do - # expect(parser.errors[:field_113]).to eql(["This managing organisation does not have a relationship with the owning organisation"]) - # end + it "returns an error" do + expect(parser.errors[:field_16]).to be_present + end + end - # it "blocks log creation" do - # expect(parser).to be_block_log_creation - # end - # end - # end - - # describe "#field_134" do - # context "when an unpermitted value" do - # let(:attributes) { { bulk_upload:, field_134: 3 } } - - # it "has errors on the field" do - # expect(parser.errors[:field_134]).to be_present - # end - # end - # end - - # describe "#field_103" do - # context "when null" do - # let(:attributes) { setup_section_params.merge({ field_103: nil }) } - - # it "returns an error" do - # expect(parser.errors[:field_103]).to be_present - # end - - # it "populates with correct error message" do - # expect(parser.errors[:field_103]).to eql(["You must answer type of building"]) - # end - # end + context "when location exists" do + let(:scheme) { create(:scheme, :with_old_visible_id, owning_organisation: owning_org) } + let(:attributes) do + { + bulk_upload:, + field_5: "1", + field_15: scheme.old_visible_id, + field_16: location.old_visible_id, + field_1: owning_org.old_visible_id, + } + end - # context "when unpermitted values" do - # let(:attributes) { setup_section_params.merge({ field_103: "4" }) } + it "does not return an error" do + expect(parser.errors[:field_16]).to be_blank + end + end + + context "when location exists but not related" do + let(:location) { create(:scheme, :with_old_visible_id) } + let(:attributes) do + { + bulk_upload:, + field_5: "1", + field_15: scheme.old_visible_id, + field_16: location.old_visible_id, + field_1: owning_org.old_visible_id, + } + end + + it "returns an error" do + expect(parser.errors[:field_16]).to be_present + end + end + end + + describe "#field_102" do # leaving reason + context "when field_6 is 1 meaning it is a renewal" do + context "when field_102 is 40" do + let(:attributes) { { bulk_upload:, field_102: "40", field_6: "1" } } + + it "is permitted" do + expect(parser.errors[:field_102]).to be_blank + end + end + + context "when field_102 is 42" do + let(:attributes) { { bulk_upload:, field_102: "42", field_6: "1" } } + + it "is permitted" do + expect(parser.errors[:field_102]).to be_blank + end + end + + context "when field_102 is not 40 or 42" do + let(:attributes) { { bulk_upload:, field_102: "1", field_6: "1" } } + + it "is not permitted" do + expect(parser.errors[:field_102]).to be_present + end + end + end + end + + describe "#field_83, #field_84, #field_85" do + context "when more than one item selected" do + let(:attributes) { { bulk_upload:, field_83: "1", field_84: "1" } } + + it "is not permitted" do + expect(parser.errors[:field_83]).to be_present + expect(parser.errors[:field_84]).to be_present + expect(parser.errors[:field_85]).to be_present + end + end + end + + describe "#field_87" do + context "when 1 and another disability field selected" do + let(:attributes) { { bulk_upload:, field_87: "1", field_86: "1" } } + + it "is not permitted" do + expect(parser.errors[:field_87]).to be_present + end + end + end + + describe "#field_88" do + context "when 1 and another disability field selected" do + let(:attributes) { { bulk_upload:, field_88: "1", field_86: "1" } } + + it "is not permitted" do + expect(parser.errors[:field_88]).to be_present + end + end + end + + describe "#field_87, #field_88" do + context "when both 1" do + let(:attributes) { { bulk_upload:, field_87: "1", field_88: "1" } } + + it "is not permitted" do + expect(parser.errors[:field_87]).to be_present + expect(parser.errors[:field_88]).to be_present + end + end + end + + describe "#field_119" do # referral + context "when 3 ie PRP nominated by LA and owning org is LA" do + let(:attributes) { { bulk_upload:, field_119: "3", field_1: owning_org.old_visible_id } } + + it "is not permitted" do + expect(parser.errors[:field_119]).to be_present + end + end + + context "when 4 ie referred by LA and is general needs and owning org is LA" do + let(:attributes) { { bulk_upload:, field_119: "4", field_1: owning_org.old_visible_id.to_s, field_4: "1" } } + + it "is not permitted" do + expect(parser.errors[:field_119]).to be_present + end + end - # it "returns an error" do - # expect(parser.errors[:field_103]).to be_present - # end - # end - # end + context "when 4 ie referred by LA and is general needs and owning org is PRP" do + let(:owning_org) { create(:organisation, :prp, :with_old_visible_id) } + + let(:attributes) { { bulk_upload:, field_119: "4", field_1: owning_org.old_visible_id.to_s } } + + it "is permitted" do + expect(parser.errors[:field_119]).to be_blank + end + end + + context "when 4 ie referred by LA and is not general needs" do + let(:bulk_upload) { create(:bulk_upload, :lettings, user:) } + let(:attributes) { { bulk_upload:, field_119: "4", field_4: "2" } } + + it "is permitted" do + expect(parser.errors[:field_119]).to be_blank + end + end + end + + describe "fields 7, 8, 9 => startdate" do + context "when any one of these fields is blank" do + let(:attributes) { { bulk_upload:, field_5: "1", field_7: nil, field_8: nil, field_9: nil } } + + it "returns an error" do + parser.valid? + + expect(parser.errors[:field_7]).to be_present + expect(parser.errors[:field_8]).to be_present + expect(parser.errors[:field_9]).to be_present + end + end + + context "when field_9 is 4 digits instead of 2" do + let(:attributes) { { bulk_upload:, field_9: "2022" } } + + it "returns an error" do + parser.valid? + + expect(parser.errors[:field_9]).to include("Tenancy start year must be 2 digits") + end + end + + context "when invalid date given" do + let(:attributes) { { bulk_upload:, field_5: "1", field_7: "a", field_8: "12", field_9: "22" } } + + it "does not raise an error" do + expect { parser.valid? }.not_to raise_error + end + end + + context "when inside of collection year" do + let(:attributes) { { bulk_upload:, field_7: "1", field_8: "10", field_9: "22" } } + + let(:bulk_upload) { create(:bulk_upload, :lettings, user:, year: 2022) } + + it "does not return errors" do + parser.valid? + + expect(parser.errors[:field_7]).not_to be_present + expect(parser.errors[:field_8]).not_to be_present + expect(parser.errors[:field_9]).not_to be_present + end + end + + context "when outside of collection year" do + around do |example| + Timecop.freeze(Date.new(2022, 4, 2)) do + example.run + end + end + + let(:attributes) { { bulk_upload:, field_7: "1", field_8: "1", field_9: "22" } } + + let(:bulk_upload) { create(:bulk_upload, :lettings, user:, year: 2022) } + + it "returns errors" do + parser.valid? + + expect(parser.errors[:field_7]).to be_present + expect(parser.errors[:field_8]).to be_present + expect(parser.errors[:field_9]).to be_present + end + end + end + + describe "#field_1" do # owning org + context "when cannot find owning org" do + let(:attributes) { { bulk_upload:, field_1: "donotexist" } } + + it "is not permitted" do + expect(parser.errors[:field_1]).to eql(["The owning organisation code is incorrect"]) + end + end + + context "when org is not stock owning" do + let(:owning_org) { create(:organisation, :with_old_visible_id, :does_not_own_stock) } + + let(:attributes) { { bulk_upload:, field_1: owning_org.old_visible_id } } + + it "is not permitted" do + expect(parser.errors[:field_1]).to eql(["The owning organisation code provided is for an organisation that does not own stock"]) + end + + it "blocks log creation" do + expect(parser).to be_block_log_creation + end + end + + context "when not affiliated with owning org" do + let(:unaffiliated_org) { create(:organisation, :with_old_visible_id) } + + let(:attributes) { { bulk_upload:, field_1: unaffiliated_org.old_visible_id } } + + it "is not permitted" do + expect(parser.errors[:field_1]).to eql(["You do not have permission to add logs for this owning organisation"]) + end + + it "blocks log creation" do + expect(parser).to be_block_log_creation + end + end + end + + describe "#field_2" do # managing org + context "when cannot find managing org" do + let(:attributes) { { bulk_upload:, field_2: "donotexist" } } + + it "is not permitted" do + expect(parser.errors[:field_2]).to eql(["The managing organisation code is incorrect"]) + end + end + + context "when not affiliated with managing org" do + let(:unaffiliated_org) { create(:organisation, :with_old_visible_id) } + + let(:attributes) { { bulk_upload:, field_1: owning_org.old_visible_id, field_2: unaffiliated_org.old_visible_id } } + + it "is not permitted" do + expect(parser.errors[:field_2]).to eql(["This managing organisation does not have a relationship with the owning organisation"]) + end + + it "blocks log creation" do + expect(parser).to be_block_log_creation + end + end + end + + describe "#field_6" do + context "when an unpermitted value" do + let(:attributes) { { bulk_upload:, field_6: "3" } } + + it "has errors on the field" do + expect(parser.errors[:field_6]).to be_present + end + end + end + + describe "#field_30" do + context "when null" do + let(:attributes) { setup_section_params.merge({ field_30: nil }) } + + it "returns an error" do + expect(parser.errors[:field_30]).to be_present + end + + it "populates with correct error message" do + expect(parser.errors[:field_30]).to eql(["You must answer type of building"]) + end + end + + context "when unpermitted values" do + let(:attributes) { setup_section_params.merge({ field_30: "4" }) } + + it "returns an error" do + expect(parser.errors[:field_30]).to be_present + end + end + end end - # describe "#log" do - # [ - # %w[age1_known age1 field_12], - # %w[age2_known age2 field_13], - # %w[age3_known age3 field_14], - # %w[age4_known age4 field_15], - # %w[age5_known age5 field_16], - # %w[age6_known age6 field_17], - # %w[age7_known age7 field_18], - # %w[age8_known age8 field_19], - # ].each do |known, age, field| - # describe "##{known} and ##{age}" do - # context "when #{field} is R" do - # let(:attributes) { { bulk_upload:, field.to_s => "R" } } - - # it "sets ##{known} 1" do - # expect(parser.log.public_send(known)).to be(1) - # end - - # it "sets ##{age} to nil" do - # expect(parser.log.public_send(age)).to be_nil - # end - # end - - # context "when #{field} is a number" do - # let(:attributes) { { bulk_upload:, field.to_s => "50" } } - - # it "sets ##{known} to 0" do - # expect(parser.log.public_send(known)).to be(0) - # end - - # it "sets ##{age} to given age" do - # expect(parser.log.public_send(age)).to be(50) - # end - # end - # end - # end - - # describe "#location" do - # context "when lookup is via new core id" do - # let(:attributes) { { bulk_upload:, field_4: scheme.old_visible_id, field_5: location.id, field_111: owning_org } } - - # it "assigns the correct location" do - # expect(parser.log.location).to eql(location) - # end - # end - # end - - # describe "#scheme" do - # context "when lookup is via id prefixed with S" do - # let(:attributes) { { bulk_upload:, field_4: "S#{scheme.id}", field_111: owning_org } } - - # it "assigns the correct scheme" do - # expect(parser.log.scheme).to eql(scheme) - # end - # end - # end - - # describe "#owning_organisation" do - # context "when lookup is via id prefixed with ORG" do - # let(:attributes) { { bulk_upload:, field_111: "ORG#{owning_org.id}" } } - - # it "assigns the correct org" do - # expect(parser.log.owning_organisation).to eql(owning_org) - # end - # end - # end - - # describe "#managing_organisation" do - # context "when lookup is via id prefixed with ORG" do - # let(:attributes) { { bulk_upload:, field_113: "ORG#{managing_org.id}" } } - - # it "assigns the correct org" do - # expect(parser.log.managing_organisation).to eql(managing_org) - # end - # end - # end - - # describe "#cbl" do - # context "when field_75 is yes ie 1" do - # let(:attributes) { { bulk_upload:, field_75: 1 } } - - # it "sets value to 1" do - # expect(parser.log.cbl).to be(1) - # end - # end - - # context "when field_75 is no ie 2" do - # let(:attributes) { { bulk_upload:, field_75: 2 } } - - # it "sets value to 0" do - # expect(parser.log.cbl).to be(0) - # end - # end - # end - - # describe "#chr" do - # context "when field_76 is yes ie 1" do - # let(:attributes) { { bulk_upload:, field_76: 1 } } - - # it "sets value to 1" do - # expect(parser.log.chr).to be(1) - # end - # end - - # context "when field_76 is no ie 2" do - # let(:attributes) { { bulk_upload:, field_76: 2 } } - - # it "sets value to 0" do - # expect(parser.log.chr).to be(0) - # end - # end - # end - - # describe "#cap" do - # context "when field_77 is yes ie 1" do - # let(:attributes) { { bulk_upload:, field_77: 1 } } - - # it "sets value to 1" do - # expect(parser.log.cap).to be(1) - # end - # end - - # context "when field_77 is no ie 2" do - # let(:attributes) { { bulk_upload:, field_77: 2 } } - - # it "sets value to 0" do - # expect(parser.log.cap).to be(0) - # end - # end - # end - - # describe "#letting_allocation_unknown" do - # context "when field_75, 76, 77 are no ie 2" do - # let(:attributes) { { bulk_upload:, field_75: 2, field_76: 2, field_77: 2 } } - - # it "sets value to 1" do - # expect(parser.log.letting_allocation_unknown).to be(1) - # end - # end - - # context "when any one of field_75, 76, 77 is yes ie 1" do - # let(:attributes) { { bulk_upload:, field_75: 1 } } - - # it "sets value to 0" do - # expect(parser.log.letting_allocation_unknown).to be(0) - # end - # end - # end - - # describe "#renewal" do - # context "when field_134 is no ie 2" do - # let(:attributes) { { bulk_upload:, field_134: 2 } } - - # it "sets value to 0" do - # expect(parser.log.renewal).to eq(0) - # end - # end - - # context "when field_134 is null but rsnvac/field_106 is 14" do - # let(:attributes) { { bulk_upload:, field_134: "", field_106: "14" } } - - # it "sets renewal to 1" do - # expect(parser.log.renewal).to eq(1) - # end - # end - # end - - # describe "#sexN fields" do - # let(:attributes) do - # { - # bulk_upload:, - # field_20: "F", - # field_21: "M", - # field_22: "X", - # field_23: "R", - # field_24: "F", - # field_25: "M", - # field_26: "X", - # field_27: "R", - # } - # end - - # it "sets value from correct mapping" do - # expect(parser.log.sex1).to eql("F") - # expect(parser.log.sex2).to eql("M") - # expect(parser.log.sex3).to eql("X") - # expect(parser.log.sex4).to eql("R") - # expect(parser.log.sex5).to eql("F") - # expect(parser.log.sex6).to eql("M") - # expect(parser.log.sex7).to eql("X") - # expect(parser.log.sex8).to eql("R") - # end - # end - - # describe "#ecstatN fields" do - # let(:attributes) do - # { - # bulk_upload:, - # field_35: "1", - # field_36: "2", - # field_37: "6", - # field_38: "7", - # field_39: "8", - # field_40: "9", - # field_41: "0", - # field_42: "10", - # } - # end - - # it "sets value from correct mapping", aggregate_failures: true do - # expect(parser.log.ecstat1).to eq(1) - # expect(parser.log.ecstat2).to eq(2) - # expect(parser.log.ecstat3).to eq(6) - # expect(parser.log.ecstat4).to eq(7) - # expect(parser.log.ecstat5).to eq(8) - # expect(parser.log.ecstat6).to eq(9) - # expect(parser.log.ecstat7).to eq(0) - # expect(parser.log.ecstat8).to eq(10) - # end - # end - - # describe "#relatN fields" do - # let(:attributes) do - # { - # bulk_upload:, - # field_28: "P", - # field_29: "C", - # field_30: "X", - # field_31: "R", - # field_32: "P", - # field_33: "C", - # field_34: "X", - # } - # end - - # it "sets value from correct mapping", aggregate_failures: true do - # expect(parser.log.relat2).to eq("P") - # expect(parser.log.relat3).to eq("C") - # expect(parser.log.relat4).to eq("X") - # expect(parser.log.relat5).to eq("R") - # expect(parser.log.relat6).to eq("P") - # expect(parser.log.relat7).to eq("C") - # expect(parser.log.relat8).to eq("X") - # end - # end - - # describe "#net_income_known" do - # let(:attributes) { { bulk_upload:, field_51: "1" } } - - # it "sets value from correct mapping" do - # expect(parser.log.net_income_known).to eq(0) - # end - # end - - # describe "#unitletas" do - # let(:attributes) { { bulk_upload:, field_105: "1" } } - - # it "sets value from correct mapping" do - # expect(parser.log.unitletas).to eq(1) - # end - # end - - # describe "#rsnvac" do - # let(:attributes) { { bulk_upload:, field_106: "5" } } - - # it "sets value from correct mapping" do - # expect(parser.log.rsnvac).to eq(5) - # end - # end - - # describe "#sheltered" do - # let(:attributes) { { bulk_upload:, field_117: "1" } } - - # it "sets value from correct mapping" do - # expect(parser.log.sheltered).to eq(1) - # end - # end - - # describe "illness fields" do - # mapping = [ - # { attribute: :illness_type_1, field: :field_119 }, - # { attribute: :illness_type_2, field: :field_120 }, - # { attribute: :illness_type_3, field: :field_121 }, - # { attribute: :illness_type_4, field: :field_122 }, - # { attribute: :illness_type_5, field: :field_123 }, - # { attribute: :illness_type_6, field: :field_124 }, - # { attribute: :illness_type_7, field: :field_125 }, - # { attribute: :illness_type_8, field: :field_126 }, - # { attribute: :illness_type_9, field: :field_127 }, - # { attribute: :illness_type_10, field: :field_128 }, - # ] - - # mapping.each do |hash| - # describe "##{hash[:attribute]}" do - # context "when yes" do - # let(:attributes) { { bulk_upload:, hash[:field] => "1" } } - - # it "sets value from correct mapping" do - # expect(parser.log.public_send(hash[:attribute])).to eq(1) - # end - # end - - # context "when no" do - # let(:attributes) { { bulk_upload:, hash[:field] => "" } } - - # it "sets value from correct mapping" do - # expect(parser.log.public_send(hash[:attribute])).to be_nil - # end - # end - # end - # end - # end - - # describe "#irproduct_other" do - # let(:attributes) { { bulk_upload:, field_131: "some other product" } } - - # it "sets value to given free text string" do - # expect(parser.log.irproduct_other).to eql("some other product") - # end - # end - - # describe "#tenancyother" do - # let(:attributes) { { bulk_upload:, field_10: "some other tenancy" } } - - # it "sets value to given free text string" do - # expect(parser.log.tenancyother).to eql("some other tenancy") - # end - # end - - # describe "#tenancylength" do - # let(:attributes) { { bulk_upload:, field_11: "2" } } - - # it "sets value to given free text string" do - # expect(parser.log.tenancylength).to eq(2) - # end - # end - - # describe "#earnings" do - # let(:attributes) { { bulk_upload:, field_50: "104.50" } } - - # it "rounds to the nearest whole pound" do - # expect(parser.log.earnings).to eq(105) - # end - # end - - # describe "#reasonother" do - # let(:attributes) { { bulk_upload:, field_53: "some other reason" } } - - # it "sets value to given free text string" do - # expect(parser.log.reasonother).to eql("some other reason") - # end - # end - - # describe "#ppcodenk" do - # let(:attributes) { { bulk_upload:, field_65: "2" } } - - # it "sets correct value from mapping" do - # expect(parser.log.ppcodenk).to eq(0) - # end - # end - - # describe "#household_charge" do - # let(:attributes) { { bulk_upload:, field_86: "1" } } - - # it "sets correct value from mapping" do - # expect(parser.log.household_charge).to eq(1) - # end - # end - - # describe "#chcharge" do - # let(:attributes) { { bulk_upload:, field_85: "123.45" } } - - # it "sets value given" do - # expect(parser.log.chcharge).to eq(123.45) - # end - # end - - # describe "#tcharge" do - # let(:attributes) { { bulk_upload:, field_84: "123.45" } } - - # it "sets value given" do - # expect(parser.log.tcharge).to eq(123.45) - # end - # end - - # describe "#supcharg" do - # let(:attributes) { { bulk_upload:, field_83: "123.45" } } - - # it "sets value given" do - # expect(parser.log.supcharg).to eq(123.45) - # end - # end - - # describe "#pscharge" do - # let(:attributes) { { bulk_upload:, field_82: "123.45" } } - - # it "sets value given" do - # expect(parser.log.pscharge).to eq(123.45) - # end - # end - - # describe "#scharge" do - # let(:attributes) { { bulk_upload:, field_81: "123.45" } } - - # it "sets value given" do - # expect(parser.log.scharge).to eq(123.45) - # end - # end - - # describe "#offered" do - # let(:attributes) { { bulk_upload:, field_99: "3" } } - - # it "sets value given" do - # expect(parser.log.offered).to eq(3) - # end - # end - - # describe "#propcode" do - # let(:attributes) { { bulk_upload:, field_100: "abc123" } } - - # it "sets value given" do - # expect(parser.log.propcode).to eq("abc123") - # end - # end - - # describe "#mrcdate" do - # let(:attributes) { { bulk_upload:, field_92: "13", field_93: "12", field_94: "22" } } - - # it "sets value given" do - # expect(parser.log.mrcdate).to eq(Date.new(2022, 12, 13)) - # end - # end - - # describe "#majorrepairs" do - # context "when mrcdate given" do - # let(:attributes) { { bulk_upload:, field_92: "13", field_93: "12", field_94: "22" } } - - # it "sets #majorrepairs to 1" do - # expect(parser.log.majorrepairs).to eq(1) - # end - # end - - # context "when mrcdate not given" do - # let(:attributes) { { bulk_upload:, field_92: "", field_93: "", field_94: "" } } - - # it "sets #majorrepairs to 0" do - # expect(parser.log.majorrepairs).to eq(0) - # end - # end - # end - - # describe "#voiddate" do - # let(:attributes) { { bulk_upload:, field_89: "13", field_90: "12", field_91: "22" } } - - # it "sets value given" do - # expect(parser.log.voiddate).to eq(Date.new(2022, 12, 13)) - # end - # end - - # describe "#startdate" do - # let(:attributes) { { bulk_upload:, field_96: now.day.to_s, field_97: now.month.to_s, field_98: now.strftime("%g") } } - - # it "sets value given" do - # expect(parser.log.startdate).to eq(now) - # end - # end - - # describe "#postcode_full" do - # let(:attributes) { { bulk_upload:, field_108: " EC1N ", field_109: " 2TD " } } - - # it "strips whitespace" do - # expect(parser.log.postcode_full).to eql("EC1N 2TD") - # end - # end - - # describe "#la" do - # let(:attributes) { { bulk_upload:, field_107: "E07000223" } } - - # it "sets to given value" do - # expect(parser.log.la).to eql("E07000223") - # end - # end - - # describe "#prevloc" do - # let(:attributes) { { bulk_upload:, field_62: "E07000223" } } - - # it "sets to given value" do - # expect(parser.log.prevloc).to eql("E07000223") - # end - # end - - # describe "#previous_la_known" do - # context "when known" do - # let(:attributes) { { bulk_upload:, field_62: "E07000223" } } - - # it "sets to 1" do - # expect(parser.log.previous_la_known).to eq(1) - # end - # end - - # context "when not known" do - # let(:attributes) { { bulk_upload:, field_62: "" } } - - # it "sets to 0" do - # expect(parser.log.previous_la_known).to eq(0) - # end - # end - # end - - # describe "#first_time_property_let_as_social_housing" do - # context "when field_106 is 15, 16, or 17" do - # let(:attributes) { { bulk_upload:, field_106: %w[15 16 17].sample } } - - # it "sets to 1" do - # expect(parser.log.first_time_property_let_as_social_housing).to eq(1) - # end - # end - - # context "when field_106 is not 15, 16, or 17" do - # let(:attributes) { { bulk_upload:, field_106: "1" } } - - # it "sets to 0" do - # expect(parser.log.first_time_property_let_as_social_housing).to eq(0) - # end - # end - # end - - # describe "#housingneeds" do - # context "when no disabled needs" do - # let(:attributes) { { bulk_upload:, field_59: "1" } } - - # it "sets to 2" do - # expect(parser.log.housingneeds).to eq(2) - # end - # end - - # context "when dont know about disabled needs" do - # let(:attributes) { { bulk_upload:, field_60: "1" } } - - # it "sets to 3" do - # expect(parser.log.housingneeds).to eq(3) - # end - # end - # end - - # describe "#housingneeds_type" do - # context "when field_55 is 1" do - # let(:attributes) { { bulk_upload:, field_55: "1" } } - - # it "set to 0" do - # expect(parser.log.housingneeds_type).to eq(0) - # end - # end - - # context "when field_56 is 1" do - # let(:attributes) { { bulk_upload:, field_56: "1" } } - - # it "set to 1" do - # expect(parser.log.housingneeds_type).to eq(1) - # end - # end - - # context "when field_57 is 1" do - # let(:attributes) { { bulk_upload:, field_57: "1" } } - - # it "set to 2" do - # expect(parser.log.housingneeds_type).to eq(2) - # end - # end - # end - - # describe "#housingneeds_other" do - # context "when field_58 is 1" do - # let(:attributes) { { bulk_upload:, field_58: "1" } } - - # it "sets to 1" do - # expect(parser.log.housingneeds_other).to eq(1) - # end - # end - # end - # end - - # describe "#start_date" do - # context "when year of 9 is passed to represent 2009" do - # let(:attributes) { { bulk_upload:, field_96: "1", field_97: "1", field_98: "9" } } - - # it "uses the year 2009" do - # expect(parser.send(:start_date)).to eql(Date.new(2009, 1, 1)) - # end - # end - # end + describe "#log" do + [ + %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| + describe "##{known} and ##{age}" do + context "when #{field} is R" do + let(:attributes) { { bulk_upload:, field.to_s => "R" } } + + it "sets ##{known} 1" do + expect(parser.log.public_send(known)).to be(1) + end + + it "sets ##{age} to nil" do + expect(parser.log.public_send(age)).to be_nil + end + end + + context "when #{field} is a number" do + let(:attributes) { { bulk_upload:, field.to_s => "50" } } + + it "sets ##{known} to 0" do + expect(parser.log.public_send(known)).to be(0) + end + + it "sets ##{age} to given age" do + expect(parser.log.public_send(age)).to be(50) + end + end + end + end + + describe "#location" do + context "when lookup is via new core id" do + let(:attributes) { { bulk_upload:, field_15: scheme.old_visible_id, field_16: location.id, field_1: owning_org } } + + it "assigns the correct location" do + expect(parser.log.location).to eql(location) + end + end + end + + describe "#scheme" do + context "when lookup is via id prefixed with S" do + let(:attributes) { { bulk_upload:, field_15: "S#{scheme.id}", field_1: owning_org } } + + it "assigns the correct scheme" do + expect(parser.log.scheme).to eql(scheme) + end + end + end + + describe "#owning_organisation" do + context "when lookup is via id prefixed with ORG" do + let(:attributes) { { bulk_upload:, field_1: "ORG#{owning_org.id}" } } + + it "assigns the correct org" do + expect(parser.log.owning_organisation).to eql(owning_org) + end + end + end + + describe "#managing_organisation" do + context "when lookup is via id prefixed with ORG" do + let(:attributes) { { bulk_upload:, field_2: "ORG#{managing_org.id}" } } + + it "assigns the correct org" do + expect(parser.log.managing_organisation).to eql(managing_org) + end + end + end + + describe "#cbl" do + context "when field_116 is yes ie 1" do + let(:attributes) { { bulk_upload:, field_116: 1 } } + + it "sets value to 1" do + expect(parser.log.cbl).to be(1) + end + end + + context "when field_116 is no ie 2" do + let(:attributes) { { bulk_upload:, field_116: 2 } } + + it "sets value to 0" do + expect(parser.log.cbl).to be(0) + end + end + end + + describe "#chr" do + context "when field_118 is yes ie 1" do + let(:attributes) { { bulk_upload:, field_118: 1 } } + + it "sets value to 1" do + expect(parser.log.chr).to be(1) + end + end + + context "when field_118 is no ie 2" do + let(:attributes) { { bulk_upload:, field_118: 2 } } + + it "sets value to 0" do + expect(parser.log.chr).to be(0) + end + end + end + + describe "#cap" do + context "when field_117 is yes ie 1" do + let(:attributes) { { bulk_upload:, field_117: 1 } } + + it "sets value to 1" do + expect(parser.log.cap).to be(1) + end + end + + context "when field_117 is no ie 2" do + let(:attributes) { { bulk_upload:, field_117: 2 } } + + it "sets value to 0" do + expect(parser.log.cap).to be(0) + end + end + end + + describe "#letting_allocation_unknown" do + context "when field_116, 117, 118 are no ie 2" do + let(:attributes) { { bulk_upload:, field_116: 2, field_117: 2, field_118: 2 } } + + it "sets value to 1" do + expect(parser.log.letting_allocation_unknown).to be(1) + end + end + + context "when any one of field_116, 117, 118 is yes ie 1" do + let(:attributes) { { bulk_upload:, field_116: 1 } } + + it "sets value to 0" do + expect(parser.log.letting_allocation_unknown).to be(0) + end + end + end + + describe "#renewal" do + context "when field_6 is no ie 2" do + let(:attributes) { { bulk_upload:, field_6: 2 } } + + it "sets value to 0" do + expect(parser.log.renewal).to eq(0) + end + end + + context "when field_6 is null but rsnvac/field_27 is 14" do + let(:attributes) { { bulk_upload:, field_6: "", field_27: "14" } } + + it "sets renewal to 1" do + expect(parser.log.renewal).to eq(1) + end + end + end + + describe "#sexN fields" do + let(:attributes) do + { + bulk_upload:, + field_47: "F", + field_53: "M", + field_57: "X", + field_61: "R", + field_65: "F", + field_69: "M", + field_73: "X", + field_77: "R", + } + end + + it "sets value from correct mapping" do + expect(parser.log.sex1).to eql("F") + expect(parser.log.sex2).to eql("M") + expect(parser.log.sex3).to eql("X") + expect(parser.log.sex4).to eql("R") + expect(parser.log.sex5).to eql("F") + expect(parser.log.sex6).to eql("M") + expect(parser.log.sex7).to eql("X") + expect(parser.log.sex8).to eql("R") + end + end + + describe "#ecstatN fields" do + let(:attributes) do + { + bulk_upload:, + field_50: "1", + field_54: "2", + field_58: "6", + field_62: "7", + field_66: "8", + field_70: "9", + field_74: "0", + field_78: "10", + } + end + + it "sets value from correct mapping", aggregate_failures: true do + expect(parser.log.ecstat1).to eq(1) + expect(parser.log.ecstat2).to eq(2) + expect(parser.log.ecstat3).to eq(6) + expect(parser.log.ecstat4).to eq(7) + expect(parser.log.ecstat5).to eq(8) + expect(parser.log.ecstat6).to eq(9) + expect(parser.log.ecstat7).to eq(0) + expect(parser.log.ecstat8).to eq(10) + end + end + + describe "#relatN fields" do + let(:attributes) do + { + bulk_upload:, + field_51: "P", + field_55: "C", + field_59: "X", + field_63: "R", + field_67: "P", + field_71: "C", + field_75: "X", + } + end + + it "sets value from correct mapping", aggregate_failures: true do + expect(parser.log.relat2).to eq("P") + expect(parser.log.relat3).to eq("C") + expect(parser.log.relat4).to eq("X") + expect(parser.log.relat5).to eq("R") + expect(parser.log.relat6).to eq("P") + expect(parser.log.relat7).to eq("C") + expect(parser.log.relat8).to eq("X") + end + end + + describe "#net_income_known" do + let(:attributes) { { bulk_upload:, field_120: "1" } } + + it "sets value from correct mapping" do + expect(parser.log.net_income_known).to eq(0) + end + end + + describe "#unitletas" do + let(:attributes) { { bulk_upload:, field_26: "1" } } + + it "sets value from correct mapping" do + expect(parser.log.unitletas).to eq(1) + end + end + + describe "#rsnvac" do + let(:attributes) { { bulk_upload:, field_27: "5" } } + + it "sets value from correct mapping" do + expect(parser.log.rsnvac).to eq(5) + end + end + + describe "#sheltered" do + let(:attributes) { { bulk_upload:, field_44: "1" } } + + it "sets value from correct mapping" do + expect(parser.log.sheltered).to eq(1) + end + end + + describe "illness fields" do + mapping = [ + { attribute: :illness_type_1, field: :field_98 }, + { attribute: :illness_type_2, field: :field_92 }, + { attribute: :illness_type_3, field: :field_95 }, + { attribute: :illness_type_4, field: :field_90 }, + { attribute: :illness_type_5, field: :field_91 }, + { attribute: :illness_type_6, field: :field_93 }, + { attribute: :illness_type_7, field: :field_94 }, + { attribute: :illness_type_8, field: :field_97 }, + { attribute: :illness_type_9, field: :field_96 }, + { attribute: :illness_type_10, field: :field_99 }, + ] + + mapping.each do |hash| + describe "##{hash[:attribute]}" do + context "when yes" do + let(:attributes) { { bulk_upload:, hash[:field] => "1" } } + + it "sets value from correct mapping" do + expect(parser.log.public_send(hash[:attribute])).to eq(1) + end + end + + context "when no" do + let(:attributes) { { bulk_upload:, hash[:field] => "" } } + + it "sets value from correct mapping" do + expect(parser.log.public_send(hash[:attribute])).to be_nil + end + end + end + end + end + + describe "#irproduct_other" do + let(:attributes) { { bulk_upload:, field_12: "some other product" } } + + it "sets value to given free text string" do + expect(parser.log.irproduct_other).to eql("some other product") + end + end + + describe "#tenancyother" do + let(:attributes) { { bulk_upload:, field_42: "some other tenancy" } } + + it "sets value to given free text string" do + expect(parser.log.tenancyother).to eql("some other tenancy") + end + end + + describe "#tenancylength" do + let(:attributes) { { bulk_upload:, field_43: "2" } } + + it "sets value to given free text string" do + expect(parser.log.tenancylength).to eq(2) + end + end + + describe "#earnings" do + let(:attributes) { { bulk_upload:, field_122: "104.50" } } + + it "rounds to the nearest whole pound" do + expect(parser.log.earnings).to eq(105) + end + end + + describe "#reasonother" do + let(:attributes) { { bulk_upload:, field_103: "some other reason" } } + + it "sets value to given free text string" do + expect(parser.log.reasonother).to eql("some other reason") + end + end + + describe "#ppcodenk" do + let(:attributes) { { bulk_upload:, field_106: "2" } } + + it "sets correct value from mapping" do + expect(parser.log.ppcodenk).to eq(0) + end + end + + describe "#household_charge" do + let(:attributes) { { bulk_upload:, field_125: "1" } } + + it "sets correct value from mapping" do + expect(parser.log.household_charge).to eq(1) + end + end + + describe "#chcharge" do + let(:attributes) { { bulk_upload:, field_127: "123.45" } } + + it "sets value given" do + expect(parser.log.chcharge).to eq(123.45) + end + end + + describe "#tcharge" do + let(:attributes) { { bulk_upload:, field_132: "123.45" } } + + it "sets value given" do + expect(parser.log.tcharge).to eq(123.45) + end + end + + describe "#supcharg" do + let(:attributes) { { bulk_upload:, field_131: "123.45" } } + + it "sets value given" do + expect(parser.log.supcharg).to eq(123.45) + end + end + + describe "#pscharge" do + let(:attributes) { { bulk_upload:, field_130: "123.45" } } + + it "sets value given" do + expect(parser.log.pscharge).to eq(123.45) + end + end + + describe "#scharge" do + let(:attributes) { { bulk_upload:, field_129: "123.45" } } + + it "sets value given" do + expect(parser.log.scharge).to eq(123.45) + end + end + + describe "#offered" do + let(:attributes) { { bulk_upload:, field_28: "3" } } + + it "sets value given" do + expect(parser.log.offered).to eq(3) + end + end + + describe "#propcode" do + let(:attributes) { { bulk_upload:, field_14: "abc123" } } + + it "sets value given" do + expect(parser.log.propcode).to eq("abc123") + end + end + + describe "#mrcdate" do + let(:attributes) { { bulk_upload:, field_36: "13", field_37: "12", field_38: "22" } } + + it "sets value given" do + expect(parser.log.mrcdate).to eq(Date.new(2022, 12, 13)) + end + end + + describe "#majorrepairs" do + context "when mrcdate given" do + let(:attributes) { { bulk_upload:, field_36: "13", field_37: "12", field_38: "22" } } + + it "sets #majorrepairs to 1" do + expect(parser.log.majorrepairs).to eq(1) + end + end + + context "when mrcdate not given" do + let(:attributes) { { bulk_upload:, field_36: "", field_37: "", field_38: "" } } + + it "sets #majorrepairs to 0" do + expect(parser.log.majorrepairs).to eq(0) + end + end + end + + describe "#voiddate" do + let(:attributes) { { bulk_upload:, field_33: "13", field_34: "12", field_35: "22" } } + + it "sets value given" do + expect(parser.log.voiddate).to eq(Date.new(2022, 12, 13)) + end + end + + describe "#startdate" do + let(:attributes) { { bulk_upload:, field_7: now.day.to_s, field_8: now.month.to_s, field_9: now.strftime("%g") } } + + it "sets value given" do + expect(parser.log.startdate).to eq(now) + end + end + + describe "#postcode_full" do + let(:attributes) { { bulk_upload:, field_23: " EC1N ", field_24: " 2TD " } } + + it "strips whitespace" do + expect(parser.log.postcode_full).to eql("EC1N 2TD") + end + end + + describe "#la" do + let(:attributes) { { bulk_upload:, field_25: "E07000223" } } + + it "sets to given value" do + expect(parser.log.la).to eql("E07000223") + end + end + + describe "#prevloc" do + let(:attributes) { { bulk_upload:, field_109: "E07000223" } } + + it "sets to given value" do + expect(parser.log.prevloc).to eql("E07000223") + end + end + + describe "#previous_la_known" do + context "when known" do + let(:attributes) { { bulk_upload:, field_109: "E07000223" } } + + it "sets to 1" do + expect(parser.log.previous_la_known).to eq(1) + end + end + + context "when not known" do + let(:attributes) { { bulk_upload:, field_109: "" } } + + it "sets to 0" do + expect(parser.log.previous_la_known).to eq(0) + end + end + end + + describe "#first_time_property_let_as_social_housing" do + context "when field_27 is 15, 16, or 17" do + let(:attributes) { { bulk_upload:, field_27: %w[15 16 17].sample } } + + it "sets to 1" do + expect(parser.log.first_time_property_let_as_social_housing).to eq(1) + end + end + + context "when field_27 is not 15, 16, or 17" do + let(:attributes) { { bulk_upload:, field_27: "1" } } + + it "sets to 0" do + expect(parser.log.first_time_property_let_as_social_housing).to eq(0) + end + end + end + + describe "#housingneeds" do + context "when no disabled needs" do + let(:attributes) { { bulk_upload:, field_87: "1" } } + + it "sets to 2" do + expect(parser.log.housingneeds).to eq(2) + end + end + + context "when dont know about disabled needs" do + let(:attributes) { { bulk_upload:, field_88: "1" } } + + it "sets to 3" do + expect(parser.log.housingneeds).to eq(3) + end + end + end + + describe "#housingneeds_type" do + context "when field_83 is 1" do + let(:attributes) { { bulk_upload:, field_83: "1" } } + + it "set to 0" do + expect(parser.log.housingneeds_type).to eq(0) + end + end + + context "when field_84 is 1" do + let(:attributes) { { bulk_upload:, field_84: "1" } } + + it "set to 1" do + expect(parser.log.housingneeds_type).to eq(1) + end + end + + context "when field_85 is 1" do + let(:attributes) { { bulk_upload:, field_85: "1" } } + + it "set to 2" do + expect(parser.log.housingneeds_type).to eq(2) + end + end + end + + describe "#housingneeds_other" do + context "when field_58 is 1" do + let(:attributes) { { bulk_upload:, field_86: "1" } } + + it "sets to 1" do + expect(parser.log.housingneeds_other).to eq(1) + end + end + end + end + + describe "#start_date" do + context "when year of 9 is passed to represent 2009" do + let(:attributes) { { bulk_upload:, field_7: "1", field_8: "1", field_9: "9" } } + + it "uses the year 2009" do + expect(parser.send(:start_date)).to eql(Date.new(2009, 1, 1)) + end + end + end end