Browse Source

Update sales validations

pull/2256/head
Kat 2 years ago
parent
commit
296c8f8aac
  1. 2
      app/models/form/sales/questions/age2.rb
  2. 14
      app/models/validations/household_validations.rb
  3. 57
      app/models/validations/sales/household_validations.rb
  4. 4
      app/views/layouts/application.html.erb
  5. 15
      config/locales/en.yml
  6. 438
      spec/models/validations/sales/household_validations_spec.rb

2
app/models/form/sales/questions/age2.rb

@ -12,7 +12,7 @@ class Form::Sales::Questions::Age2 < ::Form::Question
}] }]
@check_answers_card_number = 2 @check_answers_card_number = 2
@max = 110 @max = 110
@min = 0 @min = 16
@step = 1 @step = 1
@question_number = QUESTION_NUMBER_FROM_YEAR[form.start_date.year] || QUESTION_NUMBER_FROM_YEAR[QUESTION_NUMBER_FROM_YEAR.keys.max] @question_number = QUESTION_NUMBER_FROM_YEAR[form.start_date.year] || QUESTION_NUMBER_FROM_YEAR[QUESTION_NUMBER_FROM_YEAR.keys.max]
end end

14
app/models/validations/household_validations.rb

@ -100,7 +100,7 @@ module Validations::HouseholdValidations
if record.form.start_year_after_2024? && (age > 19 && tenant_is_child?(relationship)) if record.form.start_year_after_2024? && (age > 19 && tenant_is_child?(relationship))
record.errors.add "relat#{person_num}", I18n.t("validations.household.relat.child_over_19", person_num:) record.errors.add "relat#{person_num}", I18n.t("validations.household.relat.child_over_19", person_num:)
record.errors.add "age#{person_num}", I18n.t("validations.household.age.child_over_19_relat", person_num:) record.errors.add "age#{person_num}", I18n.t("validations.household.age.child_over_19_relat", person_num:, person: "lead tenant")
end end
end end
end end
@ -119,7 +119,7 @@ module Validations::HouseholdValidations
if age_between_16_19 && !(student || economic_status_refused) && child if age_between_16_19 && !(student || economic_status_refused) && child
if record.form.start_year_after_2024? if record.form.start_year_after_2024?
record.errors.add "ecstat#{person_num}", I18n.t("validations.household.ecstat.student_16_19.must_be_student_2024", person_num:) record.errors.add "ecstat#{person_num}", I18n.t("validations.household.ecstat.student_16_19.must_be_student_2024", person_num:, person: "lead tenant")
else else
record.errors.add "age#{person_num}", I18n.t("validations.household.age.student_16_19.cannot_be_16_19.child_not_student") record.errors.add "age#{person_num}", I18n.t("validations.household.age.student_16_19.cannot_be_16_19.child_not_student")
record.errors.add "ecstat#{person_num}", I18n.t("validations.household.ecstat.student_16_19.must_be_student") # 1 record.errors.add "ecstat#{person_num}", I18n.t("validations.household.ecstat.student_16_19.must_be_student") # 1
@ -129,7 +129,7 @@ module Validations::HouseholdValidations
if !age_between_16_19 && student && child if !age_between_16_19 && student && child
if record.form.start_year_after_2024? if record.form.start_year_after_2024?
record.errors.add "age#{person_num}", I18n.t("validations.household.age.student_16_19.must_be_16_19_2024", person_num:) record.errors.add "age#{person_num}", I18n.t("validations.household.age.student_16_19.must_be_16_19_2024", person_num:, person: "lead tenant")
else else
record.errors.add "age#{person_num}", I18n.t("validations.household.age.student_16_19.must_be_16_19") # 2 record.errors.add "age#{person_num}", I18n.t("validations.household.age.student_16_19.must_be_16_19") # 2
record.errors.add "ecstat#{person_num}", I18n.t("validations.household.ecstat.student_16_19.cannot_be_student.child_not_16_19") record.errors.add "ecstat#{person_num}", I18n.t("validations.household.ecstat.student_16_19.cannot_be_student.child_not_16_19")
@ -138,7 +138,7 @@ module Validations::HouseholdValidations
end end
if student && age_between_16_19 && !child && record.form.start_year_after_2024? if student && age_between_16_19 && !child && record.form.start_year_after_2024?
record.errors.add "relat#{person_num}", I18n.t("validations.household.relat.student_16_19.must_be_child_2024", person_num:) record.errors.add "relat#{person_num}", I18n.t("validations.household.relat.student_16_19.must_be_child_2024", person_num:, person: "lead tenant")
end end
end end
end end
@ -153,9 +153,9 @@ module Validations::HouseholdValidations
next unless age > record.age1 - 12 && tenant_is_child?(relationship) next unless age > record.age1 - 12 && tenant_is_child?(relationship)
record.errors.add "age1", I18n.t("validations.household.age.age1_child_12_years_younger_2024", person_num:) record.errors.add "age1", I18n.t("validations.household.age.age1_child_12_years_younger_2024", person_num:, person: "lead tenant")
record.errors.add "age#{person_num}", I18n.t("validations.household.age.child_12_years_younger_2024", person_num:) record.errors.add "age#{person_num}", I18n.t("validations.household.age.child_12_years_younger_2024", person_num:, person: "lead tenant")
record.errors.add "relat#{person_num}", I18n.t("validations.household.relat.child_12_years_younger_2024", person_num:) record.errors.add "relat#{person_num}", I18n.t("validations.household.relat.child_12_years_younger_2024", person_num:, person: "lead tenant")
end end
end end

