Browse Source

CLDC-4074: Fix model tests

CLDC-4074-update-tests-for-edit-closing
Samuel Young 1 week ago
parent
commit
99e5c53136
  1. 6
      spec/models/log_spec.rb
  2. 2
      spec/models/scheme_spec.rb
  3. 4
      spec/models/validations/date_validations_spec.rb
  4. 164
      spec/models/validations/household_validations_spec.rb
  5. 83
      spec/models/validations/sales/financial_validations_spec.rb
  6. 215
      spec/models/validations/sales/household_validations_spec.rb
  7. 67
      spec/models/validations/sales/property_validations_spec.rb
  8. 191
      spec/models/validations/sales/sale_information_validations_spec.rb
  9. 75
      spec/models/validations/sales/soft_validations_spec.rb

6
spec/models/log_spec.rb

@ -1,6 +1,8 @@
require "rails_helper" require "rails_helper"
RSpec.describe Log, type: :model do RSpec.describe Log, type: :model do
include CollectionTimeHelper
it "has two child log classes" do it "has two child log classes" do
expect(SalesLog).to be < described_class expect(SalesLog).to be < described_class
expect(LettingsLog).to be < described_class expect(LettingsLog).to be < described_class
@ -62,12 +64,12 @@ RSpec.describe Log, type: :model do
end end
context "when a non setup field is invalid for a lettings log" do context "when a non setup field is invalid for a lettings log" do
subject(:model) { build_stubbed(:lettings_log, :completed, startdate: Time.zone.local(2023, 12, 12), offered: 234) } subject(:model) { build_stubbed(:lettings_log, :completed, startdate: current_collection_start_date, age1: 234) }
it "blanks it" do it "blanks it" do
model.valid? model.valid?
model.blank_invalid_non_setup_fields! model.blank_invalid_non_setup_fields!
expect(model.offered).to be_nil expect(model.age1).to be_nil
end end
end end

2
spec/models/scheme_spec.rb

@ -472,7 +472,7 @@ RSpec.describe Scheme, type: :model do
context "when the most recently created deactivation is not the current one" do context "when the most recently created deactivation is not the current one" do
before do before do
FactoryBot.create(:scheme_deactivation_period, deactivation_date: Time.zone.today - 300.days, reactivation_date: Time.zone.today - 200.days, scheme:) FactoryBot.create(:scheme_deactivation_period, deactivation_date: Time.zone.today - 5.days, reactivation_date: Time.zone.today - 4.days, scheme:)
end end
it "returns reactivating_soon" do it "returns reactivating_soon" do

4
spec/models/validations/date_validations_spec.rb

@ -56,7 +56,7 @@ RSpec.describe Validations::DateValidations do
context "with 2024 logs or earlier" do context "with 2024 logs or earlier" do
it "cannot be more than 10 years before the tenancy start date" do it "cannot be more than 10 years before the tenancy start date" do
record.startdate = Time.zone.local(2024, 2, 1) record.startdate = Time.zone.local(2024, 4, 1)
record.mrcdate = Time.zone.local(2014, 1, 31) record.mrcdate = Time.zone.local(2014, 1, 31)
date_validator.validate_property_major_repairs(record) date_validator.validate_property_major_repairs(record)
expect(record.errors["mrcdate"]) expect(record.errors["mrcdate"])
@ -150,7 +150,7 @@ RSpec.describe Validations::DateValidations do
context "with 2024 logs or earlier" do context "with 2024 logs or earlier" do
it "cannot be more than 10 years before the tenancy start date" do it "cannot be more than 10 years before the tenancy start date" do
record.startdate = Time.zone.local(2024, 2, 1) record.startdate = Time.zone.local(2024, 4, 1)
record.voiddate = Time.zone.local(2014, 1, 31) record.voiddate = Time.zone.local(2014, 1, 31)
date_validator.validate_property_void_date(record) date_validator.validate_property_void_date(record)
expect(record.errors["voiddate"]) expect(record.errors["voiddate"])

164
spec/models/validations/household_validations_spec.rb

