Browse Source

add a validation to prevent an inconsistent combination of values and tests for this validation

pull/1512/head
Arthur Campbell 3 years ago
parent
commit
6873221cb8
  1. 12
      app/models/validations/sales/household_validations.rb
  2. 2
      config/locales/en.yml
  3. 56
      spec/models/validations/sales/household_validations_spec.rb

12
app/models/validations/sales/household_validations.rb

@ -20,6 +20,18 @@ module Validations::Sales::HouseholdValidations
end end
end end
def validate_buyers_living_in_property(record)
return unless record.form.start_date.year >= 2023
if record.buyers_will_live_in? &&
record.joint_purchase? &&
record.buyer_one_will_not_live_in_property? &&
record.buyer_two_will_not_live_in_property?
record.errors.add :buy1livein, I18n.t("validations.household.buyers_will_live_in_property.buyers_live_but_no_buyers_live")
record.errors.add :buy2livein, I18n.t("validations.household.buyers_will_live_in_property.buyers_live_but_no_buyers_live")
end
end
private private
def validate_person_age_matches_relationship(record, person_num) def validate_person_age_matches_relationship(record, person_num)

2
config/locales/en.yml

@ -442,6 +442,8 @@ en:
no_choices: "You cannot answer this question as you told us nobody in the household has a physical or mental health condition (or other illness) expected to last 12 months or more" no_choices: "You cannot answer this question as you told us nobody in the household has a physical or mental health condition (or other illness) expected to last 12 months or more"
postcode: postcode:
discounted_ownership: "Last settled accommodation and discounted ownership property postcodes must match" discounted_ownership: "Last settled accommodation and discounted ownership property postcodes must match"
buyers_will_live_in_property:
buyers_live_but_no_buyers_live: "You have already told us that the buyers will live in the property. Either buyer 1 or buyer 2 must live in the property"
tenancy: tenancy:
length: length:

56
spec/models/validations/sales/household_validations_spec.rb

@ -201,4 +201,60 @@ RSpec.describe Validations::Sales::HouseholdValidations do
end end
end end
end end
describe "validating fields about buyers living in the property" do
let(:sales_log) { FactoryBot.create(:sales_log, :outright_sale, noint: 1, companybuy: 2, buylivein:, jointpur:, jointmore:, buy1livein:) }
context "when buyers will live in the property and the sale is a joint purchase" do
let(:buylivein) { 1 }
let(:jointpur) { 1 }
let(:jointmore) { 2 }
context "and buyer one will live in the property" do
let(:buy1livein) { 1 }
it "does not add validations regardless of whether buyer two will live in the property" do
sales_log.buy2livein = 1
household_validator.validate_buyers_living_in_property(sales_log)
expect(sales_log.errors).to be_empty
sales_log.buy2livein = 2
household_validator.validate_buyers_living_in_property(sales_log)
expect(sales_log.errors).to be_empty
end
end
context "and buyer one will not live in the property" do
let(:buy1livein) { 2 }
it "does not add validations if buyer two will live in the property or if we do not yet know" do
sales_log.buy2livein = 1
household_validator.validate_buyers_living_in_property(sales_log)
expect(sales_log.errors).to be_empty
sales_log.buy2livein = nil
household_validator.validate_buyers_living_in_property(sales_log)
expect(sales_log.errors).to be_empty
end
it "triggers a validation if buyer two will also not live in the property" do
sales_log.buy2livein = 2
household_validator.validate_buyers_living_in_property(sales_log)
expect(sales_log.errors[:buy2livein]).to include I18n.t("validations.household.buyers_will_live_in_property.buyers_live_but_no_buyers_live")
expect(sales_log.errors[:buy1livein]).to include I18n.t("validations.household.buyers_will_live_in_property.buyers_live_but_no_buyers_live")
end
end
context "and we don't know whether buyer one will live in the property" do
let(:buy1livein) { nil }
it "does not add validations regardless of whether buyer two will live in the property" do
sales_log.buy2livein = 1
household_validator.validate_buyers_living_in_property(sales_log)
expect(sales_log.errors).to be_empty
sales_log.buy2livein = 2
household_validator.validate_buyers_living_in_property(sales_log)
expect(sales_log.errors).to be_empty
end
end
end
end
end end

Loading…
Cancel
Save