57
app/models/validations/sales/household_validations.rb

@ -35,8 +35,13 @@ module Validations::Sales::HouseholdValidations
record.errors.add "age#{person_num}", I18n.t("validations.household.age.child_under_16_relat_sales", person_num:) record.errors.add "age#{person_num}", I18n.t("validations.household.age.child_under_16_relat_sales", person_num:)
record.errors.add "relat#{person_num}", I18n.t("validations.household.relat.child_under_16_sales", person_num:) record.errors.add "relat#{person_num}", I18n.t("validations.household.relat.child_under_16_sales", person_num:)
elsif age >= 20 && person_is_child?(relationship) elsif age >= 20 && person_is_child?(relationship)
record.errors.add "age#{person_num}", I18n.t("validations.household.age.child_over_20") if record.form.start_year_after_2024?
record.errors.add "relat#{person_num}", I18n.t("validations.household.relat.child_over_20") record.errors.add "relat#{person_num}", I18n.t("validations.household.relat.child_over_19", person_num:)
record.errors.add "age#{person_num}", I18n.t("validations.household.age.child_over_19_relat", person_num:, person: "buyer")
else
record.errors.add "age#{person_num}", I18n.t("validations.household.age.child_over_20")
record.errors.add "relat#{person_num}", I18n.t("validations.household.relat.child_over_20")
end
end end
end end
end end
@ -54,20 +59,34 @@ module Validations::Sales::HouseholdValidations
child = person_is_child?(relationship) child = person_is_child?(relationship)
if age_between_16_19 && !(student || economic_status_refused) && child if age_between_16_19 && !(student || economic_status_refused) && child
record.errors.add "age#{person_num}", I18n.t("validations.household.age.student_16_19.cannot_be_16_19.child_not_student") if record.form.start_year_after_2024?
record.errors.add "ecstat#{person_num}", I18n.t("validations.household.ecstat.student_16_19.must_be_student") record.errors.add "ecstat#{person_num}", I18n.t("validations.household.ecstat.student_16_19.must_be_student_2024", person_num:, person: "buyer")
record.errors.add "relat#{person_num}", I18n.t("validations.household.relat.student_16_19.cannot_be_child.16_19_not_student") else
record.errors.add "age#{person_num}", I18n.t("validations.household.age.student_16_19.cannot_be_16_19.child_not_student")
record.errors.add "ecstat#{person_num}", I18n.t("validations.household.ecstat.student_16_19.must_be_student")
record.errors.add "relat#{person_num}", I18n.t("validations.household.relat.student_16_19.cannot_be_child.16_19_not_student")
end
end end
next unless !age_between_16_19 && student && child if !age_between_16_19 && student && child
if record.form.start_year_after_2024?
record.errors.add "age#{person_num}", I18n.t("validations.household.age.student_16_19.must_be_16_19_2024", person_num:, person: "buyer")
else
record.errors.add "age#{person_num}", I18n.t("validations.household.age.student_16_19.must_be_16_19")
record.errors.add "ecstat#{person_num}", I18n.t("validations.household.ecstat.student_16_19.cannot_be_student.child_not_16_19")
record.errors.add "relat#{person_num}", I18n.t("validations.household.relat.student_16_19.cannot_be_child.student_not_16_19")
end
end
record.errors.add "age#{person_num}", I18n.t("validations.household.age.student_16_19.must_be_16_19") if student && age_between_16_19 && !child && record.form.start_year_after_2024?
record.errors.add "ecstat#{person_num}", I18n.t("validations.household.ecstat.student_16_19.cannot_be_student.child_not_16_19") record.errors.add "relat#{person_num}", I18n.t("validations.household.relat.student_16_19.must_be_child_2024", person_num:, person: "buyer")
record.errors.add "relat#{person_num}", I18n.t("validations.household.relat.student_16_19.cannot_be_child.student_not_16_19") end
end end
end end
def validate_person_age_matches_economic_status(record) def validate_person_age_matches_economic_status(record)
return if record.form.start_year_after_2024?
(2..6).each do |person_num| (2..6).each do |person_num|
age = record.public_send("age#{person_num}") age = record.public_send("age#{person_num}")
economic_status = record.public_send("ecstat#{person_num}") economic_status = record.public_send("ecstat#{person_num}")
@ -93,9 +112,23 @@ module Validations::Sales::HouseholdValidations
next unless person_age > buyer_1_age - 12 && person_is_child?(relationship) next unless person_age > buyer_1_age - 12 && person_is_child?(relationship)
record.errors.add "age1", I18n.t("validations.household.age.child_12_years_younger") if record.form.start_year_after_2024?
record.errors.add "age#{person_num}", I18n.t("validations.household.age.child_12_years_younger") record.errors.add "age1", I18n.t("validations.household.age.age1_child_12_years_younger_2024", person_num:, person: "buyer")
record.errors.add "relat#{person_num}", I18n.t("validations.household.age.child_12_years_younger") record.errors.add "age#{person_num}", I18n.t("validations.household.age.child_12_years_younger_2024", person_num:, person: "buyer")
record.errors.add "relat#{person_num}", I18n.t("validations.household.relat.child_12_years_younger_2024", person_num:, person: "buyer")
else
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
end
def validate_buyer_2_not_child(record)
return unless record.form.start_year_after_2024?
if record.joint_purchase? && person_is_child?(record.relat2)
record.errors.add "relat2", I18n.t("validations.household.relat.buyer_is_a_child")
end end
end end