@ -50,17 +50,6 @@ RSpec.describe Validations::HouseholdValidations do
expect(record.errors["reasonother"]).to be_empty expect(record.errors["reasonother"]).to be_empty
end end
context "when form year is before 2024" do
let(:startdate) { Time.zone.local(2024, 1, 1) }
it "does not validate the content of reasonother for phrases indicating homelessness" do
record.reason = 20
record.reasonother = "Temp accommodation"
household_validator.validate_reason_for_leaving_last_settled_home(record)
expect(record.errors["reason"]).to be_empty
end
end
context "when form year is >= 2024" do context "when form year is >= 2024" do
let(:startdate) { Time.zone.local(2024, 4, 1) } let(:startdate) { Time.zone.local(2024, 4, 1) }
@ -273,53 +262,7 @@ RSpec.describe Validations::HouseholdValidations do
end end
end end
describe "#validate_partner_count" do
let(:startdate) { Time.zone.local(2023, 4, 1) }
it "validates that only 1 partner exists" do
record.relat2 = "P"
record.relat3 = "P"
household_validator.validate_partner_count(record)
expect(record.errors["relat2"])
.to include(match I18n.t("validations.lettings.household.relat.one_partner"))
expect(record.errors["relat3"])
.to include(match I18n.t("validations.lettings.household.relat.one_partner"))
expect(record.errors["relat4"])
.not_to include(match I18n.t("validations.lettings.household.relat.one_partner"))
end
it "expects that a tenant can have a partner" do
record.relat3 = "P"
household_validator.validate_partner_count(record)
expect(record.errors["base"]).to be_empty
end
end
describe "#validate_person_age_matches_relationship" do describe "#validate_person_age_matches_relationship" do
context "with 2023 logs" do
let(:startdate) { Time.zone.local(2023, 4, 1) }
context "when the household contains a person under 16" do
it "validates that person must be a child of the tenant" do
record.age2 = 14
record.relat2 = "P"
household_validator.validate_person_age_matches_relationship(record)
expect(record.errors["relat2"])
.to include(match I18n.t("validations.lettings.household.relat.child_under_16", person_num: 2))
expect(record.errors["age2"])
.to include(match I18n.t("validations.lettings.household.age.child_under_16_relat", person_num: 2))
end
it "expects that person is a child of the tenant" do
record.age2 = 14
record.relat2 = "C"
household_validator.validate_person_age_matches_relationship(record)
expect(record.errors["relat2"]).to be_empty
expect(record.errors["age2"]).to be_empty
end
end
end
context "with 2024 logs" do context "with 2024 logs" do
let(:startdate) { Time.zone.local(2024, 4, 1) } let(:startdate) { Time.zone.local(2024, 4, 1) }
@ -342,41 +285,6 @@ RSpec.describe Validations::HouseholdValidations do
end end
describe "#validate_person_age_matches_economic_status" do describe "#validate_person_age_matches_economic_status" do
context "with 2023 logs" do
let(:startdate) { Time.zone.local(2023, 4, 1) }
context "when the household contains a person under 16" do
it "validates that person's economic status must be Child" do
record.age2 = 14
record.ecstat2 = 1
household_validator.validate_person_age_matches_economic_status(record)
expect(record.errors["ecstat2"])
.to include(match I18n.t("validations.lettings.household.ecstat.child_under_16", person_num: 2))
expect(record.errors["age2"])
.to include(match I18n.t("validations.lettings.household.age.child_under_16_ecstat", person_num: 2))
end
it "expects that person's economic status is Child" do
record.age2 = 14
record.ecstat2 = 9
household_validator.validate_person_age_matches_economic_status(record)
expect(record.errors["ecstat2"]).to be_empty
expect(record.errors["age2"]).to be_empty
end
it "validates that a person with economic status 'child' must be under 16" do
record.age2 = 21
record.relat2 = "C"
record.ecstat2 = 9
household_validator.validate_person_age_matches_economic_status(record)
expect(record.errors["ecstat2"])
.to include(match I18n.t("validations.lettings.household.ecstat.child_over_16", person_num: 2))
expect(record.errors["age2"])
.to include(match I18n.t("validations.lettings.household.age.child_over_16", person_num: 2))
end
end
end
context "with 2024 logs" do context "with 2024 logs" do
let(:startdate) { Time.zone.local(2024, 4, 1) } let(:startdate) { Time.zone.local(2024, 4, 1) }
@ -393,78 +301,6 @@ RSpec.describe Validations::HouseholdValidations do
end end
describe "#validate_person_age_and_relationship_matches_economic_status" do describe "#validate_person_age_and_relationship_matches_economic_status" do
context "with 2023 logs" do
let(:startdate) { Time.zone.local(2023, 4, 1) }
context "when the household contains a tenant’s child between the ages of 16 and 19" do
it "validates that person's economic status must be full time student or refused" do
record.age2 = 17
record.relat2 = "C"
record.ecstat2 = 1
household_validator.validate_person_age_and_relationship_matches_economic_status(record)
expect(record.errors["ecstat2"])
.to include(match I18n.t("validations.lettings.household.ecstat.student_16_19.must_be_student", person_num: 2))
expect(record.errors["age2"])
.to include(match I18n.t("validations.lettings.household.age.student_16_19.cannot_be_16_19.child_not_student", person_num: 2))
expect(record.errors["relat2"])
.to include(match I18n.t("validations.lettings.household.relat.student_16_19.cannot_be_child.16_19_not_student", person_num: 2))
end
it "expects that person can be a full time student" do
record.age2 = 17
record.relat2 = "C"
record.ecstat2 = 7
household_validator.validate_person_age_and_relationship_matches_economic_status(record)
expect(record.errors["ecstat2"]).to be_empty
expect(record.errors["age2"]).to be_empty
expect(record.errors["relat2"]).to be_empty
end
it "expects that person can refuse to share their work status" do
record.age2 = 17
record.relat2 = "C"
record.ecstat2 = 10
household_validator.validate_person_age_and_relationship_matches_economic_status(record)
expect(record.errors["ecstat2"]).to be_empty
expect(record.errors["age2"]).to be_empty
expect(record.errors["relat2"]).to be_empty
end
end
it "does not add an error for a person aged 16-19 who is a student but not a child of the lead tenant" do
record.age2 = 18
record.ecstat2 = "7"
record.relat2 = "P"
household_validator.validate_person_age_and_relationship_matches_economic_status(record)
expect(record.errors["relat2"]).to be_empty
expect(record.errors["ecstat2"]).to be_empty
expect(record.errors["age2"]).to be_empty
end
it "does not add an error for a person not aged 16-19 who is a student but not a child of the lead tenant" do
record.age2 = 20
record.ecstat2 = "7"
record.relat2 = "P"
household_validator.validate_person_age_and_relationship_matches_economic_status(record)
expect(record.errors["relat2"]).to be_empty
expect(record.errors["ecstat2"]).to be_empty
expect(record.errors["age2"]).to be_empty
end
it "adds errors for a person who is a child of the lead tenant and a student but not aged 16-19" do
record.age2 = 14
record.ecstat2 = "7"
record.relat2 = "C"
household_validator.validate_person_age_and_relationship_matches_economic_status(record)
expect(record.errors["relat2"])
.to include(match I18n.t("validations.lettings.household.relat.student_16_19.cannot_be_child.student_not_16_19"))
expect(record.errors["age2"])
.to include(match I18n.t("validations.lettings.household.age.student_16_19.must_be_16_19"))
expect(record.errors["ecstat2"])
.to include(match I18n.t("validations.lettings.household.ecstat.student_16_19.cannot_be_student.child_not_16_19"))
end
end
context "with 2024 logs" do context "with 2024 logs" do
let(:startdate) { Time.zone.local(2024, 4, 1) } let(:startdate) { Time.zone.local(2024, 4, 1) }

83
spec/models/validations/sales/financial_validations_spec.rb

