Browse Source

Reasonable preference

pull/297/head
baarkerlounger 3 years ago
parent
commit
78577d9fa3
  1. 1
      app/models/validations/household_validations.rb
  2. 41
      spec/models/case_log_spec.rb
  3. 61
      spec/models/validations/household_validations_spec.rb

1
app/models/validations/household_validations.rb

@ -6,6 +6,7 @@ module Validations::HouseholdValidations
def validate_reasonable_preference(record)
if record.homeless == "No" && record.reasonpref == "Yes"
record.errors.add :reasonpref, I18n.t("validations.household.reasonpref.not_homeless")
record.errors.add :homeless, I18n.t("validations.household.reasonpref.not_homeless")
elsif record.reasonpref == "No"
if [record.rp_homeless, record.rp_insan_unsat, record.rp_medwel, record.rp_hardship, record.rp_dontknow].any? { |a| a == "Yes" }
record.errors.add :reasonable_preference_reason, I18n.t("validations.household.reasonable_preference_reason.reason_not_required")

41
spec/models/case_log_spec.rb

@ -33,43 +33,6 @@ RSpec.describe CaseLog do
end
# TODO: replace these with validator specs and checks for method call here
context "when a reasonable preference is set to yes" do
it "validates that previously homeless should be selected" do
expect {
described_class.create!(
homeless: "No",
reasonpref: "Yes",
owning_organisation: owning_organisation,
managing_organisation: managing_organisation,
)
}.to raise_error(ActiveRecord::RecordInvalid)
end
end
context "when a reasonable preference is set to no" do
it "validates no reason is needed" do
expect {
described_class.create!(
reasonpref: "No",
rp_homeless: "No",
owning_organisation: owning_organisation,
managing_organisation: managing_organisation,
)
}.not_to raise_error
end
it "validates that no reason has been provided" do
expect {
described_class.create!(
reasonpref: "No",
rp_medwel: "Yes",
owning_organisation: owning_organisation,
managing_organisation: managing_organisation,
)
}.to raise_error(ActiveRecord::RecordInvalid)
end
end
context "with a reason for leaving last settled home validation" do
it "checks the reason for leaving must be don’t know if reason for leaving settled home (Q9a) is don’t know." do
expect {
@ -757,6 +720,10 @@ RSpec.describe CaseLog do
it "validates the net income" do
expect(validator).to receive(:validate_net_income)
end
it "validates reasonable preference" do
expect(validator).to receive(:validate_reasonable_preference)
end
end
describe "status" do

61
spec/models/validations/household_validations_spec.rb

@ -48,5 +48,66 @@ RSpec.describe Validations::HouseholdValidations do
expect(record.errors["age4"])
.to include(match I18n.t("validations.household.age.must_be_valid", lower_bound: 1))
end
it "validates that person 1's age is between 16 and 120" do
record.age1 = 63
household_validator.validate_person_1_age(record)
expect(record.errors["age1"]).to be_empty
end
it "validates that other household member ages are between 1 and 120" do
record.age6 = 45
household_validator.validate_household_number_of_other_members(record)
expect(record.errors["age6"]).to be_empty
end
end
describe "reasonable preference validations" do
context "when reasonable preference is given" do
context "when the tenant was not previously homeless" do
it "adds an error" do
record.homeless = "No"
record.reasonpref = "Yes"
household_validator.validate_reasonable_preference(record)
expect(record.errors["reasonpref"])
.to include(match I18n.t("validations.household.reasonpref.not_homeless"))
expect(record.errors["homeless"])
.to include(match I18n.t("validations.household.reasonpref.not_homeless"))
end
end
context "when reasonable preference is given" do
context "when the tenant was previously homeless" do
it "does not add an error" do
record.homeless = "Yes - other homelessness"
record.reasonpref = "Yes"
household_validator.validate_reasonable_preference(record)
expect(record.errors["reasonpref"]).to be_empty
expect(record.errors["homeless"]).to be_empty
record.homeless = "Yes - assessed as homeless by a local authority and owed a homelessness duty. Including if threatened with homelessness within 56 days"
household_validator.validate_reasonable_preference(record)
expect(record.errors["reasonpref"]).to be_empty
expect(record.errors["homeless"]).to be_empty
end
end
end
end
context "when reasonable preference is not given" do
it "validates that no reason is needed" do
record.reasonpref = "No"
record.rp_homeless = "No"
household_validator.validate_reasonable_preference(record)
expect(record.errors["reasonpref"]).to be_empty
end
it "validates that no reason is given" do
record.reasonpref = "No"
record.rp_medwel = "Yes"
household_validator.validate_reasonable_preference(record)
expect(record.errors["reasonable_preference_reason"])
.to include(match I18n.t("validations.household.reasonable_preference_reason.reason_not_required"))
end
end
end
end

Loading…
Cancel
Save