4
app/views/layouts/application.html.erb

@ -7,6 +7,10 @@
<%= tag.meta name: "viewport", content: "width=device-width, initial-scale=1" %> <%= tag.meta name: "viewport", content: "width=device-width, initial-scale=1" %>
<%= tag.meta property: "og:image", content: asset_path("images/govuk-opengraph-image.png") %> <%= tag.meta property: "og:image", content: asset_path("images/govuk-opengraph-image.png") %>
<%= tag.meta name: "theme-color", content: "#0b0c0c" %> <%= tag.meta name: "theme-color", content: "#0b0c0c" %>
<%= favicon_link_tag asset_path("images/favicon.ico"), type: nil, sizes: "48x48" %>
<%= favicon_link_tag asset_path("images/favicon.svg"), type: "image/svg+xml", sizes: "any" %>
<%= favicon_link_tag asset_path("images/govuk-icon-mask.svg"), rel: "mask-icon", color: "#0b0c0c", type: nil %>
<%= favicon_link_tag asset_path("images/govuk-icon-180.png"), rel: "apple-touch-icon", type: nil %>
<%= stylesheet_link_tag "application" %> <%= stylesheet_link_tag "application" %>
<%= javascript_include_tag "vendor/html5shiv.min.js" %> <%= javascript_include_tag "vendor/html5shiv.min.js" %>
<script> <script>

15
config/locales/en.yml