@ -1,6 +1,8 @@
require "rails_helper" require "rails_helper"
RSpec.describe Validations::Sales::FinancialValidations do RSpec.describe Validations::Sales::FinancialValidations do
include CollectionTimeHelper
subject(:financial_validator) { validator_class.new } subject(:financial_validator) { validator_class.new }
let(:validator_class) { Class.new { include Validations::Sales::FinancialValidations } } let(:validator_class) { Class.new { include Validations::Sales::FinancialValidations } }
@ -208,12 +210,7 @@ RSpec.describe Validations::Sales::FinancialValidations do
end end
describe "#validate_percentage_bought_not_equal_percentage_owned" do describe "#validate_percentage_bought_not_equal_percentage_owned" do
let(:record) { FactoryBot.build(:sales_log) } let(:record) { FactoryBot.build(:sales_log, :saledate_today) }
context "with 24/25 logs" do
before do
record.saledate = Time.zone.local(2024, 4, 3)
end
it "does not add an error if the percentage bought is less than the percentage owned" do it "does not add an error if the percentage bought is less than the percentage owned" do
record.stairbought = 20 record.stairbought = 20
@ -238,20 +235,6 @@ RSpec.describe Validations::Sales::FinancialValidations do
end end
end end
context "with 23/24 logs" do
before do
record.saledate = Time.zone.local(2023, 4, 3)
end
it "does not add an error if the percentage bought is equal to the percentage owned" do
record.stairbought = 30
record.stairowned = 30
financial_validator.validate_percentage_bought_not_equal_percentage_owned(record)
expect(record.errors).to be_empty
end
end
end
describe "#validate_monthly_leasehold_charges" do describe "#validate_monthly_leasehold_charges" do
let(:record) { FactoryBot.build(:sales_log) } let(:record) { FactoryBot.build(:sales_log) }
@ -348,8 +331,7 @@ RSpec.describe Validations::Sales::FinancialValidations do
describe "#validate_equity_in_range_for_year_and_type" do describe "#validate_equity_in_range_for_year_and_type" do
let(:record) { FactoryBot.build(:sales_log, saledate:, resale: nil) } let(:record) { FactoryBot.build(:sales_log, saledate:, resale: nil) }
context "with a log in the 22/23 collection year" do let(:saledate) { current_collection_start_date }
let(:saledate) { Time.zone.local(2023, 1, 1) }
it "adds an error for type 2, equity below min with the correct percentage" do it "adds an error for type 2, equity below min with the correct percentage" do
record.type = 2 record.type = 2
@ -367,47 +349,19 @@ RSpec.describe Validations::Sales::FinancialValidations do
expect(record.errors["type"]).to include(match I18n.t("validations.sales.financial.type.equity_under_min", min_equity: 10)) expect(record.errors["type"]).to include(match I18n.t("validations.sales.financial.type.equity_under_min", min_equity: 10))
end end
it "does not add an error for equity in range with the correct percentage" do it "adds an error for type 18, low equity below min with the correct percentage" do
record.type = 2 record.type = 18
record.equity = 50 record.equity = 10
financial_validator.validate_equity_in_range_for_year_and_type(record)
expect(record.errors).to be_empty
end
it "adds an error for equity above max with the correct percentage" do
record.type = 2
record.equity = 90
financial_validator.validate_equity_in_range_for_year_and_type(record)
expect(record.errors["equity"]).to include(match I18n.t("validations.sales.financial.equity.equity_over_max", max_equity: 75))
expect(record.errors["type"]).to include(match I18n.t("validations.sales.financial.type.equity_over_max", max_equity: 75))
end
it "does not add an error if it's a resale" do
record.type = 2
record.equity = 90
record.resale = 1
financial_validator.validate_equity_in_range_for_year_and_type(record)
expect(record.errors).to be_empty
end
end
context "with a log in 23/24 collection year" do
let(:saledate) { Time.zone.local(2024, 1, 1) }
it "adds an error for type 2, equity below min with the correct percentage" do
record.type = 2
record.equity = 1
financial_validator.validate_equity_in_range_for_year_and_type(record) financial_validator.validate_equity_in_range_for_year_and_type(record)
expect(record.errors["equity"]).to include(match I18n.t("validations.sales.financial.equity.equity_under_min", min_equity: 25)) expect(record.errors["equity"]).to include(match I18n.t("validations.sales.financial.equity.equity_under_min", min_equity: 25))
expect(record.errors["type"]).to include(match I18n.t("validations.sales.financial.type.equity_under_min", min_equity: 25)) expect(record.errors["type"]).to include(match I18n.t("validations.sales.financial.type.equity_under_min", min_equity: 25))
end end
it "adds an error for type 30, equity below min with the correct percentage" do it "does not add an error for type 30, low equity in range with the correct percentage" do
record.type = 30 record.type = 30
record.equity = 1 record.equity = 10
financial_validator.validate_equity_in_range_for_year_and_type(record) financial_validator.validate_equity_in_range_for_year_and_type(record)
expect(record.errors["equity"]).to include(match I18n.t("validations.sales.financial.equity.equity_under_min", min_equity: 10)) expect(record.errors).to be_empty
expect(record.errors["type"]).to include(match I18n.t("validations.sales.financial.type.equity_under_min", min_equity: 10))
end end
it "does not add an error for equity in range with the correct percentage" do it "does not add an error for equity in range with the correct percentage" do
@ -425,25 +379,11 @@ RSpec.describe Validations::Sales::FinancialValidations do
expect(record.errors["type"]).to include(match I18n.t("validations.sales.financial.type.equity_over_max", max_equity: 75)) expect(record.errors["type"]).to include(match I18n.t("validations.sales.financial.type.equity_over_max", max_equity: 75))
end end
end end
end
describe "#validate_staircase_difference" do describe "#validate_staircase_difference" do
let(:record) { FactoryBot.build(:sales_log, saledate:) } let(:record) { FactoryBot.build(:sales_log, saledate:) }
context "with a log in the 23/24 collection year" do let(:saledate) { current_collection_start_date }
let(:saledate) { Time.zone.local(2023, 4, 1) }
it "does not add an error" do
record.stairbought = 2
record.stairowned = 3
record.equity = 2
financial_validator.validate_staircase_difference(record)
expect(record.errors).to be_empty
end
end
context "with a log in 24/25 collection year" do
let(:saledate) { Time.zone.local(2024, 4, 1) }
it "adds errors if equity is more than stairowned - stairbought for joint purchase" do it "adds errors if equity is more than stairowned - stairbought for joint purchase" do
record.stairbought = 2.5 record.stairbought = 2.5
@ -539,4 +479,3 @@ RSpec.describe Validations::Sales::FinancialValidations do
end end
end end
end end
end

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

