require "rails_helper" RSpec.describe Validations::Sales::HouseholdValidations do subject(:household_validator) { validator_class.new } let(:validator_class) { Class.new { include Validations::Sales::HouseholdValidations } } describe "#validate_number_of_other_people_living_in_the_property" do context "when within permitted bounds" do let(:record) { build(:sales_log, hholdcount: 2) } it "does not add an error" do household_validator.validate_number_of_other_people_living_in_the_property(record) expect(record.errors[:hholdcount]).not_to be_present end end context "when blank" do let(:record) { build(:sales_log, hholdcount: nil) } it "does not add an error" do household_validator.validate_number_of_other_people_living_in_the_property(record) expect(record.errors[:hholdcount]).not_to be_present end end context "when below lower bound" do let(:record) { build(:sales_log, hholdcount: -1) } it "adds an error" do household_validator.validate_number_of_other_people_living_in_the_property(record) expect(record.errors[:hholdcount]).to be_present end end context "when higher than upper bound" do let(:record) { build(:sales_log, hholdcount: 5) } it "adds an error" do household_validator.validate_number_of_other_people_living_in_the_property(record) expect(record.errors[:hholdcount]).to be_present end end end describe "household member validations" do let(:record) { build(:sales_log) } it "validates that only 1 partner exists" do record.relat2 = "P" record.relat3 = "P" household_validator.validate_household_number_of_other_members(record) expect(record.errors["relat2"]) .to include(match I18n.t("validations.household.relat.one_partner")) expect(record.errors["relat3"]) .to include(match I18n.t("validations.household.relat.one_partner")) expect(record.errors["relat4"]) .not_to include(match I18n.t("validations.household.relat.one_partner")) end it "expects that a tenant can have a partner" do record.relat3 = "P" household_validator.validate_household_number_of_other_members(record) expect(record.errors["base"]).to be_empty end context "when the household contains a person under 16" do it "expects that person is a child of the tenant" do record.age2 = 14 record.relat2 = "C" household_validator.validate_household_number_of_other_members(record) expect(record.errors["relat2"]).to be_empty expect(record.errors["age2"]).to be_empty end it "validates that a person under 16 must not be a partner of the buyer" do record.age2 = 14 record.relat2 = "P" household_validator.validate_household_number_of_other_members(record) expect(record.errors["relat2"]) .to include(match I18n.t("validations.household.relat.partner_under_16")) expect(record.errors["age2"]) .to include(match I18n.t("validations.household.age.partner_under_16")) end it "validates that person's economic status must be Child" do record.age2 = 14 record.ecstat2 = 1 household_validator.validate_household_number_of_other_members(record) expect(record.errors["ecstat2"]) .to include(match I18n.t("validations.household.ecstat.child_under_16", person_num: 2)) expect(record.errors["age2"]) .to include(match I18n.t("validations.household.age.child_under_16", person_num: 2)) end it "expects that person's economic status is Child" do record.age2 = 14 record.ecstat2 = 9 household_validator.validate_household_number_of_other_members(record) expect(record.errors["ecstat2"]).to be_empty expect(record.errors["age2"]).to be_empty end it "validates that a person with economic status 'child' must be under 16" do record.age2 = 21 record.ecstat2 = 9 household_validator.validate_household_number_of_other_members(record) expect(record.errors["ecstat2"]) .to include(match I18n.t("validations.household.ecstat.child_over_16", person_num: 2)) expect(record.errors["age2"]) .to include(match I18n.t("validations.household.age.child_over_16", person_num: 2)) end end it "validates that a person over 20 must not be a child of the buyer" do record.age2 = 21 record.relat2 = "C" household_validator.validate_household_number_of_other_members(record) expect(record.errors["relat2"]) .to include(match I18n.t("validations.household.relat.child_over_20")) expect(record.errors["age2"]) .to include(match I18n.t("validations.household.age.child_over_20")) end it "adds errors for a person aged 16-19 who is a student but not a child of the buyer" do record.age2 = 18 record.ecstat2 = "7" record.relat2 = "P" household_validator.validate_household_number_of_other_members(record) expect(record.errors["relat2"]) .to include(match I18n.t("validations.household.relat.student_16_19.must_be_child")) expect(record.errors["age2"]) .to include(match I18n.t("validations.household.age.student_16_19.cannot_be_16_19.student_not_child")) expect(record.errors["ecstat2"]) .to include(match I18n.t("validations.household.ecstat.student_16_19.cannot_be_student.16_19_not_child")) end it "adds errors for a person aged 16-19 who is a child of the buyer but not a student" do record.age2 = 17 record.ecstat2 = "1" record.relat2 = "C" household_validator.validate_household_number_of_other_members(record) expect(record.errors["relat2"]) .to include(match I18n.t("validations.household.relat.student_16_19.cannot_be_child.16_19_not_student")) expect(record.errors["age2"]) .to include(match I18n.t("validations.household.age.student_16_19.cannot_be_16_19.child_not_student")) expect(record.errors["ecstat2"]) .to include(match I18n.t("validations.household.ecstat.student_16_19.must_be_student")) end it "adds errors for a person who is a child of the buyer and a student but not aged 16-19" do record.age2 = 14 record.ecstat2 = "7" record.relat2 = "C" household_validator.validate_household_number_of_other_members(record) expect(record.errors["relat2"]) .to include(match I18n.t("validations.household.relat.student_16_19.cannot_be_child.student_not_16_19")) expect(record.errors["age2"]) .to include(match I18n.t("validations.household.age.student_16_19.must_be_16_19")) expect(record.errors["ecstat2"]) .to include(match I18n.t("validations.household.ecstat.student_16_19.cannot_be_student.child_not_16_19")) end context "when it is a joint purchase and both buyers are over 64" do let(:record) { FactoryBot.build(:sales_log, jointpur: 1, age1: 65, age2: 66, type: 24) } it "does not add an error" do household_validator.validate_buyers_age_for_old_persons_shared_ownership(record) expect(record.errors).not_to be_present end end context "when it is a joint purchase and first buyer is over 64" do let(:record) { FactoryBot.build(:sales_log, jointpur: 1, age1: 65, age2: 40, type: 24) } it "does not add an error" do household_validator.validate_buyers_age_for_old_persons_shared_ownership(record) expect(record.errors).not_to be_present end end context "when it is a joint purchase and second buyer is over 64" do let(:record) { FactoryBot.build(:sales_log, jointpur: 1, age1: 43, age2: 64, type: 24) } it "does not add an error" do household_validator.validate_buyers_age_for_old_persons_shared_ownership(record) expect(record.errors).not_to be_present end end context "when it is a joint purchase and neither of the buyers are over 64" do let(:record) { FactoryBot.build(:sales_log, jointpur: 1, age1: 43, age2: 33, type: 24) } it "adds an error" do household_validator.validate_buyers_age_for_old_persons_shared_ownership(record) expect(record.errors["age1"]) .to include(match I18n.t("validations.household.old_persons_shared_ownership")) expect(record.errors["age2"]) .to include(match I18n.t("validations.household.old_persons_shared_ownership")) expect(record.errors["type"]) .to include(match I18n.t("validations.household.old_persons_shared_ownership")) end end context "when it is a joint purchase and first buyer is under 64 and the second buyers' age is unknown" do let(:record) { FactoryBot.build(:sales_log, jointpur: 1, age1: 43, age2_known: 1, type: 24) } it "adds an error" do household_validator.validate_buyers_age_for_old_persons_shared_ownership(record) expect(record.errors["age1"]) .to include(match I18n.t("validations.household.old_persons_shared_ownership")) expect(record.errors["age2"]) .to include(match I18n.t("validations.household.old_persons_shared_ownership")) expect(record.errors["type"]) .to include(match I18n.t("validations.household.old_persons_shared_ownership")) end end context "when it is a joint purchase and neither of the buyers ages are known" do let(:record) { FactoryBot.build(:sales_log, jointpur: 1, age1_known: 1, age2_known: 1, type: 24) } it "adds an error" do household_validator.validate_buyers_age_for_old_persons_shared_ownership(record) expect(record.errors["age1"]) .to include(match I18n.t("validations.household.old_persons_shared_ownership")) expect(record.errors["age2"]) .to include(match I18n.t("validations.household.old_persons_shared_ownership")) expect(record.errors["type"]) .to include(match I18n.t("validations.household.old_persons_shared_ownership")) end end context "when it is not a joint purchase and the buyer is over 64" do let(:record) { FactoryBot.build(:sales_log, jointpur: 2, age1: 70, type: 24) } it "does not add an error" do household_validator.validate_buyers_age_for_old_persons_shared_ownership(record) expect(record.errors).not_to be_present end end context "when it is not a joint purchase and the buyer is under 64" do let(:record) { FactoryBot.build(:sales_log, jointpur: 2, age1: 20, type: 24) } it "adds an error" do household_validator.validate_buyers_age_for_old_persons_shared_ownership(record) expect(record.errors["age1"]) .to include(match I18n.t("validations.household.old_persons_shared_ownership")) expect(record.errors["age2"]) .to be_empty expect(record.errors["type"]) .to include(match I18n.t("validations.household.old_persons_shared_ownership")) end end context "when it is not a joint purchase and the buyers age is not known" do let(:record) { FactoryBot.build(:sales_log, jointpur: 2, age1_known: 1, type: 24) } it "adds an error" do household_validator.validate_buyers_age_for_old_persons_shared_ownership(record) expect(record.errors["age1"]) .to include(match I18n.t("validations.household.old_persons_shared_ownership")) expect(record.errors["age2"]) .to be_empty expect(record.errors["type"]) .to include(match I18n.t("validations.household.old_persons_shared_ownership")) end end end end