@ -463,17 +463,17 @@ en:
child_over_16: "Answer cannot be over 16 as person’s %{person_num} working situation is ‘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_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" child_12_years_younger: "A child must be at least 12 years younger than their parent"
child_12_years_younger_2024: "Age must be at least 12 years younger than the lead tenant as person %{person_num}'s relationship to lead tenant is child." child_12_years_younger_2024: "Age must be at least 12 years younger than the %{person} as person %{person_num}'s relationship to %{person} is child."
age1_child_12_years_younger_2024: "Age must be at least 12 years older than person %{person_num}'s age as person %{person_num}'s relationship to lead tenant is child." age1_child_12_years_younger_2024: "Age must be at least 12 years older than person %{person_num}'s age as person %{person_num}'s relationship to %{person} is child."
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" 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: student_16_19:
cannot_be_16_19: cannot_be_16_19:
child_not_student: "Person cannot be aged 16-19 if they have relationship ‘child’ but are not a student" child_not_student: "Person cannot be aged 16-19 if they have relationship ‘child’ but are not a student"
must_be_16_19: "Person must be aged 16-19 if they are a student and have relationship ‘child’" must_be_16_19: "Person must be aged 16-19 if they are a student and have relationship ‘child’"
must_be_16_19_2024: "Person %{person_num}'s age must be 16-19 as their working situation is student and their relationship to the lead tenant is child" must_be_16_19_2024: "Person %{person_num}'s age must be 16-19 as their working situation is student and their relationship to the %{person} is child"
lead: lead:
over_25: "The lead tenant must be under 26 as you told us their housing situation immediately before this letting was a children’s home or foster care" over_25: "The lead tenant must be under 26 as you told us their housing situation immediately before this letting was a children’s home or foster care"
child_over_19_relat: "Age must be 19 or under as you told us person 2's relationship to the lead tenant is child" child_over_19_relat: "Age must be 19 or under as you told us person 2's relationship to the %{person} is child"
ecstat: ecstat:
retired_over_70: "Person %{person_num} must be retired if over 70" retired_over_70: "Person %{person_num} must be retired if over 70"
child_under_16: "Person %{person_num}’s working situation must be ‘child under 16’, ‘other’ or ‘prefers not to say’ as you told us they’re under 16" child_under_16: "Person %{person_num}’s working situation must be ‘child under 16’, ‘other’ or ‘prefers not to say’ as you told us they’re under 16"
@ -483,7 +483,7 @@ en:
cannot_be_student: cannot_be_student:
child_not_16_19: "Person cannot be a student if they are not aged 16-19 but have relationship ‘child’" child_not_16_19: "Person cannot be a student if they are not aged 16-19 but have relationship ‘child’"
must_be_student: "Person must be a student if they are aged 16-19 and have relationship ‘child’" must_be_student: "Person must be a student if they are aged 16-19 and have relationship ‘child’"
must_be_student_2024: "Person %{person_num}'s working situation must be student or prefers not to say, as their age is 16-19 and their relationship to the lead tenant is child" must_be_student_2024: "Person %{person_num}'s working situation must be student or prefers not to say, as their age is 16-19 and their relationship to the %{person} is child"
retired_male: "Answer cannot be ‘retired’ as the male tenant is under 65" retired_male: "Answer cannot be ‘retired’ as the male tenant is under 65"
retired_female: "Answer cannot be ‘retired’ as the female tenant is under 60" retired_female: "Answer cannot be ‘retired’ as the female tenant is under 60"
relat: relat:
@ -496,9 +496,10 @@ en:
cannot_be_child: cannot_be_child:
student_not_16_19: "Answer cannot be ‘child’ if the person is a student but not aged 16-19" student_not_16_19: "Answer cannot be ‘child’ if the person is a student but not aged 16-19"
16_19_not_student: "Answer cannot be ‘child’ if the person is aged 16-19 but not a student" 16_19_not_student: "Answer cannot be ‘child’ if the person is aged 16-19 but not a student"
must_be_child_2024: "Person %{person_num}'s relationship to the lead tenant must be child as their working situation is student and their age is 16-19" must_be_child_2024: "Person %{person_num}'s relationship to the %{person} must be child as their working situation is student and their age is 16-19"
child_over_19: "Answer cannot be child as you told us person %{person_num} is over 19" child_over_19: "Answer cannot be child as you told us person %{person_num} is over 19"
child_12_years_younger_2024: "Relationship cannot be child, as person %{person_num} is less than 12 years younger than the lead tenant" child_12_years_younger_2024: "Relationship cannot be child, as person %{person_num} is less than 12 years younger than the %{person}"
buyer_is_a_child: "Relationship cannot be child, as this person is a buyer"
housingneeds_a: housingneeds_a:
one_or_two_choices: "You can only select one option or ‘other disabled access needs’ plus ‘wheelchair-accessible housing’, ‘wheelchair access to essential rooms’ or ‘level access housing’" one_or_two_choices: "You can only select one option or ‘other disabled access needs’ plus ‘wheelchair-accessible housing’, ‘wheelchair access to essential rooms’ or ‘level access housing’"
housingneeds_type: housingneeds_type:

