diff --git a/app/validations/household_validations.rb b/app/validations/household_validations.rb index d458b7607..347b87c59 100644 --- a/app/validations/household_validations.rb +++ b/app/validations/household_validations.rb @@ -57,6 +57,7 @@ module HouseholdValidations validate_person_age_matches_economic_status(record, n) validate_person_age_matches_relationship(record, n) validate_person_age_and_gender_match_economic_status(record, n) + validate_person_age_and_relationship_matches_economic_status(record, n) end validate_partner_count(record) end @@ -95,9 +96,6 @@ private if age < 16 && economic_status != "Child under 16" record.errors.add "person_#{person_num}_economic_status", "Tenant #{person_num} economic status must be Child under 16 if their age is under 16" end - if age >= 16 && age <= 19 && (economic_status != "Full-time student" || economic_status != "Prefer not to say") - record.errors.add "person_#{person_num}_economic_status", "If age is between 16 and 19 - tenant #{person_num} must be a full time student or prefer not to say." - end end def validate_person_age_matches_relationship(record, person_num) @@ -110,6 +108,17 @@ private end end + def validate_person_age_and_relationship_matches_economic_status(record, person_num) + age = record.public_send("person_#{person_num}_age") + economic_status = record.public_send("person_#{person_num}_economic_status") + relationship = record.public_send("person_#{person_num}_relationship") + return unless age && economic_status && relationship + + if age >= 16 && age <= 19 && relationship == "Child - includes young adult and grown-up" && (economic_status != "Full-time student" || economic_status != "Prefer not to say") + record.errors.add "person_#{person_num}_economic_status", "If age is between 16 and 19 - tenant #{person_num} must be a full time student or prefer not to say." + end + end + def validate_person_age_and_gender_match_economic_status(record, person_num) age = record.public_send("person_#{person_num}_age") gender = record.public_send("person_#{person_num}_gender") diff --git a/spec/models/case_log_spec.rb b/spec/models/case_log_spec.rb index 9c39c3a1a..80f45e64a 100644 --- a/spec/models/case_log_spec.rb +++ b/spec/models/case_log_spec.rb @@ -206,10 +206,38 @@ RSpec.describe Form, type: :model do end end - context "household_member_validations" do - # it "validate that persons aged under 16 must have relationship Child" do - # expect { CaseLog.create!(person_2_age: 14, person_2_relationship: "Partner") }.to raise_error(ActiveRecord::RecordInvalid) - # end + context "household_member_validations" do + it "validate that persons aged under 16 must have relationship Child" do + expect { CaseLog.create!(person_2_age: 14, person_2_relationship: "Partner") }.to raise_error(ActiveRecord::RecordInvalid) + end + + it "validate that persons aged over 70 must be retired" do + expect { CaseLog.create!(person_2_age: 71, person_2_economic_status: "Full-time - 30 hours or more") }.to raise_error(ActiveRecord::RecordInvalid) + end + + it "validate that a male, retired persons must be over 65" do + expect { CaseLog.create!(person_2_age: 64, person_2_gender: "Male", person_2_economic_status: "Retired") }.to raise_error(ActiveRecord::RecordInvalid) + end + + it "validate that a female, retired persons must be over 60" do + expect { CaseLog.create!(person_2_age: 59, person_2_gender: "Female", person_2_economic_status: "Retired") }.to raise_error(ActiveRecord::RecordInvalid) + end + + it "validate that persons aged under 16 must be a child (economically speaking)" do + expect { CaseLog.create!(person_2_age: 15, person_2_economic_status: "Full-time - 30 hours or more") }.to raise_error(ActiveRecord::RecordInvalid) + end + + it "validate that persons aged between 16 and 19 that are a child must be a full time student or economic status refused" do + expect { CaseLog.create!(person_2_age: 17, person_2_relationship: "Child - includes young adult and grown-up", person_2_economic_status: "Full-time - 30 hours or more") }.to raise_error(ActiveRecord::RecordInvalid) + end + + it "validate that persons aged under 16 must be a child relationship" do + expect { CaseLog.create!(person_2_age: 15, person_2_relationship: "Partner") }.to raise_error(ActiveRecord::RecordInvalid) + end + + it "validate that no more than 1 partner relationship exists" do + expect { CaseLog.create!(person_2_relationship: "Partner", person_3_relationship: "Partner") }.to raise_error(ActiveRecord::RecordInvalid) + end end context "other tenancy type validation" do