@ -1,6 +1,8 @@
require "rails_helper" require "rails_helper"
RSpec.describe Validations::Sales::HouseholdValidations do RSpec.describe Validations::Sales::HouseholdValidations do
include CollectionTimeHelper
subject(:household_validator) { validator_class.new } subject(:household_validator) { validator_class.new }
let(:validator_class) { Class.new { include Validations::Sales::HouseholdValidations } } let(:validator_class) { Class.new { include Validations::Sales::HouseholdValidations } }
@ -8,64 +10,20 @@ RSpec.describe Validations::Sales::HouseholdValidations do
let(:saledate) { Time.zone.now } let(:saledate) { Time.zone.now }
describe "#validate_partner_count" do describe "#validate_partner_count" do
let(:saledate) { Time.zone.local(2023, 4, 1) } let(:saledate) { current_collection_start_date }
it "validates that only 1 partner exists" do it "does not validate" do
record.relat2 = "P" record.relat2 = "P"
record.relat3 = "P" record.relat3 = "P"
household_validator.validate_partner_count(record) household_validator.validate_partner_count(record)
expect(record.errors["relat2"])
.to include(match I18n.t("validations.sales.household.relat.one_partner"))
expect(record.errors["relat3"])
.to include(match I18n.t("validations.sales.household.relat.one_partner"))
expect(record.errors["relat4"])
.not_to include(match I18n.t("validations.sales.household.relat.one_partner"))
end
it "expects that a tenant can have a partner" do
record.relat3 = "P"
household_validator.validate_partner_count(record)
expect(record.errors["base"]).to be_empty
end
end
describe "#validate_person_age_matches_relationship" do
context "with 2023 logs" do
let(:saledate) { Time.zone.local(2023, 4, 1) }
context "when the household contains a person under 16" do
it "expects that person is a child of the tenant" do
record.age2 = 14
record.relat2 = "C"
household_validator.validate_person_age_matches_relationship(record)
expect(record.errors["relat2"]).to be_empty expect(record.errors["relat2"]).to be_empty
expect(record.errors["age2"]).to be_empty expect(record.errors["relat3"]).to be_empty
end expect(record.errors["relat4"]).to be_empty
it "validates that a person under 16 must not be a partner of the buyer" do
record.age2 = 14
record.relat2 = "P"
household_validator.validate_person_age_matches_relationship(record)
expect(record.errors["relat2"])
.to include(match I18n.t("validations.sales.household.relat.child_under_16", person_num: 2))
expect(record.errors["age2"])
.to include(match I18n.t("validations.sales.household.age.child_under_16", person_num: 2))
end
end
it "validates that a person over 20 must not be a child of the buyer" do
record.age2 = 21
record.relat2 = "C"
household_validator.validate_person_age_matches_relationship(record)
expect(record.errors["relat2"])
.to include(match I18n.t("validations.sales.household.relat.child_over_20"))
expect(record.errors["age2"])
.to include(match I18n.t("validations.sales.household.age.child_over_20"))
end end
end end
context "with 2024 logs" do describe "#validate_person_age_matches_relationship" do
let(:saledate) { Time.zone.local(2024, 4, 1) } let(:saledate) { current_collection_start_date }
it "does not add error if person under 16 is a partner" do it "does not add error if person under 16 is a partner" do
record.age2 = 14 record.age2 = 14
@ -83,29 +41,9 @@ RSpec.describe Validations::Sales::HouseholdValidations do
expect(record.errors["relat2"]).to be_empty expect(record.errors["relat2"]).to be_empty
end end
end end
end
describe "#validate_person_age_matches_economic_status" do describe "#validate_person_age_matches_economic_status" do
context "with 2023 logs" do let(:saledate) { current_collection_start_date }
let(:saledate) { Time.zone.local(2023, 4, 1) }
it "validates that person's economic status must be Child" do
record.age2 = 14
record.ecstat2 = 1
household_validator.validate_person_age_matches_economic_status(record)
expect(record.errors["ecstat2"])
.to include(match I18n.t("validations.sales.household.ecstat.child_under_16", person_num: 2))
expect(record.errors["age2"])
.to include(match I18n.t("validations.sales.household.age.child_under_16_ecstat", person_num: 2))
end
it "expects that person's economic status is Child" do
record.age2 = 14
record.ecstat2 = 9
household_validator.validate_person_age_matches_economic_status(record)
expect(record.errors["ecstat2"]).to be_empty
expect(record.errors["age2"]).to be_empty
end
it "validates that a person with economic status 'child' must be under 16" do it "validates that a person with economic status 'child' must be under 16" do
record.age2 = 21 record.age2 = 21
@ -118,51 +56,8 @@ RSpec.describe Validations::Sales::HouseholdValidations do
end end
end end
context "with 2024 logs" do
let(:saledate) { Time.zone.local(2024, 4, 1) }
it "does not run the validation" do
record.age2 = 14
record.ecstat2 = 1
household_validator.validate_person_age_matches_economic_status(record)
expect(record.errors["ecstat2"])
.not_to include(match I18n.t("validations.sales.household.ecstat.child_under_16", person_num: 2))
expect(record.errors["age2"])
.not_to include(match I18n.t("validations.sales.household.age.child_under_16_ecstat", person_num: 2))
end
end
end
describe "#validate_child_12_years_younger" do describe "#validate_child_12_years_younger" do
context "with 2023 logs" do let(:saledate) { current_collection_start_date }
let(:saledate) { Time.zone.local(2023, 4, 1) }
it "validates the child is at least 12 years younger than buyer 1" do
record.age1 = 30
record.age2 = record.age1 - 11
record.relat2 = "C"
household_validator.validate_child_12_years_younger(record)
expect(record.errors["age1"])
.to include(match I18n.t("validations.sales.household.age.child_12_years_younger", person_num: 2))
expect(record.errors["age2"])
.to include(match I18n.t("validations.sales.household.age.child_12_years_younger", person_num: 2))
expect(record.errors["relat2"])
.to include(match I18n.t("validations.sales.household.age.child_12_years_younger", person_num: 2))
end
it "expects the child is at least 12 years younger than buyer 1" do
record.age1 = 30
record.age2 = record.age1 - 12
record.relat2 = "C"
household_validator.validate_child_12_years_younger(record)
expect(record.errors["age1"]).to be_empty
expect(record.errors["age2"]).to be_empty
expect(record.errors["relate2"]).to be_empty
end
end
context "with 2024 logs" do
let(:saledate) { Time.zone.local(2024, 4, 1) }
it "does not validate that child is at least 12 year younger than buyer" do it "does not validate that child is at least 12 year younger than buyer" do
record.age1 = 20 record.age1 = 20
@ -174,61 +69,9 @@ RSpec.describe Validations::Sales::HouseholdValidations do
expect(record.errors["relat2"]).to be_empty expect(record.errors["relat2"]).to be_empty
end end
end end
end
describe "#validate_person_age_and_relationship_matches_economic_status" do describe "#validate_person_age_and_relationship_matches_economic_status" do
context "with 2023 logs" do let(:saledate) { current_collection_start_date }
let(:saledate) { Time.zone.local(2023, 4, 1) }
it "does not add an error for a person aged 16-19 who is a student but not a child of the buyer" do
record.age2 = 18
record.ecstat2 = "7"
record.relat2 = "P"
household_validator.validate_person_age_and_relationship_matches_economic_status(record)
expect(record.errors["relat2"]).to be_empty
expect(record.errors["ecstat2"]).to be_empty
expect(record.errors["age2"]).to be_empty
end
it "does not add an error for a person not aged 16-19 who is a student but not a child of the buyer" do
record.age2 = 20
record.ecstat2 = "7"
record.relat2 = "P"
household_validator.validate_person_age_and_relationship_matches_economic_status(record)
expect(record.errors["relat2"]).to be_empty
expect(record.errors["ecstat2"]).to be_empty
expect(record.errors["age2"]).to be_empty
end
it "adds errors for a person aged 16-19 who is a child of the buyer but not a student" do
record.age2 = 17
record.ecstat2 = "1"
record.relat2 = "C"
household_validator.validate_person_age_and_relationship_matches_economic_status(record)
expect(record.errors["relat2"])
.to include(match I18n.t("validations.sales.household.relat.student_16_19.cannot_be_child.16_19_not_student"))
expect(record.errors["age2"])
.to include(match I18n.t("validations.sales.household.age.student_16_19.cannot_be_16_19.child_not_student"))
expect(record.errors["ecstat2"])
.to include(match I18n.t("validations.sales.household.ecstat.student_16_19.must_be_student"))
end
it "adds errors for a person who is a child of the buyer and a student but not aged 16-19" do
record.age2 = 14
record.ecstat2 = "7"
record.relat2 = "C"
household_validator.validate_person_age_and_relationship_matches_economic_status(record)
expect(record.errors["relat2"])
.to include(match I18n.t("validations.sales.household.relat.student_16_19.cannot_be_child.student_not_16_19"))
expect(record.errors["age2"])
.to include(match I18n.t("validations.sales.household.age.student_16_19.must_be_16_19"))
expect(record.errors["ecstat2"])
.to include(match I18n.t("validations.sales.household.ecstat.student_16_19.cannot_be_student.child_not_16_19"))
end
end
context "with 2024 logs" do
let(:saledate) { Time.zone.local(2024, 4, 1) }
context "when the household contains a tenant’s child between the ages of 16 and 19" do context "when the household contains a tenant’s child between the ages of 16 and 19" do
it "does not add an error" do it "does not add an error" do
@ -275,7 +118,6 @@ RSpec.describe Validations::Sales::HouseholdValidations do
expect(record.errors["age2"]).to be_empty expect(record.errors["age2"]).to be_empty
end end
end end
end
describe "validating fields about buyers living in the property" do describe "validating fields about buyers living in the property" do
let(:sales_log) { build(:sales_log, :outright_sale_setup_complete, saledate:, noint: 1, companybuy: 2, buylivein:, jointpur:, jointmore:, buy1livein:) } let(:sales_log) { build(:sales_log, :outright_sale_setup_complete, saledate:, noint: 1, companybuy: 2, buylivein:, jointpur:, jointmore:, buy1livein:) }
@ -310,9 +152,6 @@ RSpec.describe Validations::Sales::HouseholdValidations do
expect(sales_log.errors).to be_empty expect(sales_log.errors).to be_empty
end end
context "with 2023 logs" do
let(:saledate) { Time.zone.local(2023, 4, 1) }
it "triggers a validation if buyer two will also not live in the property" do it "triggers a validation if buyer two will also not live in the property" do
sales_log.buy2livein = 2 sales_log.buy2livein = 2
household_validator.validate_buyers_living_in_property(sales_log) household_validator.validate_buyers_living_in_property(sales_log)
@ -321,7 +160,6 @@ RSpec.describe Validations::Sales::HouseholdValidations do
expect(sales_log.errors[:buy1livein]).to include I18n.t("validations.sales.household.buy1livein.buyers_will_live_in_property_values_inconsistent") expect(sales_log.errors[:buy1livein]).to include I18n.t("validations.sales.household.buy1livein.buyers_will_live_in_property_values_inconsistent")
end end
end end
end
context "and we don't know whether buyer one will live in the property" do context "and we don't know whether buyer one will live in the property" do
let(:buy1livein) { nil } let(:buy1livein) { nil }
@ -383,38 +221,10 @@ RSpec.describe Validations::Sales::HouseholdValidations do
expect(record.errors).to be_empty expect(record.errors).to be_empty
end end
end end
context "with 23/24 logs" do
let(:saledate) { Time.zone.local(2023, 4, 4) }
it "does not add an error for outright sale" do
record.ownershipsch = 2
[1, 2, 3, 4, 5, 6, 7, 9, 0].each do |prevten|
record.prevten = prevten
household_validator.validate_buyer1_previous_tenure(record)
expect(record.errors).to be_empty
end
end
end
end end
describe "#validate_buyer_not_child" do describe "#validate_buyer_not_child" do
context "with 2023 logs" do let(:saledate) { current_collection_start_date }
let(:saledate) { Time.zone.local(2023, 4, 1) }
it "does not add an error if either buyer is a child" do
record.jointpur = 1
record.ecstat1 = 9
record.ecstat2 = 9
household_validator.validate_buyer_not_child(record)
expect(record.errors["ecstat1"]).to be_empty
expect(record.errors["ecstat2"]).to be_empty
end
end
context "with 2024 logs" do
let(:saledate) { Time.zone.local(2024, 4, 1) }
it "validates buyer 1 isn't a child" do it "validates buyer 1 isn't a child" do
record.ecstat1 = 9 record.ecstat1 = 9
@ -439,4 +249,3 @@ RSpec.describe Validations::Sales::HouseholdValidations do
end end
end end
end end
end