438
spec/models/validations/sales/household_validations_spec.rb

@ -4,7 +4,8 @@ RSpec.describe Validations::Sales::HouseholdValidations do
subject(:household_validator) { validator_class.new } subject(:household_validator) { validator_class.new }
let(:validator_class) { Class.new { include Validations::Sales::HouseholdValidations } } let(:validator_class) { Class.new { include Validations::Sales::HouseholdValidations } }
let(:record) { build(:sales_log) } let(:record) { build(:sales_log, saledate: log_date) }
let(:log_date) { Time.zone.local(2023, 4, 1) }
describe "#validate_partner_count" do describe "#validate_partner_count" do
it "validates that only 1 partner exists" do it "validates that only 1 partner exists" do
@ -27,137 +28,380 @@ RSpec.describe Validations::Sales::HouseholdValidations do
end end
describe "#validate_person_age_matches_relationship" do describe "#validate_person_age_matches_relationship" do
context "when the household contains a person under 16" do before do
it "expects that person is a child of the tenant" do Timecop.freeze(log_date)
record.age2 = 14 Singleton.__init__(FormHandler)
end
after do
Timecop.return
Singleton.__init__(FormHandler)
end
context "with 2023 logs" do
let(:log_date) { Time.zone.local(2023, 4, 1) }
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_person_age_matches_relationship(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_person_age_matches_relationship(record)
expect(record.errors["relat2"])
.to include(match I18n.t("validations.household.relat.child_under_16_sales", person_num: 2))
expect(record.errors["age2"])
.to include(match I18n.t("validations.household.age.child_under_16_relat_sales", 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" record.relat2 = "C"
household_validator.validate_person_age_matches_relationship(record) household_validator.validate_person_age_matches_relationship(record)
expect(record.errors["relat2"]).to be_empty expect(record.errors["relat2"])
expect(record.errors["age2"]).to be_empty .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 end
end
context "with 2024 logs" do
let(:log_date) { Time.zone.local(2024, 4, 1) }
it "validates that a person under 16 must not be a partner of the buyer" do it "validates person under 16 is not partner" do
record.age2 = 14 record.age2 = 14
record.relat2 = "P" record.relat2 = "P"
household_validator.validate_person_age_matches_relationship(record) household_validator.validate_person_age_matches_relationship(record)
expect(record.errors["relat2"]) expect(record.errors["relat2"])
.to include(match I18n.t("validations.household.relat.child_under_16_sales", person_num: 2)) .to include("Answer cannot be ‘partner’ as you told us person 2's age is under 16")
expect(record.errors["age2"]) expect(record.errors["age2"])
.to include(match I18n.t("validations.household.age.child_under_16_relat_sales", person_num: 2)) .not_to include("Age cannot be under 16 as you told us person 2's relationship to the buyer is partner.")
end end
end
it "validates that a person over 20 must not be a child of the buyer" do it "validates person over 19 is not child" do
record.age2 = 21 record.age2 = 20
record.relat2 = "C" record.relat2 = "C"
household_validator.validate_person_age_matches_relationship(record) household_validator.validate_person_age_matches_relationship(record)
expect(record.errors["relat2"]) expect(record.errors["age2"])
.to include(match I18n.t("validations.household.relat.child_over_20")) .to include("Age must be 19 or under as you told us person 2's relationship to the buyer is child")
expect(record.errors["age2"]) expect(record.errors["relat2"])
.to include(match I18n.t("validations.household.age.child_over_20")) .not_to include("Answer cannot be child, as you told us person 2 is over 19")
end
end end
end end
describe "#validate_person_age_matches_economic_status" do describe "#validate_person_age_matches_economic_status" do
it "validates that person's economic status must be Child" do before do
record.age2 = 14 Timecop.freeze(log_date)
record.ecstat2 = 1 Singleton.__init__(FormHandler)
household_validator.validate_person_age_matches_economic_status(record) end
expect(record.errors["ecstat2"])
.to include(match I18n.t("validations.household.ecstat.child_under_16", person_num: 2)) after do
expect(record.errors["age2"]) Timecop.return
.to include(match I18n.t("validations.household.age.child_under_16_ecstat", person_num: 2)) Singleton.__init__(FormHandler)
end end
it "expects that person's economic status is Child" do context "with 2023 logs" do
record.age2 = 14 let(:log_date) { Time.zone.local(2023, 4, 1) }
record.ecstat2 = 9
household_validator.validate_person_age_matches_economic_status(record) it "validates that person's economic status must be Child" do
expect(record.errors["ecstat2"]).to be_empty record.age2 = 14
expect(record.errors["age2"]).to be_empty record.ecstat2 = 1
household_validator.validate_person_age_matches_economic_status(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_ecstat", person_num: 2))
end
it "expects that person's economic status is Child" do
record.age2 = 14
record.ecstat2 = 9
household_validator.validate_person_age_matches_economic_status(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_person_age_matches_economic_status(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 end
it "validates that a person with economic status 'child' must be under 16" do context "with 2024 logs" do
record.age2 = 21 let(:log_date) { Time.zone.local(2024, 4, 1) }
record.ecstat2 = 9
household_validator.validate_person_age_matches_economic_status(record) it "does not run the validation" do
expect(record.errors["ecstat2"]) record.age2 = 14
.to include(match I18n.t("validations.household.ecstat.child_over_16", person_num: 2)) record.ecstat2 = 1
expect(record.errors["age2"]) household_validator.validate_person_age_matches_economic_status(record)
.to include(match I18n.t("validations.household.age.child_over_16", person_num: 2)) expect(record.errors["ecstat2"])
.not_to include(match I18n.t("validations.household.ecstat.child_under_16", person_num: 2))
expect(record.errors["age2"])
.not_to include(match I18n.t("validations.household.age.child_under_16_ecstat", person_num: 2))
end
end end
end end
describe "#validate_child_12_years_younger" do describe "#validate_child_12_years_younger" do
it "validates the child is at least 12 years younger than buyer 1" do before do
record.age1 = 30 Timecop.freeze(log_date)
record.age2 = record.age1 - 11 Singleton.__init__(FormHandler)
record.relat2 = "C" end
household_validator.validate_child_12_years_younger(record)
expect(record.errors["age1"]) after do
.to include(match I18n.t("validations.household.age.child_12_years_younger", person_num: 2)) Timecop.return
expect(record.errors["age2"]) Singleton.__init__(FormHandler)
.to include(match I18n.t("validations.household.age.child_12_years_younger", person_num: 2)) end
expect(record.errors["relat2"])
.to include(match I18n.t("validations.household.age.child_12_years_younger", person_num: 2)) context "with 2023 logs" do
let(:log_date) { Time.zone.local(2023, 4, 1) }
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_child_12_years_younger(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_child_12_years_younger(record)
expect(record.errors["age1"]).to be_empty
expect(record.errors["age2"]).to be_empty
expect(record.errors["relate2"]).to be_empty
end
end end
it "expects the child is at least 12 years younger than buyer 1" do context "with 2024 logs" do
record.age1 = 30 let(:log_date) { Time.zone.local(2024, 4, 1) }
record.age2 = record.age1 - 12
record.relat2 = "C" it "validates that child is at least 12 year younger than buyer" do
household_validator.validate_child_12_years_younger(record) record.age1 = 20
expect(record.errors["age1"]).to be_empty record.age2 = 17
expect(record.errors["age2"]).to be_empty record.relat2 = "C"
expect(record.errors["relate2"]).to be_empty household_validator.validate_child_12_years_younger(record)
expect(record.errors["age1"])
.to include("Age must be at least 12 years older than person 2's age as person 2's relationship to buyer is child.")
expect(record.errors["age2"])
.to include("Age must be at least 12 years younger than the buyer as person 2's relationship to buyer is child.")
expect(record.errors["relat2"])
.to include("Relationship cannot be child, as person 2 is less than 12 years younger than the buyer")
end
it "expects that child is at least 12 years younger than buyer" do
record.age1 = 30
record.age2 = 17
record.relat2 = "C"
household_validator.validate_child_12_years_younger(record)
expect(record.errors["age2"]).to be_empty
expect(record.errors["relat2"]).to be_empty
end
end end
end end
describe "#validate_person_age_and_relationship_matches_economic_status" do describe "#validate_person_age_and_relationship_matches_economic_status" do
it "does not add an error for a person aged 16-19 who is a student but not a child of the buyer" do before do
record.age2 = 18 Timecop.freeze(log_date)
record.ecstat2 = "7" Singleton.__init__(FormHandler)
record.relat2 = "P"
household_validator.validate_person_age_and_relationship_matches_economic_status(record)
expect(record.errors["relat2"]).to be_empty
expect(record.errors["ecstat2"]).to be_empty
expect(record.errors["age2"]).to be_empty
end end
it "does not add an error for a person not aged 16-19 who is a student but not a child of the buyer" do after do
record.age2 = 20 Timecop.return
record.ecstat2 = "7" Singleton.__init__(FormHandler)
record.relat2 = "P"
household_validator.validate_person_age_and_relationship_matches_economic_status(record)
expect(record.errors["relat2"]).to be_empty
expect(record.errors["ecstat2"]).to be_empty
expect(record.errors["age2"]).to be_empty
end end
it "adds errors for a person aged 16-19 who is a child of the buyer but not a student" do context "with 2023 logs" do
record.age2 = 17 let(:log_date) { Time.zone.local(2023, 4, 1) }
record.ecstat2 = "1"
record.relat2 = "C" it "does not add an error for a person aged 16-19 who is a student but not a child of the buyer" do
household_validator.validate_person_age_and_relationship_matches_economic_status(record) record.age2 = 18
expect(record.errors["relat2"]) record.ecstat2 = "7"
.to include(match I18n.t("validations.household.relat.student_16_19.cannot_be_child.16_19_not_student")) record.relat2 = "P"
expect(record.errors["age2"]) household_validator.validate_person_age_and_relationship_matches_economic_status(record)
.to include(match I18n.t("validations.household.age.student_16_19.cannot_be_16_19.child_not_student")) expect(record.errors["relat2"]).to be_empty
expect(record.errors["ecstat2"]) expect(record.errors["ecstat2"]).to be_empty
.to include(match I18n.t("validations.household.ecstat.student_16_19.must_be_student")) expect(record.errors["age2"]).to be_empty
end
it "does not add an error for a person not aged 16-19 who is a student but not a child of the buyer" do
record.age2 = 20
record.ecstat2 = "7"
record.relat2 = "P"
household_validator.validate_person_age_and_relationship_matches_economic_status(record)
expect(record.errors["relat2"]).to be_empty
expect(record.errors["ecstat2"]).to be_empty
expect(record.errors["age2"]).to be_empty
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_person_age_and_relationship_matches_economic_status(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_person_age_and_relationship_matches_economic_status(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
end end
it "adds errors for a person who is a child of the buyer and a student but not aged 16-19" do context "with 2024 logs" do
record.age2 = 14 let(:log_date) { Time.zone.local(2024, 4, 1) }
record.ecstat2 = "7"
record.relat2 = "C" it "does not add 2023 errors" do
household_validator.validate_person_age_and_relationship_matches_economic_status(record) record.age2 = 17
expect(record.errors["relat2"]) record.relat2 = "C"
.to include(match I18n.t("validations.household.relat.student_16_19.cannot_be_child.student_not_16_19")) record.ecstat2 = 1
expect(record.errors["age2"]) household_validator.validate_person_age_and_relationship_matches_economic_status(record)
.to include(match I18n.t("validations.household.age.student_16_19.must_be_16_19")) expect(record.errors["ecstat2"])
expect(record.errors["ecstat2"]) .not_to include(match I18n.t("validations.household.ecstat.student_16_19.must_be_student", person_num: 2))
.to include(match I18n.t("validations.household.ecstat.student_16_19.cannot_be_student.child_not_16_19")) expect(record.errors["age2"])
.not_to include(match I18n.t("validations.household.age.student_16_19.cannot_be_16_19.child_not_student", person_num: 2))
expect(record.errors["relat2"])
.not_to include(match I18n.t("validations.household.relat.student_16_19.cannot_be_child.16_19_not_student", person_num: 2))
end
context "when the household contains a tenant’s child between the ages of 16 and 19" do
it "validates that person's economic status must be full time student or refused" do
record.age2 = 17
record.relat2 = "C"
record.ecstat2 = 1
household_validator.validate_person_age_and_relationship_matches_economic_status(record)
expect(record.errors["ecstat2"])
.to include("Person 2's working situation must be student or prefers not to say, as their age is 16-19 and their relationship to the buyer is child")
expect(record.errors["age2"])
.to be_empty
expect(record.errors["relat2"])
.to be_empty
end
it "expects that person can be a full time student" do
record.age2 = 17
record.relat2 = "C"
record.ecstat2 = 7
household_validator.validate_person_age_and_relationship_matches_economic_status(record)
expect(record.errors["ecstat2"]).to be_empty
expect(record.errors["age2"]).to be_empty
expect(record.errors["relat2"]).to be_empty
end
it "expects that person can refuse to share their work status" do
record.age2 = 17
record.relat2 = "C"
record.ecstat2 = 10
household_validator.validate_person_age_and_relationship_matches_economic_status(record)
expect(record.errors["ecstat2"]).to be_empty
expect(record.errors["age2"]).to be_empty
expect(record.errors["relat2"]).to be_empty
end
end
it "does not add an error for a person not aged 16-19 who is a student but not a child of the buyer" do
record.age2 = 20
record.ecstat2 = "7"
record.relat2 = "P"
household_validator.validate_person_age_and_relationship_matches_economic_status(record)
expect(record.errors["relat2"]).to be_empty
expect(record.errors["ecstat2"]).to be_empty
expect(record.errors["age2"]).to be_empty
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_person_age_and_relationship_matches_economic_status(record)
expect(record.errors["relat2"])
.to be_empty
expect(record.errors["age2"])
.to include("Person 2's age must be 16-19 as their working situation is student and their relationship to the buyer is child")
expect(record.errors["ecstat2"])
.to be_empty
end
it "adds errors for a person who is a student and aged 16-19 but not child" do
record.age2 = 17
record.ecstat2 = "7"
record.relat2 = "X"
household_validator.validate_person_age_and_relationship_matches_economic_status(record)
expect(record.errors["relat2"])
.to include("Person 2's relationship to the buyer must be child as their working situation is student and their age is 16-19")
expect(record.errors["age2"])
.to be_empty
expect(record.errors["ecstat2"])
.to be_empty
end
end
end
describe "#validate_buyer_2_not_child" do
before do
Timecop.freeze(log_date)
Singleton.__init__(FormHandler)
end
after do
Timecop.return
Singleton.__init__(FormHandler)
end
context "with 2023 logs" do
let(:log_date) { Time.zone.local(2023, 4, 1) }
it "does not add an error if buyer 2 is a child" do
record.jointpur = 1
record.relat2 = "C"
household_validator.validate_buyer_2_not_child(record)
expect(record.errors["relat2"]).to be_empty
end
end
context "with 2024 logs" do
let(:log_date) { Time.zone.local(2024, 4, 1) }
it "validates buyer 2 isn't a child" do
record.jointpur = 1
record.relat2 = "C"
household_validator.validate_buyer_2_not_child(record)
expect(record.errors["relat2"])
.to include("Relationship cannot be child, as this person is a buyer")
end
end end
end end

Loading…
Cancel
Save