Browse Source

Update the rent range validation to validate all related fields and check for the correct collection year

pull/411/head
Kat 3 years ago
parent
commit
da50fba030
  1. 10
      app/models/validations/financial_validations.rb
  2. 35
      spec/models/validations/financial_validations_spec.rb

10
app/models/validations/financial_validations.rb

@ -133,10 +133,18 @@ private
end end
def validate_rent_range(record) def validate_rent_range(record)
rent_range = LaRentRange.find_by(start_year: record.year, la: record.la, beds: record.beds, lettype: record.lettype) return if record.startdate.blank?
window_end_date = Time.zone.local(record.year, 4, 1)
collection_year = record.startdate < window_end_date ? record.year - 1 : record.year
rent_range = LaRentRange.find_by(start_year: collection_year, la: record.la, beds: record.beds, lettype: record.lettype)
if rent_range.present? && !weekly_value_in_range(record, "brent", rent_range.hard_min, rent_range.hard_max) if rent_range.present? && !weekly_value_in_range(record, "brent", rent_range.hard_min, rent_range.hard_max)
record.errors.add :brent, I18n.t("validations.financial.brent.not_in_range") record.errors.add :brent, I18n.t("validations.financial.brent.not_in_range")
record.errors.add :beds, I18n.t("validations.financial.brent.beds.not_in_range")
record.errors.add :la, I18n.t("validations.financial.brent.la.not_in_range")
record.errors.add :rent_type, I18n.t("validations.financial.brent.rent_type.not_in_range")
record.errors.add :needstype, I18n.t("validations.financial.brent.needstype.not_in_range")
end end
end end
end end

35
spec/models/validations/financial_validations_spec.rb

@ -735,6 +735,7 @@ RSpec.describe Validations::FinancialValidations do
record.la = "E07000223" record.la = "E07000223"
record.beds = 1 record.beds = 1
record.year = 2021 record.year = 2021
record.startdate = Time.zone.local(2021, 9, 17)
record.brent = 9.17 record.brent = 9.17
financial_validator.validate_rent_amount(record) financial_validator.validate_rent_amount(record)
@ -747,17 +748,51 @@ RSpec.describe Validations::FinancialValidations do
record.period = 1 record.period = 1
record.la = "E07000223" record.la = "E07000223"
record.beds = 1 record.beds = 1
record.startdate = Time.zone.local(2021, 9, 17)
record.year = 2021 record.year = 2021
record.brent = 200 record.brent = 200
financial_validator.validate_rent_amount(record) financial_validator.validate_rent_amount(record)
expect(record.errors["brent"]) expect(record.errors["brent"])
.to include(match I18n.t("validations.financial.brent.not_in_range")) .to include(match I18n.t("validations.financial.brent.not_in_range"))
expect(record.errors["beds"])
.to include(match I18n.t("validations.financial.brent.beds.not_in_range"))
expect(record.errors["la"])
.to include(match I18n.t("validations.financial.brent.la.not_in_range"))
expect(record.errors["rent_type"])
.to include(match I18n.t("validations.financial.brent.rent_type.not_in_range"))
expect(record.errors["needstype"])
.to include(match I18n.t("validations.financial.brent.needstype.not_in_range"))
end
it "validates hard max for correct collection year" do
record.lettype = 1
record.period = 1
record.la = "E07000223"
record.startdate = Time.zone.local(2022, 2, 5)
record.beds = 1
record.year = 2022
record.month = 2
record.day = 5
record.brent = 200
financial_validator.validate_rent_amount(record)
expect(record.errors["brent"])
.to include(match I18n.t("validations.financial.brent.not_in_range"))
expect(record.errors["beds"])
.to include(match I18n.t("validations.financial.brent.beds.not_in_range"))
expect(record.errors["la"])
.to include(match I18n.t("validations.financial.brent.la.not_in_range"))
expect(record.errors["rent_type"])
.to include(match I18n.t("validations.financial.brent.rent_type.not_in_range"))
expect(record.errors["needstype"])
.to include(match I18n.t("validations.financial.brent.needstype.not_in_range"))
end end
it "does not error if some of the fields are missing" do it "does not error if some of the fields are missing" do
record.managing_organisation.provider_type = 2 record.managing_organisation.provider_type = 2
record.year = 2021 record.year = 2021
record.startdate = Time.zone.local(2021, 9, 17)
record.brent = 200 record.brent = 200
financial_validator.validate_rent_amount(record) financial_validator.validate_rent_amount(record)

Loading…
Cancel
Save