67
spec/models/validations/sales/property_validations_spec.rb

@ -1,80 +1,21 @@
require "rails_helper" require "rails_helper"
RSpec.describe Validations::Sales::PropertyValidations do RSpec.describe Validations::Sales::PropertyValidations do
include CollectionTimeHelper
subject(:property_validator) { property_validator_class.new } subject(:property_validator) { property_validator_class.new }
let(:property_validator_class) { Class.new { include Validations::Sales::PropertyValidations } } let(:property_validator_class) { Class.new { include Validations::Sales::PropertyValidations } }
describe "#validate_postcodes_match_if_discounted_ownership" do describe "#validate_postcodes_match_if_discounted_ownership" do
context "when ownership scheme is not discounted ownership" do let(:record) { build(:sales_log, ownershipsch: 1, saledate: current_collection_start_date) }
let(:record) { build(:sales_log, ownershipsch: 1) }
it "when postcodes match no error is added" do
record.postcode_full = "SW1A 1AA"
record.ppostcode_full = "SW1A 1AA"
property_validator.validate_postcodes_match_if_discounted_ownership(record)
expect(record.errors["postcode_full"]).to be_empty
end
end
context "when ownership scheme is discounted ownership" do
let(:record) { build(:sales_log, ownershipsch: 2, saledate: Time.zone.local(2023, 4, 5)) }
it "when ppostcode_full is not present no error is added" do it "is not validated" do
record.postcode_full = "SW1A 1AA"
property_validator.validate_postcodes_match_if_discounted_ownership(record)
expect(record.errors["postcode_full"]).to be_empty
expect(record.errors["ppostcode_full"]).to be_empty
expect(record.errors["ownershipsch"]).to be_empty
end
it "when postcode_full is not present no error is added" do
record.ppostcode_full = "SW1A 1AA"
property_validator.validate_postcodes_match_if_discounted_ownership(record)
expect(record.errors["postcode_full"]).to be_empty
expect(record.errors["ppostcode_full"]).to be_empty
expect(record.errors["ownershipsch"]).to be_empty
end
it "when postcodes match no error is added" do
record.postcode_full = "SW1A 1AA"
record.ppostcode_full = "SW1A 1AA"
property_validator.validate_postcodes_match_if_discounted_ownership(record)
expect(record.errors["postcode_full"]).to be_empty
expect(record.errors["ppostcode_full"]).to be_empty
expect(record.errors["ownershipsch"]).to be_empty
end
it "when postcodes do not match an error is added for joint purchase" do
record.postcode_full = "SW1A 1AA"
record.ppostcode_full = "SW1A 0AA"
record.jointpur = 1
property_validator.validate_postcodes_match_if_discounted_ownership(record)
expect(record.errors["postcode_full"]).to include("Buyers’ last accommodation and discounted ownership postcodes must match.")
expect(record.errors["ppostcode_full"]).to include("Buyers’ last accommodation and discounted ownership postcodes must match.")
expect(record.errors["ownershipsch"]).to include("Buyers’ last accommodation and discounted ownership postcodes must match.")
end
it "when postcodes do not match an error is added for non joint purchase" do
record.postcode_full = "SW1A 1AA" record.postcode_full = "SW1A 1AA"
record.ppostcode_full = "SW1A 0AA" record.ppostcode_full = "SW1A 0AA"
record.jointpur = 2
property_validator.validate_postcodes_match_if_discounted_ownership(record)
expect(record.errors["postcode_full"]).to include("Buyer’s last accommodation and discounted ownership postcodes must match.")
expect(record.errors["ppostcode_full"]).to include("Buyer’s last accommodation and discounted ownership postcodes must match.")
expect(record.errors["ownershipsch"]).to include("Buyer’s last accommodation and discounted ownership postcodes must match.")
end
it "does not add error for 2024 log" do
record.postcode_full = "SW1A 1AA"
record.ppostcode_full = "SW1A 0AA"
record.saledate = Time.zone.local(2024, 4, 5)
property_validator.validate_postcodes_match_if_discounted_ownership(record) property_validator.validate_postcodes_match_if_discounted_ownership(record)
expect(record.errors["postcode_full"]).to be_empty expect(record.errors["postcode_full"]).to be_empty
expect(record.errors["ppostcode_full"]).to be_empty
expect(record.errors["ownershipsch"]).to be_empty
end
end end
end end

191
spec/models/validations/sales/sale_information_validations_spec.rb

