From 948bab46f8e5af3b5a3a1820bd89f8a825db8bcc Mon Sep 17 00:00:00 2001 From: Kat Date: Fri, 20 Jan 2023 09:57:49 +0000 Subject: [PATCH] validate that under 16s are economic children --- .../sales/household_validations.rb | 20 +++++++++++++ .../sales/household_validations_spec.rb | 29 +++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/app/models/validations/sales/household_validations.rb b/app/models/validations/sales/household_validations.rb index 385659c9a..a502e5fe3 100644 --- a/app/models/validations/sales/household_validations.rb +++ b/app/models/validations/sales/household_validations.rb @@ -13,6 +13,7 @@ module Validations::Sales::HouseholdValidations (2..6).each do |n| 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) end shared_validate_partner_count(record, 6) end @@ -46,6 +47,21 @@ private end end + def validate_person_age_matches_economic_status(record, person_num) + age = record.public_send("age#{person_num}") + economic_status = record.public_send("ecstat#{person_num}") + return unless age && economic_status + + if age < 16 && !tenant_is_economic_child?(economic_status) + record.errors.add "ecstat#{person_num}", I18n.t("validations.household.ecstat.child_under_16", person_num:) + record.errors.add "age#{person_num}", I18n.t("validations.household.age.child_under_16", person_num:) + end + if tenant_is_economic_child?(economic_status) && age > 16 + record.errors.add "ecstat#{person_num}", I18n.t("validations.household.ecstat.child_over_16", person_num:) + record.errors.add "age#{person_num}", I18n.t("validations.household.age.child_over_16", person_num:) + end + end + def person_is_partner?(relationship) relationship == "P" end @@ -61,4 +77,8 @@ private def person_is_child?(relationship) relationship == "C" end + + def tenant_is_economic_child?(economic_status) + economic_status == 9 + end end diff --git a/spec/models/validations/sales/household_validations_spec.rb b/spec/models/validations/sales/household_validations_spec.rb index b20ac15f4..9a1976633 100644 --- a/spec/models/validations/sales/household_validations_spec.rb +++ b/spec/models/validations/sales/household_validations_spec.rb @@ -86,6 +86,35 @@ RSpec.describe Validations::Sales::HouseholdValidations do 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.relat2 = "C" + 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