From bd8fd3fc1f1195e536bb76ad0e259c0b61b48a59 Mon Sep 17 00:00:00 2001 From: natdeanlewissoftwire <94526761+natdeanlewissoftwire@users.noreply.github.com> Date: Tue, 28 Mar 2023 17:05:30 +0100 Subject: [PATCH] CLDC-2015 Validate children to be 12 or more years younger than buyer 1 (#1444) * feat: add validation * feat: add tests * refactor: improve test readability * refactor: improve test readability --- .../sales/household_validations.rb | 14 +++++++++++ config/locales/en.yml | 1 + .../sales/household_validations_spec.rb | 23 +++++++++++++++++++ 3 files changed, 38 insertions(+) diff --git a/app/models/validations/sales/household_validations.rb b/app/models/validations/sales/household_validations.rb index e53b7154e..5d4552eb3 100644 --- a/app/models/validations/sales/household_validations.rb +++ b/app/models/validations/sales/household_validations.rb @@ -6,6 +6,7 @@ module Validations::Sales::HouseholdValidations validate_person_age_matches_relationship(record, n) validate_person_age_and_relationship_matches_economic_status(record, n) validate_person_age_matches_economic_status(record, n) + validate_child_12_years_younger(record, n) end shared_validate_partner_count(record, 6) end @@ -73,6 +74,19 @@ private end end + def validate_child_12_years_younger(record, person_num) + buyer_1_age = record.public_send("age1") + person_age = record.public_send("age#{person_num}") + relationship = record.public_send("relat#{person_num}") + return unless buyer_1_age && person_age && relationship + + if person_age > buyer_1_age - 12 && person_is_child?(relationship) + record.errors.add "age1", I18n.t("validations.household.age.child_12_years_younger") + record.errors.add "age#{person_num}", I18n.t("validations.household.age.child_12_years_younger") + record.errors.add "relat#{person_num}", I18n.t("validations.household.age.child_12_years_younger") + end + end + def person_is_partner?(relationship) relationship == "P" end diff --git a/config/locales/en.yml b/config/locales/en.yml index bef0a1130..45cbde94a 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -347,6 +347,7 @@ en: child_under_16: "Answer cannot be under 16 as person’s %{person_num} working situation is not ‘child under 16’" child_over_16: "Answer cannot be over 16 as person’s %{person_num} working situation is ‘child under 16‘" child_over_20: "Answer cannot be 20 or over as the relationship is ‘child’" + child_12_years_younger: "A child must be at least 12 years younger than their parent" not_student_16_19: "Answer cannot be between 16 and 19 as person %{person_num} is a child of the lead tenant but is not a full-time student" student_16_19: cannot_be_16_19: diff --git a/spec/models/validations/sales/household_validations_spec.rb b/spec/models/validations/sales/household_validations_spec.rb index 5c7a921e4..953f27c1d 100644 --- a/spec/models/validations/sales/household_validations_spec.rb +++ b/spec/models/validations/sales/household_validations_spec.rb @@ -72,6 +72,29 @@ RSpec.describe Validations::Sales::HouseholdValidations do expect(record.errors["age2"]) .to include(match I18n.t("validations.household.age.child_over_16", person_num: 2)) end + + it "validates the child is at least 12 years younger than buyer 1" do + record.age1 = 30 + record.age2 = record.age1 - 11 + record.relat2 = "C" + household_validator.validate_household_number_of_other_members(record) + expect(record.errors["age1"]) + .to include(match I18n.t("validations.household.age.child_12_years_younger", person_num: 2)) + expect(record.errors["age2"]) + .to include(match I18n.t("validations.household.age.child_12_years_younger", person_num: 2)) + expect(record.errors["relat2"]) + .to include(match I18n.t("validations.household.age.child_12_years_younger", person_num: 2)) + end + + it "expects the child is at least 12 years younger than buyer 1" do + record.age1 = 30 + record.age2 = record.age1 - 12 + record.relat2 = "C" + household_validator.validate_household_number_of_other_members(record) + expect(record.errors["age1"]).to be_empty + expect(record.errors["age2"]).to be_empty + expect(record.errors["relate2"]).to be_empty + end end it "validates that a person over 20 must not be a child of the buyer" do