Browse Source

CLDC-3676: lettings property validations

CLDC-3676-remove-all-pre-2025-validations
samyou-softwire 1 week ago
parent
commit
d50af5998d
  1. 3
      app/models/validations/property_validations.rb
  2. 294
      spec/models/validations/property_validations_spec.rb

3
app/models/validations/property_validations.rb

@ -70,7 +70,6 @@ module Validations::PropertyValidations
# see also: this validation in sales/property_validations.rb # see also: this validation in sales/property_validations.rb
def validate_la_in_england(record) def validate_la_in_england(record)
return unless record.form.start_year_2025_or_later?
return unless record.la return unless record.la
return if record.la.in?(LocalAuthority.england.pluck(:code)) return if record.la.in?(LocalAuthority.england.pluck(:code))
@ -94,7 +93,7 @@ module Validations::PropertyValidations
# see also: this validation in sales/property_validations.rb # see also: this validation in sales/property_validations.rb
def validate_la_is_active(record) def validate_la_is_active(record)
return unless record.form.start_year_2025_or_later? && record.startdate.present? return unless record.startdate.present?
return unless record.la return unless record.la
la = LocalAuthority.england.find_by(code: record.la) la = LocalAuthority.england.find_by(code: record.la)

294
spec/models/validations/property_validations_spec.rb