@ -1,6 +1,8 @@
require "rails_helper" require "rails_helper"
RSpec.describe Validations::Sales::SaleInformationValidations do RSpec.describe Validations::Sales::SaleInformationValidations do
include CollectionTimeHelper
subject(:sale_information_validator) { validator_class.new } subject(:sale_information_validator) { validator_class.new }
let(:validator_class) { Class.new { include Validations::Sales::SaleInformationValidations } } let(:validator_class) { Class.new { include Validations::Sales::SaleInformationValidations } }
@ -47,7 +49,7 @@ RSpec.describe Validations::Sales::SaleInformationValidations do
end end
context "when hodate less than 3 years before saledate" do context "when hodate less than 3 years before saledate" do
let(:record) { build(:sales_log, hodate: Date.new(2021, 12, 2), saledate: Date.new(2024, 12, 1)) } let(:record) { build(:sales_log, hodate: current_collection_start_date - 3.years + 1.day, saledate: current_collection_start_date) }
it "does not add an error" do it "does not add an error" do
sale_information_validator.validate_practical_completion_date(record) sale_information_validator.validate_practical_completion_date(record)
@ -57,8 +59,8 @@ RSpec.describe Validations::Sales::SaleInformationValidations do
end end
context "when hodate 3 or more years before saledate" do context "when hodate 3 or more years before saledate" do
context "and form year is 2023 or earlier" do context "and form year is 2024 or earlier" do
let(:record) { build(:sales_log, hodate: Date.new(2020, 12, 1), saledate: Date.new(2023, 12, 1)) } let(:record) { build(:sales_log, hodate: previous_collection_start_date - 3.years, saledate: previous_collection_start_date) }
it "does add an error" do it "does add an error" do
sale_information_validator.validate_practical_completion_date(record) sale_information_validator.validate_practical_completion_date(record)
@ -68,17 +70,29 @@ RSpec.describe Validations::Sales::SaleInformationValidations do
end end
end end
context "and form year is 2024 or later" do context "and form year is 2025 or later" do
let(:record) { build(:sales_log, hodate: Date.new(2021, 12, 1), saledate: Date.new(2024, 12, 1)) } let(:record) { build(:sales_log, hodate: current_collection_start_date - 3.years, saledate: current_collection_start_date) }
it "adds an error" do it "does not add an error" do
sale_information_validator.validate_practical_completion_date(record) sale_information_validator.validate_practical_completion_date(record)
expect(record.errors[:hodate]).to be_present expect(record.errors[:hodate]).to be_empty
expect(record.errors[:saledate]).to be_empty
end end
end end
end end
context "when hodate 5 or more years before saledate" do
let(:record) { build(:sales_log, hodate: current_collection_start_date - 5.years, saledate: current_collection_start_date) }
it "does add an error" do
sale_information_validator.validate_practical_completion_date(record)
expect(record.errors[:hodate]).to be_present
expect(record.errors[:saledate]).to be_present
end
end
context "when hodate after saledate" do context "when hodate after saledate" do
let(:record) { build(:sales_log, hodate: 1.month.ago, saledate: 2.months.ago) } let(:record) { build(:sales_log, hodate: 1.month.ago, saledate: 2.months.ago) }
@ -90,7 +104,7 @@ RSpec.describe Validations::Sales::SaleInformationValidations do
end end
context "when hodate == saledate" do context "when hodate == saledate" do
let(:record) { build(:sales_log, hodate: Time.zone.parse("2023-07-01"), saledate: Time.zone.parse("2023-07-01")) } let(:record) { build(:sales_log, hodate: current_collection_start_date, saledate: current_collection_start_date) }
it "does not add an error" do it "does not add an error" do
sale_information_validator.validate_practical_completion_date(record) sale_information_validator.validate_practical_completion_date(record)
@ -172,7 +186,7 @@ RSpec.describe Validations::Sales::SaleInformationValidations do
end end
context "when exdate == saledate" do context "when exdate == saledate" do
let(:record) { build(:sales_log, exdate: Time.zone.parse("2023-07-01"), saledate: Time.zone.parse("2023-07-01")) } let(:record) { build(:sales_log, exdate: current_collection_start_date, saledate: current_collection_start_date) }
it "does not add an error" do it "does not add an error" do
sale_information_validator.validate_exchange_date(record) sale_information_validator.validate_exchange_date(record)
@ -235,7 +249,7 @@ RSpec.describe Validations::Sales::SaleInformationValidations do
end end
context "when initial purchase date == saledate" do context "when initial purchase date == saledate" do
let(:record) { build(:sales_log, initialpurchase: Time.zone.parse("2023-07-01"), saledate: Time.zone.parse("2023-07-01")) } let(:record) { build(:sales_log, initialpurchase: current_collection_start_date, saledate: current_collection_start_date) }
it "does not add an error" do it "does not add an error" do
sale_information_validator.validate_staircasing_initial_purchase_date(record) sale_information_validator.validate_staircasing_initial_purchase_date(record)
@ -298,7 +312,7 @@ RSpec.describe Validations::Sales::SaleInformationValidations do
end end
context "when last transaction date == saledate" do context "when last transaction date == saledate" do
let(:record) { build(:sales_log, lasttransaction: Time.zone.parse("2023-07-01"), saledate: Time.zone.parse("2023-07-01")) } let(:record) { build(:sales_log, lasttransaction: current_collection_start_date, saledate: current_collection_start_date) }
it "does not add an error" do it "does not add an error" do
sale_information_validator.validate_staircasing_last_transaction_date(record) sale_information_validator.validate_staircasing_last_transaction_date(record)
@ -329,7 +343,7 @@ RSpec.describe Validations::Sales::SaleInformationValidations do
end end
context "when last transaction date == initial purchase date" do context "when last transaction date == initial purchase date" do
let(:record) { build(:sales_log, lasttransaction: Time.zone.parse("2023-07-01"), initialpurchase: Time.zone.parse("2023-07-01")) } let(:record) { build(:sales_log, lasttransaction: current_collection_start_date, initialpurchase: current_collection_start_date) }
it "does not add an error" do it "does not add an error" do
sale_information_validator.validate_staircasing_last_transaction_date(record) sale_information_validator.validate_staircasing_last_transaction_date(record)
@ -649,27 +663,10 @@ RSpec.describe Validations::Sales::SaleInformationValidations do
expect(record.errors["grant"]).to be_empty expect(record.errors["grant"]).to be_empty
end end
end end
context "when it is a 2023 log" do
let(:record) { FactoryBot.build(:sales_log, mortgageused: 1, deposit: 5_000, grant: 3_000, value: 20_000, discount: 10, ownershipsch: 2, saledate: Time.zone.local(2023, 4, 1)) }
it "returns false" do
record.mortgage = 10
sale_information_validator.validate_discounted_ownership_value(record)
expect(record.errors["mortgageused"]).to be_empty
expect(record.errors["mortgage"]).to be_empty
expect(record.errors["value"]).to be_empty
expect(record.errors["deposit"]).to be_empty
expect(record.errors["ownershipsch"]).to be_empty
expect(record.errors["discount"]).to be_empty
expect(record.errors["grant"]).to be_empty
end
end
end end
describe "#validate_outright_sale_value_matches_mortgage_plus_deposit" do describe "#validate_outright_sale_value_matches_mortgage_plus_deposit" do
context "with a 2024 outright sale log" do let(:record) { FactoryBot.build(:sales_log, value: 300_000, ownershipsch: 3, saledate: current_collection_start_date) }
let(:record) { FactoryBot.build(:sales_log, value: 300_000, ownershipsch: 3, saledate: Time.zone.local(2024, 5, 1)) }
context "when a mortgage is used" do context "when a mortgage is used" do
before do before do
@ -742,19 +739,6 @@ RSpec.describe Validations::Sales::SaleInformationValidations do
end end
end end
context "with a 2023 outright sale log" do
let(:record) { FactoryBot.build(:sales_log, value: 300_000, ownershipsch: 3, saledate: Time.zone.local(2023, 5, 1)) }
it "does not add errors" do
record.mortgageused = 1
record.mortgage = 100_000
record.deposit = 100_000
sale_information_validator.validate_outright_sale_value_matches_mortgage_plus_deposit(record)
expect(record.errors).to be_empty
end
end
end
describe "#validate_basic_monthly_rent" do describe "#validate_basic_monthly_rent" do
context "when within permitted bounds" do context "when within permitted bounds" do
let(:record) { build(:sales_log, mrent: 9998, ownershipsch: 1, type: 2) } let(:record) { build(:sales_log, mrent: 9998, ownershipsch: 1, type: 2) }
@ -814,7 +798,7 @@ RSpec.describe Validations::Sales::SaleInformationValidations do
describe "#validate_grant_amount" do describe "#validate_grant_amount" do
context "when within permitted bounds" do context "when within permitted bounds" do
let(:record) { build(:sales_log, grant: 10_000, saledate: Time.zone.local(2024, 4, 5)) } let(:record) { build(:sales_log, grant: 10_000, saledate: current_collection_start_date) }
it "does not add an error" do it "does not add an error" do
sale_information_validator.validate_grant_amount(record) sale_information_validator.validate_grant_amount(record)
@ -824,7 +808,7 @@ RSpec.describe Validations::Sales::SaleInformationValidations do
end end
context "when over the max" do context "when over the max" do
let(:record) { build(:sales_log, type: 8, grant: 17_000, saledate: Time.zone.local(2024, 4, 5)) } let(:record) { build(:sales_log, type: 8, grant: 17_000, saledate: current_collection_start_date) }
it "adds an error" do it "adds an error" do
sale_information_validator.validate_grant_amount(record) sale_information_validator.validate_grant_amount(record)
@ -834,7 +818,7 @@ RSpec.describe Validations::Sales::SaleInformationValidations do
end end
context "when under the min" do context "when under the min" do
let(:record) { build(:sales_log, type: 21, grant: 3, saledate: Time.zone.local(2024, 4, 5)) } let(:record) { build(:sales_log, type: 21, grant: 3, saledate: current_collection_start_date) }
it "adds an error" do it "adds an error" do
sale_information_validator.validate_grant_amount(record) sale_information_validator.validate_grant_amount(record)
@ -844,7 +828,7 @@ RSpec.describe Validations::Sales::SaleInformationValidations do
end end
context "when grant is blank" do context "when grant is blank" do
let(:record) { build(:sales_log, type: 21, grant: nil, saledate: Time.zone.local(2024, 4, 5)) } let(:record) { build(:sales_log, type: 21, grant: nil, saledate: current_collection_start_date) }
it "does not add an error" do it "does not add an error" do
sale_information_validator.validate_grant_amount(record) sale_information_validator.validate_grant_amount(record)
@ -854,7 +838,7 @@ RSpec.describe Validations::Sales::SaleInformationValidations do
end end
context "when over the max and type is not RTA of social homebuy" do context "when over the max and type is not RTA of social homebuy" do
let(:record) { build(:sales_log, type: 9, grant: 17_000, saledate: Time.zone.local(2024, 4, 5)) } let(:record) { build(:sales_log, type: 9, grant: 17_000, saledate: current_collection_start_date) }
it "does not add an error" do it "does not add an error" do
sale_information_validator.validate_grant_amount(record) sale_information_validator.validate_grant_amount(record)
@ -864,7 +848,7 @@ RSpec.describe Validations::Sales::SaleInformationValidations do
end end
context "when under the min and type is not RTA of social homebuy" do context "when under the min and type is not RTA of social homebuy" do
let(:record) { build(:sales_log, type: 9, grant: 17_000, saledate: Time.zone.local(2024, 4, 5)) } let(:record) { build(:sales_log, type: 9, grant: 17_000, saledate: current_collection_start_date) }
it "does not add error" do it "does not add error" do
sale_information_validator.validate_grant_amount(record) sale_information_validator.validate_grant_amount(record)
@ -872,20 +856,10 @@ RSpec.describe Validations::Sales::SaleInformationValidations do
expect(record.errors).not_to be_present expect(record.errors).not_to be_present
end end
end end
context "with log before 2024/25 collection" do
let(:record) { build(:sales_log, type: 8, grant: 3, saledate: Time.zone.local(2023, 4, 5)) }
it "does not add an error" do
sale_information_validator.validate_grant_amount(record)
expect(record.errors).not_to be_present
end
end
end end
describe "#validate_stairbought" do describe "#validate_stairbought" do
let(:saledate) { Time.zone.local(2024, 4, 4) } let(:saledate) { current_collection_start_date }
[ [
["Shared Ownership (new model lease)", 30, 90], ["Shared Ownership (new model lease)", 30, 90],
@ -930,23 +904,12 @@ RSpec.describe Validations::Sales::SaleInformationValidations do
end end
end end
end end
context "when the collection year is before 2024" do
let(:record) { build(:sales_log, ownershipsch: 1, type: 24, saledate:, stairbought: 90) }
let(:saledate) { Time.zone.local(2023, 4, 4) }
it "does not add an error" do
sale_information_validator.validate_stairbought(record)
expect(record.errors).to be_empty
end
end
end end
describe "#validate_discount_and_value" do describe "#validate_discount_and_value" do
let(:record) { FactoryBot.build(:sales_log, value: 200_000, discount: 50, ownershipsch: 2, type: 9, saledate:) } let(:record) { FactoryBot.build(:sales_log, value: 200_000, discount: 50, ownershipsch: 2, type: 9, saledate:) }
let(:saledate) { Time.zone.local(2024, 4, 1) } let(:saledate) { current_collection_start_date }
context "with a log in the 24/25 collection year" do
context "when in London" do context "when in London" do
before do before do
record.la = "E09000001" record.la = "E09000001"
@ -1008,25 +971,10 @@ RSpec.describe Validations::Sales::SaleInformationValidations do
end end
end end
context "when it is a 2023 log" do
let(:record) { FactoryBot.build(:sales_log, value: 200_000, discount: 80, ownershipsch: 2, type: 9, saledate: Time.zone.local(2023, 4, 1), la: "E06000015") }
it "does not add an error" do
sale_information_validator.validate_discount_and_value(record)
expect(record.errors["value"]).to be_empty
expect(record.errors["discount"]).to be_empty
expect(record.errors["la"]).to be_empty
expect(record.errors["postcode_full"]).to be_empty
expect(record.errors["uprn"]).to be_empty
end
end
end
describe "#validate_non_staircasing_mortgage" do describe "#validate_non_staircasing_mortgage" do
let(:record) { FactoryBot.build(:sales_log, mortgageused: 1, mortgage: 10_000, deposit: 5_000, value: 30_000, equity: 28, ownershipsch: 1, type: 30, saledate:) } let(:record) { FactoryBot.build(:sales_log, mortgageused: 1, mortgage: 10_000, deposit: 5_000, value: 30_000, equity: 28, ownershipsch: 1, type: 30, saledate:) }
context "with a log in the 24/25 collection year" do let(:saledate) { current_collection_start_date }
let(:saledate) { Time.zone.local(2024, 4, 4) }
context "when MORTGAGE + DEPOSIT does not equal VALUE * EQUITY/100 " do context "when MORTGAGE + DEPOSIT does not equal VALUE * EQUITY/100 " do
context "and it is not a staircase transaction" do context "and it is not a staircase transaction" do
@ -1225,27 +1173,10 @@ RSpec.describe Validations::Sales::SaleInformationValidations do
end end
end end
context "when it is a 2023 log" do
let(:saledate) { Time.zone.local(2023, 4, 1) }
let(:record) { FactoryBot.build(:sales_log, mortgage: 10_000, staircase: 2, deposit: 5_000, value: 30_000, equity: 28, ownershipsch: 1, type: 30, saledate:) }
it "does not add an error" do
sale_information_validator.validate_non_staircasing_mortgage(record)
expect(record.errors["mortgage"]).to be_empty
expect(record.errors["value"]).to be_empty
expect(record.errors["deposit"]).to be_empty
expect(record.errors["equity"]).to be_empty
expect(record.errors["cashdis"]).to be_empty
expect(record.errors["type"]).to be_empty
end
end
end
describe "#validate_staircasing_mortgage" do describe "#validate_staircasing_mortgage" do
let(:record) { FactoryBot.build(:sales_log, mortgageused: 1, mortgage: 10_000, deposit: 5_000, value: 30_000, stairbought: 28, ownershipsch: 1, type: 30, saledate:) } let(:record) { FactoryBot.build(:sales_log, mortgageused: 1, mortgage: 10_000, deposit: 5_000, value: 30_000, stairbought: 28, ownershipsch: 1, type: 30, saledate:) }
context "with a log in the 24/25 collection year" do let(:saledate) { current_collection_start_date }
let(:saledate) { Time.zone.local(2024, 4, 4) }
context "when MORTGAGE + DEPOSIT does not equal STAIRBOUGHT/100 * VALUE" do context "when MORTGAGE + DEPOSIT does not equal STAIRBOUGHT/100 * VALUE" do
context "and it is a staircase transaction" do context "and it is a staircase transaction" do
@ -1340,26 +1271,9 @@ RSpec.describe Validations::Sales::SaleInformationValidations do
expect(record.errors["type"]).to be_empty expect(record.errors["type"]).to be_empty
end end
end end
end
context "when it is a 2023 log" do
let(:saledate) { Time.zone.local(2023, 4, 1) }
let(:record) { FactoryBot.build(:sales_log, mortgage: 10_000, staircase: 1, deposit: 5_000, value: 30_000, stairbought: 28, ownershipsch: 1, type: 30, saledate:) }
it "does not add an error" do
sale_information_validator.validate_staircasing_mortgage(record)
expect(record.errors["mortgage"]).to be_empty
expect(record.errors["value"]).to be_empty
expect(record.errors["deposit"]).to be_empty
expect(record.errors["stairbought"]).to be_empty
expect(record.errors["cashdis"]).to be_empty
expect(record.errors["type"]).to be_empty
end
end
context "when mortgage is not used" do context "when mortgage is not used" do
context "with a log in the 24/25 collection year" do let(:saledate) { current_collection_start_date }
let(:saledate) { Time.zone.local(2024, 4, 4) }
before do before do
record.mortgageused = 2 record.mortgageused = 2
@ -1459,22 +1373,6 @@ RSpec.describe Validations::Sales::SaleInformationValidations do
end end
end end
end end
context "when it is a 2023 log" do
let(:saledate) { Time.zone.local(2023, 4, 1) }
let(:record) { FactoryBot.build(:sales_log, mortgageused: 2, staircase: 1, deposit: 5_000, value: 30_000, stairbought: 28, ownershipsch: 1, type: 30, saledate:) }
it "does not add an error" do
sale_information_validator.validate_staircasing_mortgage(record)
expect(record.errors["mortgageused"]).to be_empty
expect(record.errors["value"]).to be_empty
expect(record.errors["deposit"]).to be_empty
expect(record.errors["stairbought"]).to be_empty
expect(record.errors["cashdis"]).to be_empty
expect(record.errors["type"]).to be_empty
end
end
end
end end
describe "#validate_mortgage_used_dont_know" do describe "#validate_mortgage_used_dont_know" do
@ -1501,23 +1399,10 @@ RSpec.describe Validations::Sales::SaleInformationValidations do
context "and it is an outright sale" do context "and it is an outright sale" do
let(:ownershipsch) { 3 } let(:ownershipsch) { 3 }
context "with a saledate before 24/25" do
let(:saledate) { Time.zone.local(2023, 9, 9) }
it "adds errors" do
expect(sales_log.errors[:mortgageused]).to include "Enter a valid value for was a mortgage used for the purchase of this property?"
expect(sales_log.errors[:saledate]).to include "You must answer either ‘yes’ or ‘no’ to the question ‘was a mortgage used’ for the selected year."
end
end
context "with a saledate from 24/25 or after" do
let(:saledate) { Time.zone.today }
it "does not add any errors" do it "does not add any errors" do
expect(sales_log.errors).to be_empty expect(sales_log.errors).to be_empty
end end
end end
end
context "and it is a shared ownership scheme sale" do context "and it is a shared ownership scheme sale" do
let(:ownershipsch) { 1 } let(:ownershipsch) { 1 }

75
spec/models/validations/sales/soft_validations_spec.rb

@ -1,6 +1,8 @@
require "rails_helper" require "rails_helper"
RSpec.describe Validations::Sales::SoftValidations do RSpec.describe Validations::Sales::SoftValidations do
include CollectionTimeHelper
let(:record) { build(:sales_log) } let(:record) { build(:sales_log) }
describe "income validations" do describe "income validations" do
@ -404,77 +406,7 @@ RSpec.describe Validations::Sales::SoftValidations do
end end
context "when validating extra borrowing" do context "when validating extra borrowing" do
context "when the log is for 2023" do let(:record) { build(:sales_log, saledate: previous_collection_start_date) }
let(:record) { build(:sales_log, saledate: Time.zone.local(2023, 12, 1)) }
it "returns false if extrabor not present" do
record.mortgage = 50_000
record.deposit = 40_000
record.value = 100_000
record.discount = 11
expect(record)
.not_to be_extra_borrowing_expected_but_not_reported
end
it "returns false if mortgage not present" do
record.extrabor = 2
record.deposit = 40_000
record.value = 100_000
record.discount = 11
expect(record)
.not_to be_extra_borrowing_expected_but_not_reported
end
it "returns false if deposit not present" do
record.extrabor = 2
record.mortgage = 50_000
record.value = 100_000
record.discount = 11
expect(record)
.not_to be_extra_borrowing_expected_but_not_reported
end
it "returns false if value not present" do
record.extrabor = 2
record.mortgage = 50_000
record.deposit = 40_000
record.discount = 11
expect(record)
.not_to be_extra_borrowing_expected_but_not_reported
end
it "returns false if discount not present" do
record.extrabor = 2
record.mortgage = 50_000
record.deposit = 40_000
record.value = 100_000
expect(record)
.not_to be_extra_borrowing_expected_but_not_reported
end
it "returns false if extra borrowing expected and reported" do
record.extrabor = 1
record.mortgage = 50_000
record.deposit = 40_000
record.value = 100_000
record.discount = 11
expect(record)
.not_to be_extra_borrowing_expected_but_not_reported
end
it "returns true if extra borrowing expected but not reported" do
record.extrabor = 2
record.mortgage = 50_000
record.deposit = 40_000
record.value = 100_000
record.discount = 11
expect(record)
.to be_extra_borrowing_expected_but_not_reported
end
end
context "when the log is for 2024" do
let(:record) { build(:sales_log, saledate: Time.zone.local(2024, 12, 1)) }
it "returns false for logs from 2024 onwards" do it "returns false for logs from 2024 onwards" do
record.extrabor = 2 record.extrabor = 2
@ -487,7 +419,6 @@ RSpec.describe Validations::Sales::SoftValidations do
end end
end end
end end
end
describe "savings amount validations" do describe "savings amount validations" do
context "when validating soft max" do context "when validating soft max" do

Loading…
Cancel
Save