From 3b9c934279f94d80db2a0de53fac9f4a228a28ab Mon Sep 17 00:00:00 2001 From: baarkerlounger <5101747+baarkerlounger@users.noreply.github.com> Date: Thu, 17 Feb 2022 09:21:17 +0000 Subject: [PATCH] Refactor validation specs (#307) * Vacancy reason validations * Rubocop * Accessibility requirement specs * Rename context --- spec/models/case_log_spec.rb | 100 +----------------- .../validations/household_validations_spec.rb | 40 +++++++ .../validations/property_validations_spec.rb | 61 +++++++++++ 3 files changed, 106 insertions(+), 95 deletions(-) diff --git a/spec/models/case_log_spec.rb b/spec/models/case_log_spec.rb index bd06b98cc..b8bb8f9d9 100644 --- a/spec/models/case_log_spec.rb +++ b/spec/models/case_log_spec.rb @@ -43,8 +43,7 @@ RSpec.describe CaseLog do end end - # TODO: replace these with validator specs and checks for method call here - context "when saving income ranges" do + context "when soft validations exist" do context "with an income in upper soft range" do let(:case_log) do FactoryBot.create(:case_log, @@ -75,99 +74,6 @@ RSpec.describe CaseLog do end end end - - context "with accessibility requirements" do - it "validates that only one option can be selected" do - expect { - described_class.create!(housingneeds_a: "Yes", - housingneeds_b: "Yes", - rent_type: "London Affordable rent", - owning_organisation:, - managing_organisation:) - }.to raise_error(ActiveRecord::RecordInvalid) - end - - it "validates that only one option a, b, or c can be selected in conjunction with f" do - expect { - described_class.create!(housingneeds_a: "Yes", - housingneeds_f: "Yes", - rent_type: "London Affordable rent", - owning_organisation:, - managing_organisation:) - }.not_to raise_error - - expect { - described_class.create!(housingneeds_b: "Yes", - housingneeds_f: "Yes", - rent_type: "London Affordable rent", - owning_organisation:, - managing_organisation:) - }.not_to raise_error - - expect { - described_class.create!(housingneeds_c: "Yes", - housingneeds_f: "Yes", - rent_type: "London Affordable rent", - owning_organisation:, - managing_organisation:) - }.not_to raise_error - - expect { - described_class.create!(housingneeds_g: "Yes", - housingneeds_f: "Yes", - rent_type: "London Affordable rent", - owning_organisation:, - managing_organisation:) - }.to raise_error(ActiveRecord::RecordInvalid) - - expect { - described_class.create!(housingneeds_a: "Yes", - housingneeds_b: "Yes", - housingneeds_f: "Yes", - rent_type: "London Affordable rent", - owning_organisation:, - managing_organisation:) - }.to raise_error(ActiveRecord::RecordInvalid) - end - end - - context "when validating reason for vacancy" do - def check_rsnvac_validation(prevten) - expect { - described_class.create!(rsnvac: "Relet to tenant who occupied same property as temporary accommodation", - prevten:, - owning_organisation:, - managing_organisation:) - }.to raise_error(ActiveRecord::RecordInvalid) - end - - def check_rsnvac_referral_validation(referral) - expect { - described_class.create!(rsnvac: "Relet to tenant who occupied same property as temporary accommodation", - referral:, - owning_organisation:, - managing_organisation:) - }.to raise_error(ActiveRecord::RecordInvalid) - end - - it "cannot be temp accommodation if previous tenancy was non temp" do - check_rsnvac_validation("Tied housing or rented with job") - check_rsnvac_validation("Supported housing") - check_rsnvac_validation("Sheltered accommodation") - check_rsnvac_validation("Home Office Asylum Support") - check_rsnvac_validation("Any other accommodation") - end - - it "cannot be temp accommodation if source of letting referral " do - check_rsnvac_referral_validation("Re-located through official housing mobility scheme") - check_rsnvac_referral_validation("Other social landlord") - check_rsnvac_referral_validation("Police, probation or prison") - check_rsnvac_referral_validation("Youth offending team") - check_rsnvac_referral_validation("Community mental health team") - check_rsnvac_referral_validation("Health service") - end - end - # END TODO end describe "#update" do @@ -251,6 +157,10 @@ RSpec.describe CaseLog do it "validates reason for vacancy" do expect(validator).to receive(:validate_rsnvac) end + + it "validates accessibility requirements" do + expect(validator).to receive(:validate_accessibility_requirements) + end end describe "status" do diff --git a/spec/models/validations/household_validations_spec.rb b/spec/models/validations/household_validations_spec.rb index c06cc6f29..95edc1313 100644 --- a/spec/models/validations/household_validations_spec.rb +++ b/spec/models/validations/household_validations_spec.rb @@ -437,4 +437,44 @@ RSpec.describe Validations::HouseholdValidations do end end end + + describe "accessibility requirement validations" do + it "validates that mutually exclusive options can't be selected together" do + record.housingneeds_a = "Yes" + record.housingneeds_b = "Yes" + household_validator.validate_accessibility_requirements(record) + expect(record.errors["accessibility_requirements"]) + .to include(match I18n.t("validations.household.housingneeds_a.one_or_two_choices")) + record.housingneeds_a = "No" + record.housingneeds_b = "No" + record.housingneeds_g = "Yes" + record.housingneeds_f = "Yes" + household_validator.validate_accessibility_requirements(record) + expect(record.errors["accessibility_requirements"]) + .to include(match I18n.t("validations.household.housingneeds_a.one_or_two_choices")) + record.housingneeds_a = "Yes" + record.housingneeds_g = "Yes" + record.housingneeds_f = "Yes" + household_validator.validate_accessibility_requirements(record) + expect(record.errors["accessibility_requirements"]) + .to include(match I18n.t("validations.household.housingneeds_a.one_or_two_choices")) + end + + it "validates that non-mutually exclusive options can be selected together" do + record.housingneeds_a = "Yes" + record.housingneeds_f = "Yes" + household_validator.validate_accessibility_requirements(record) + expect(record.errors["accessibility_requirements"]).to be_empty + record.housingneeds_a = "No" + record.housingneeds_b = "Yes" + record.housingneeds_f = "Yes" + household_validator.validate_accessibility_requirements(record) + expect(record.errors["accessibility_requirements"]).to be_empty + record.housingneeds_b = "No" + record.housingneeds_c = "Yes" + record.housingneeds_f = "Yes" + household_validator.validate_accessibility_requirements(record) + expect(record.errors["accessibility_requirements"]).to be_empty + end + end end diff --git a/spec/models/validations/property_validations_spec.rb b/spec/models/validations/property_validations_spec.rb index fa328dc35..a223bfec9 100644 --- a/spec/models/validations/property_validations_spec.rb +++ b/spec/models/validations/property_validations_spec.rb @@ -233,5 +233,66 @@ RSpec.describe Validations::PropertyValidations do expect(record.errors["rsnvac"]).to be_empty end end + + context "when the property has been let before" do + let(:non_temporary_previous_tenancies) do + [ + "Tied housing or rented with job", + "Supported housing", + "Sheltered accommodation", + "Home Office Asylum Support", + "Any other accommodation", + ] + end + + context "when the previous tenancy was not temporary" do + let(:referral_sources) do + [ + "Re-located through official housing mobility scheme", + "Other social landlord", + "Police, probation or prison", + "Youth offending team", + "Community mental health team", + "Health service", + ] + end + + it "validates that the property is not being relet to tenant who occupied as temporary" do + non_temporary_previous_tenancies.each do |rsn| + record.rsnvac = "Relet to tenant who occupied same property as temporary accommodation" + record.prevten = rsn + property_validator.validate_rsnvac(record) + expect(record.errors["rsnvac"]) + .to include(match I18n.t("validations.property.rsnvac.non_temp_accommodation")) + end + end + + it "validates that the letting source is not a referral" do + referral_sources.each do |src| + record.rsnvac = "Relet to tenant who occupied same property as temporary accommodation" + record.referral = src + property_validator.validate_rsnvac(record) + expect(record.errors["rsnvac"]) + .to include(match I18n.t("validations.property.rsnvac.referral_invalid")) + end + end + end + + context "when the previous tenancy was temporary" do + it "expects that the property can be relet to a tenant who previously occupied it as temporary" do + record.prevten = "Fixed-term local authority general needs tenancy" + record.rsnvac = "Relet to tenant who occupied same property as temporary accommodation" + property_validator.validate_rsnvac(record) + expect(record.errors["rsnvac"]).to be_empty + end + + it "expects that the letting source can be a referral" do + record.prevten = "Fixed-term local authority general needs tenancy" + record.referral = "Re-located through official housing mobility scheme" + property_validator.validate_rsnvac(record) + expect(record.errors["rsnvac"]).to be_empty + end + end + end end end