@ -210,71 +210,47 @@ RSpec.describe Validations::PropertyValidations do
end end
describe "#validate_la_in_england" do describe "#validate_la_in_england" do
context "with a log on or after 2025" do context "and the local authority is not in England for general needs log" do
before do let(:log) { build(:lettings_log, la: "S12000019", needstype: 1) }
allow(log.form).to receive(:start_year_2025_or_later?).and_return true
end
context "and the local authority is not in England for general needs log" do
let(:log) { build(:lettings_log, la: "S12000019", needstype: 1) }
it "adds an error" do
property_validator.validate_la_in_england(log)
expect(log.errors["la"]).to include(I18n.t("validations.lettings.property.la.not_in_england"))
expect(log.errors["postcode_full"]).to include(I18n.t("validations.lettings.property.postcode_full.not_in_england"))
expect(log.errors["uprn"]).to include(I18n.t("validations.lettings.property.uprn.not_in_england"))
expect(log.errors["uprn_selection"]).to include(I18n.t("validations.lettings.property.uprn_selection.not_in_england"))
expect(log.errors["startdate"]).to include(I18n.t("validations.lettings.property.startdate.postcode_not_in_england"))
expect(log.errors["scheme_id"]).to be_empty
expect(log.errors["location_id"]).to be_empty
end
end
context "and the local authority is not in England for supported housing log" do it "adds an error" do
let(:location) { create(:location, location_code: "S12000019") } property_validator.validate_la_in_england(log)
let(:log) { build(:lettings_log, la: "S12000019", needstype: 2, location:) } expect(log.errors["la"]).to include(I18n.t("validations.lettings.property.la.not_in_england"))
expect(log.errors["postcode_full"]).to include(I18n.t("validations.lettings.property.postcode_full.not_in_england"))
it "adds an error" do expect(log.errors["uprn"]).to include(I18n.t("validations.lettings.property.uprn.not_in_england"))
property_validator.validate_la_in_england(log) expect(log.errors["uprn_selection"]).to include(I18n.t("validations.lettings.property.uprn_selection.not_in_england"))
expect(log.errors["scheme_id"]).to include(I18n.t("validations.lettings.property.scheme_id.not_in_england")) expect(log.errors["startdate"]).to include(I18n.t("validations.lettings.property.startdate.postcode_not_in_england"))
expect(log.errors["location_id"]).to include(I18n.t("validations.lettings.property.location_id.not_in_england")) expect(log.errors["scheme_id"]).to be_empty
expect(log.errors["startdate"]).to include(I18n.t("validations.lettings.property.startdate.location_not_in_england")) expect(log.errors["location_id"]).to be_empty
expect(log.errors["la"]).to include(I18n.t("validations.lettings.property.la.not_in_england"))
expect(log.errors["postcode_full"]).to include(I18n.t("validations.lettings.property.postcode_full.not_in_england"))
expect(log.errors["uprn"]).to include(I18n.t("validations.lettings.property.uprn.not_in_england"))
expect(log.errors["uprn_selection"]).to include(I18n.t("validations.lettings.property.uprn_selection.not_in_england"))
end
end end
end
context "and the local authority is in England" do context "and the local authority is not in England for supported housing log" do
let(:log) { build(:lettings_log, la: "E06000002") } let(:location) { create(:location, location_code: "S12000019") }
let(:log) { build(:lettings_log, la: "S12000019", needstype: 2, location:) }
it "does not add an error" do it "adds an error" do
property_validator.validate_la_in_england(log) property_validator.validate_la_in_england(log)
expect(log.errors["la"]).to be_empty expect(log.errors["scheme_id"]).to include(I18n.t("validations.lettings.property.scheme_id.not_in_england"))
expect(log.errors["postcode_full"]).to be_empty expect(log.errors["location_id"]).to include(I18n.t("validations.lettings.property.location_id.not_in_england"))
expect(log.errors["uprn"]).to be_empty expect(log.errors["startdate"]).to include(I18n.t("validations.lettings.property.startdate.location_not_in_england"))
expect(log.errors["uprn_selection"]).to be_empty expect(log.errors["la"]).to include(I18n.t("validations.lettings.property.la.not_in_england"))
expect(log.errors["startdate"]).to be_empty expect(log.errors["postcode_full"]).to include(I18n.t("validations.lettings.property.postcode_full.not_in_england"))
end expect(log.errors["uprn"]).to include(I18n.t("validations.lettings.property.uprn.not_in_england"))
expect(log.errors["uprn_selection"]).to include(I18n.t("validations.lettings.property.uprn_selection.not_in_england"))
end end
end end
context "with a log before 2025" do context "and the local authority is in England" do
before do let(:log) { build(:lettings_log, la: "E06000002") }
allow(log.form).to receive(:start_year_2025_or_later?).and_return false
end
context "and the local authority is not in England" do it "does not add an error" do
let(:log) { build(:lettings_log, la: "S12000019") } property_validator.validate_la_in_england(log)
expect(log.errors["la"]).to be_empty
it "does not add an error" do expect(log.errors["postcode_full"]).to be_empty
property_validator.validate_la_in_england(log) expect(log.errors["uprn"]).to be_empty
expect(log.errors["la"]).to be_empty expect(log.errors["uprn_selection"]).to be_empty
expect(log.errors["postcode_full"]).to be_empty expect(log.errors["startdate"]).to be_empty
expect(log.errors["uprn"]).to be_empty
expect(log.errors["uprn_selection"]).to be_empty
end
end end
end end
end end
@ -285,98 +261,77 @@ RSpec.describe Validations::PropertyValidations do
let(:local_authority_active) { LocalAuthority.find_by(code: la_ecode_active) } let(:local_authority_active) { LocalAuthority.find_by(code: la_ecode_active) }
let(:local_authority_inactive) { LocalAuthority.find_by(code: la_ecode_inactive) } let(:local_authority_inactive) { LocalAuthority.find_by(code: la_ecode_inactive) }
context "with a log on or after 2025" do before do
before do allow(log.form).to receive(:start_year_2025_or_later?).and_return true
allow(log.form).to receive(:start_year_2025_or_later?).and_return true end
context "and the local authority is active for general needs log" do
let(:log) { build(:lettings_log, :completed, la: la_ecode_active, needstype: 1) }
it "does not add an error" do
property_validator.validate_la_is_active(log)
expect(log.errors["la"]).to be_empty
expect(log.errors["postcode_full"]).to be_empty
expect(log.errors["uprn"]).to be_empty
expect(log.errors["uprn_selection"]).to be_empty
expect(log.errors["startdate"]).to be_empty
end end
end
context "and the local authority is active for general needs log" do context "and the local authority is inactive for general needs log" do
let(:log) { build(:lettings_log, :completed, la: la_ecode_active, needstype: 1) } let(:log) { build(:lettings_log, :completed, la: la_ecode_inactive, needstype: 1) }
it "does not add an error" do it "adds an error" do
property_validator.validate_la_is_active(log) property_validator.validate_la_is_active(log)
expect(log.errors["la"]).to be_empty expect(log.errors["la"]).to include(I18n.t("validations.lettings.property.la.la_not_valid_for_date", la: local_authority_inactive.name))
expect(log.errors["postcode_full"]).to be_empty expect(log.errors["postcode_full"]).to include(I18n.t("validations.lettings.property.postcode_full.la_not_valid_for_date", la: local_authority_inactive.name))
expect(log.errors["uprn"]).to be_empty expect(log.errors["uprn"]).to include(I18n.t("validations.lettings.property.uprn.la_not_valid_for_date", la: local_authority_inactive.name))
expect(log.errors["uprn_selection"]).to be_empty expect(log.errors["uprn_selection"]).to include(I18n.t("validations.lettings.property.uprn_selection.la_not_valid_for_date", la: local_authority_inactive.name))
expect(log.errors["startdate"]).to be_empty expect(log.errors["startdate"]).to include(I18n.t("validations.lettings.property.startdate.la_not_valid_for_date", la: local_authority_inactive.name))
end expect(log.errors["scheme_id"]).to be_empty
expect(log.errors["location_id"]).to be_empty
end end
end
context "and the local authority is inactive for general needs log" do context "when 2025", metadata: { year: 25 } do
let(:log) { build(:lettings_log, :completed, la: la_ecode_inactive, needstype: 1) } let(:startdate) { collection_start_date_for_year(2025) }
it "adds an error" do context "and the local authority is active for supported housing log" do
let(:location) { create(:location, location_code: la_ecode_active) }
let(:log) { build(:lettings_log, :completed, startdate:, needstype: 2, location:) }
it "does not add an error" do
property_validator.validate_la_is_active(log) property_validator.validate_la_is_active(log)
expect(log.errors["la"]).to include(I18n.t("validations.lettings.property.la.la_not_valid_for_date", la: local_authority_inactive.name))
expect(log.errors["postcode_full"]).to include(I18n.t("validations.lettings.property.postcode_full.la_not_valid_for_date", la: local_authority_inactive.name))
expect(log.errors["uprn"]).to include(I18n.t("validations.lettings.property.uprn.la_not_valid_for_date", la: local_authority_inactive.name))
expect(log.errors["uprn_selection"]).to include(I18n.t("validations.lettings.property.uprn_selection.la_not_valid_for_date", la: local_authority_inactive.name))
expect(log.errors["startdate"]).to include(I18n.t("validations.lettings.property.startdate.la_not_valid_for_date", la: local_authority_inactive.name))
expect(log.errors["scheme_id"]).to be_empty expect(log.errors["scheme_id"]).to be_empty
expect(log.errors["location_id"]).to be_empty expect(log.errors["location_id"]).to be_empty
expect(log.errors["startdate"]).to be_empty
expect(log.errors["la"]).to be_empty
expect(log.errors["postcode_full"]).to be_empty
expect(log.errors["uprn"]).to be_empty
expect(log.errors["uprn_selection"]).to be_empty
end end
end end
context "when 2025", metadata: { year: 25 } do context "and the local authority is inactive for supported housing log" do
let(:startdate) { collection_start_date_for_year(2025) } let(:location) { create(:location, location_code: la_ecode_inactive) }
let(:log) { build(:lettings_log, :completed, startdate:, needstype: 2, location:) }
context "and the local authority is active for supported housing log" do
let(:location) { create(:location, location_code: la_ecode_active) }
let(:log) { build(:lettings_log, :completed, startdate:, needstype: 2, location:) }
it "does not add an error" do context "and the inactive local authority is not linked to an active one" do
it "adds an error" do
property_validator.validate_la_is_active(log) property_validator.validate_la_is_active(log)
expect(log.errors["scheme_id"]).to be_empty expect(log.errors["scheme_id"]).to include(I18n.t("validations.lettings.property.scheme_id.la_not_valid_for_date", la: local_authority_inactive.name))
expect(log.errors["location_id"]).to be_empty expect(log.errors["location_id"]).to include(I18n.t("validations.lettings.property.location_id.la_not_valid_for_date", la: local_authority_inactive.name))
expect(log.errors["startdate"]).to be_empty expect(log.errors["startdate"]).to include(I18n.t("validations.lettings.property.startdate.la_not_valid_for_date", la: local_authority_inactive.name))
expect(log.errors["la"]).to be_empty expect(log.errors["la"]).to include(I18n.t("validations.lettings.property.la.la_not_valid_for_date", la: local_authority_inactive.name))
expect(log.errors["postcode_full"]).to be_empty expect(log.errors["postcode_full"]).to include(I18n.t("validations.lettings.property.postcode_full.la_not_valid_for_date", la: local_authority_inactive.name))
expect(log.errors["uprn"]).to be_empty expect(log.errors["uprn"]).to include(I18n.t("validations.lettings.property.uprn.la_not_valid_for_date", la: local_authority_inactive.name))
expect(log.errors["uprn_selection"]).to be_empty expect(log.errors["uprn_selection"]).to include(I18n.t("validations.lettings.property.uprn_selection.la_not_valid_for_date", la: local_authority_inactive.name))
end end
end end
context "and the local authority is inactive for supported housing log" do context "and the inactive local authority is linked to an active one" do
let(:location) { create(:location, location_code: la_ecode_inactive) }
let(:log) { build(:lettings_log, :completed, startdate:, needstype: 2, location:) }
context "and the inactive local authority is not linked to an active one" do
it "adds an error" do
property_validator.validate_la_is_active(log)
expect(log.errors["scheme_id"]).to include(I18n.t("validations.lettings.property.scheme_id.la_not_valid_for_date", la: local_authority_inactive.name))
expect(log.errors["location_id"]).to include(I18n.t("validations.lettings.property.location_id.la_not_valid_for_date", la: local_authority_inactive.name))
expect(log.errors["startdate"]).to include(I18n.t("validations.lettings.property.startdate.la_not_valid_for_date", la: local_authority_inactive.name))
expect(log.errors["la"]).to include(I18n.t("validations.lettings.property.la.la_not_valid_for_date", la: local_authority_inactive.name))
expect(log.errors["postcode_full"]).to include(I18n.t("validations.lettings.property.postcode_full.la_not_valid_for_date", la: local_authority_inactive.name))
expect(log.errors["uprn"]).to include(I18n.t("validations.lettings.property.uprn.la_not_valid_for_date", la: local_authority_inactive.name))
expect(log.errors["uprn_selection"]).to include(I18n.t("validations.lettings.property.uprn_selection.la_not_valid_for_date", la: local_authority_inactive.name))
end
end
context "and the inactive local authority is linked to an active one" do
it "does not add an error" do
LocalAuthorityLink.create!(local_authority: local_authority_inactive, linked_local_authority: local_authority_active)
property_validator.validate_la_is_active(log)
expect(log.errors["scheme_id"]).to be_empty
expect(log.errors["location_id"]).to be_empty
expect(log.errors["startdate"]).to be_empty
expect(log.errors["la"]).to be_empty
expect(log.errors["postcode_full"]).to be_empty
expect(log.errors["uprn"]).to be_empty
expect(log.errors["uprn_selection"]).to be_empty
end
end
end
end
context "when 2026 or later", metadata: { year: 26 } do
let(:startdate) { collection_start_date_for_year_or_later(2026) }
context "and the local authority is active for supported housing log" do
let(:log) { build(:lettings_log, :completed, startdate:, la: la_ecode_active, needstype: 2) }
it "does not add an error" do it "does not add an error" do
LocalAuthorityLink.create!(local_authority: local_authority_inactive, linked_local_authority: local_authority_active)
property_validator.validate_la_is_active(log) property_validator.validate_la_is_active(log)
expect(log.errors["scheme_id"]).to be_empty expect(log.errors["scheme_id"]).to be_empty
expect(log.errors["location_id"]).to be_empty expect(log.errors["location_id"]).to be_empty
@ -387,57 +342,58 @@ RSpec.describe Validations::PropertyValidations do
expect(log.errors["uprn_selection"]).to be_empty expect(log.errors["uprn_selection"]).to be_empty
end end
end end
context "and the local authority is inactive for supported housing log" do
let(:log) { build(:lettings_log, :completed, startdate:, la: la_ecode_inactive, needstype: 2) }
context "and the inactive local authority is not linked to an active one" do
it "adds an error" do
property_validator.validate_la_is_active(log)
expect(log.errors["scheme_id"]).to include(I18n.t("validations.lettings.property.scheme_id.la_not_valid_for_date", la: local_authority_inactive.name))
expect(log.errors["location_id"]).to include(I18n.t("validations.lettings.property.location_id.la_not_valid_for_date", la: local_authority_inactive.name))
expect(log.errors["startdate"]).to include(I18n.t("validations.lettings.property.startdate.la_not_valid_for_date", la: local_authority_inactive.name))
expect(log.errors["la"]).to include(I18n.t("validations.lettings.property.la.la_not_valid_for_date", la: local_authority_inactive.name))
expect(log.errors["postcode_full"]).to include(I18n.t("validations.lettings.property.postcode_full.la_not_valid_for_date", la: local_authority_inactive.name))
expect(log.errors["uprn"]).to include(I18n.t("validations.lettings.property.uprn.la_not_valid_for_date", la: local_authority_inactive.name))
expect(log.errors["uprn_selection"]).to include(I18n.t("validations.lettings.property.uprn_selection.la_not_valid_for_date", la: local_authority_inactive.name))
end
end
context "and the inactive local authority is linked to an active one" do
# the link code was only ever used if the LA was drawn from the location
it "adds an error" do
LocalAuthorityLink.create!(local_authority: local_authority_inactive, linked_local_authority: local_authority_active)
property_validator.validate_la_is_active(log)
expect(log.errors["scheme_id"]).to include(I18n.t("validations.lettings.property.scheme_id.la_not_valid_for_date", la: local_authority_inactive.name))
expect(log.errors["location_id"]).to include(I18n.t("validations.lettings.property.location_id.la_not_valid_for_date", la: local_authority_inactive.name))
expect(log.errors["startdate"]).to include(I18n.t("validations.lettings.property.startdate.la_not_valid_for_date", la: local_authority_inactive.name))
expect(log.errors["la"]).to include(I18n.t("validations.lettings.property.la.la_not_valid_for_date", la: local_authority_inactive.name))
expect(log.errors["postcode_full"]).to include(I18n.t("validations.lettings.property.postcode_full.la_not_valid_for_date", la: local_authority_inactive.name))
expect(log.errors["uprn"]).to include(I18n.t("validations.lettings.property.uprn.la_not_valid_for_date", la: local_authority_inactive.name))
expect(log.errors["uprn_selection"]).to include(I18n.t("validations.lettings.property.uprn_selection.la_not_valid_for_date", la: local_authority_inactive.name))
end
end
end
end end
end end
context "with a log before 2025" do context "when 2026 or later", metadata: { year: 26 } do
before do let(:startdate) { collection_start_date_for_year_or_later(2026) }
allow(log.form).to receive(:start_year_2025_or_later?).and_return false
end
context "and the local authority is inactive" do context "and the local authority is active for supported housing log" do
let(:log) { build(:lettings_log, :completed, la: la_ecode_inactive) } let(:log) { build(:lettings_log, :completed, startdate:, la: la_ecode_active, needstype: 2) }
it "does not add an error" do it "does not add an error" do
property_validator.validate_la_is_active(log) property_validator.validate_la_is_active(log)
expect(log.errors["scheme_id"]).to be_empty
expect(log.errors["location_id"]).to be_empty
expect(log.errors["startdate"]).to be_empty
expect(log.errors["la"]).to be_empty expect(log.errors["la"]).to be_empty
expect(log.errors["postcode_full"]).to be_empty expect(log.errors["postcode_full"]).to be_empty
expect(log.errors["uprn"]).to be_empty expect(log.errors["uprn"]).to be_empty
expect(log.errors["uprn_selection"]).to be_empty expect(log.errors["uprn_selection"]).to be_empty
end end
end end
context "and the local authority is inactive for supported housing log" do
let(:log) { build(:lettings_log, :completed, startdate:, la: la_ecode_inactive, needstype: 2) }
context "and the inactive local authority is not linked to an active one" do
it "adds an error" do
property_validator.validate_la_is_active(log)
expect(log.errors["scheme_id"]).to include(I18n.t("validations.lettings.property.scheme_id.la_not_valid_for_date", la: local_authority_inactive.name))
expect(log.errors["location_id"]).to include(I18n.t("validations.lettings.property.location_id.la_not_valid_for_date", la: local_authority_inactive.name))
expect(log.errors["startdate"]).to include(I18n.t("validations.lettings.property.startdate.la_not_valid_for_date", la: local_authority_inactive.name))
expect(log.errors["la"]).to include(I18n.t("validations.lettings.property.la.la_not_valid_for_date", la: local_authority_inactive.name))
expect(log.errors["postcode_full"]).to include(I18n.t("validations.lettings.property.postcode_full.la_not_valid_for_date", la: local_authority_inactive.name))
expect(log.errors["uprn"]).to include(I18n.t("validations.lettings.property.uprn.la_not_valid_for_date", la: local_authority_inactive.name))
expect(log.errors["uprn_selection"]).to include(I18n.t("validations.lettings.property.uprn_selection.la_not_valid_for_date", la: local_authority_inactive.name))
end
end
context "and the inactive local authority is linked to an active one" do
# the link code was only ever used if the LA was drawn from the location
it "adds an error" do
LocalAuthorityLink.create!(local_authority: local_authority_inactive, linked_local_authority: local_authority_active)
property_validator.validate_la_is_active(log)
expect(log.errors["scheme_id"]).to include(I18n.t("validations.lettings.property.scheme_id.la_not_valid_for_date", la: local_authority_inactive.name))
expect(log.errors["location_id"]).to include(I18n.t("validations.lettings.property.location_id.la_not_valid_for_date", la: local_authority_inactive.name))
expect(log.errors["startdate"]).to include(I18n.t("validations.lettings.property.startdate.la_not_valid_for_date", la: local_authority_inactive.name))
expect(log.errors["la"]).to include(I18n.t("validations.lettings.property.la.la_not_valid_for_date", la: local_authority_inactive.name))
expect(log.errors["postcode_full"]).to include(I18n.t("validations.lettings.property.postcode_full.la_not_valid_for_date", la: local_authority_inactive.name))
expect(log.errors["uprn"]).to include(I18n.t("validations.lettings.property.uprn.la_not_valid_for_date", la: local_authority_inactive.name))
expect(log.errors["uprn_selection"]).to include(I18n.t("validations.lettings.property.uprn_selection.la_not_valid_for_date", la: local_authority_inactive.name))
end
end
end
end end
end end
end end

Loading…
Cancel
Save