Browse Source

CLDC-4325: Fix remaining model specs

pull/3250/head
samyou-softwire 4 weeks ago
parent
commit
aea35f4bf2
  1. 52
      spec/models/bulk_upload_spec.rb
  2. 9
      spec/models/form/lettings/questions/hhmemb_spec.rb
  3. 28
      spec/models/lettings_log_derived_fields_spec.rb
  4. 10
      spec/models/organisation_name_change_spec.rb
  5. 43
      spec/models/sales_log_spec.rb
  6. 61
      spec/models/validations/property_validations_spec.rb

52
spec/models/bulk_upload_spec.rb

@ -22,8 +22,12 @@ RSpec.describe BulkUpload, type: :model do
end end
describe "value check clearing" do describe "value check clearing" do
context "when 2025", metadata: { year: 25 } do
let(:startdate) { collection_start_date_for_year(2025) }
let(:saledate) { collection_start_date_for_year(2025) }
context "with a lettings log bulk upload" do context "with a lettings log bulk upload" do
let(:log) { build(:lettings_log, startdate: current_collection_start_date, bulk_upload:) } let(:log) { build(:lettings_log, startdate:, bulk_upload:) }
it "has the correct number of value checks to be set as confirmed" do it "has the correct number of value checks to be set as confirmed" do
expect(bulk_upload.fields_to_confirm(log)).to match_array %w[rent_value_check void_date_value_check major_repairs_date_value_check pregnancy_value_check retirement_value_check referral_value_check net_income_value_check scharge_value_check pscharge_value_check supcharg_value_check multiple_partners_value_check partner_under_16_value_check reasonother_value_check] expect(bulk_upload.fields_to_confirm(log)).to match_array %w[rent_value_check void_date_value_check major_repairs_date_value_check pregnancy_value_check retirement_value_check referral_value_check net_income_value_check scharge_value_check pscharge_value_check supcharg_value_check multiple_partners_value_check partner_under_16_value_check reasonother_value_check]
@ -31,7 +35,28 @@ RSpec.describe BulkUpload, type: :model do
end end
context "with a sales log bulk upload" do context "with a sales log bulk upload" do
let(:log) { build(:sales_log, saledate: current_collection_start_date, bulk_upload:) } let(:log) { build(:sales_log, saledate:, bulk_upload:) }
it "has the correct number of value checks to be set as confirmed" do
expect(bulk_upload.fields_to_confirm(log)).to match_array %w[value_value_check monthly_charges_value_check percentage_discount_value_check income1_value_check income2_value_check combined_income_value_check retirement_value_check old_persons_shared_ownership_value_check buyer_livein_value_check wheel_value_check mortgage_value_check savings_value_check deposit_value_check staircase_bought_value_check stairowned_value_check hodate_check shared_ownership_deposit_value_check extrabor_value_check grant_value_check discounted_sale_value_check deposit_and_mortgage_value_check multiple_partners_value_check partner_under_16_value_check]
end
end
end
context "when 2026", metadata: { year: 26 } do
let(:startdate) { collection_start_date_for_year(2026) }
let(:saledate) { collection_start_date_for_year(2026) }
context "with a lettings log bulk upload" do
let(:log) { build(:lettings_log, startdate:, bulk_upload:) }
it "has the correct number of value checks to be set as confirmed" do
expect(bulk_upload.fields_to_confirm(log)).to match_array %w[rent_value_check void_date_value_check major_repairs_date_value_check pregnancy_value_check retirement_value_check net_income_value_check scharge_value_check pscharge_value_check supcharg_value_check reasonother_value_check tenancyother_value_check working_situation_illness_check]
end
end
context "with a sales log bulk upload" do
let(:log) { build(:sales_log, saledate:, bulk_upload:) }
it "has the correct number of value checks to be set as confirmed" do it "has the correct number of value checks to be set as confirmed" do
expect(bulk_upload.fields_to_confirm(log)).to match_array %w[value_value_check monthly_charges_value_check percentage_discount_value_check income1_value_check income2_value_check combined_income_value_check retirement_value_check old_persons_shared_ownership_value_check buyer_livein_value_check wheel_value_check mortgage_value_check savings_value_check deposit_value_check staircase_bought_value_check stairowned_value_check hodate_check shared_ownership_deposit_value_check extrabor_value_check grant_value_check discounted_sale_value_check deposit_and_mortgage_value_check multiple_partners_value_check partner_under_16_value_check] expect(bulk_upload.fields_to_confirm(log)).to match_array %w[value_value_check monthly_charges_value_check percentage_discount_value_check income1_value_check income2_value_check combined_income_value_check retirement_value_check old_persons_shared_ownership_value_check buyer_livein_value_check wheel_value_check mortgage_value_check savings_value_check deposit_value_check staircase_bought_value_check stairowned_value_check hodate_check shared_ownership_deposit_value_check extrabor_value_check grant_value_check discounted_sale_value_check deposit_and_mortgage_value_check multiple_partners_value_check partner_under_16_value_check]
@ -39,6 +64,29 @@ RSpec.describe BulkUpload, type: :model do
end end
end end
# included to ensure we're testing the next collection we're working on before go live
context "when next collection year" do
let(:startdate) { next_collection_start_date }
let(:saledate) { next_collection_start_date }
context "with a lettings log bulk upload" do
let(:log) { build(:lettings_log, startdate:, bulk_upload:) }
it "has the correct number of value checks to be set as confirmed" do
expect(bulk_upload.fields_to_confirm(log)).to match_array %w[rent_value_check void_date_value_check major_repairs_date_value_check pregnancy_value_check retirement_value_check net_income_value_check scharge_value_check pscharge_value_check supcharg_value_check reasonother_value_check tenancyother_value_check working_situation_illness_check]
end
end
context "with a sales log bulk upload" do
let(:log) { build(:sales_log, saledate:, bulk_upload:) }
it "has the correct number of value checks to be set as confirmed" do
expect(bulk_upload.fields_to_confirm(log)).to match_array %w[value_value_check monthly_charges_value_check percentage_discount_value_check income1_value_check income2_value_check combined_income_value_check retirement_value_check old_persons_shared_ownership_value_check buyer_livein_value_check wheel_value_check mortgage_value_check savings_value_check deposit_value_check staircase_bought_value_check stairowned_value_check hodate_check shared_ownership_deposit_value_check extrabor_value_check grant_value_check discounted_sale_value_check deposit_and_mortgage_value_check multiple_partners_value_check partner_under_16_value_check]
end
end
end
end
describe "year_combo" do describe "year_combo" do
[ [
{ year: 2023, expected_value: "2023 to 2024" }, { year: 2023, expected_value: "2023 to 2024" },

9
spec/models/form/lettings/questions/hhmemb_spec.rb

@ -8,8 +8,9 @@ RSpec.describe Form::Lettings::Questions::Hhmemb, type: :model do
let(:question_definition) { nil } let(:question_definition) { nil }
let(:page) { instance_double(Form::Page) } let(:page) { instance_double(Form::Page) }
let(:subsection) { instance_double(Form::Subsection) } let(:subsection) { instance_double(Form::Subsection) }
let(:start_year_2026_or_later?) { false } let(:start_year_2026_or_later?) { true }
let(:form) { instance_double(Form, start_date: current_collection_start_date, start_year_2026_or_later?: start_year_2026_or_later?) } let(:startdate) { current_collection_start_date }
let(:form) { instance_double(Form, start_date:, start_year_2026_or_later?: start_year_2026_or_later?) }
before do before do
allow(page).to receive(:subsection).and_return(subsection) allow(page).to receive(:subsection).and_return(subsection)
@ -37,6 +38,9 @@ RSpec.describe Form::Lettings::Questions::Hhmemb, type: :model do
end end
context "when in 2025", { year: 25 } do context "when in 2025", { year: 25 } do
let(:start_year_2026_or_later?) { false }
let(:startdate) { collection_start_date_for_year(2025) }
it "does not have check answers card title" do it "does not have check answers card title" do
expect(question.check_answers_card_title).to be_nil expect(question.check_answers_card_title).to be_nil
end end
@ -48,6 +52,7 @@ RSpec.describe Form::Lettings::Questions::Hhmemb, type: :model do
context "when in 2026", { year: 26 } do context "when in 2026", { year: 26 } do
let(:start_year_2026_or_later?) { true } let(:start_year_2026_or_later?) { true }
let(:startdate) { collection_start_date_for_year(2026) }
it "has correct check answers card title" do it "has correct check answers card title" do
expect(question.check_answers_card_title).to eq("Household") expect(question.check_answers_card_title).to eq("Household")

28
spec/models/lettings_log_derived_fields_spec.rb

@ -1068,9 +1068,11 @@ RSpec.describe LettingsLog, type: :model do
describe "variables dependent on whether a letting is a renewal" do describe "variables dependent on whether a letting is a renewal" do
let(:organisation) { create(:organisation) } let(:organisation) { create(:organisation) }
let(:user) { create(:user, organisation:) } let(:user) { create(:user, organisation:) }
let(:startdate) { Time.zone.today }
let(:persisted_renewal_lettings_log) { create(:lettings_log, :setup_completed, startdate:, renewal: 1, assigned_to: user) } let(:persisted_renewal_lettings_log) { create(:lettings_log, :setup_completed, startdate:, renewal: 1, assigned_to: user) }
context "when 2025", metadata: { year: 25 } do
let(:startdate) { collection_start_date_for_year(2025) }
it "derives waityear offered referral first_time_property_let_as_social_housing rsnvac when renewal" do it "derives waityear offered referral first_time_property_let_as_social_housing rsnvac when renewal" do
log.renewal = 1 log.renewal = 1
expect { log.set_derived_fields! } expect { log.set_derived_fields! }
@ -1089,6 +1091,30 @@ RSpec.describe LettingsLog, type: :model do
.and change(persisted_renewal_lettings_log, :first_time_property_let_as_social_housing).from(0).to(nil) .and change(persisted_renewal_lettings_log, :first_time_property_let_as_social_housing).from(0).to(nil)
.and change(persisted_renewal_lettings_log, :rsnvac).from(14).to(nil) .and change(persisted_renewal_lettings_log, :rsnvac).from(14).to(nil)
end end
end
context "when 2026", metadata: { year: 26 } do
let(:startdate) { collection_start_date_for_year(2026) }
it "derives waityear offered referral first_time_property_let_as_social_housing rsnvac when renewal" do
log.renewal = 1
expect { log.set_derived_fields! }
.to change(log, :waityear).to(2)
.and change(log, :offered).to(0)
.and change(log, :referral_register).to(1)
.and change(log, :first_time_property_let_as_social_housing).to(0)
.and change(log, :rsnvac).to(14)
end
it "clears waityear offered referral first_time_property_let_as_social_housing rsnvac when not a renewal" do
expect { persisted_renewal_lettings_log.update!(renewal: 0) }
.to change(persisted_renewal_lettings_log, :waityear).from(2).to(nil)
.and change(persisted_renewal_lettings_log, :offered).from(0).to(nil)
.and change(persisted_renewal_lettings_log, :referral_register).from(1).to(nil)
.and change(persisted_renewal_lettings_log, :first_time_property_let_as_social_housing).from(0).to(nil)
.and change(persisted_renewal_lettings_log, :rsnvac).from(14).to(nil)
end
end
describe "deriving voiddate from startdate" do describe "deriving voiddate from startdate" do
let(:startdate) { Time.zone.now.beginning_of_day } let(:startdate) { Time.zone.now.beginning_of_day }

10
spec/models/organisation_name_change_spec.rb

@ -30,13 +30,13 @@ RSpec.describe OrganisationNameChange, type: :model do
it "is invalid if name is the same as the current name on the change date" do it "is invalid if name is the same as the current name on the change date" do
create(:organisation_name_change, organisation:, name: "New Name", startdate: 1.day.ago) create(:organisation_name_change, organisation:, name: "New Name", startdate: 1.day.ago)
name_change = build(:organisation_name_change, organisation:, name: "New Name", startdate: Time.zone.now) name_change = build(:organisation_name_change, organisation:, name: "New Name", startdate: Time.zone.today)
expect(name_change).not_to be_valid expect(name_change).not_to be_valid
expect(name_change.errors[:name]).to include(I18n.t("validations.organisation.name_changes.name.must_be_different")) expect(name_change.errors[:name]).to include(I18n.t("validations.organisation.name_changes.name.must_be_different"))
end end
it "is invalid if startdate is after the organisation's merge date" do it "is invalid if startdate is after the organisation's merge date" do
organisation.update!(merge_date: Time.zone.now) organisation.update!(merge_date: Time.zone.today)
name_change = build(:organisation_name_change, organisation:, immediate_change: false, startdate: Time.zone.tomorrow) name_change = build(:organisation_name_change, organisation:, immediate_change: false, startdate: Time.zone.tomorrow)
expect(name_change).not_to be_valid expect(name_change).not_to be_valid
expect(name_change.errors[:startdate]).to include(I18n.t("validations.organisation.name_changes.startdate.must_be_before_merge_date", merge_date: organisation.merge_date.to_formatted_s(:govuk_date))) expect(name_change.errors[:startdate]).to include(I18n.t("validations.organisation.name_changes.startdate.must_be_before_merge_date", merge_date: organisation.merge_date.to_formatted_s(:govuk_date)))
@ -54,12 +54,12 @@ RSpec.describe OrganisationNameChange, type: :model do
it "returns changes before a specific date" do it "returns changes before a specific date" do
name_change = create(:organisation_name_change, organisation:, startdate: 1.day.ago) name_change = create(:organisation_name_change, organisation:, startdate: 1.day.ago)
expect(described_class.before_date(Time.zone.now)).to include(name_change) expect(described_class.before_date(Time.zone.today)).to include(name_change)
end end
it "returns changes after a specific date" do it "returns changes after a specific date" do
name_change = create(:organisation_name_change, organisation:, startdate: 2.days.from_now) name_change = create(:organisation_name_change, organisation:, startdate: 2.days.from_now)
expect(described_class.after_date(Time.zone.now)).to include(name_change) expect(described_class.after_date(Time.zone.today)).to include(name_change)
end end
end end
@ -84,7 +84,7 @@ RSpec.describe OrganisationNameChange, type: :model do
describe "#includes_date?" do describe "#includes_date?" do
it "returns true if the date is within the change period" do it "returns true if the date is within the change period" do
name_change = create(:organisation_name_change, organisation:, startdate: 1.day.ago) name_change = create(:organisation_name_change, organisation:, startdate: 1.day.ago)
expect(name_change.includes_date?(Time.zone.now)).to be true expect(name_change.includes_date?(Time.zone.today)).to be true
end end
it "returns false if the date is outside the change period" do it "returns false if the date is outside the change period" do

43
spec/models/sales_log_spec.rb

@ -688,8 +688,9 @@ RSpec.describe SalesLog, type: :model do
create( create(
:sales_log, :sales_log,
:completed, :completed,
saledate:,
jointpur: 1, jointpur: 1,
hholdcount: 4, hholdcount:,
details_known_3: 1, details_known_3: 1,
details_known_4: 1, details_known_4: 1,
details_known_5: 1, details_known_5: 1,
@ -711,6 +712,10 @@ RSpec.describe SalesLog, type: :model do
) )
end end
context "when 2025", metadata: { year: 2025 } do
let(:saledate) { collection_start_date_for_year(2025) }
let(:hholdcount) { 4 }
it "correctly derives and saves hhmemb" do it "correctly derives and saves hhmemb" do
record_from_db = described_class.find(sales_log.id) record_from_db = described_class.find(sales_log.id)
expect(record_from_db["hhmemb"]).to eq(6) expect(record_from_db["hhmemb"]).to eq(6)
@ -738,6 +743,38 @@ RSpec.describe SalesLog, type: :model do
end end
end end
context "when 2026", metadata: { year: 2026 } do
let(:saledate) { collection_start_date_for_year(2026) }
let(:hholdcount) { 6 }
it "correctly derives and saves hhmemb" do
record_from_db = described_class.find(sales_log.id)
expect(record_from_db["hhmemb"]).to eq(6)
end
it "correctly derives and saves hhmemb if it's a joint purchase" do
sales_log.update!(jointpur: 2, jointmore: 2)
record_from_db = described_class.find(sales_log.id)
expect(record_from_db["hhmemb"]).to eq(6)
end
it "correctly derives and saves totchild" do
record_from_db = described_class.find(sales_log.id)
expect(record_from_db["totchild"]).to eq(2)
end
it "correctly derives and saves totadult" do
record_from_db = described_class.find(sales_log.id)
expect(record_from_db["totadult"]).to eq(4)
end
it "correctly derives and saves hhtype" do
record_from_db = described_class.find(sales_log.id)
expect(record_from_db["hhtype"]).to eq(9)
end
end
end
context "when saving previous address" do context "when saving previous address" do
def check_previous_postcode_fields(postcode_field) def check_previous_postcode_fields(postcode_field)
record_from_db = described_class.find(address_sales_log.id) record_from_db = described_class.find(address_sales_log.id)
@ -806,10 +843,10 @@ RSpec.describe SalesLog, type: :model do
end end
describe "expected_shared_ownership_deposit_value" do describe "expected_shared_ownership_deposit_value" do
let!(:completed_sales_log) { create(:sales_log, :completed, ownershipsch: 1, type: 2, value: 1000, equity: 50, staircase: 1) } let!(:completed_sales_log) { create(:sales_log, :completed, ownershipsch: 1, type: 2, value: 15_000, equity: 50, staircase: 1) }
it "is set to completed for a completed sales log" do it "is set to completed for a completed sales log" do
expect(completed_sales_log.expected_shared_ownership_deposit_value).to eq(500) expect(completed_sales_log.expected_shared_ownership_deposit_value).to eq(7500)
end end
end end

61
spec/models/validations/property_validations_spec.rb

@ -1,6 +1,8 @@
require "rails_helper" require "rails_helper"
RSpec.describe Validations::PropertyValidations do RSpec.describe Validations::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::PropertyValidations } } let(:property_validator_class) { Class.new { include Validations::PropertyValidations } }
@ -316,9 +318,12 @@ RSpec.describe Validations::PropertyValidations do
end end
end end
context "when 2025", metadata: { year: 25 } do
let(:startdate) { collection_start_date_for_year(2025) }
context "and the local authority is active for supported housing log" do context "and the local authority is active for supported housing log" do
let(:location) { create(:location, location_code: la_ecode_active) } let(:location) { create(:location, location_code: la_ecode_active) }
let(:log) { build(:lettings_log, :completed, needstype: 2, location:) } let(:log) { build(:lettings_log, :completed, startdate:, needstype: 2, location:) }
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)
@ -334,7 +339,7 @@ RSpec.describe Validations::PropertyValidations do
context "and the local authority is inactive for supported housing log" do context "and the local authority is inactive for supported housing log" do
let(:location) { create(:location, location_code: la_ecode_inactive) } let(:location) { create(:location, location_code: la_ecode_inactive) }
let(:log) { build(:lettings_log, :completed, needstype: 2, location:) } let(:log) { build(:lettings_log, :completed, startdate:, needstype: 2, location:) }
context "and the inactive local authority is not linked to an active one" do context "and the inactive local authority is not linked to an active one" do
it "adds an error" do it "adds an error" do
@ -365,6 +370,58 @@ RSpec.describe Validations::PropertyValidations do
end end
end end
context "when 2026", metadata: { year: 26 } do
let(:startdate) { collection_start_date_for_year(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
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
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
context "with a log before 2025" do context "with a log before 2025" do
before do before do
allow(log.form).to receive(:start_year_2025_or_later?).and_return false allow(log.form).to receive(:start_year_2025_or_later?).and_return false

Loading…
Cancel
Save