From a65073615f11e0f8e7f63959c2a6efc8f555c6ff Mon Sep 17 00:00:00 2001 From: baarkerlounger Date: Mon, 14 Feb 2022 13:47:42 +0000 Subject: [PATCH] Void date validations --- spec/models/case_log_spec.rb | 58 ++----------------- .../validations/date_validations_spec.rb | 49 ++++++++++++++++ 2 files changed, 53 insertions(+), 54 deletions(-) diff --git a/spec/models/case_log_spec.rb b/spec/models/case_log_spec.rb index 7f99e5e2b..65adaf965 100644 --- a/spec/models/case_log_spec.rb +++ b/spec/models/case_log_spec.rb @@ -354,60 +354,6 @@ RSpec.describe CaseLog do end end - context "when saving void date" do - it "must have less than 10 years between the tenancy start date and void" do - expect { - described_class.create!( - startdate: Date.new(2021, 10, 10), - property_void_date: Date.new(2009, 10, 10), - owning_organisation:, - managing_organisation:, - ) - }.to raise_error(ActiveRecord::RecordInvalid) - - expect { - described_class.create!( - startdate: Date.new(2021, 10, 10), - property_void_date: Date.new(2015, 10, 10), - owning_organisation:, - managing_organisation:, - ) - }.not_to raise_error - end - - it "must be before the tenancy start date" do - expect { - described_class.create!( - startdate: Date.new(2021, 10, 10), - property_void_date: Date.new(2021, 10, 11), - owning_organisation:, - managing_organisation:, - ) - }.to raise_error(ActiveRecord::RecordInvalid) - - expect { - described_class.create!( - startdate: Date.new(2021, 10, 10), - property_void_date: Date.new(2019, 10, 10), - owning_organisation:, - managing_organisation:, - ) - }.not_to raise_error - end - - it "must be before major repairs date if major repairs date provided" do - expect { - described_class.create!( - startdate: Date.new(2021, 10, 10), - mrcdate: Date.new(2019, 10, 10), - property_void_date: Date.new(2019, 11, 11), - owning_organisation:, - managing_organisation:, - ) - }.to raise_error(ActiveRecord::RecordInvalid) - end - end - context "when validating local authority" do it "Has to be london if rent type london affordable rent" do expect { @@ -572,6 +518,10 @@ RSpec.describe CaseLog do it "validates property major repairs date" do expect(validator).to receive(:validate_property_major_repairs) end + + it "validates property void date" do + expect(validator).to receive(:validate_property_void_date) + end end describe "status" do diff --git a/spec/models/validations/date_validations_spec.rb b/spec/models/validations/date_validations_spec.rb index 67163ce5e..7c9477a40 100644 --- a/spec/models/validations/date_validations_spec.rb +++ b/spec/models/validations/date_validations_spec.rb @@ -98,4 +98,53 @@ RSpec.describe Validations::DateValidations do end end end + + describe "property void date" do + it "cannot be after the tenancy start date" do + record.startdate = Time.zone.local(2022, 1, 1) + record.property_void_date = Time.zone.local(2022, 2, 1) + date_validator.validate_property_void_date(record) + expect(record.errors["property_void_date"]) + .to include(match I18n.t("validations.property.void_date.before_tenancy_start")) + end + + it "must be before the tenancy start date" do + record.startdate = Time.zone.local(2022, 2, 1) + record.property_void_date = Time.zone.local(2022, 1, 1) + date_validator.validate_property_void_date(record) + expect(record.errors["property_void_date"]).to be_empty + end + + it "cannot be more than 10 years before the tenancy start date" do + record.startdate = Time.zone.local(2022, 2, 1) + record.property_void_date = Time.zone.local(2012, 1, 1) + date_validator.validate_property_void_date(record) + expect(record.errors["property_void_date"]) + .to include(match I18n.t("validations.property.void_date.ten_years_before_tenancy_start")) + end + + it "must be within 10 years of the tenancy start date" do + record.startdate = Time.zone.local(2022, 2, 1) + record.property_void_date = Time.zone.local(2012, 3, 1) + date_validator.validate_property_void_date(record) + expect(record.errors["property_void_date"]).to be_empty + end + + context "when major repairs have been carried out" do + it "cannot be after major repairs date" do + record.mrcdate = Time.zone.local(2022, 1, 1) + record.property_void_date = Time.zone.local(2022, 2, 1) + date_validator.validate_property_void_date(record) + expect(record.errors["property_void_date"]) + .to include(match I18n.t("validations.property.void_date.after_mrcdate")) + end + + it "must be before major repairs date" do + record.mrcdate = Time.zone.local(2022, 2, 1) + record.property_void_date = Time.zone.local(2022, 1, 1) + date_validator.validate_property_void_date(record) + expect(record.errors["property_void_date"]).to be_empty + end + end + end end