Browse Source

Add some test for our validations

pull/74/head
baarkerlounger 3 years ago
parent
commit
d240e299bd
  1. 15
      app/validations/household_validations.rb
  2. 36
      spec/models/case_log_spec.rb

15
app/validations/household_validations.rb

@ -57,6 +57,7 @@ module HouseholdValidations
validate_person_age_matches_economic_status(record, n) validate_person_age_matches_economic_status(record, n)
validate_person_age_matches_relationship(record, n) validate_person_age_matches_relationship(record, n)
validate_person_age_and_gender_match_economic_status(record, n) validate_person_age_and_gender_match_economic_status(record, n)
validate_person_age_and_relationship_matches_economic_status(record, n)
end end
validate_partner_count(record) validate_partner_count(record)
end end
@ -95,9 +96,6 @@ private
if age < 16 && economic_status != "Child under 16" 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" 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 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 end
def validate_person_age_matches_relationship(record, person_num) def validate_person_age_matches_relationship(record, person_num)
@ -110,6 +108,17 @@ private
end end
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) def validate_person_age_and_gender_match_economic_status(record, person_num)
age = record.public_send("person_#{person_num}_age") age = record.public_send("person_#{person_num}_age")
gender = record.public_send("person_#{person_num}_gender") gender = record.public_send("person_#{person_num}_gender")

36
spec/models/case_log_spec.rb

@ -206,10 +206,38 @@ RSpec.describe Form, type: :model do
end end
end end
context "household_member_validations" do context "household_member_validations" do
# it "validate that persons aged under 16 must have relationship Child" 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) expect { CaseLog.create!(person_2_age: 14, person_2_relationship: "Partner") }.to raise_error(ActiveRecord::RecordInvalid)
# end 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 end
context "other tenancy type validation" do context "other tenancy type validation" do

Loading…
Cancel
Save